
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class UISaveLoadPanel : MonoBehaviour
{
    public ScrollRect ScrollScene;
    public ScrollRect ScrollPose;

    public Button ButtonScene;
    public Button ButtonPose;

    public List<UISceneContainer> ScenePresetEntries = new List<UISceneContainer>();
    public List<UIPoseContainer> PosePresetEntries = new List<UIPoseContainer>();

    public void AddPose(UIPoseContainer pose)
    {
        PosePresetEntries.Add(pose);
    }

    public void AddScene(UISceneContainer scene)
    {
        ScenePresetEntries.Add(scene);
    }

    public void OnPoseButton()
    {
        ButtonScene.interactable = true;
        ButtonPose.interactable = false;
        ScrollScene.gameObject.SetActive(false);
        ScrollPose.gameObject.SetActive(true);
    }

    public void OnSceneButton()
    {
        ButtonScene.interactable = false;
        ButtonPose.interactable = true;
        ScrollScene.gameObject.SetActive(true);
        ScrollPose.gameObject.SetActive(false);
    }

    public bool GetPoseByName(string name, out UIPoseContainer pose)
    {
        pose = PosePresetEntries.FirstOrDefault(p => p.PoseName == name);
        return pose != null;
    }

    public bool GetSceneByName(string name, out UISceneContainer scene)
    {
        scene = ScenePresetEntries.FirstOrDefault(p => p.SceneData.Filename == name);
        return scene != null;
    }

    public void Init()
    {
        InitPoses();
        InitScenes();
    }

    private void InitPoses()
    {
        string path = Settings.SavePoseDirectory;

        foreach (var fileName in Directory.GetFiles(path).Where(f => f.EndsWith(".pose")))
        {
            try
            {
                string fName = Path.GetFileNameWithoutExtension(fileName);
                var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto };
                var bc = JsonConvert.DeserializeObject<KeyframeData>(File.ReadAllText(fileName), settings);
                UIPoseContainer.CreateNew(fName, bc, false);
            }
            catch (Exception e)
            {
                Debug.LogWarning(e);
                Error.Log(Color.red, "Loading " + fileName + " failed!");
            }
        }
    }

    private void InitScenes()
    {
        string path = Settings.SaveSceneDirectory;
        List<SceneSerializable> scenes = new List<SceneSerializable>();

        foreach (var fileName in Directory.GetFiles(path).Where(f => f.EndsWith(".scene")))
        {
            try
            {
                string fName = Path.GetFileNameWithoutExtension(fileName);
                var settings = new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto };
                var bc = JsonConvert.DeserializeObject<SceneSerializable>(File.ReadAllText(fileName), settings);
                if (string.IsNullOrEmpty(bc.Date))
                {
                    bc.Date = "1990-01-01 00:00";
                }
                scenes.Add(bc);
            }
            catch (Exception e)
            {
                Debug.LogWarning(e);
                Error.Log(Color.red, "Loading " + fileName + " failed!");
            }
        }

        foreach (var scene in scenes.OrderBy(s => DateTime.ParseExact(s.Date, "yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture)).ThenBy(s => s.Filename))
        {
            UISceneContainer.CreateNew(scene, false);
        }
    }
}