package com.yutou.jianr_mg.Fragments;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.appcompat.widget.LinearLayoutCompat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yutou.jianr_mg.Adapters.ModRecyclerAdapter;
import com.yutou.jianr_mg.Data.MAppMg;
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.Collections;
import java.util.List;

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

public class ModListFragmentData {
    private MAppMg mgList;
    private RefreshRecyclerView recyclerView;
    private ModRecyclerAdapter adapter;
    private List<TMod> datas;
    private int min=0, max=10,index=-1;
    private boolean isInit=true;
    private Handler handler;
    private View view;
    private Context context;
    private Activity activity;
    public View onCreateView(Context context,Activity activity,LayoutInflater inflater){
        this.context=context;
        this.activity=activity;
        if(view!=null){
            return view;
        }
        view=inflater.inflate(R.layout.fragment_mg,null);
        handler=new Handler();
        initViews();
        return view;
    }
    public void setMgList(MAppMg mgList){
        this.mgList=mgList;
    }
    public void setIndex(int index){
        this.index=index;
    }
    public void initViews(){
        recyclerView=view.findViewById(R.id.recyclerView);
        recyclerView.setSwipeRefreshColors(0xFF437845,0xFFE44F98,0xFF2FAC21);
        recyclerView.setLayoutManager(new GridLayoutManager(context,3));
        adapter=new ModRecyclerAdapter(context);
        TextView endText=new TextView(context);
        endText.setLayoutParams(new LinearLayoutCompat.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Utils.getPixelsFromDp(activity,48)));
        endText.setTextSize(16);
        endText.setGravity(Gravity.CENTER);
        endText.setText(" --已经没有了--");
        adapter.setFooter(endText);
        recyclerView.setAdapter(adapter);
        recyclerView.addRefreshAction(new Action() {
            @Override
            public void onAction() {
                min=0;
                max=10;
                isInit=true;
                initData();
            }
        });
        recyclerView.setLoadMoreAction(new Action() {
            @Override
            public void onAction() {
                if(isInit){
                    recyclerView.showNoMore();
                }else {
                    min = max;
                    max += 10;
                    initData();
                }
            }
        });
        recyclerView.showSwipeRefresh();
        initData();
    }
    private void initData(){
        try {
            datas=new ArrayList<>();
            Log.i("载入数据");
            JSONObject json=new JSONObject();
            json.put("min_str",min);
            json.put("max_str",max);
            if(mgList.getIndexs()>3)
                json.put("jType",mgList.getIndexs());
            else
                json.put("mType",mgList.getIndexs());
            final String url=mgList.getIndexs()==0? HttpApi.MOD_ALL: HttpApi.MOD_TYPE;
            HttpUtils.post(HttpApi.HOME_URL + url, json, new HttpInterface() {
                @Override
                public void httpError(Exception e) {

                }

                @Override
                public void httpGetData(String string, int code) {
                    Log.i(url,string);
                    try{
                        JSONObject json=new JSONObject(string);
                        if(json.getInt("code")==100){
                            datas= Utils.ListRemoveNull(JSON.parseArray(json.getString("data"), TMod.class));
                            Collections.sort(datas);
                            if(index!=-1) {
                                List<TMod> tmp = new ArrayList<>();
                                for (TMod data : datas) {
                                    if (!data.getModtype().equals(index + "")) {
                                        tmp.add(data);
                                    }
                                }
                                datas.removeAll(tmp);
                                tmp.clear();
                                tmp = null;
                            }
                        }
                        handler.post(new Runnable() {
                            @Override
                            public void run() {
                                setData();
                            }
                        });
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void setData(){
        if(isInit){
            adapter.clear();
            adapter.addAll(datas);
            recyclerView.getRecyclerView().scrollToPosition(0);
        }else{
            adapter.addAll(datas);
        }
        recyclerView.dismissSwipeRefresh();
    }
}
