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

/**
 * 发现页面的任务说明的点击条目跳转第二个页面
 */
public class TaskExplainFragment2 extends BaseFragment implements View.OnClickListener {

    private ImageView ivTaskExplain2;
    private XRecyclerView taskExplain2Recycler;
    private ArrayList<TaskExplainInfo.BodyBean.DataBean> dataBeans2;
    private TaskExplainAdapter2 taskExplainAdapter2;

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

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

    @Override
    protected void initView() {
        super.initView();
        ivTaskExplain2 = findViewById(R.id.iv_task_explain2_finish);
        taskExplain2Recycler = findViewById(R.id.task_explain2_recycler);
        ivTaskExplain2.setOnClickListener(this::onClick);
        taskExplain2Recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        //下划线
        taskExplain2Recycler.addItemDecoration(new DividerItemDecoration(getActivity(),DividerItemDecoration.VERTICAL));
        taskExplain2Recycler.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        taskExplain2Recycler.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        //取消上拉加载，刷新功能。
//        taskExplain2Recycler.setPullRefreshEnabled(false);
//        taskExplain2Recycler.setLoadingMoreEnabled(false);

        taskExplainAdapter2 = new TaskExplainAdapter2(getActivity());
        taskExplain2Recycler.setAdapter(taskExplainAdapter2);
        taskExplain2Recycler.getDefaultFootView().setNoMoreHint("加载完毕");
        taskExplain2Recycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {

            }
        });
        taskExplainAdapter2.setOnItemClick(new TaskExplainAdapter2.OnItemClick2() {
            @Override
            public void onClick2(int pos) {
                Intent intent = new Intent(getActivity(), WebActivity.class);
                intent.putExtra("int",pos);
                startActivity(intent);

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

    private void initNetWork2() {
        showLoadingDialog();
        HttpParams httpParams = new HttpParams();
        httpParams.put("fid", "3");
        httpParams.put("pageNum", "1");
        httpParams.put("pageSize", "20");
        OkGoBuilder.getInstance()
                .Builder(getActivity())
                .url(HttpInterface.LIST_TASK_EXPLAIN)
                .cls(TaskExplainInfo.class)
                .params(new HttpParams())
                .token(Constant.ACCESS_TOKEN)
                .getRequest(new Callback<TaskExplainInfo>() {
                    @Override
                    public void onSuccess(TaskExplainInfo response, int id) {
                        dismissLoadingDialog();
                        taskExplainAdapter2.setExplainList2(response.getBody().getData());
                        Log.d("TAG", "onSuccess: " + response.getMsg() + "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_explain2_finish:
                getActivity().finish();
                break;
        }
    }
}