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.fragment.app.FragmentManager;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.lzy.okgo.model.HttpParams;
import com.navinfo.outdoor.R;
import com.navinfo.outdoor.activity.FragmentManagement;
import com.navinfo.outdoor.adapter.IssueProblemAdapter;
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.umeng.umcrash.UMCrash;

import org.json.JSONObject;

import java.util.ArrayList;

import okhttp3.OkHttpClient;

/**
 * 常见问题页面的fragment
 */
public class IssueFragment extends BaseFragment implements View.OnClickListener {
    private ImageView ivIssueFinish;
    private XRecyclerView mainIssueRecycler;
    private IssueProblemAdapter issueProblemAdapter;

    public static IssueFragment newInstance(Bundle bundle) {
        IssueFragment fragment = new IssueFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    @Override
    protected int getLayout() {
        return R.layout.issue_fragment;
    }

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

    private void initNetWork(boolean b) {
        showLoadingDialog();
        HttpParams httpParams = new HttpParams();
        httpParams.put("fid", "1");
        httpParams.put("pageNum", "1");
        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){
                            issueProblemAdapter.setExplainList(response.getBody().getList());
                        }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();
                        // Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_SHORT).show();
                        Log.d("TAG", "onError: " + e.getMessage());
                    }
                });
        dismissDialog();
    }

    @Override
    protected void initView() {
        super.initView();
        ivIssueFinish =findViewById(R.id.iv_issue_finish);
        mainIssueRecycler =findViewById(R.id.main_issue_recycler);
        ivIssueFinish.setOnClickListener(this::onClick);
        mainIssueRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        //设置下划线
        mainIssueRecycler.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        mainIssueRecycler.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mainIssueRecycler.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        //取消下拉刷新上拉加载
        mainIssueRecycler.setPullRefreshEnabled(false);
        mainIssueRecycler.setLoadingMoreEnabled(false);
        issueProblemAdapter = new IssueProblemAdapter(getActivity());
        mainIssueRecycler.setAdapter(issueProblemAdapter);
        mainIssueRecycler.getDefaultFootView().setNoMoreHint("成功加载完毕");
        mainIssueRecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {}
            @Override
            public void onLoadMore() {}
        });

        issueProblemAdapter.setOnIssueClick(new IssueProblemAdapter.OnIssueClick() {
            @Override
            public void onClick(int pos) {
                Intent issueWeb = new Intent(getActivity(), FragmentManagement.class);
                issueWeb.putExtra("tag",16);
                startActivity(issueWeb);
            }

        });
    }

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