package com.yutou.jianr_mg.Fragments.GameDataFragments;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.yutou.jianr_mg.Adapters.GameDataAdapters.WarAdapter;
import com.yutou.jianr_mg.Data.AppData;
import com.yutou.jianr_mg.Databases.JianRDataDataBase;
import com.yutou.jianr_mg.R;

import org.angmarch.views.NiceSpinner;
import org.json.JSONObject;

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

import cn.lemon.view.RefreshRecyclerView;

public class WarFragment extends Fragment {
    private static WarFragment fragment;
    private View view;
    private NiceSpinner src, type;
    private JianRDataDataBase database;
    private RefreshRecyclerView recyclerView;
    private WarAdapter adapter;
    private List<Map<String, JSONObject>> srcList;
    private List<List<Map<String, JSONObject>>> data;
    private Map<String, List<JSONObject>> map;

    public static WarFragment init() {
        if (fragment == null) {
            fragment = new WarFragment();
            fragment.map = new HashMap<>();
            fragment.srcList = new ArrayList<>();
            fragment.database = JianRDataDataBase.init();
        }
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view != null) {
            return view;
        }
        view = inflater.inflate(R.layout.fragment_drop, null);
        data = new ArrayList<>();
        if(srcList==null){
            srcList = new ArrayList<>();
        }
        initView();
        return view;
    }

    public void initView() {
        src = view.findViewById(R.id.src);
        type = view.findViewById(R.id.type);
        recyclerView = view.findViewById(R.id.recyclerView);
        recyclerView.setSwipeRefreshColors(0xFF437845, 0xFFE44F98, 0xFF2FAC21);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        adapter = new WarAdapter(getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.dismissSwipeRefresh();
        recyclerView.addRefreshAction(() -> {
            try {
                src.setSelectedIndex(0);
                type.setSelectedIndex(0);
                initData();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
        initSpinner();

    }
    private List<String> srcTypes;
    private ArrayAdapter srcAdapter,typeAdapter;
    private void initSpinner() {
        try {
            srcTypes = database.getTypes();
            if(srcTypes.size()==0){
                srcTypes.add("无参数");
            }
            if(srcAdapter==null){
                srcAdapter=new ArrayAdapter<>(getContext(), android.R.layout.simple_spinner_item, srcTypes);
            }
            if(typeAdapter==null){
                typeAdapter=new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, database.getNames(srcTypes.get(0)));
            }
            src.setAdapter(srcAdapter);
            src.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    typeAdapter.clear();
                    typeAdapter.addAll(database.getNames(srcTypes.get(i)));
                    typeAdapter.notifyDataSetChanged();
                    initData();
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
            src.setSelectedIndex(0);

            List<String> types=database.getNames(srcTypes.get(0));
            if(types.size()==0){
                types.add("无参数");
            }
            typeAdapter.clear();
            typeAdapter.addAll(types);
            type.setAdapter(typeAdapter);
            type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                    initData();
                }

                @Override
                public void onNothingSelected(AdapterView<?> adapterView) {

                }
            });
            typeAdapter.notifyDataSetChanged();
            type.setSelectedIndex(0);

            srcList = new ArrayList<>();
            initData();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void initData() {
        try {
            data.clear();
            new Thread(() -> {
                try {
                    srcList = database.getDataList(database.getTNameId((String) typeAdapter.getItem(type.getSelectedIndex())));
                    for (int i = 0; i < srcList.size(); i++) {
                        Map<String, JSONObject> maps = srcList.get(i);
                        JSONObject item = maps.get("data");

                        if (item == null) {
                            return;
                        }

                        System.out.println(">>>>>>"+item.getInt("mapId"));
                        if(Objects.requireNonNull(maps.get("DealNode")).getString("type").equals("DealNode")) {
                            getItem(i, srcList);
                        }
                    }
                    AppData.handler.post(() -> {
                        adapter.clear();
                        adapter.addAll(data);
                        adapter.notifyDataSetChanged();
                        recyclerView.dismissSwipeRefresh();
                        recyclerView.showNoMore();
                    });

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

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

    private void getItem(int index, List<Map<String, JSONObject>> srcList) {
        List<Map<String,JSONObject>> list = new ArrayList<>();
        try {
            for (int i = index; i < srcList.size(); i++) {
                Map<String, JSONObject> maps = srcList.get(i);
                JSONObject item = maps.get("data");
                if (item == null) {
                    return;
                }
                if (Objects.requireNonNull(maps.get("DealNode")).getString("type").equals("DealNode")) {

                    if (item.getInt("mapId") % 10 == 2&&i>index) {
                        saveData(list);
                        return;
                    }else{
                        list.add(srcList.get(i));
                    }
                }

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

    private void saveData(List<Map<String, JSONObject>> list) {
        if (list.size() > 0)
            data.add(list);
    }
}
