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.FragmentManagement;
import com.navinfo.outdoor.adapter.TaskExplainAdapter;
import com.navinfo.outdoor.api.Constant;
import com.navinfo.outdoor.base.BaseFragment;
import com.navinfo.outdoor.bean.TaskExplainInfo;
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.List;

/**
 * 发现-任务说明
 */
public class TaskExplainFragment extends BaseFragment implements View.OnClickListener {
    private ImageView ivFindTaskExplain;
    private XRecyclerView taskExplainRecycle;

    private TaskExplainAdapter taskExplainAdapter;
    private int taskPage = 1;
    private Integer fId;


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

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

    @Override
    protected void initView() {
        super.initView();
        ivFindTaskExplain = findViewById(R.id.iv_find_task_explain);
        taskExplainRecycle =findViewById(R.id.task_explain_recycler);
        ivFindTaskExplain.setOnClickListener(this::onClick);

        taskExplainRecycle.setLayoutManager(new LinearLayoutManager(getActivity()));
        //下划线
        taskExplainRecycle.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));

        taskExplainRecycle.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        taskExplainRecycle.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        //取消上啦加载下拉刷新
//        taskExplainRecycle.setPullRefreshEnabled(false);
//        taskExplainRecycle.setLoadingMoreEnabled(false);
        taskExplainAdapter = new TaskExplainAdapter(getActivity());
        taskExplainRecycle.setAdapter(taskExplainAdapter);
        taskExplainRecycle.getDefaultFootView().setNoMoreHint("加载完毕");
        taskExplainRecycle.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {

            }
        });
        taskExplainAdapter.setOnItemClick(new TaskExplainAdapter.OnItemClick() {
            @Override
            public void onClick(int pos) {
                Intent itemIntent = new Intent(getActivity(), FragmentManagement.class);
                itemIntent.putExtra("tag", 10);
                itemIntent.putExtra("id",fId);
                startActivity(itemIntent);

            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        initNetWork();
    }

    private void initNetWork() {
        showLoadingDialog();
        HttpParams httpParams = new HttpParams();
        httpParams.put("fid", "3");
        httpParams.put("pageNum", taskPage);
        httpParams.put("pageSize", "20");
        OkGoBuilder.getInstance()
                .Builder(getActivity())
                .url(HttpInterface.LIST_TASK_EXPLAIN)
                .cls(TaskPrefectureBean.class)
                .params(httpParams)
                .token(Constant.ACCESS_TOKEN)
                .getRequest(new Callback<TaskPrefectureBean>() {
                    @Override
                    public void onSuccess(TaskPrefectureBean response, int id) {
                        dismissLoadingDialog();
                        if (response.getCode() == 200){
                            taskExplainAdapter.setExplainList(response.getBody().getList());
                            TaskPrefectureBean.BodyBean body = response.getBody();
//                            List<TaskPrefectureBean.BodyBean.ListBean> list = body.getList();
//                            for (int i = 0; i < list.size(); i++) {
//                                fId = list.get(i).getId();
//                            }
                            taskPage++;
                        }else {
                            Toast.makeText(getActivity(), response.getMessage()+"", Toast.LENGTH_SHORT).show();
                        }
                        Log.d("TAG", "onSuccess: " + response.getMessage() + "");
                    }

                    @Override
                    public void onError(Throwable e, int id) {
                        dismissLoadingDialog();

                        Log.d("TAG", "onError: " + e.getMessage());
                    }
                });
        dismissDialog();
    }


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

        }
    }
}
