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

namespace HistoryDLL
{
    public class WindowsResolution
    {
        public CyclicScroller.MonitorFlag ncm;

        public WindowsResolution()
        {
            int Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
            int Width = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;

            //判斷是否為多螢幕~由 width 判斷
            double value = Math.Ceiling((double)Width / 1920);

            value = 3;

            if (value == 1)
            {
                ncm = CyclicScroller.MonitorFlag.Monitor1_1;
                ConfigSettingClass.MainBackCollect.ScreenSetting = 1;
            }
            else if (value == 2)
            {
                ncm = CyclicScroller.MonitorFlag.Monitor2_1;
                ConfigSettingClass.MainBackCollect.ScreenSetting = 2;
            }
            else if (value == 3)
            {
                ncm = CyclicScroller.MonitorFlag.Monitor3_1;
                ConfigSettingClass.MainBackCollect.ScreenSetting = 3;
            }
            else if (value == 4)
            {
                ncm = CyclicScroller.MonitorFlag.Monitor4_1;
                ConfigSettingClass.MainBackCollect.ScreenSetting = 4;
            }
            else if (value == 5)
            {
                ncm = CyclicScroller.MonitorFlag.Monitor5_1;
                ConfigSettingClass.MainBackCollect.ScreenSetting = 5;
            }
            else if (value == 6)
            {
                ncm = CyclicScroller.MonitorFlag.Monitor6_1;
                ConfigSettingClass.MainBackCollect.ScreenSetting = 6;
            }
        }
    }

    public class ConfigSettingClass
    {
        #region Static Members
        public static MainBackground MainBackCollect = new MainBackground()
        {
            BackgroundColor = Color.FromArgb(255, 255, 255, 255),
            BackgroundColorPoint = new Point(0, 0),
            ImgBackground = String.Empty,
            ImgLogo = String.Empty,
            DefaultLangIndex = 0,
            BackHomeMillisecond = 20,
            ScreenSetting = 1,
            LangText = new string[] { "中文", "English", "", "" },
            LangFlowDir = new FlowDirection[] { FlowDirection.LeftToRight, FlowDirection.LeftToRight, FlowDirection.LeftToRight, FlowDirection.LeftToRight },
            IsUsing4kResolution = false
        };

        public static Event EventCollect = new Event()
        {
            BackgroundColor = Color.FromArgb(230, 3, 115, 185),
            BackgroundColorPoint = new Point(0, 0),
            TextColor = Color.FromArgb(255, 232, 238, 255),
            TextColorPoint = new Point(0, 0),
            ThicknessColor = Color.FromArgb(255, 177, 232, 255),
            ThicknessColorPoint = new Point(0, 0)
        };
        public static EventContent EventContentCollect = new EventContent()
        {
            BackgroundColor = Color.FromArgb(233, 115, 217, 255),
            BackgroundColorPoint = new Point(0, 0),
            TextColor = Color.FromArgb(198, 0, 6, 9),
            TextColorPoint = new Point(0, 0),
            ThicknessColor = Color.FromArgb(255, 198, 255, 255),
            ThicknessColorPoint = new Point(0, 0)
        };
        public static Timeline TimelineCollect = new Timeline()
        {
            BackgroundColor = Color.FromArgb(177, 168, 197, 255),
            BackgroundColorPoint = new Point(0, 0),
            TextColor = Color.FromArgb(176, 255, 255, 255),
            TextColorPoint = new Point(0, 0),
            ThicknessColor = Color.FromArgb(255, 0, 87, 154),
            ThicknessColorPoint = new Point(0, 0)
        };
        public static TimelineExtent TimelineExtentCollect = new TimelineExtent()
        {
            BackgroundColor = Color.FromArgb(204, 0, 41, 104),
            BackgroundColorPoint = new Point(0, 0),
            TextColor = Color.FromArgb(255, 162, 220, 255),
            TextColorPoint = new Point(0, 0),
            ThicknessColor = Color.FromArgb(255, 76, 49, 0),
            ThicknessColorPoint = new Point(0, 0)
        };
        public static ButtonSelect BtnSelectCollect = new ButtonSelect()
        {
            BackgroundColor = Color.FromArgb(168, 12, 0, 0),
            BackgroundColorPoint = new Point(0, 0),
            TextColor = Color.FromArgb(255, 222, 241, 255),
            TextColorPoint = new Point(0, 0),
            BackgroundColorUnselect = Color.FromArgb(168, 0, 129, 181),
            BackgroundColorUnselectPoint = new Point(0, 0),
            TextColorUnselect = Color.FromArgb(120, 255, 255, 255),
            TextColorUnselectPoint = new Point(0, 0)
        };
        #endregion

        public ConfigSettingClass(string ConfigPath, WindowsResolution windowsresolution)
        {
            if (File.Exists(ConfigPath))
            {
                string jsonResult;
                using (StreamReader sr = new StreamReader(ConfigPath))
                {
                    jsonResult = sr.ReadToEnd();
                }
                var container = DeserializeFromJson<DataCollect>(jsonResult);
                if (container != null)
                {
                    MainBackCollect = container.MainBackgroundCollect;
                    EventCollect = container.EventCollect;
                    EventContentCollect = container.EventContentCollect;
                    TimelineCollect = container.TimelineCollect;
                    TimelineExtentCollect = container.TimelineExtentCollect;
                    BtnSelectCollect = container.ButtonSelectCollect;
                }

                try
                {
                    if (ConfigSettingClass.MainBackCollect.LangFlowDir == null || ConfigSettingClass.MainBackCollect.LangFlowDir.Length < 4 ||
                    ConfigSettingClass.MainBackCollect.LangText == null || ConfigSettingClass.MainBackCollect.LangText.Length < 4)
                    {
                        MainBackCollect = null;
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MainBackCollect = null;
                    return;
                }
            }
            else
            {
                SetDefaultBgAndLogo(windowsresolution);
            }

            if (ConfigSettingClass.MainBackCollect.BackHomeMillisecond <= 5)
            {
                ConfigSettingClass.MainBackCollect.BackHomeMillisecond = 5;
            }
        }

        private void SetDefaultBgAndLogo(WindowsResolution windowsresolution)
        {
            MainBackCollect.ImgLogo = "logo.png";

            switch (windowsresolution.ncm)
            {
                case CyclicScroller.MonitorFlag.Monitor1_1:
                    {
                        //MainBackCollect.ImgBackground = "1x1.jpg";
                        break;
                    }
                case CyclicScroller.MonitorFlag.Monitor2_1:
                    {
                        MainBackCollect.ImgBackground = "2x1.jpg";
                        break;
                    }
                case CyclicScroller.MonitorFlag.Monitor3_1:
                    {
                        MainBackCollect.ImgBackground = "3x1.jpg";
                        break;
                    }
            }
        }

        public static T DeserializeFromJson<T>(string json)
        {
            T deserializedProduct = JsonConvert.DeserializeObject<T>(json);
            return deserializedProduct;
        }
    }
}
