using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;

namespace FNZCM.Core
{
    public interface IConfigFile
    {
        string ListenPrefix { get; }
        IReadOnlyCollection<string> AliasPrefix { get; }
        IReadOnlyCollection<string> AccessControlAllowOrigin { get; }
        string M3uPrefix { get; }

        string Title { get; }
        string AdminPassword { get; }

        bool LibraryPathFollowLink { get; }
        public string RealConfigFilePath { get; }
        IReadOnlyDictionary<string, string> Libraries { get; }

        string[] MediaFilePattern { get; }
        string[] BkFilePattern { get; }

        IReadOnlyDictionary<string, ModuleEntry> Modules { get; }

    }

    public class ConfigFile : IConfigFile
    {
        static string ConfigFilePath => Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "config.json");
        public string RealConfigFilePath => new FileInfo(ConfigFilePath).LinkTarget ?? ConfigFilePath;

        static ConfigFile() => Reload();

        public static void Reload() => Instance = JsonConvert.DeserializeObject<ConfigFile>(File.ReadAllText(ConfigFilePath));

        public string ListenPrefix { get; set; }
        public IReadOnlyCollection<string> AliasPrefix { get; set; }
        public IReadOnlyCollection<string> AccessControlAllowOrigin { get; set; }
        public string M3uPrefix { get; set; }

        public string Title { get; set; }
        public string AdminPassword { get; set; }

        public bool LibraryPathFollowLink { get; set; }
        public IReadOnlyDictionary<string, string> Libraries { get; set; }

        public string[] MediaFilePattern { get; set; }
        public string[] BkFilePattern { get; set; }

        public static IConfigFile Instance { get; private set; }

        public IReadOnlyDictionary<string, ModuleEntry> Modules { get; set; }
    }

    public class ModuleEntry
    {
        public bool IsDefault { get; set; }
        public string DisplayText { get; set; }
        public string Path { get; set; }

        public bool EnableFallbackRoute { get; set; }
        public string DefaultDocument { get; set; }
        public string HtmlBaseReplace { get; set; }
    }
}
