package com.yutou.jianr_mg.Fragments.GameDataFragments;

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.LinearLayoutManager;
import android.util.ArrayMap;
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.DropAdapter;
import com.yutou.jianr_mg.Data.AppData;
import com.yutou.jianr_mg.Databases.JianRDataDataBase;
import com.yutou.jianr_mg.R;
import com.yutou.jianr_mg.Tools.JianRUtils;

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

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.lemon.view.RefreshRecyclerView;

public class DropFragment extends Fragment {
    private static DropFragment fragment;
    private View view;
    private NiceSpinner src, type;
    private JianRDataDataBase database;
    private RefreshRecyclerView recyclerView;
    private DropAdapter adapter;

    public static DropFragment init() {
        if (fragment == null) {
            fragment = new DropFragment();
        }
        return fragment;
    }

    public DropFragment() {
        database = JianRDataDataBase.init();
    }

    @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);
        initView();
        initData();
        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 DropAdapter(getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.dismissSwipeRefresh();
        recyclerView.addRefreshAction(() -> {
            try {
                src.setSelectedIndex(0);
                type.setSelectedIndex(0);
                getData(0);
            }catch (Exception e){
                e.printStackTrace();
            }
            new Handler().postAtTime(() -> {
                recyclerView.dismissSwipeRefresh();
                recyclerView.showNoMore();
            }, 0, 100);
        });
    }

    private List<String> srcTypes;
    private ArrayAdapter srcAdapter,typeAdapter;

    private void initData() {
        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<>(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();
                    getData(type.getSelectedIndex());
                }

                @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) {
                    getData(i);
                }

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

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

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

    private void getData(int i) {
        try {
            if(srcTypes.size()==0){
                return;
            }
            new Thread(() -> {
                try {
                    String name = database.getNames(srcTypes.get(src.getSelectedIndex())).get(i);
                    List<JSONObject> list = database.getDrops(name);
                    List<Map<String, String>> ldata = new ArrayList<>();
                    for (JSONObject json : list) {
                        Map<String, String> map = new ArrayMap<>();
                        String cid = json.get("cid").toString();
                        String title = json.getString("title");
                        map.put("icon", "http://jianr.jianrmod.cn/jianr/ship/S_NORMAL_" + JianRUtils.jianCidToid(cid) + ".png");
                        map.put("name", title);
         /*   map.put("time", new SimpleDateFormat("yyyy年MM月dd HH:mm:ss")
                    .format(new Date(Long.valueOf(json.getJSONObject("data").getString("createTime")))));*/
                        map.put("time",SimpleDateFormat.getDateTimeInstance(DateFormat.YEAR_FIELD,DateFormat.SHORT).format(new Date(Long.valueOf(json.getJSONObject("data").getString("createTime")))));
                        ldata.add(map);

                    }
                    Collections.reverse(ldata);
                    AppData.handler.post(() -> {
                        adapter.clear();
                        adapter.addAll(ldata);
                        adapter.notifyDataSetChanged();
                        recyclerView.showNoMore();
                    });

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

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