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.CapacityEvaluationAdapter;

import com.navinfo.outdoor.api.Constant;
import com.navinfo.outdoor.base.BaseFragment;

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 com.umeng.umcrash.UMCrash;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 发现-能力测评
 */
public class CapacityEvaluationFragment extends BaseFragment implements View.OnClickListener {
    private ImageView ivEvaluationTaskFinish;
    private XRecyclerView capacityEvaluationRecycler;
    private CapacityEvaluationAdapter capacityEvaluationAdapter;
    private int capacityPage = 1;


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

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

    @Override
    protected void initView() {
        super.initView();
        ivEvaluationTaskFinish = findViewById(R.id.iv_evaluation_task_finish);
        capacityEvaluationRecycler = findViewById(R.id.capacity_evaluation_recycler);
        ivEvaluationTaskFinish.setOnClickListener(this::onClick);
        capacityEvaluationRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        capacityEvaluationRecycler.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        capacityEvaluationRecycler.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        capacityEvaluationRecycler.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        //取消上拉加载，刷新功能
//        capacityEvaluationRecycler.setPullRefreshEnabled(false);
//        capacityEvaluationRecycler.setLoadingMoreEnabled(false);
        capacityEvaluationAdapter = new CapacityEvaluationAdapter(getActivity());
        capacityEvaluationRecycler.setAdapter(capacityEvaluationAdapter);
        capacityEvaluationRecycler.getDefaultFootView().setNoMoreHint("加载成功");
        capacityEvaluationRecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {

            }
        });
        capacityEvaluationAdapter.setOnCapacityClick(new CapacityEvaluationAdapter.OnCapacityClick() {
            @Override
            public void onClick(int id) {
                Intent capacityEvaWeb = new Intent(getActivity(), FragmentManagement.class);
                capacityEvaWeb.putExtra("tag", 14);
                startActivity(capacityEvaWeb);
            }
        });
    }

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

        initNewWork();
    }

    private void initNewWork() {
        showLoadingDialog();
        HttpParams httpParams = new HttpParams();
        httpParams.put("fid", "4");
        httpParams.put("pageNum", capacityPage);
        httpParams.put("pageSize", "10");
        OkGoBuilder.getInstance()
                .Builder(getActivity())
                .url(HttpInterface.MSG_LISt)
                .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){
                            capacityEvaluationAdapter.setCapacityList(response.getBody().getList());
                            capacityPage++;
                        }else {
                            Toast.makeText(getActivity(), response.getMessage(), Toast.LENGTH_SHORT).show();
                        }


                        Log.d("TAG", "onSuccess: " + response.getMessage() + "sssssssssssss");
                    }

                    @Override
                    public void onError(Throwable e, int id) {
                        dismissLoadingDialog();
                        Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        Log.d("TAG", "onError: " + e.getMessage());
                        /**
                         * 友盟+
                         * 返回自定义报错信息
                         */
                        UMCrash.generateCustomLog("网络请求报错-位置：CapacityEvaluationFragment" + e.getMessage(), "UmengException");
                    }
                });
        dismissDialog();
    }

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