package com.sheep.gamegroup.view.fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.Friend;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.TimeUtil;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ljy on 2018/8/17.
 */

public class FgtAskGetMoneyFriend extends BaseFragment {

    @BindView(R.id.empty_view)
    View emptyView;
    @BindView(R.id.pager_view)
    View pagerView;
    @BindView(R.id.friend_list_ll)
    LinearLayout friendListView;
    @BindView(R.id.friend_extract_page_tv)
    TextView pageIndexView;

    private int totalPage = 0;
    private int pageSize = 5;
    private int curPage = 1;
    private boolean isLoading = false;

    private List<Friend> friends = new ArrayList<>();

    public static FgtAskGetMoneyFriend newInstance() {
        return new FgtAskGetMoneyFriend();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fgt_askgetmoney_friend;
    }

    @Override
    public void onViewCreated() {
        loadPageData(curPage);
    }

    public void setFriendExtractCount(int count) {
        totalPage = ListUtil.getTotalPage(count, pageSize);
        ViewUtil.setVisibility(pagerView, true);
        ViewUtil.setVisibility(emptyView, false);
        pageIndexView.setText(curPage + "/" + totalPage);
    }

    //无好友，不显示翻页操作按钮
    public void updateByNoFriend() {
        ViewUtil.setVisibility(pagerView, false);
        ViewUtil.setVisibility(emptyView, true);
    }

    private void loadPageData(int index) {
        isLoading = true;
        SheepApp.getInstance().getNetComponent().getApiService().getUserFriendList(index, pageSize)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<Friend> friendList = baseMessage.getDatas(Friend.class);
                        if (friendList == null) {
                            onError(baseMessage);
                        } else {
                            friends.clear();
                            friendListView.removeAllViews();
                            setFriendExtractCount(baseMessage.getTotal());
                            friends.addAll(friendList);
                            isLoading = false;
                            showDataListView();
                        }
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        CommonUtil.getInstance().updateEmptyView(emptyView, friends.isEmpty(), true);
                        isLoading = false;
                        updateByNoFriend();
                        friends.clear();
                        friendListView.removeAllViews();
                    }
                });
    }

    private void showDataListView() {
        for (Friend friend : friends) {
            View separetor = LayoutInflater.from(getContext()).inflate(R.layout.separetor_margin_13, friendListView, false);
            View friendView = LayoutInflater.from(getContext()).inflate(R.layout.x_ask_getmoney_act_layout_item, friendListView, false);
            TextView friend_item_id = friendView.findViewById(R.id.sheep_no_view);
            friend_item_id.setText(friend.getInvitation_code());
            TextView friend_item_name = friendView.findViewById(R.id.register_time_view);
            friend_item_name.setText(TimeUtil.TimeStamp2Date(friend.getCreate_time(), "yyyy/MM/dd HH:mm"));
            TextView friend_item_extract = friendView.findViewById(R.id.source_page_view);
            ViewUtil.setText(friend_item_extract, friend.getSource_page());
            TextView friend_item_time = friendView.findViewById(R.id.award_view);;
            ViewUtil.setText(friend_item_time, friend.getAward_amount());
            friendListView.addView(separetor);
            friendListView.addView(friendView);
        }
    }

    @OnClick(R.id.friend_extract_last_page_tv)
    public void doLastPage(View v) {
        if (curPage > 1 && !isLoading) {
            loadPageData(--curPage);
        }
    }

    @OnClick(R.id.friend_extract_next_page_tv)
    public void doNextPage(View v) {
        if (curPage < totalPage && !isLoading) {
            loadPageData(++curPage);
        }
    }

}
