using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HistoryWallEditTool.Utilities;
using HistoryWallEditTool.Helper;

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

        private Utilities.UtilityClass.ScreenSet screenCount = Utilities.UtilityClass.ScreenSet.Single;

        #endregion

        #region Properties

        public Utilities.UtilityClass.ScreenSet ScreenCount
        {
            get { return screenCount; }
            set
            {
                screenCount = value;
                ChangeScreenSet();
                LoadUiConfig();
            }
        }

        #endregion

        #region Constructor

        public ScreenViewer()
        {
            InitializeComponent();
        }

        #endregion

        #region Methods

        private void ChangeScreenSet()
        {
            switch (screenCount)
            {
                case UtilityClass.ScreenSet.Single:
                case UtilityClass.ScreenSet.Complete:
                    {
                        //this.Width = 1354.5;
                        //this.Height = 761.5;
                        uxLogoGrid.Height = 64;
                        uxLogoGrid.Width = 924.4;
                        uxBgImageGrid.Width = 924.4;
                        uxBgImageGrid.Height = 520;
                        uxBgImageGrid.Margin = new Thickness(0);
                        uxLogoGrid.Margin = new Thickness(0, 33, 0, 0);
                        Rectangle rect1 = uxLogoGrid.Children[0] as Rectangle;
                        Rectangle rect2 = uxLogoGrid.Children[2] as Rectangle;
                        rect1.Height = rect2.Height = uxLeftGrid.Height = uxRightGrid.Height = uxLeftImg.Height = uxRightImg.Height = 64;
                        rect1.Width = rect2.Width = uxLeftGrid.Width = uxRightGrid.Width = uxLeftImg.Width = uxRightImg.Width = 162;
                        uxLeftGrid.Margin = rect1.Margin = new Thickness(25, 0, 0, 0);
                        uxRightGrid.Margin = rect2.Margin = new Thickness(737, 0, 0, 0);
                        uxTimelineSingleGrid.Visibility = Visibility.Visible;
                        uxTimelineDoubleGrid.Visibility = Visibility.Collapsed;
                        uxTimelineTripleGrid.Visibility = Visibility.Collapsed;
                        uxEventSingleGrid.Visibility = Visibility.Visible;
                        uxEventDoubleGrid.Visibility = Visibility.Collapsed;
                        uxEventTripleGrid.Visibility = Visibility.Collapsed;
                    }
                    break;
                case UtilityClass.ScreenSet.Double:
                    {
                        //this.Width = 1900;
                        //this.Height = 535.5;
                        //this.Margin = new Thickness(10, 0, 10, 0);
                        uxLogoGrid.Height = 55;
                        uxLogoGrid.Width = 1730;
                        uxBgImageGrid.Width = 1730;
                        uxBgImageGrid.Height = 486.5625;
                        uxBgImageGrid.Margin = new Thickness(0);
                        uxLogoGrid.Margin = new Thickness(0, 33, 0, 0);
                        uxLogoGrid.Margin = new Thickness(0, 16.72, 0, 0);
                        Rectangle rect1 = uxLogoGrid.Children[0] as Rectangle;
                        Rectangle rect2 = uxLogoGrid.Children[2] as Rectangle;
                        rect1.Height = rect2.Height = uxLeftGrid.Height = uxRightGrid.Height = uxLeftImg.Height = uxRightImg.Height = 55;
                        rect1.Width = rect2.Width = uxLeftGrid.Width = uxRightGrid.Width = uxLeftImg.Width = uxRightImg.Width = 162;
                        uxLeftGrid.Margin = rect1.Margin = new Thickness(25, 0, 0, 0);
                        uxRightGrid.Margin = rect2.Margin = new Thickness(1543, 0, 0, 0);
                        uxTimelineSingleGrid.Visibility = Visibility.Collapsed;
                        uxTimelineDoubleGrid.Visibility = Visibility.Visible;
                        uxTimelineTripleGrid.Visibility = Visibility.Collapsed;
                        uxEventSingleGrid.Visibility = Visibility.Collapsed;
                        uxEventDoubleGrid.Visibility = Visibility.Visible;
                        uxEventTripleGrid.Visibility = Visibility.Collapsed;
                    }
                    break;
                case UtilityClass.ScreenSet.Triple:
                    {
                        //this.Width = 1900;
                        //this.Height = 358.5;
                        //this.Margin = new Thickness(10, 0, 10, 0);
                        uxLogoGrid.Height = 44;
                        uxLogoGrid.Width = 1730;
                        uxBgImageGrid.Width = 1730;
                        uxBgImageGrid.Height = 324.375;
                        uxBgImageGrid.Margin = new Thickness(0, 100, 0, 0);
                        uxLogoGrid.Margin = new Thickness(0, 123, 0, 0);
                        Rectangle rect1 = uxLogoGrid.Children[0] as Rectangle;
                        Rectangle rect2 = uxLogoGrid.Children[2] as Rectangle;
                        rect1.Height = rect2.Height = uxLeftGrid.Height = uxRightGrid.Height = uxLeftImg.Height = uxRightImg.Height = 44;
                        rect1.Width = rect2.Width = uxLeftGrid.Width = uxRightGrid.Width = uxLeftImg.Width = uxRightImg.Width = 131;
                        uxLeftGrid.Margin = rect1.Margin = new Thickness(25, 0, 0, 0);
                        uxRightGrid.Margin = rect2.Margin = new Thickness(1543, 0, 0, 0);
                        uxTimelineSingleGrid.Visibility = Visibility.Collapsed;
                        uxTimelineDoubleGrid.Visibility = Visibility.Collapsed;
                        uxTimelineTripleGrid.Visibility = Visibility.Visible;
                        uxEventSingleGrid.Visibility = Visibility.Collapsed;
                        uxEventDoubleGrid.Visibility = Visibility.Collapsed;
                        uxEventTripleGrid.Visibility = Visibility.Visible;
                    }
                    break;
            }
            if (screenCount == UtilityClass.ScreenSet.Complete)
            {
                uxLogoGrid.Children[0].Visibility = Visibility.Hidden;
                uxLogoGrid.Children[2].Visibility = Visibility.Hidden;
            }
        }

        private void LoadUiConfig()
        {
            if (HistoryWall.MainBackCollect.ImgBackground != string.Empty)
            {
                ChangeBackgrundImage(GlobalFunction.thumbPath + HistoryWall.MainBackCollect.ImgBackground);
            }
            else
            {
                ChangeBackgrundColor(HistoryWall.MainBackCollect.BackgroundColor);
            }
            BitmapImage img = UtilityFunctions.LoadBitmapImage(GlobalFunction.thumbPath +
                HistoryWall.MainBackCollect.ImgLogo, (int)uxLeftGrid.Width);
            uxLeftImg.Source = uxRightImg.Source = img;
            ChangeEventColor();
            ChangeTimelineColor();
        }

        private void ChangeEventColor()
        {
            Grid tempGd = uxEventSingleGrid;
            switch (screenCount)
            {
                case Utilities.UtilityClass.ScreenSet.Single:
                    tempGd = uxEventSingleGrid;
                    break;
                case Utilities.UtilityClass.ScreenSet.Double:
                    tempGd = uxEventDoubleGrid;
                    break;
                case Utilities.UtilityClass.ScreenSet.Triple:
                    tempGd = uxEventTripleGrid;
                    break;
            }
            SolidColorBrush brushBackground = new SolidColorBrush(HistoryWall.EventCollect.BackgroundColor);
            SolidColorBrush brushText = new SolidColorBrush(HistoryWall.EventCollect.TextColor);
            SolidColorBrush brushThickness = new SolidColorBrush(HistoryWall.EventCollect.ThicknessColor);
            for (int i = 0; i < tempGd.Children.Count; i++)
            {
                Label lbl = tempGd.Children[i] as Label;
                lbl.Background = brushBackground;
                lbl.Foreground = brushText;
                lbl.BorderBrush = Brushes.Transparent;//brushThickness;
            }
        }

        private void ChangeTimelineColor()
        {
            StackPanel tempSP = uxTimelineSingleGrid;
            switch (screenCount)
            {
                case Utilities.UtilityClass.ScreenSet.Single:
                    tempSP = uxTimelineSingleGrid;
                    break;
                case Utilities.UtilityClass.ScreenSet.Double:
                    tempSP = uxTimelineDoubleGrid;
                    break;
                case Utilities.UtilityClass.ScreenSet.Triple:
                    tempSP = uxTimelineTripleGrid;
                    break;
            }
            SolidColorBrush brushBackground = new SolidColorBrush(HistoryWall.TimelineCollect.BackgroundColor);
            SolidColorBrush brushText = new SolidColorBrush(HistoryWall.TimelineCollect.TextColor);
            SolidColorBrush brushThickness = new SolidColorBrush(HistoryWall.TimelineCollect.ThicknessColor);
            for (int i = 0; i < tempSP.Children.Count; i++)
            {
                Label lbl = tempSP.Children[i] as Label;
                lbl.Background = brushBackground;
                lbl.Foreground = brushText;
                lbl.BorderBrush = Brushes.Transparent;//brushThickness;
            }
        }

        private void ChangeBackgrundColor(Color color)
        {
            SolidColorBrush brush = new SolidColorBrush(color);
            uxBgImageGrid.Background = brush;
        }

        private void ChangeBackgrundImage(string path)
        {
            uxBgImageGrid.Background = new ImageBrush(UtilityFunctions.LoadBitmapImage(path, (int)this.Width))
            {
                Stretch = Stretch.Fill
            };
        }

        #endregion
    }
}
