package com.yutou.jianr_mg.views;

import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import com.alibaba.fastjson.JSON;
import com.umeng.analytics.MobclickAgent;
import com.yutou.jianr_mg.Adapters.ModRecyclerAdapter;
import com.yutou.jianr_mg.Data.TMod;
import com.yutou.jianr_mg.Interfaces.HttpInterface;
import com.yutou.jianr_mg.Network.HttpApi;
import com.yutou.jianr_mg.Network.HttpUtils;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.Log;
import com.yutou.jianr_mg.Tools.Utils;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.lemon.view.RefreshRecyclerView;
import cn.lemon.view.adapter.Action;

public class FavoritesActivity extends AppCompatActivity {
    private static final String TAG = "收藏夹";
    private RefreshRecyclerView recyclerView;
    private ModRecyclerAdapter adapter;
    private List<TMod> modList;
    private Handler handler;
    private boolean isReload = false, isNext = true;
    private int con = 0, max = 10;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorite);
        handler = new Handler();
        initView();
    }

    private void initView() {
        recyclerView = findViewById(R.id.recyclerView);
        recyclerView.setSwipeRefreshColors(0xFF437845, 0xFFE44F98, 0xFF2FAC21);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        adapter = new ModRecyclerAdapter(this);
        recyclerView.setAdapter(adapter);
        recyclerView.addRefreshAction(new Action() {
            @Override
            public void onAction() {
                con = 0;
                max = 10;
                isReload = true;
                getData();
            }
        });
        recyclerView.setLoadMoreAction(new Action() {
            @Override
            public void onAction() {
                con = max;
                max += 10;
                isReload = false;
                getData();
            }
        });
        recyclerView.showSwipeRefresh();
        getData();
    }

    private void getData() {
        try {
            modList = new ArrayList<>();
            JSONObject json = new JSONObject();
            json.put("min", con);
            json.put("max", max);
            HttpUtils.post(HttpApi.HOME_URL + HttpApi.MOD_LIST_COLLCET, json, new HttpInterface() {
                @Override
                public void httpGetData(String string, int code) {
                    try {
                        JSONObject json = new JSONObject(string);
                        final String msg = json.getString("msg");
                        if (json.getInt("code") != 100) {
                            Log.i(TAG, string);
                            handler.post(() -> {
                                Utils.toast(FavoritesActivity.this, msg);
                                recyclerView.destroyDrawingCache();
                            });
                            return;
                        }
                        modList = Utils.ListRemoveNull(JSON.parseArray(json.getString("data"), TMod.class));
                        isNext = json.getBoolean("next");
                        handler.post(() -> setData());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void httpError(Exception e) {

                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void setData() {
        System.out.println("列表数量");
        System.out.println(modList.size());
        recyclerView.dismissSwipeRefresh();
        if (isReload) {
            adapter.clear();
            adapter.addAll(modList);
            //     recyclerView.getRecyclerView().scrollToPosition(0);
        } else {
            adapter.addAll(modList);
        }
        if (!isNext)
            recyclerView.showNoMore();
    }
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
}
