package com.navinfo.outdoor.fragment;

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.adapter.CapacityItemAdapter;
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 org.json.JSONObject;

/**
 * 能力测评的第二个页面
 */
public class CapacityEvaluationFragment2 extends BaseFragment implements View.OnClickListener {

    private ImageView ivCapItemFinish;
    private XRecyclerView capacityRlv;
    private CapacityItemAdapter capacityItemAdapter;


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

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

    @Override
    protected void initView() {
        super.initView();
        ivCapItemFinish = findViewById(R.id.iv_task_capacity2_finish);
        capacityRlv = findViewById(R.id.task_capacity2_recycler);
        ivCapItemFinish.setOnClickListener(this::onClick);
        capacityRlv.setLayoutManager(new LinearLayoutManager(getActivity()));
        capacityRlv.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        capacityRlv.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        capacityRlv.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        capacityRlv.setPullRefreshEnabled(false);
        capacityRlv.setLoadingMoreEnabled(false);
        capacityItemAdapter = new CapacityItemAdapter(getActivity());
        capacityRlv.setAdapter(capacityItemAdapter);
        capacityRlv.getDefaultFootView().setNoMoreHint("已经加载成功");
        capacityRlv.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {

            }
        });
        capacityItemAdapter.setSetOnClick(new CapacityItemAdapter.setOnClick() {
            @Override
            public void onClick(int position) {
            }
        });

    }

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

        initNetWorks(true);
    }

    private void initNetWorks(boolean b) {
        showLoadingDialog();
        OkGoBuilder.getInstance()
                .Builder(getActivity())
                .url(HttpInterface.MSG_LISt)
                .token(Constant.ACCESS_TOKEN)
                .params(new HttpParams())
                .cls(TaskPrefectureBean.class)
                .getRequest(new Callback<TaskPrefectureBean>() {
                    @Override
                    public void onSuccess(TaskPrefectureBean response, int id) {
                        dismissLoadingDialog();
                        if (response.getCode() == 200){
                            capacityItemAdapter.setCapItemList(response.getBody().getList());
                        }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());
                    }
                });
        dismissDialog();
    }

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