using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

[System.Serializable]
public class Settings
{
    public static Settings Instance = new Settings();
    public static string GameVersion => Instance.gameVersion;
    public static string AssetsUrl => Instance.assetsUrl;

    public static string ScreenshotDirectory => Format(Instance.screenshotDirectory);
    public static string SavePoseDirectory => Format(Instance.savePoseDirectory);
    public static string SaveSceneDirectory => Format(Instance.saveSceneDirectory);
    public static string AssetsDirectory => Format(Instance.assetsDirectory);
    public static string AssetListDirectory => Format(Instance.assetListDirectory);
    public static string CacheDirectory => Format(Instance.cacheDirectory);
    public static string LooseAssetsDirectory => Format(Instance.looseAssetsDirectory);


    public string gameVersion = "2.9.0";
    public string assetsUrl = "https://parade-mobile-prod-cdn.kemono-friends-3.jp/AssetBundles/139.0/6fe6f44c2162621fcc3d8246188789ca/139.0";
    public string screenshotDirectory = "{VIEWERDIR}/Screenshots";
    public string savePoseDirectory = "{VIEWERDIR}/Poses";
    public string saveSceneDirectory = "{VIEWERDIR}/Scenes";
    public string assetsDirectory = "{KF3ASSETS}/assets";
    public string assetListDirectory = "{KF3ASSETS}";
    public string cacheDirectory = "{KF3CACHE}";
    public string looseAssetsDirectory = "{VIEWERDIR}/Assets";
    public bool showHints = true;

    public static void Save()
    {
        string savePath = Path.GetFullPath(Application.dataPath + "/../Settings.txt");
        File.WriteAllText(savePath, JsonUtility.ToJson(Settings.Instance, true));
        Error.Log(Color.green, $"Settings file saved in {savePath}");

    }

    public static void Load()
    {
        string savePath = Path.GetFullPath(Application.dataPath + "/../Settings.txt");
        Debug.Log(savePath);
        if (!File.Exists(savePath))
        {
            Save();
        }
        Instance = JsonUtility.FromJson<Settings>(File.ReadAllText(savePath));
    }

    public static string Format(string value)
    {
        value = value.Replace("{VIEWERDIR}", Application.dataPath + "/../")
            .Replace("{KF3ASSETS}", Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "Low/Appirits/けもフレ３/StreamingAssets/")
            .Replace("{KF3CACHE}", Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/Temp/Appirits/けもフレ３/");
        
        if (!Directory.Exists(value))
        {
            Directory.CreateDirectory(value);
            Error.Log(Color.yellow, "Created directory: " + Path.GetFullPath(value));
        }

        return value;
    }
}
