package com.xwkj.jcjcgz.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.hjq.toast.ToastUtils;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;

import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.constant.SpinnerStyle;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.xwkj.jcjcgz.di.component.DaggerTaskDxzpComponent;
import com.xwkj.jcjcgz.mvp.contract.TaskDxzpContract;
import com.xwkj.jcjcgz.mvp.model.entity.TaskDxzpEntity;
import com.xwkj.jcjcgz.mvp.model.entity.TaskEntity;
import com.xwkj.jcjcgz.mvp.presenter.TaskDxzpPresenter;

import com.xwkj.jcjcgz.R;
import com.xwkj.jcjcgz.mvp.ui.adapter.TaskAdapter;
import com.xwkj.jcjcgz.mvp.ui.adapter.TaskDxzpAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description: 电信诈骗
 * <p>
 * Created by MVPArmsTemplate on 07/16/2024 14:19
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class TaskDxzpFragment extends BaseFragment<TaskDxzpPresenter> implements TaskDxzpContract.View, OnRefreshListener, OnLoadMoreListener {
    @BindView(R.id.task_rv)
    RecyclerView rv;
    @BindView(R.id.smart_refresh)
    SmartRefreshLayout smart_refresh;

    private List<TaskDxzpEntity.Task> list;
    private TaskDxzpAdapter mAdapter;
    private int pageNo = 1;
    private int pageSize = 10;
    public static TaskDxzpFragment newInstance() {
        TaskDxzpFragment fragment = new TaskDxzpFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(@NonNull AppComponent appComponent) {
        DaggerTaskDxzpComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_task_dxzp, container, false);
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        smart_refresh.setEnableRefresh(false);
        //设置 Header
        smart_refresh.setRefreshHeader(new ClassicsHeader(getActivity()).setEnableLastTime(true));
        //设置 Footer
        smart_refresh.setRefreshFooter(new ClassicsFooter(getActivity()).setSpinnerStyle(SpinnerStyle.FixedBehind));

        smart_refresh.setOnRefreshListener(this);
        smart_refresh.setOnLoadMoreListener(this);
        rv.setNestedScrollingEnabled(false);
        rv.setFocusable(false);
        rv.setLayoutManager(new LinearLayoutManager(getActivity()));
        list = new ArrayList<>();
        mAdapter = new TaskDxzpAdapter(list);
        rv.setAdapter(mAdapter);
        requestData(pageNo = 1, pageSize);
    }

    /**
     * 通过此方法可以使 Fragment 能够与外界做一些交互和通信, 比如说外部的 Activity 想让自己持有的某个 Fragment 对象执行一些方法,
     * 建议在有多个需要与外界交互的方法时, 统一传 {@link Message}, 通过 what 字段来区分不同的方法, 在 {@link #setData(Object)}
     * 方法中就可以 {@code switch} 做不同的操作, 这样就可以用统一的入口方法做多个不同的操作, 可以起到分发的作用
     * <p>
     * 调用此方法时请注意调用时 Fragment 的生命周期, 如果调用 {@link #setData(Object)} 方法时 {@link Fragment#onCreate(Bundle)} 还没执行
     * 但在 {@link #setData(Object)} 里却调用了 Presenter 的方法, 是会报空的, 因为 Dagger 注入是在 {@link Fragment#onCreate(Bundle)} 方法中执行的
     * 然后才创建的 Presenter, 如果要做一些初始化操作,可以不必让外部调用 {@link #setData(Object)}, 在 {@link #initData(Bundle)} 中初始化就可以了
     * <p>
     * Example usage:
     * <pre>
     * public void setData(@Nullable Object data) {
     *     if (data != null && data instanceof Message) {
     *         switch (((Message) data).what) {
     *             case 0:
     *                 loadData(((Message) data).arg1);
     *                 break;
     *             case 1:
     *                 refreshUI();
     *                 break;
     *             default:
     *                 //do something
     *                 break;
     *         }
     *     }
     * }
     *
     * // call setData(Object):
     * Message data = new Message();
     * data.what = 0;
     * data.arg1 = 1;
     * fragment.setData(data);
     * </pre>
     *
     * @param data 当不需要参数时 {@code data} 可以为 {@code null}
     */
    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }
    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        list.clear();
        mAdapter.notifyDataSetChanged();
        requestData(pageNo = 1, pageSize);
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {

        requestData(++pageNo, pageSize);
    }

    private void requestData(int pageNo, int pageSize) {

        Map<String, String> map = new HashMap<>();
        map.put("current", pageNo + "");
        map.put("size", pageSize + "");
        if (mPresenter != null) {
            mPresenter.getList(map);
        }

    }

    @Override
    public void onSuccess(String result) {
        smart_refresh.finishLoadMore();
        smart_refresh.finishRefresh();
        TaskDxzpEntity taskEntity = new Gson().fromJson(result, TaskDxzpEntity.class);
        if (taskEntity.getCode() == 200) {
            List<TaskDxzpEntity.Task> records = taskEntity.getData().getRecords();
            if (records.size() > 0 && records != null) {
                list.addAll(records);
                list.addAll(records);
                list.addAll(records);
                list.addAll(records);
                mAdapter.notifyDataSetChanged();
            } else {
                ToastUtils.show("没有更多数据了");
            }
        } else {
            ToastUtils.show("获取数据出错，请重试");
        }
    }
}
