package com.bbitcn.silk.utils.database;

import com.bbitcn.silk.model.net.AllType;
import com.bbitcn.silk.utils.MMKVUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

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

/**
 * @Description TODO
 * @Author DuanKaiji
 * @CreateTime 2024年04月03日 15:20:10
 */
public class AllTypeDataBase {

    public static final String ALL_TYPE = "ALL_TYPE";

    public static void setAllType(List<AllType.DataDTO> temp) {
        MMKVUtil.put(ALL_TYPE, new Gson().toJson(temp));
    }

    public static String getSysIdByName(String name) {
        String json = MMKVUtil.get(ALL_TYPE, "[]");
        List<AllType.DataDTO> temp = new Gson().fromJson(json, new TypeToken<List<AllType.DataDTO>>() {
        }.getType());
        for(AllType.DataDTO dataDTO : temp) {
            if(dataDTO.getName().equals(name)) {
                return dataDTO.getSysid();
            }
        }
        return null;
    }

    public static String[] getAllTypeName() {
        String json = MMKVUtil.get(ALL_TYPE, "[]");
        List<AllType.DataDTO> temp = new Gson().fromJson(json, new TypeToken<List<AllType.DataDTO>>() {
        }.getType());
        List<String> result = new ArrayList<>();
        for(AllType.DataDTO dataDTO : temp) {
            result.add(dataDTO.getName());
        }
        return result.toArray(new String[0]);
    }

}
