using BarRaider.SdTools;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BarRaider.SdTools.Wrappers;
using Newtonsoft.Json.Linq;
using NAudio.MediaFoundation;

namespace ClipTrimDotNet
{
    public class FileEntry
    {
        public FileEntry()
        {
            Volume = 1.0;
            Playtype = "Play/Overlap";
        }
        [JsonProperty(PropertyName = "Volume")]
        public double Volume { get; set; }
        [JsonProperty(PropertyName = "Playtype")]
        public string Playtype { get; set; }
    }
    public class CollectionEntry
    {
        public CollectionEntry()
        {
            Files = new Dictionary<string, FileEntry>();
        }
        [JsonProperty(PropertyName = "Files")]
        public Dictionary<string, FileEntry> Files { get; set; }
    }

    public class GlobalSettings
    {
        public static GlobalSettings? _inst;
        public static GlobalSettings Instance
        {
            get
            {
                _inst ??= CreateDefaultSettings();
                return _inst;
            }
            set
            {
                _inst = value;
            }
        }
        public static GlobalSettings CreateDefaultSettings()
        {
            GlobalSettings instance = new GlobalSettings();
            instance.BasePath = null;
            instance.ProfileName = null;
            instance.Collections = new Dictionary<string, CollectionEntry>();
            return instance;
        }


        [FilenameProperty]
        [JsonProperty(PropertyName = "basePath")]
        public string? BasePath { get; set; }

        [JsonProperty(PropertyName = "profileName")]
        public string? ProfileName { get; set; }

        [JsonProperty(PropertyName = "outputDevice")]
        public string? OutputDevice { get; set; }

        [JsonProperty(PropertyName = "collections")]
        public Dictionary<string, CollectionEntry> Collections { get; set; }

        public void SetCurrentProfile(string profile)
        {
            ProfileName = profile;
            if(!Collections.ContainsKey(profile))
            {
                Collections.Add(profile, new CollectionEntry());
            }
        }

        public FileEntry GetFileOptionsInCurrentProfile(string filename)
        {
            if(!Collections.TryGetValue(ProfileName, out CollectionEntry collection))
            {
                return new FileEntry();
            }
            if(!collection.Files.TryGetValue(filename, out FileEntry file))
            {
                return new FileEntry();
            }
            Logger.Instance.LogMessage(TracingLevel.INFO, "fetched file settings " + filename + JsonConvert.SerializeObject(file));
            return file;
        }

        public void SetFileOptionsCurrentProfile(string filename, FileEntry file)
        {
            Logger.Instance.LogMessage(TracingLevel.INFO, "SetFileOptionsCurrentProfile ");
            if (!Collections.TryGetValue(ProfileName, out CollectionEntry collection))
            {
                return;
            }
            Logger.Instance.LogMessage(TracingLevel.INFO, "SetFileOptionsCurrentProfile 2");
            //collection.Files[filename] = file;
            Collections[ProfileName].Files[filename] = file;
        }
    }
}
