using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using HistoryWallEditTool.ControlsForData;
using System.IO;
using System.Windows.Markup;

namespace HistoryWallEditTool.Controls
{
    /// <summary>
    /// SettingWindow.xaml 的互動邏輯
    /// </summary>
    public partial class SettingWindow : Window
    {
        #region Class Members

        private Button btnClicked = null;
        private bool isVerticalLayout = true;
        private string[] parameterName;
        private string titleName;
        private Utilities.UtilityClass.SettingType typeSet;

        #endregion

        #region Properties

        public Utilities.UtilityClass.SettingType TypeSet
        {
            get { return typeSet; }
            set
            {
                typeSet = value;
                switch (typeSet)
                {
                    case Utilities.UtilityClass.SettingType.BtnSelect:
                        ParameterName = new string[]
                        {
                            "fc_btnTextColorSelect",
                            "fc_btnBackgroundColorSelect",
                            "fc_btnTextColorUnselect",
                            "fc_btnBackgroundColorUnselect"
                        };
                        TitleName = "fc_title_SetButton";
                        break;
                    case Utilities.UtilityClass.SettingType.EventContent:
                        ParameterName = new string[] { "fc_TextColor", "fc_BackgroundColor", "fc_ImageThicknessColor" };
                        TitleName = "fc_title_SetEventContent";
                        break;
                    case Utilities.UtilityClass.SettingType.Event:
                        ParameterName = new string[] { "fc_TextColor", "fc_BackgroundColor", "fc_ThicknessColor" };
                        TitleName = "fc_title_SetEventCube";
                        break;
                    case Utilities.UtilityClass.SettingType.TimelineExtent:
                        ParameterName = new string[] { "fc_TextColor", "fc_BackgroundColor", "fc_ThicknessColor" };
                        TitleName = "fc_title_SetExtentColor";
                        break;
                    case Utilities.UtilityClass.SettingType.Timeline:
                        ParameterName = new string[] { "fc_TextColor", "fc_BackgroundColor", "fc_ThicknessColor" };
                        TitleName = "fc_title_SetTimeline";
                        break;
                    default:
                        break;
                }
            }
        }

        public string[] ParameterName
        {
            get { return parameterName; }
            private set
            {
                parameterName = value;
            }
        }

        public string TitleName
        {
            get { return titleName; }
            private set
            {
                titleName = value;
                if (!isVerticalLayout)
                {
                    SettingControlHorizontal control = uxRootGrid.Children[0] as SettingControlHorizontal;
                    control.TitleString = parameterName;
                    control.TitleName = titleName;
                    control.Type = typeSet;
                }
            }
        }

        public string LangString
        {
            set
            {
                LoadLanguageResource(value);
            }
        }

        #endregion

        #region Constructor
        public static CurrentDisplayLanguage cdl;

        public SettingWindow(bool isVertical = true)
        {
            InitializeComponent();
            SysLanChange();

            isVerticalLayout = isVertical;
            if (!isVertical)
            {
                this.Width = 648;
                this.Height = 473;
                uxRootGrid.Children.Add(new SettingControlHorizontal());
            }
            else
            {
                uxRootGrid.Children.Add(new SettingControlVertical());
            }
            LangString = HistoryWall.LangString;
            this.Closing += SettingWindow_Closing;
        }

        private void SysLanChange()
        {
            string resourcePrefix = "StringResources";
            string culturesFolder = "Culture";

            this.Resources.MergedDictionaries.Clear();
            string loadedFileName = string.Format("{0}\\{1}\\{2}.{3}.xaml", GlobalFunction.rootPath, culturesFolder, resourcePrefix, "zh-TW");

            if (cdl == CurrentDisplayLanguage.ENG)
            {
                loadedFileName = string.Format("{0}\\{1}\\{2}.{3}.xaml", GlobalFunction.rootPath, culturesFolder, resourcePrefix, "en-US");
            }

            FileStream fileStream = new FileStream(loadedFileName, FileMode.Open);
            ResourceDictionary resourceDictionary = XamlReader.Load(fileStream) as ResourceDictionary;
            this.Resources.MergedDictionaries.Add(resourceDictionary);
        }

        #endregion

        #region Events & EventHandler

        private void SettingWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            selectImage.Visibility = Visibility.Hidden;
            btnClicked.Visibility = Visibility.Visible;
            foreach (Button btn in hiddenBtn)
            {
                btn.Visibility = Visibility.Visible;
            }
            if (!isVerticalLayout)
            {
                SettingControlHorizontal control = uxRootGrid.Children[0] as SettingControlHorizontal;
                List<ColorPicker> infoCollect = new List<ColorPicker>();
                for (int i = 0; i < control.uxColorPickerGrid.Children.Count; i++)
                {
                    try
                    {
                        ColorPicker picker = control.uxColorPickerGrid.Children[i] as ColorPicker;
                        infoCollect.Add(picker);
                    }
                    catch { }
                }
                switch (typeSet)
                {
                    case Utilities.UtilityClass.SettingType.BtnSelect:
                        HistoryWall.BtnSelectCollect.TextColor = infoCollect[0].SelectColor;
                        HistoryWall.BtnSelectCollect.TextColorPoint = infoCollect[0].SelectPoint;
                        HistoryWall.BtnSelectCollect.BackgroundColor = infoCollect[1].SelectColor;
                        HistoryWall.BtnSelectCollect.BackgroundColorPoint = infoCollect[1].SelectPoint;
                        HistoryWall.BtnSelectCollect.TextColorUnselect = infoCollect[2].SelectColor;
                        HistoryWall.BtnSelectCollect.TextColorUnselectPoint = infoCollect[2].SelectPoint;
                        HistoryWall.BtnSelectCollect.BackgroundColorUnselect = infoCollect[3].SelectColor;
                        HistoryWall.BtnSelectCollect.BackgroundColorUnselectPoint = infoCollect[3].SelectPoint;
                        break;
                    case Utilities.UtilityClass.SettingType.EventContent:
                        HistoryWall.EventContentCollect.TextColor = infoCollect[0].SelectColor;
                        HistoryWall.EventContentCollect.TextColorPoint = infoCollect[0].SelectPoint;
                        HistoryWall.EventContentCollect.BackgroundColor = infoCollect[1].SelectColor;
                        HistoryWall.EventContentCollect.BackgroundColorPoint = infoCollect[1].SelectPoint;
                        if (infoCollect.Count > 2)
                        {
                            HistoryWall.EventContentCollect.ThicknessColor = infoCollect[2].SelectColor;
                            HistoryWall.EventContentCollect.ThicknessColorPoint = infoCollect[2].SelectPoint;
                        }
                        break;
                    case Utilities.UtilityClass.SettingType.Event:
                        HistoryWall.EventCollect.TextColor = infoCollect[0].SelectColor;
                        HistoryWall.EventCollect.TextColorPoint = infoCollect[0].SelectPoint;
                        HistoryWall.EventCollect.BackgroundColor = infoCollect[1].SelectColor;
                        HistoryWall.EventCollect.BackgroundColorPoint = infoCollect[1].SelectPoint;
                        if (infoCollect.Count > 2)
                        {
                            HistoryWall.EventCollect.ThicknessColor = infoCollect[2].SelectColor;
                            HistoryWall.EventCollect.ThicknessColorPoint = infoCollect[2].SelectPoint;
                        }
                        break;
                    case Utilities.UtilityClass.SettingType.TimelineExtent:
                        HistoryWall.TimelineExtentCollect.TextColor = infoCollect[0].SelectColor;
                        HistoryWall.TimelineExtentCollect.TextColorPoint = infoCollect[0].SelectPoint;
                        HistoryWall.TimelineExtentCollect.BackgroundColor = infoCollect[1].SelectColor;
                        HistoryWall.TimelineExtentCollect.BackgroundColorPoint = infoCollect[1].SelectPoint;
                        if (infoCollect.Count > 2)
                        {
                            HistoryWall.TimelineExtentCollect.ThicknessColor = infoCollect[2].SelectColor;
                            HistoryWall.TimelineExtentCollect.ThicknessColorPoint = infoCollect[2].SelectPoint;
                        }
                        break;
                    case Utilities.UtilityClass.SettingType.Timeline:
                        HistoryWall.TimelineCollect.TextColor = infoCollect[0].SelectColor;
                        HistoryWall.TimelineCollect.TextColorPoint = infoCollect[0].SelectPoint;
                        HistoryWall.TimelineCollect.BackgroundColor = infoCollect[1].SelectColor;
                        HistoryWall.TimelineCollect.BackgroundColorPoint = infoCollect[1].SelectPoint;
                        if (infoCollect.Count > 2)
                        {
                            HistoryWall.TimelineCollect.ThicknessColor = infoCollect[2].SelectColor;
                            HistoryWall.TimelineCollect.ThicknessColorPoint = infoCollect[2].SelectPoint;
                        }
                        break;
                }
            }
            else
            {
                SettingControlVertical control = uxRootGrid.Children[0] as SettingControlVertical;
                Utilities.UtilityClass.ImagePointColor data = control.GetData();
                HistoryWall.MainBackCollect.BackgroundColor = data.SelectColor;
                HistoryWall.MainBackCollect.BackgroundColorPoint = data.SelectPoint;
                HistoryWall.MainBackCollect.ImgBackground = data.ImgPath;
            }
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            base.DragMove();
        }

        private void mw_create_event(string state)
        {
            if (state == "zh-TW" || state == "en-US")
            {
                LoadLanguageResource(state);
            }
            else
            {
                //Close
            }
        }

        #endregion

        #region Methods

        private List<Button> hiddenBtn;
        private Button selectImage;

        public void SetDelegate(HistoryWall mw, Button btn, List<Button> btns, Button img)
        {
            mw.create_event += new HistoryWall.CallBackToUIThread(mw_create_event);
            btnClicked = btn;
            hiddenBtn = btns;
            selectImage = img;
        }

        public void SetBackgroundControl(Grid backgroundGrid)
        {
            if (isVerticalLayout)
            {
                SettingControlVertical control = uxRootGrid.Children[0] as SettingControlVertical;
                control.SetterGridBackground = backgroundGrid;
            }
            else
            {
                SettingControlHorizontal control = uxRootGrid.Children[0] as SettingControlHorizontal;
                //control.SetterGridBackground = backgroundGrid;
            }
        }

        public void SetEventControls(Grid eventParentGrid)
        {
            SettingControlHorizontal control = uxRootGrid.Children[0] as SettingControlHorizontal;
            control.SetterEventControl(eventParentGrid);
        }

        public void SetTimelineControl(StackPanel sp)
        {
            SettingControlHorizontal control = uxRootGrid.Children[0] as SettingControlHorizontal;
            control.SetTimelineControl(sp);
        }

        public void SetEventContentControl(Border border, StackPanel textPanel, Grid imgThumbGrid)
        {
            SettingControlHorizontal control = uxRootGrid.Children[0] as SettingControlHorizontal;
            control.SetEventContentControl(border, textPanel, imgThumbGrid);
        }

        public void SetExtentTimelineControl(StackPanel extentPanel)
        {
            SettingControlHorizontal control = uxRootGrid.Children[0] as SettingControlHorizontal;
            control.SetExtentTimelineControl(extentPanel);
        }

        public void SetButtonControl(Grid btnGrid)
        {
            SettingControlHorizontal control = uxRootGrid.Children[0] as SettingControlHorizontal;
            control.SetButtonControl(btnGrid);
        }

        private void LoadLanguageResource(string lang)
        {
            ResourceDictionary rd = null;
            try
            {
                rd = Application.LoadComponent(
                    new Uri(@"Controls\Localizing\" + lang + ".xaml", UriKind.Relative)) as ResourceDictionary;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            if (rd != null)
            {
                if (Resources.MergedDictionaries.Count > 0)
                {
                    Resources.MergedDictionaries.Clear();
                }
                Resources.MergedDictionaries.Add(rd);
            }
        }

        #endregion
    }
}
