package com.yutou.jianr_mg.Fragments;


import android.os.Bundle;
import android.os.Handler;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.fastjson.JSON;
import com.yutou.jianr_mg.Adapters.MainUtilsAdapter;
import com.yutou.jianr_mg.Data.AppData;
import com.yutou.jianr_mg.Data.MAppTools;
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 org.json.JSONObject;

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

import cn.lemon.view.RefreshRecyclerView;
import dalvik.system.DexClassLoader;

public class UtilsFragments extends Fragment {
    private static UtilsFragments utilsFragments=null;
    private View view;
    private RefreshRecyclerView recyclerView;
    private int min=0,max=0;
    private boolean isStart=true;
    private List<MAppTools> datas;
    private Handler handler;
    private MainUtilsAdapter adapter;

    public static UtilsFragments init(){
        if(utilsFragments==null){
            utilsFragments=new UtilsFragments();
        }
        return utilsFragments;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if(view==null){
            view=inflater.inflate(R.layout.fragment_mg,null);
        }
        initView();
        return view;
    }
    private void initView(){
        handler=new Handler();
        adapter=new MainUtilsAdapter(getActivity());
        recyclerView=view.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(getContext(),3));
        recyclerView.setAdapter(adapter);
        recyclerView.addRefreshAction(() -> {
            min=0;
            max=0;
            isStart=true;
            adapter.clear();
            initData();
        });
        recyclerView.setLoadMoreAction(()->{
            recyclerView.showNoMore();
        });
        initData();
        recyclerView.showSwipeRefresh();
    }
    private void initData(){
        try{
            datas=new ArrayList<>();
            JSONObject json=new JSONObject();
            json.put("min",min);
            json.put("max",max);
            HttpUtils.post(HttpApi.HOME_URL + HttpApi.MG_TOOLS, json, new HttpInterface() {
                @Override
                public void httpGetData(String string, int code) {
                    try{
                        JSONObject json=new JSONObject(string);
                        datas= JSON.parseArray(json.getJSONArray("data").toString(),MAppTools.class);
                        setLocalData();
                        debug();
                        handler.post(()->setData());
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }

                @Override
                public void httpError(Exception e) {
                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void debug(){


    }
    private Map<String, DexClassLoader> map;
    private void setLocalData(){
        try{
            map=AppData.plugsin;
            if(map==null||map.isEmpty()){
                return;
            }
            Iterator iterator=map.keySet().iterator();
            while (iterator.hasNext()){
                String key= (String) iterator.next();
                if(map.containsKey(key)){
                    Class plugins=map.get(key).loadClass("com.yutou.jianr_mg.plugins.Data");
                    MAppTools tools=new MAppTools();
                    tools.setName((String) plugins.getField("title").get(null));
                    tools.setPluginName((String) plugins.getField("name").get(null));
                    tools.setImage((String) plugins.getField("image").get(null));
                    tools.setMainFunction((String) plugins.getField("mainFunction").get(null));
                    tools.setUrl((String) plugins.getField("url").get(null));
                    tools.setPermission((Integer) plugins.getField("permission").get(null));
                    tools.setIsshow(1);

                    List<MAppTools> tmpTools=new ArrayList<>();
                    for (MAppTools data : datas) {
                        if (data.getMainFunction().equals(tools.getMainFunction())) {
                            tmpTools.add(data);
                        }
                    }
                    datas.removeAll(tmpTools);
                    datas.add(tools);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    private void setData() {
        System.out.println(datas.size());
       if(datas.size()>0){
           adapter.addAll(datas);
           recyclerView.dismissSwipeRefresh();
       }else{
           recyclerView.dismissSwipeRefresh();
           recyclerView.showNoMore();
       }
    }
}
