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.EventPrefectureAdapter;
import com.navinfo.outdoor.api.Constant;
import com.navinfo.outdoor.base.BaseFragment;
import com.navinfo.outdoor.bean.EventPrefectureBean;
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 EventPrefectureFragment extends BaseFragment implements View.OnClickListener {

    private ImageView ivEventTask;
    private XRecyclerView eventRecycler;
    private List<TaskPrefectureBean.BodyBean.ListBean> taskExplainBean;
    private EventPrefectureAdapter eventPrefectureAdapter;
    private int eventPage = 1;


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



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

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

    private void initNetwork() {
        showLoadingDialog();
        HttpParams httpParams = new HttpParams();
        httpParams.put("fid", "2");
        httpParams.put("pageNum", eventPage);
        httpParams.put("pageSize", "20");
        OkGoBuilder.getInstance()
                .Builder(getActivity())
                .url(HttpInterface.LIST_EVENT)
                .cls(TaskPrefectureBean.class)
                .params(httpParams)
                .token(Constant.ACCESS_TOKEN)
                .getRequest(new Callback<TaskPrefectureBean>() {
                    @Override
                    public void onSuccess(TaskPrefectureBean taskPrefectureBean, int id) {
                        dismissLoadingDialog();
                        if (taskPrefectureBean.getCode() == 200){
                            eventPrefectureAdapter.setDataBeans(taskPrefectureBean.getBody().getList());
                            eventPage++;
                        }else {
                            Toast.makeText(getActivity(), taskPrefectureBean.getMessage()+"", Toast.LENGTH_SHORT).show();
                        }

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

                        //initEventPrefecture(eventPrefectureBean,b);
                    }

                    @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();
    }

//    private void initEventPrefecture(TaskPrefectureBean taskPrefectureBean, boolean start) {
//        if (taskPrefectureBean.getStatus() == 0) {
//            if (start) {
//                if (taskExplainBean.size() > 0) {
//                    taskExplainBean.clear();
//                }
//                if (taskPrefectureBean.getBody() == null) {
//                    Toast.makeText(getActivity(), "没有相关数据", Toast.LENGTH_SHORT).show();
//                } else {
//                    taskExplainBean.addAll(taskPrefectureBean.getBody().getList());
//                    page++;
//                }
//                eventPrefectureAdapter.notifyDataSetChanged();
//                eventRecycler.refreshComplete();
//            } else {
//                if (taskPrefectureBean.getBody() == null) {
//                    eventRecycler.setNoMore(true);
//                } else {
//                    taskExplainBean.addAll(taskPrefectureBean.getBody().getList());
//                    eventPrefectureAdapter.notifyDataSetChanged();
//                    eventRecycler.loadMoreComplete();
//                    page++;
//                }
//            }
//        } else if (taskPrefectureBean.getStatus() == 2) {
//            if (start) {
//                if (taskExplainBean.size() > 0) {
//                    taskExplainBean.clear();
//                    eventPrefectureAdapter.notifyDataSetChanged();
//                }
//                eventRecycler.refreshComplete();
//            } else {
//                eventRecycler.setNoMore(true);
//            }
//            Toast.makeText(getActivity(), taskPrefectureBean.getMessage(), Toast.LENGTH_SHORT).show();
//        } else {
//            Toast.makeText(getActivity(), taskPrefectureBean.getMessage(), Toast.LENGTH_SHORT).show();
//        }
//    }

    @Override
    protected void initView() {
        super.initView();
        ivEventTask = findViewById(R.id.iv_event_task);
        ivEventTask.setOnClickListener(this::onClick);
        eventRecycler = findViewById(R.id.event_recycler);
        eventRecycler.setLayoutManager(new LinearLayoutManager(getActivity()));
        eventRecycler.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        eventRecycler.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        eventRecycler.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        //取消上拉加载，刷新功能。
//        eventRecycler.setPullRefreshEnabled(false);
//        eventRecycler.setLoadingMoreEnabled(false);
        eventPrefectureAdapter = new EventPrefectureAdapter(getActivity());
        eventRecycler.setAdapter(eventPrefectureAdapter);
        eventRecycler.getDefaultFootView().setNoMoreHint("已全部加载完毕");
        eventRecycler.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {

            }

            @Override
            public void onLoadMore() {

            }
        });
        eventPrefectureAdapter.setClickItem(new EventPrefectureAdapter.clickItem() {
            @Override
            public void item(int aInt) {
                Intent intent = new Intent(getContext(), WebActivity.class);
                intent.putExtra("id",aInt);
                startActivity(intent);
            }
        });
    }

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