using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bellwether.Utility
{
    public static class Setting
    {
        public static readonly List<string> PageNameList = new List<string>() { "大事記", "產品展示", "社會責任" };

        public static int BackHomeSec { get => settingJsonFile.BackHomeSec; }
        public static string SolarEnergyUrl { get => frontpageJsonFile.SolarEnergyUrl; }
        public static List<string> MediaList { get => frontpageJsonFile.MediaList; }
        public static int AutoPlayInterval { get => frontpageJsonFile.AutoPlayIntervalSec; }

        private static string dataDirectory = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Data");
        private static string settingJsonDataPath = System.IO.Path.Combine(dataDirectory, "setting.ini");
        private static string frontpageJsonDataPath = System.IO.Path.Combine(dataDirectory,"FrontPage", "frontpage.ini");

        private static SettingJson _settingJsonFile;
        private static SettingJson settingJsonFile
        {
            get
            {
                if (_settingJsonFile != null)
                    return _settingJsonFile;
                else
                {
                    AnalyzJsonFile();
                    return _settingJsonFile;
                }
            }
        }

        private static FrontPageJson _frontpageJsonFile;
        private static FrontPageJson frontpageJsonFile
        {
            get
            {
                if (_frontpageJsonFile != null)
                    return _frontpageJsonFile;
                else
                {
                    AnalyzJsonFile();
                    return _frontpageJsonFile;
                }
            }
        }

        public static void Reload()
        {
            AnalyzJsonFile();
        }

        #region 解析 json
        public static T DeserializeFromJson<T>(string jsonStr)
        {
            T deserializedProduct = JsonConvert.DeserializeObject<T>(jsonStr);
            return deserializedProduct;
        }

        private static void AnalyzJsonFile()
        {
            string jsonResult;

            if (!File.Exists(settingJsonDataPath))
            {
                _settingJsonFile = new SettingJson()
                {
                    BackHomeSec = 60,
                };
            }
            else
            {
                using (StreamReader sr = new StreamReader(settingJsonDataPath))
                {
                    jsonResult = sr.ReadToEnd();
                }
                _settingJsonFile = DeserializeFromJson<SettingJson>(jsonResult);
            }

            if (!File.Exists(frontpageJsonDataPath))
            {
                _frontpageJsonFile = new FrontPageJson()
                {
                    SolarEnergyUrl = "https://dsegomspoctestenv.azurewebsites.net/api/siteoverview?act=9237&token=BWsolarenergy",
                    MediaList = new List<string>(),
                };
            }
            else
            {
                using (StreamReader sr = new StreamReader(frontpageJsonDataPath))
                {
                    jsonResult = sr.ReadToEnd();
                }
                _frontpageJsonFile = DeserializeFromJson<FrontPageJson>(jsonResult);
            }
        }
        #endregion

    }

    public class SettingJson
    {
        public int BackHomeSec { get; set; }
    }

    public class FrontPageJson
    {
        public string SolarEnergyUrl { get; set; }
        public int AutoPlayIntervalSec { get; set; } = 5;
        public List<string> MediaList { get; set; }
    }
}
