package tech.nevets.jaml.util;

import com.google.gson.*;
import org.apache.commons.io.FileUtils;
import tech.nevets.jaml.JAML;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

public class CacheUtils {
    public static final String CACHE_PATH = JAML.path + "/cache/";
    public static void generateCache(String cacheName) {
        try {
            File file = new File(CACHE_PATH + "/" + cacheName + ".json");
            if (!file.exists()) {
                file.createNewFile();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void writeCache(String cacheName, String[] values) {
        try {
            Writer writer = new FileWriter(CACHE_PATH + "/" + cacheName + ".json");

            GsonBuilder builder = new GsonBuilder();
            builder.setPrettyPrinting();

            Gson gson = builder.create();
            String jsonString = gson.toJson(values);

            writer.write(jsonString);
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void addToCache(String cacheName, String value) {
        try {
            File cacheFile = new File(CACHE_PATH + "/" + cacheName + ".json");

            if (!cacheFile.exists()) {
                generateCache(cacheName);
            }

            String[] cacheArray = new Gson().fromJson(FileUtils.readFileToString(cacheFile, StandardCharsets.UTF_8), String[].class);
            ArrayList<String> cacheArrayList;

            if (cacheArray == null) {
                cacheArrayList = new ArrayList<>();
            } else {
                cacheArrayList = new ArrayList<>(List.of(cacheArray));
            }

            if (!cacheArrayList.contains(value)) {
                cacheArrayList.add(value);
            }

            writeCache(cacheName, cacheArrayList.toArray(new String[cacheArrayList.size()]));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void addToCache(String cacheName, String[] values) {
        try {
            File cacheFile = new File(CACHE_PATH + "/" + cacheName + ".json");

            if (!cacheFile.exists()) {
                generateCache(cacheName);
            }

            String[] cacheArray = new Gson().fromJson(FileUtils.readFileToString(cacheFile, StandardCharsets.UTF_8), String[].class);
            ArrayList<String> cacheArrayList;

            if (cacheArray == null) {
                cacheArrayList = new ArrayList<>();
            } else {
                cacheArrayList = new ArrayList<>(List.of(cacheArray));
            }

            for (String value : values) {
                if (!cacheArrayList.contains(value)) {
                    cacheArrayList.add(value);
                }
            }

            writeCache(cacheName, cacheArrayList.toArray(new String[cacheArrayList.size()]));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void removeFromCache(String cacheName, String value) {
        try {
            File cacheFile = new File(CACHE_PATH + "/" + cacheName + ".json");
            String[] cacheArray = new Gson().fromJson(FileUtils.readFileToString(cacheFile, StandardCharsets.UTF_8), String[].class);
            ArrayList<String> cacheArrayList;

            if (cacheArray == null) {
                cacheArrayList = new ArrayList<>();
            } else {
                cacheArrayList = new ArrayList<>(List.of(cacheArray));
            }

            cacheArrayList.remove(value);

            writeCache(cacheName, cacheArrayList.toArray(new String[cacheArrayList.size()]));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String[] getCache(String cacheName) {
        try {
            File cacheFile = new File(CACHE_PATH + "/" + cacheName + ".json");

            if (!cacheFile.exists()) {
                generateCache(cacheName);
            }

            return new Gson().fromJson(FileUtils.readFileToString(cacheFile, StandardCharsets.UTF_8), String[].class);
        } catch (IOException e) {
            e.printStackTrace();
            return new String[]{};
        }
    }
}
