package com.kfzs.duanduan.fragment;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
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.TimeUtil;
import com.sheep.gamegroup.view.activity.AskGetMoneyAct;
import com.sheep.jiuyan.samllsheep.BuildConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;
import com.sheep.jiuyan.samllsheep.utils.G;

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

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by realicing on 2018/4/25.
 * realicing@sina.com
 */
public class FgtFriendExtractPage extends BaseFragment {
    @BindView(R.id.friend_list_ll)
    LinearLayout friend_list_ll;
    @Override
    public int getLayoutId() {
        return R.layout.friend_list;
    }

    private List<Friend> list = new ArrayList<>();
    @Override
    public void onViewCreated() {
        ButterKnife.bind(this, mView);
        initData();
    }


    public static int per_page = 5;
    private int page = 1;
    private void notifyDataSetChanged(){
        int size = list.size();
        for (int i = 0; i < friend_list_ll.getChildCount(); i++) {
            ViewGroup view = (ViewGroup) friend_list_ll.getChildAt(i);
            if(i < size){
                view.setVisibility(View.VISIBLE);
                if(i % 2 == 0)
                    view.setBackgroundColor(0xffF8F8F8);
                int j = 0;
                Friend friend = list.get(i);
                TextView friend_item_id = (TextView) view.getChildAt(j++);
                friend_item_id.setText(friend.getInvitation_code());
                TextView friend_item_name = (TextView) view.getChildAt(j++);
                friend_item_name.setText(friend.getNickname());
                TextView friend_item_extract = (TextView) view.getChildAt(j++);
                friend_item_extract.setText(""+friend.getAward_amount());
                TextView friend_item_time = (TextView) view.getChildAt(j);
                friend_item_time.setText(TimeUtil.TimeStamp2Date(friend.getCreate_time(), "yyyy/MM/dd HH:mm"));
            } else {
                view.setVisibility(View.INVISIBLE);
            }
        }
    }
    private boolean isLoadding = false;
    private void initData(){
        if(list.size() >= per_page || isLoadding){
            return;
        }
        isLoadding = true;
        list.clear();
        SheepApp.getInstance().getNetComponent().getApiService().getUserFriendList(page, per_page)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new SheepSubscriber<BaseMessage>(getContext()) {
                    @Override
                    public void onNext(BaseMessage baseMessage) {
                        List<Friend> friendList = baseMessage.getDatas(Friend.class);
                        if(friendList == null) {
                            baseMessage.setMsg("您还没有邀请到好友");
                            onError(baseMessage);
                        } else {
                            Activity activity = getActivity();
                            if (activity instanceof AskGetMoneyAct) {
                                ((AskGetMoneyAct) activity).setFriendExtractCount(baseMessage.getTotal(), page, friendList.size());
                            }
                            list.addAll(friendList);
                            notifyDataSetChanged();
                            if (BuildConfig.DEBUG)
                                System.out.println("baseMessage onNext " + JSON.toJSONString(baseMessage));
                            isLoadding = false;
                        }
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        if(BuildConfig.DEBUG)
                            System.out.println("baseMessage onError "+JSON.toJSONString(baseMessage));
                        G.showToast(TextUtils.isEmpty(baseMessage.getMsg()) ? "服务器错误，请稍候再试" : baseMessage.getMsg());
                        isLoadding = false;
                    }
                });
    }
    public static FgtFriendExtractPage newInstance(int page) {
        FgtFriendExtractPage friendExtractPage = new FgtFriendExtractPage();
        friendExtractPage.page = page;
        return friendExtractPage;
    }

    public List<Friend> getList() {
        return list;
    }
}
