package com.sheep.gamegroup.view.fragment;

import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sheep.gamegroup.event.BigEvent;
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.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 org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

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

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.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;
    @BindView(R.id.empty_view)
    View empty_view;
    @Override
    public int getLayoutId() {
        return R.layout.friend_list_empty;
    }

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

    private Fragment activity;
    @Override
    public void onViewCreated() {
        activity = getParentFragment();
        if(!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
        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(); ) {
            View line = friend_list_ll.getChildAt(i);
            ViewGroup view = (ViewGroup) friend_list_ll.getChildAt(i+1);
            if(i/2 < size){
                line.setVisibility(View.VISIBLE);
                view.setVisibility(View.VISIBLE);
//                if(i % 2 == 0)
//                    view.setBackgroundColor(0xffF8F8F8);
                int j = 0;
                Friend friend = list.get(i/2);
                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(TimeUtil.TimeStamp2Date(friend.getCreate_time(), "yyyy/MM/dd HH:mm"));
                TextView friend_item_extract = (TextView) view.getChildAt(j++);
                ViewUtil.setText(friend_item_extract, friend.getSource_page());
                TextView friend_item_time = (TextView) view.getChildAt(j);
                ViewUtil.setText(friend_item_time, friend.getAward_amount());
            } else {
                line.setVisibility(View.GONE);
                view.setVisibility(View.GONE);
            }
            i += 2;
        }
        CommonUtil.getInstance().updateEmptyView(empty_view, list.isEmpty(), true);
    }
    private boolean isLoading = false;
    private void initData(){
        if(list.size() >= per_page || isLoading){
            return;
        }
        isLoading = true;
        list.clear();
        SheepApp.getInstance().getNetComponent().getApiService().getUserFriendList(page, per_page)
                .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) {
//                            baseMessage.setMsg("您还没有邀请到好友");
                            onError(baseMessage);
                        } else {
                            if (activity instanceof FgtAskGetMoneyFriend) {
                                ((FgtAskGetMoneyFriend) activity).setFriendExtractCount(baseMessage.getTotal(), page, friendList.size());
                            }
                            list.addAll(friendList);
                            notifyDataSetChanged();
                            isLoading = false;
                        }
                    }

                    @Override
                    public void onError(BaseMessage baseMessage) {
                        CommonUtil.getInstance().updateEmptyView(empty_view, list.isEmpty(), true);
                        isLoading = false;
                        if (activity instanceof FgtAskGetMoneyFriend) {
                            ((FgtAskGetMoneyFriend) activity).updateByNoFriend();
                        }
                    }
                });
    }
    public static FgtFriendExtractPage newInstance(int page) {
        FgtFriendExtractPage friendExtractPage = new FgtFriendExtractPage();
        friendExtractPage.page = page;
        return friendExtractPage;
    }

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

    @Override
    public void onDestroy() {
        super.onDestroy();

        if(EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().unregister(this);
    }

    @Subscribe
    public void onEventMainThread(BigEvent event) {
        switch (event.getEventTypes()) {
            case ASK_GETMONEY_FRIEND_REFRESH:
                initData();
                break;
        }
    }
}
