package com.sheep.gamegroup.view.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;

import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.event.EventTypes;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.TaskAcceptedEty;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.view.adapter.TaskListItemAdp;
import com.sheep.gamegroup.view.customview.RefreshLayout;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;

import org.greenrobot.eventbus.EventBus;

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

import butterknife.BindView;
import rx.functions.Action1;

import static com.sheep.gamegroup.view.fragment.FgtMainAudit.POSITION_ACTIVITY;

/**
 * 审核中心
 * Created by ljy on 2018/8/16.
 */

public class FgtAudit extends BaseFragment {

    @BindView(R.id.withdrawal_listview)
    ListView withdrawalListview;
    @BindView(R.id.swipe_container)
    RefreshLayout swipe_container;
    @BindView(R.id.empty_view)
    View empty_view;
//    @BindView(R.id.title)
//    View title;

    private Activity activity;
    public List<TaskAcceptedEty> etyList = new ArrayList<>();
    private TaskListItemAdp adp;


    private int page = 1;//页数
    private int per_page = DataUtil.PER_PAGE;
    public int type = POSITION_ACTIVITY + 1;
    public boolean firstPage = false;

    public void setFirstPage(boolean firstPage) {
        this.firstPage = firstPage;
    }

    public static FgtAudit newInstance(int type){
        FgtAudit fgt = new FgtAudit();
        Bundle bundle = new Bundle();
        bundle.putInt("type", type);
        fgt.setArguments(bundle);
        return fgt;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            type = bundle.getInt("type", POSITION_ACTIVITY + 1);
        }
        super.onCreate(savedInstanceState);
    }

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

    @Override
    public void onViewCreated() {
        activity = getActivity();
        initView();
        if(firstPage)
            refreshData();
    }

    @Override
    protected void initDataOnVisibleToUser() {
        if(!firstPage && firstVisibleToUser)
            refreshData();
    }

    private void initView() {
//        if(title == null){
//            butterKnifeBindView();
//        }
//        title.setVisibility(View.GONE);
        empty_view.setPadding(0, 50, 0, 0);
        swipe_container.setFooterView(activity, withdrawalListview, R.layout.listview_footer);
        adp = new TaskListItemAdp(activity , etyList);
        withdrawalListview.setAdapter(adp);
        adp.notifyDataSetChanged();

        swipe_container.setOnRefreshListener(new RefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                EventBus.getDefault().post(BigEvent.get().setEventTypes(EventTypes.REFRESH_AUDITACTIVITY_DATA));
                refreshData();
            }
        });

        swipe_container.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                swipe_container.setLoading(false);
                if(etyList != null && etyList.size() >= per_page*page){
                    page += 1;
                    initData();
                }else {

                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if(swipe_container == null){
                                return;
                            }
                            swipe_container.setRefreshing(false);
                        }
                    },1000);
                }

            }
        });
    }
    private void refreshData() {
        if(adp == null){
            initView();
        }
        etyList.clear();
        adp.notifyDataSetChanged();
        page = 1;
        CommonUtil.getInstance().resetEmptyView(empty_view);
        initData();
    }
    public void initData() {
        if(empty_view != null)
            empty_view.setVisibility(View.INVISIBLE);
        try {
            CommonUtil.getInstance().getAudit(new Action1<BaseMessage>() {
                @Override
                public void call(BaseMessage baseMessage) {
                    if(baseMessage != null){
                        List<TaskAcceptedEty> releaseEtyList = baseMessage.getDataList(TaskAcceptedEty.class);
                        ListUtil.removeNull(etyList);
                        ListUtil.addAll(etyList, releaseEtyList);
//                        int count = ListUtil.addAll(etyList, releaseEtyList);
//                        LogUtil.println("FgtAuditAddLine", count, ListUtil.size(releaseEtyList));
                        if(ListUtil.size(releaseEtyList) < per_page){//添加最后一条底线
                            if(!etyList.isEmpty())
                                etyList.add(null);
                        }
                    }
                    notifyDataSetChanged();
                }
            }, page, per_page, type);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void notifyDataSetChanged(){
        if(empty_view == null){
            return;
        }
        CommonUtil.getInstance().updateEmptyView(empty_view, etyList.isEmpty());
        hideProgress();
        swipe_container.setRefreshing(false);
        adp.notifyDataSetChanged();
    }
}
