package com.navinfo.outdoor.fragment;

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

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

import com.lzy.okgo.model.HttpParams;
import com.navinfo.outdoor.R;
import com.navinfo.outdoor.activity.WebActivity;
import com.navinfo.outdoor.adapter.TaskPrefectureAdapter;
import com.navinfo.outdoor.api.Constant;
import com.navinfo.outdoor.base.BaseFragment;
import com.navinfo.outdoor.bean.TaskExplainBean;
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.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import org.json.JSONObject;

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

/**
 * 发现-任务专区
 */
public class TaskPrefectureFragment extends BaseFragment implements View.OnClickListener {

    private ImageView mIvFindTask;
    private XRecyclerView taskRecycler;
    private List<TaskPrefectureBean.BodyBean.ListBean> dataBeanList;
    private TaskPrefectureAdapter taskPrefectureAdapter;
    private int taskPage = 1;


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

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

    @Override
    protected void initView() {
        super.initView();
        mIvFindTask = findViewById(R.id.iv_find_task);
        mIvFindTask.setOnClickListener(this::onClick);
        taskRecycler = findViewById(R.id.task_recycler);
        taskRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        taskRecycler.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        taskRecycler.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        taskRecycler.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        //取消上拉加载，刷新功能。
//        taskRecycler.setPullRefreshEnabled(false);
//        taskRecycler.setLoadingMoreEnabled(false);
        taskPrefectureAdapter = new TaskPrefectureAdapter(getActivity());
        taskRecycler.setAdapter(taskPrefectureAdapter);
        taskRecycler.getDefaultFootView().setNoMoreHint("已全部加载完毕");
        taskRecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {

            }
        });
        taskPrefectureAdapter.setClickItem(new TaskPrefectureAdapter.clickItem() {
            @Override
            public void item(int id) {
                Intent intent = new Intent(getContext(), WebActivity.class);
                intent.putExtra("id", id);
                startActivity(intent);
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        dataBeanList = new ArrayList<>();
        initNetwork();
    }

    private void initNetwork() {
        showLoadingDialog();
        HttpParams httpParams = new HttpParams();
        httpParams.put("fid", "1");
        httpParams.put("pageNum", taskPage);
        httpParams.put("pageSize", "20");
        OkGoBuilder.getInstance()
                .Builder(getActivity())
                .url(HttpInterface.LIST_TASK)
                .cls(TaskPrefectureBean.class)
                .params(httpParams)
                .token(Constant.ACCESS_TOKEN)
                .getRequest(new Callback<TaskPrefectureBean>() {
                    @Override
                    public void onSuccess(TaskPrefectureBean taskPrefectureBean, int id) {
                        dismissLoadingDialog();
                        if (taskPrefectureBean.getCode() == 200){
                            taskPrefectureAdapter.setDataBeans(taskPrefectureBean.getBody().getList());
                            taskPage++;
                        }else {
                            Toast.makeText(getActivity(), taskPrefectureBean.getMessage()+"", Toast.LENGTH_SHORT).show();
                        }

                        Log.d("TAG", "onSuccess: " + taskPrefectureBean.getMessage() + "");
                        //initTaskSpecification(taskSpecificationBean,start);
                    }

                    @Override
                    public void onError(Throwable e, int id) {
                        dismissLoadingDialog();
                        Log.d("TAG", "onError: " + e.getMessage());
                    }
                });
        dismissDialog();
    }

//    private void initTaskSpecification(TaskPrefectureBean taskPrefectureBean, boolean start) {
//        if (taskPrefectureBean.getStatus() == 0) {
//            if (start) {
//                if (dataBeanList.size() > 0) {
//                    dataBeanList.clear();
//                }
//                if (taskPrefectureBean.getBody() == null) {
//                    Toast.makeText(getActivity(), "没有相关数据", Toast.LENGTH_SHORT).show();
//                } else {
//                    dataBeanList.addAll(taskPrefectureBean.getBody().getList());
//                    page++;
//                }
//                taskPrefectureAdapter.notifyDataSetChanged();
//                taskRecycler.refreshComplete();
//            } else {
//                if (taskPrefectureBean.getBody() == null) {
//                    taskRecycler.setNoMore(true);
//                } else {
//                    dataBeanList.addAll(taskPrefectureBean.getBody().getList());
//                    taskPrefectureAdapter.notifyDataSetChanged();
//                    taskRecycler.loadMoreComplete();
//                    page++;
//                }
//            }
//        } else if (taskPrefectureBean.getStatus() == 2) {
//            if (start) {
//                if (dataBeanList.size() > 0) {
//                    dataBeanList.clear();
//                    taskPrefectureAdapter.notifyDataSetChanged();
//                }
//                taskRecycler.refreshComplete();
//            } else {
//                taskRecycler.setNoMore(true);
//            }
//            Toast.makeText(getActivity(), taskPrefectureBean.getMessage(), Toast.LENGTH_SHORT).show();
//        } else {
//            Toast.makeText(getActivity(), taskPrefectureBean.getMessage(), Toast.LENGTH_SHORT).show();
//        }
//    }

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