package com.navinfo.outdoor.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jcodecraeer.xrecyclerview.ArrowRefreshHeader;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lzy.okgo.model.HttpParams;
import com.navinfo.outdoor.R;
import com.navinfo.outdoor.adapter.PoiTaskAdapter;
import com.navinfo.outdoor.adapter.TaskExplainAdapter2;
import com.navinfo.outdoor.api.Constant;
import com.navinfo.outdoor.base.BaseFragment;
import com.navinfo.outdoor.bean.HasSubmitBean;
import com.navinfo.outdoor.bean.TaskPrefectureBean;
import com.navinfo.outdoor.http.Callback;
import com.navinfo.outdoor.http.HttpInterface;
import com.navinfo.outdoor.http.OkGoBuilder;
import com.umeng.commonsdk.debug.D;

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

/**
 * 记录-已提交-poi任务列表
 */
public class PoiTaskFragment extends BaseFragment implements View.OnClickListener {
    private ImageView ivPoiTaskFinish;
    private XRecyclerView poiTaskXrv;
    private int type;
    private PoiTaskAdapter poiTaskAdapter;
    private TextView tvTaskTitle;
    private int page=1;
    private List<HasSubmitBean.BodyBean.ListBean> listBeans;

    public static PoiTaskFragment newInstance(Bundle bundle) {
        PoiTaskFragment fragment = new PoiTaskFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayout() {
        return R.layout.fragment_poi_task;
    }

    @Override
    protected void initView() {
        super.initView();
        tvTaskTitle = findViewById(R.id.tv_poiTask_title);
        switch (type){
            case 1:
                tvTaskTitle.setText("POI任务列表");
                break;
            case 2:
                tvTaskTitle.setText("充电站任务列表");
                break;
            case 3:
                tvTaskTitle.setText("POI录像任务列表");
                break;
            case 4:
                tvTaskTitle.setText("道路任务列表");
                break;
            case 5:
                tvTaskTitle.setText("其他任务列表");
                break;
        }
        ivPoiTaskFinish = (ImageView) findViewById(R.id.iv_poiTask_finish);
        ivPoiTaskFinish.setOnClickListener(this::onClick);
        poiTaskXrv = (XRecyclerView) findViewById(R.id.poiTask_xrv);
        poiTaskXrv.setLayoutManager(new LinearLayoutManager(getActivity()));
        poiTaskXrv.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        poiTaskXrv.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        poiTaskXrv.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        poiTaskXrv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 1;
                initWork(type,true);
            }

            @Override
            public void onLoadMore() {
                initWork(type,false);
            }
        });
        poiTaskAdapter = new PoiTaskAdapter(getContext(), listBeans);
        poiTaskXrv.setAdapter(poiTaskAdapter);
        poiTaskXrv.getDefaultFootView().setNoMoreHint("已全部加载完毕");
    }

    @Override
    protected void initData() {
        super.initData();
        type = getArguments().getInt("type", 0);
        listBeans = new ArrayList<>();
        initWork(type,true);
    }

    private void initWork(int type, boolean aBoolean) {
        showLoadingDialog();
        HttpParams httpParams = new HttpParams();
        httpParams.put("type", type);
        httpParams.put("pageSize", "20");
        httpParams.put("pageNum", page);
        OkGoBuilder.getInstance()
                .Builder(getActivity())
                .url(HttpInterface.GET_COMMIT_LIST)
                .cls(HasSubmitBean.class)
                .params(httpParams)
                .token(Constant.ACCESS_TOKEN)
                .getRequest(new Callback<HasSubmitBean>() {
                    @Override
                    public void onSuccess(HasSubmitBean hasSubmitBean, int id) {
                        dismissLoadingDialog();
                        if (hasSubmitBean.getCode() == 200){
                            if (hasSubmitBean.getBody()!=null){
                                initHasSubmitBean(hasSubmitBean,aBoolean);
                            }
                        }else {
                            Toast.makeText(getActivity(), hasSubmitBean.getMessage()+"", Toast.LENGTH_SHORT).show();
                        }

                    }

                    @Override
                    public void onError(Throwable e, int id) {
                        dismissLoadingDialog();
                        Log.d("TAG", "onError: " + e.getMessage());
                        Toast.makeText(getActivity(), "请求失败", Toast.LENGTH_SHORT).show();
                    }
                });
        dismissDialog();
    }

    private void initHasSubmitBean(HasSubmitBean hasSubmitBean, boolean aBoolean) {
        if (hasSubmitBean.getCode() == 200) {
            if (aBoolean) {
                if (listBeans.size() > 0) {
                    listBeans.clear();
                }
                if (hasSubmitBean.getBody().getList()== null) {
                    Toast.makeText(getActivity(), "没有相关数据", Toast.LENGTH_SHORT).show();
                } else {
                    listBeans.addAll(hasSubmitBean.getBody().getList());
                    page++;
                }
                poiTaskAdapter.notifyDataSetChanged();
                poiTaskXrv.refreshComplete();
            } else {
                if (hasSubmitBean.getBody().getList() == null) {
                    poiTaskXrv.setNoMore(true);
                } else {
                    listBeans.addAll(hasSubmitBean.getBody().getList());
                    poiTaskAdapter.notifyDataSetChanged();
                    poiTaskXrv.loadMoreComplete();
                    page++;
                }
            }
        } else {
            Toast.makeText(getActivity(), hasSubmitBean.getMessage(), Toast.LENGTH_SHORT).show();
            Log.d("TAG", "" + hasSubmitBean.getMessage());
        }
    }






    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_poiTask_finish:
                getActivity().finish();
                break;
        }

    }
}