package com.sheep.gamegroup.view.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.ListView;

import com.alibaba.fastjson.JSONArray;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.di.components.DaggerTaskListComponent;
import com.sheep.gamegroup.di.modules.TaskListModule;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.TaskAcceptedEty;
import com.sheep.gamegroup.presenter.TaskListContract;
import com.sheep.gamegroup.presenter.TaskListPresenter;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.ViewUtil;
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.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

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

import javax.inject.Inject;

import butterknife.BindView;

/**
 * 任务列表
 * Created by ljy on 2018/3/21.
 */

public class TaskListAct extends BaseActivity implements TaskListContract.View {
    @BindView(R.id.withdrawal_listview)
    ListView withdrawalListview;
    @BindView(R.id.swipe_container)
    RefreshLayout swipe_container;
    @BindView(R.id.empty_view)
    View empty_view;

    @Inject
    TaskListPresenter presenter;
    private Activity activity;
    private TaskListItemAdp adp;
    private List<TaskAcceptedEty> etyList = new ArrayList<>();


    private int page = 1;//页数
    private int per_page = DataUtil.PER_PAGE;


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

    @Override
    public void initView() {
        activity = this;
        DaggerTaskListComponent.builder()
                .netComponent(SheepApp.getInstance().getNetComponent())
                .taskListModule(new TaskListModule(this))
                .build()
                .inject(this);
        TitleBarUtils.getInstance()
                .setTitle(this, "任务记录")
                .setTitleFinish(this);
        swipe_container.setFooterView(this, withdrawalListview, R.layout.listview_footer);
        adp = new TaskListItemAdp(activity , etyList);
        withdrawalListview.setAdapter(adp);
        adp.notifyDataSetChanged();
//        swipe_container.setColorSchemeResources(R.color.blue,
//                R.color.green,
//                R.color.red,
//                R.color.yellow2_end);

        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);
                }

            }
        });

    }

    private void refreshData() {
        etyList.clear();
        adp.notifyDataSetChanged();
        page = 1;
        initData();
    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        empty_view.setVisibility(View.INVISIBLE);
        presenter.acceptedTask(page,per_page);
    }

    @Override
    public void updataView(Object object) {
        if(object instanceof BaseMessage){
            List<TaskAcceptedEty> releaseEtyList = ((BaseMessage) object).getDataList(TaskAcceptedEty.class);
            ListUtil.addAll(etyList, releaseEtyList);

            if(!etyList.isEmpty())
                etyList.add(null);//底部部分----我是有底线的
        }
        notifyDataSetChanged();
    }
    private void notifyDataSetChanged(){
        CommonUtil.getInstance().updateEmptyView(empty_view, etyList.isEmpty());
        hideProgress();
        swipe_container.setRefreshing(false);
        adp.notifyDataSetChanged();
    }

    @Override
    public void failView(Object o) {
        notifyDataSetChanged();
        G.showToast(((BaseMessage) o).getMsg()+"");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode){
            case ViewUtil.REQUEST_CODE_TASK_LIST:
                if(resultCode == RESULT_OK)//进入任务详情界面提交成功
                    refreshData();
                break;
        }
    }

    @Override
    public void taskStatusSuccess(BaseMessage baseMessage) {

    }

    @Override
    public void taskStatusFaile(BaseMessage baseMessage) {
    }

}
