using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;

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

        private Utilities.UtilityClass.SettingButton btnSetting;
        private HistoryWall rootParentWindow = null;

        #endregion

        #region Constructor

        public RootController(Utilities.UtilityClass.SettingButton setting, HistoryWall window)
        {
            InitializeComponent();
            btnSetting = setting;
            rootParentWindow = window;
            this.Loaded += new RoutedEventHandler(RootController_Loaded);
        }

        #endregion

        #region Event & EventHandler

        private void RootController_Loaded(object sender, RoutedEventArgs e)
        {
            if (btnSetting != Utilities.UtilityClass.SettingButton.EventDetailSetting)
            {
                uxSetButtonBtn.Visibility = Visibility.Collapsed;
                uxSetContentBtn.Visibility = Visibility.Collapsed;
                uxSetExtentBtn.Visibility = Visibility.Collapsed;
                uxSetLangBtn.Visibility = Visibility.Collapsed;

                uxScreenSelectCombo.SelectionChanged += uxScreenSelectCombo_SelectionChanged;
                if (btnSetting == Utilities.UtilityClass.SettingButton.BackgroundSetting)
                {
                    
                    int selectIndex = HistoryWall.MainBackCollect.BackHomeMillisecond / 5 - 4;
                    uxSecSetterCombobox.SelectionChanged += new SelectionChangedEventHandler(uxSecSetterCombobox_SelectionChanged);
                    uxSecSetterCombobox.SelectedIndex =
                        selectIndex < 0 || selectIndex > uxSecSetterCombobox.Items.Count - 1 ? 0 : selectIndex;
                    uxIs4K.IsChecked = HistoryWall.MainBackCollect.IsUsing4kResolution;
                    uxIs4K.Checked += uxIs4K_Checked;
                    uxIs4K.Unchecked += UxIs4K_Unchecked;
                    uxScreenSelectCombo.SelectedIndex = HistoryWall.MainBackCollect.ScreenSetting - 1;
                    uxScreenSelectCombo.Visibility = Visibility.Collapsed;
                    uxSetBackgroundBtn.Visibility = Visibility.Visible;
                    //uxSetLogoBtn.Visibility = Visibility.Visible;
                    uxSetLogoBtn.Visibility = Visibility.Collapsed; //20180604 - not allowed
                }
                else
                {
                    //uxAutoPlaySecGrid.Visibility = Visibility.Visible;
                    uxAutoPlaySecGrid.Visibility = Visibility.Collapsed; //20180604 - not allowed
                    uxScreenSelectCombo.SelectedIndex = 0;
                    uxScreenSelectCombo.Visibility = Visibility.Collapsed;
                    uxSetTimelineBtn.Visibility = Visibility.Visible;
                    uxSetEventCubeBtn.Visibility = Visibility.Visible;
                }
            }
            else
            {
                //uxContentGrid.Width = 1354.5;
                //uxContentGrid.Height = 761.5;
                uxScreenSelectCombo.Visibility = Visibility.Collapsed;
                //uxSetButtonBtn.Visibility = Visibility.Collapsed;
                uxSetButtonBtn.Visibility = Visibility.Visible;
                uxSetContentBtn.Visibility = Visibility.Visible;
                uxSetExtentBtn.Visibility = Visibility.Visible;
                uxSetLangBtn.Visibility = Visibility.Collapsed;
                uxSetBackgroundBtn.Visibility = Visibility.Collapsed;
                uxSetLogoBtn.Visibility = Visibility.Collapsed;
                uxSetTimelineBtn.Visibility = Visibility.Collapsed;
                uxSetEventCubeBtn.Visibility = Visibility.Collapsed;
                DetailPreview viewer = new DetailPreview();
                //uxContentGrid.Margin = new Thickness(0);
                uxContentGrid.Children.Add(viewer);
                uxSetButtonBtn.Click += new RoutedEventHandler(Btn_Click);
                uxSetContentBtn.Click += new RoutedEventHandler(Btn_Click);
                uxSetExtentBtn.Click += new RoutedEventHandler(Btn_Click);
                uxSetLangBtn.Click += new RoutedEventHandler(Btn_Click);
            }
        }

        private void UxIs4K_Unchecked(object sender, RoutedEventArgs e)
        {
            HistoryWall.MainBackCollect.IsUsing4kResolution = (bool)uxIs4K.IsChecked;
        }

        private void uxIs4K_Checked(object sender, RoutedEventArgs e)
        {
            HistoryWall.MainBackCollect.IsUsing4kResolution = (bool)uxIs4K.IsChecked;
        }

        private void uxSecSetterCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBoxItem item = uxSecSetterCombobox.SelectedItem as ComboBoxItem;
            try
            {
                HistoryWall.MainBackCollect.BackHomeMillisecond = int.Parse(item.Content.ToString());
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        private void uxScreenSelectCombo_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            uxContentGrid.Children.Clear();
            switch (uxScreenSelectCombo.SelectedIndex)
            {
                case 0:
                    {
                        //uxContentGrid.Width = 1354.5;
                        //uxContentGrid.Height = 761.5;
                        //uxContentGrid.Margin = new Thickness(267, 120, 0, 0);
                        ScreenViewer viewer = new ScreenViewer();
                        viewer.ScreenCount = Utilities.UtilityClass.ScreenSet.Single;
                        uxContentGrid.Children.Add(viewer);
                        if (btnSetting == Utilities.UtilityClass.SettingButton.BackgroundSetting)
                        {
                            HistoryWall.MainBackCollect.ScreenSetting = uxScreenSelectCombo.SelectedIndex + 1;
                            uxSetBackgroundBtn.Margin = new Thickness(796, 82, 0, 0);
                            uxSetBackgroundImage.Margin = new Thickness(796, 82, 0, 0);
                            uxSetLogoBtn.Margin = new Thickness(1100, 10, 0, 0);
                            uxSetLogoImage.Margin = new Thickness(1100, 10, 0, 0);
                        }
                        else if (btnSetting == Utilities.UtilityClass.SettingButton.TimelineSetting)
                        {
                            uxSetTimelineBtn.Margin = new Thickness(750, 338, 0, 0);
                            uxSetTimelineImage.Margin = new Thickness(750, 338, 0, 0);
                            uxSetEventCubeBtn.Margin = new Thickness(910, 163, 0, 0);
                            uxSetEventCubeImage.Margin = new Thickness(910, 163, 0, 0);
                        }
                    }
                    break;
                case 1:
                    {
                        //uxContentGrid.Width = 1900;
                        //uxContentGrid.Height = 535.5;
                        //uxContentGrid.Margin = new Thickness(8, 154, 0, 0);
                        ScreenViewer viewer = new ScreenViewer();
                        viewer.ScreenCount = Utilities.UtilityClass.ScreenSet.Double;
                        uxContentGrid.Children.Add(viewer);
                        if (btnSetting == Utilities.UtilityClass.SettingButton.BackgroundSetting)
                        {
                            HistoryWall.MainBackCollect.ScreenSetting = uxScreenSelectCombo.SelectedIndex + 1;
                            uxSetBackgroundBtn.Margin = new Thickness(700, 110, 0, 0);
                            uxSetBackgroundImage.Margin = new Thickness(700, 110, 0, 0);
                            uxSetLogoBtn.Margin = new Thickness(1450, -5, 0, 0);
                            uxSetLogoImage.Margin = new Thickness(1450, -5, 0, 0);
                        }
                    }
                    break;
                case 2:
                    {
                        HistoryWall.MainBackCollect.ScreenSetting = uxScreenSelectCombo.SelectedIndex + 1;
                        //uxContentGrid.Width = 1900;
                        //uxContentGrid.Height = 358.5;
                        //uxContentGrid.Margin = new Thickness(8, 248, 0, 0);
                        ScreenViewer viewer = new ScreenViewer();
                        viewer.ScreenCount = Utilities.UtilityClass.ScreenSet.Triple;
                        uxContentGrid.Children.Add(viewer);
                        if (btnSetting == Utilities.UtilityClass.SettingButton.BackgroundSetting)
                        {
                            HistoryWall.MainBackCollect.ScreenSetting = uxScreenSelectCombo.SelectedIndex + 1;
                            uxSetBackgroundImage.Margin = new Thickness(800, 150, 0, 0);
                            uxSetBackgroundBtn.Margin = new Thickness(800, 150, 0, 0);
                            uxSetLogoImage.Margin = new Thickness(1450, 80, 0, 0);
                            uxSetLogoBtn.Margin = new Thickness(1450, 80, 0, 0);
                        }
                    }
                    break;
            }
        }

        private void Btn_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            uxSetButtonBtn.Visibility = Visibility.Hidden;
            uxSetContentBtn.Visibility = Visibility.Hidden;
            uxSetExtentBtn.Visibility = Visibility.Hidden;
            uxSetLangBtn.Visibility = Visibility.Hidden;
            switch (btn.Name)
            {
                case "uxSetButtonBtn":
                    {
                        uxSetButtonImage.Visibility = Visibility.Visible;
                        DetailPreview viewer = uxContentGrid.Children[0] as DetailPreview;
                        SettingWindow w = new SettingWindow(false);
                        List<Button> btns = new List<Button>() { uxSetExtentBtn,uxSetContentBtn };
                        w.SetDelegate(rootParentWindow, btn, btns, uxSetButtonImage);
                        w.TypeSet = Utilities.UtilityClass.SettingType.BtnSelect;
                        w.SetButtonControl(viewer.uxBtnGrid);
                        w.WindowStartupLocation = WindowStartupLocation.Manual;
                        w.Left = 265 + HistoryWall.dialogPositionDiff;
                        w.Top = 150;
                        w.ShowDialog();
                    }
                    break;
                case "uxSetContentBtn":
                    {
                        uxSetContentImage.Visibility = Visibility.Visible;
                        DetailPreview viewer = uxContentGrid.Children[0] as DetailPreview;
                        SettingWindow w = new SettingWindow(false);
                        List<Button> btns = new List<Button>() {  uxSetExtentBtn, uxSetButtonBtn };
                        w.SetDelegate(rootParentWindow, btn, btns, uxSetContentImage);
                        w.TypeSet = Utilities.UtilityClass.SettingType.EventContent;
                        w.SetEventContentControl(viewer.uxRootBorder, viewer.uxTextPanel, viewer.uxImageGrid);
                        w.WindowStartupLocation = WindowStartupLocation.Manual;
                        w.Left = 265 + HistoryWall.dialogPositionDiff;
                        w.Top = 150;
                        w.ShowDialog();
                    }
                    break;
                case "uxSetExtentBtn":
                    {
                        uxSetExtentImage.Visibility = Visibility.Visible;
                        DetailPreview viewer = uxContentGrid.Children[0] as DetailPreview;
                        SettingWindow w = new SettingWindow(false);
                        List<Button> btns = new List<Button>() { uxSetContentBtn, uxSetButtonBtn };
                        w.SetDelegate(rootParentWindow, btn, btns, uxSetExtentImage);
                        w.TypeSet = Utilities.UtilityClass.SettingType.TimelineExtent;
                        w.SetExtentTimelineControl(viewer.uxExtentTimelineStackpanel);
                        w.WindowStartupLocation = WindowStartupLocation.Manual;
                        w.Left = 265 + HistoryWall.dialogPositionDiff;
                        w.Top = 150;
                        w.ShowDialog();
                    }
                    break;
                case "uxSetLangBtn":
                    {
                        uxSetLangImage.Visibility = Visibility.Visible;
                        DetailPreview viewer = uxContentGrid.Children[0] as DetailPreview;
                        DialogWindow w = new DialogWindow();
                        List<Button> btns = new List<Button>() { uxSetExtentBtn, uxSetContentBtn, uxSetButtonBtn };
                        w.SetDelegate(rootParentWindow, btn, btns, uxSetLangImage, viewer);
                        w.SetLangControl(viewer.uxTextPanel, viewer.uxBtnGrid);
                        w.WindowStartupLocation = WindowStartupLocation.Manual;
                        w.Left = 265 + HistoryWall.dialogPositionDiff;
                        w.Top = 150;
                        w.ShowDialog();
                        HistoryWall.WINDOW.uxLangOne.Content = HistoryWall.MainBackCollect.LangText[0];
                        HistoryWall.WINDOW.uxLangTwo.Content = HistoryWall.MainBackCollect.LangText[1];
                        HistoryWall.WINDOW.uxLangThree.Content = HistoryWall.MainBackCollect.LangText[2];
                        HistoryWall.WINDOW.uxLangFour.Content = HistoryWall.MainBackCollect.LangText[3];
                    }
                    break;
            }
        }

        private void uxSetRightBtn_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btnSetting == Utilities.UtilityClass.SettingButton.BackgroundSetting)
            {
                uxSetLogoBtn.Visibility = Visibility.Hidden;
                uxSetBackgroundBtn.Visibility = Visibility.Hidden;
                uxSetLogoImage.Visibility = Visibility.Visible;
                DialogWindow w = new DialogWindow(HistoryWall.MainBackCollect.ImgLogo);
                List<Button> btns = new List<Button>() { uxSetBackgroundBtn };
                w.SetDelegate(rootParentWindow, btn, btns, uxSetLogoImage, null);
                ScreenViewer viewer = uxContentGrid.Children[0] as ScreenViewer;
                w.SetLogoControl(new Image[] { viewer.uxLeftImg, viewer.uxRightImg });
                w.WindowStartupLocation = WindowStartupLocation.Manual;
                w.Left = 265 + HistoryWall.dialogPositionDiff;
                w.Top = 150;
                w.ShowDialog();
            }
            else if (btnSetting == Utilities.UtilityClass.SettingButton.TimelineSetting)
            {
                uxSetTimelineBtn.Visibility = Visibility.Hidden;
                uxSetEventCubeBtn.Visibility = Visibility.Hidden;
                uxSetEventCubeImage.Visibility = Visibility.Visible;
                ScreenViewer viewer = uxContentGrid.Children[0] as ScreenViewer;
                SettingWindow w = new SettingWindow(false);
                List<Button> btns = new List<Button>() { uxSetTimelineBtn };
                w.SetDelegate(rootParentWindow, btn, btns, uxSetEventCubeImage);
                w.TypeSet = Utilities.UtilityClass.SettingType.Event;
                w.SetEventControls(viewer.uxEventSingleGrid);
                w.WindowStartupLocation = WindowStartupLocation.Manual;
                w.Left = 265 + HistoryWall.dialogPositionDiff;
                w.Top = 150;
                w.ShowDialog();
            }
        }

        private void uxSetCenterBtn_Click(object sender, RoutedEventArgs e)
        {
            Button btn = sender as Button;
            if (btnSetting == Utilities.UtilityClass.SettingButton.BackgroundSetting)
            {
                uxSetLogoBtn.Visibility = Visibility.Hidden;
                uxSetBackgroundBtn.Visibility = Visibility.Hidden;
                uxSetBackgroundImage.Visibility = Visibility.Visible;
                SettingWindow w = new SettingWindow(true);
                //List<Button> btns = new List<Button>() { uxSetLogoBtn };
                List<Button> btns = new List<Button>() { };
                w.SetDelegate(rootParentWindow, btn, btns, uxSetBackgroundImage);
                ScreenViewer viewer = uxContentGrid.Children[0] as ScreenViewer;
                w.SetBackgroundControl(viewer.uxBgImageGrid);
                w.WindowStartupLocation = WindowStartupLocation.Manual;
                w.Left = 265 + HistoryWall.dialogPositionDiff;
                w.Top = 150;
                w.ShowDialog();
            }
            else if (btnSetting == Utilities.UtilityClass.SettingButton.TimelineSetting)
            {
                uxSetTimelineBtn.Visibility = Visibility.Hidden;
                uxSetEventCubeBtn.Visibility = Visibility.Hidden;
                uxSetTimelineImage.Visibility = Visibility.Visible;
                SettingWindow w = new SettingWindow(false);
                List<Button> btns = new List<Button>() { uxSetEventCubeBtn };
                w.SetDelegate(rootParentWindow, btn, btns, uxSetTimelineImage);
                ScreenViewer viewer = uxContentGrid.Children[0] as ScreenViewer;
                w.TypeSet = Utilities.UtilityClass.SettingType.Timeline;
                w.SetTimelineControl(viewer.uxTimelineSingleGrid);
                w.WindowStartupLocation = WindowStartupLocation.Manual;
                w.Left = 265 + HistoryWall.dialogPositionDiff;
                w.Top = 150;
                w.ShowDialog();
            }
        }

        #endregion
    }
}
