using Newtonsoft.Json.Linq;
using SFB;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;

public class EversoulAssetLibrary : AssetLibrary
{
    public static EversoulAssetLibrary Instance => GetInstance<EversoulAssetLibrary>();

    private List<string> assets = new List<string>();
    private Dictionary<string, string> hashDirectories = new Dictionary<string, string>();
    private Dictionary<string, string> hashAssets = new Dictionary<string, string>();
    private Dictionary<string, string> assetHashes = new Dictionary<string, string>();

    public List<string> CharacterAssets = new List<string>();
    public List<string> AnimationAssets = new List<string>();

    public List<CharacterAsset> Characters;

    IEnumerator Start()
    {
        var selectedFilePath = new string[0];

        while (true)
        {
            Debug.LogError("IMPLEMENT SETTINGS!");
            selectedFilePath = StandaloneFileBrowser.OpenFilePanel("Select catalog_eversoul.json", "", "json", false);
            if(selectedFilePath.Length > 0 && !string.IsNullOrEmpty(selectedFilePath[0]))
            {
                break;
            }
            yield return new WaitForSeconds(5);
        }

        string catalogPath = selectedFilePath[0];

        selectedFilePath = new string[0];
        while (true)
        {
            Debug.LogError("IMPLEMENT SETTINGS!");
            selectedFilePath = StandaloneFileBrowser.OpenFolderPanel("Select folder with assets", "", false);
            if (selectedFilePath.Length > 0 && !string.IsNullOrEmpty(selectedFilePath[0]))
            {
                break;
            }
            yield return new WaitForSeconds(5);
        }

        LocalFilesPath = selectedFilePath[0] + "\\";

        yield return LoadAddressableCatalog(catalogPath);

        var keys = GetAddressableKeys();
        var charas = keys.Where(k => k.Contains("Character") && k.Contains("/Prefabs/") && k.EndsWith(".prefab")).ToList();

        foreach(var chara in charas)
        {
            Characters.Add(new CharacterAsset(chara));
        }

        CharacterAssets = charas;

        EversoulInterface.Instance.CharacterSelection.SetData(Characters);

        yield break;
    }
}
