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.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.TaskAcceptedEty;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.ListUtil;
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 java.util.ArrayList;
import java.util.List;

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

/**
 * 审核中心
 * 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;
    private TaskListItemAdp adp;
    public List<TaskAcceptedEty> etyList = new ArrayList<>();


    private int page = 1;//页数
    private int per_page = 10;
    public int type;

    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 int getLayoutId() {
        return R.layout.withdrawal_list;
    }

    @Override
    public void onViewCreated() {
        ButterKnife.bind(this, mView);
        activity = getActivity();
        initView();
    }

    private void initView() {
        title.setVisibility(View.GONE);
        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() {
                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() {
                            swipe_container.setRefreshing(false);
                        }
                    },1000);
                }

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

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

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getArguments();
        if(bundle != null){
            type = bundle.getInt("type", 0);
        }
    }
}
