using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows;
using System;
using HistoryWallEditTool.ControlsForData;
using System.IO;
using System.Windows.Markup;

namespace HistoryWallEditTool.Controls
{
    /// <summary>
    /// SelectLangControl.xaml 的互動邏輯
    /// </summary>
    public partial class SelectLangControl : UserControl
    {
        #region Class Members
        private List<TextBlock> langTextBlocks = null;
        private List<Label> btnLabels = null;

        #endregion

        #region Properties

        public FlowDirection[] LangDir
        {
            get
            {
                return new FlowDirection[] { flowOneDir, flowTwoDir, flowThreeDir, flowFourDir };
            }
        }

        public string[] LangText
        {
            get
            {
                return new string[] { uxDefaultLangText.Text, uxSecondLangText.Text, uxThirdLangText.Text, uxFourLangText.Text };
            }
        }

        public int DefaultLangIndex
        {
            get
            {
                if (uxDefaultLangRbtn.IsChecked == true)
                {
                    return 0;
                }
                else if (uxSecondLangRbtn.IsChecked == true)
                {
                    return 1;
                }
                else if (uxThirdLangRbtn.IsChecked == true)
                {
                    return 2;
                }
                else
                {
                    return 3;
                }
            }
        }

        #endregion

        #region Constructor
        public static CurrentDisplayLanguage cdl;

        public SelectLangControl()
        {
            InitializeComponent();

            SysLanChange();
        }

        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 uxFourLangRbtn_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            btnLabels[3].Background = btnLabels[4].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColor);
            btnLabels[3].Foreground = btnLabels[4].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColor);
            btnLabels[0].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColorUnselect);
            btnLabels[0].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColorUnselect);
            btnLabels[1].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColorUnselect);
            btnLabels[1].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColorUnselect);
            btnLabels[2].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColorUnselect);
            btnLabels[2].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColorUnselect);
            langTextBlocks[2].Text = "第四語系，第四語系，第四語系，第四語系，第四語系，第四語系";
        }

        private void uxThirdLangRbtn_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            btnLabels[2].Background = btnLabels[4].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColor);
            btnLabels[2].Foreground = btnLabels[4].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColor);
            btnLabels[0].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColorUnselect);
            btnLabels[0].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColorUnselect);
            btnLabels[1].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColorUnselect);
            btnLabels[1].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColorUnselect);
            btnLabels[3].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColorUnselect);
            btnLabels[3].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColorUnselect);
            langTextBlocks[2].Text = "第三語系，第三語系，第三語系，第三語系，第三語系，第三語系";
        }

        private void uxSecondLangRbtn_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            btnLabels[1].Background = btnLabels[4].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColor);
            btnLabels[1].Foreground = btnLabels[4].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColor);
            btnLabels[0].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColorUnselect);
            btnLabels[0].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColorUnselect);
            btnLabels[2].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColorUnselect);
            btnLabels[2].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColorUnselect);
            btnLabels[3].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColorUnselect);
            btnLabels[3].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColorUnselect);
            langTextBlocks[2].Text = "第二語系，第二語系，第二語系，第二語系，第二語系，第二語系";
        }

        private void uxDefaultLangRbtn_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            btnLabels[0].Background = btnLabels[4].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColor);
            btnLabels[0].Foreground = btnLabels[4].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColor);
            btnLabels[1].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColorUnselect);
            btnLabels[1].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColorUnselect);
            btnLabels[2].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColorUnselect);
            btnLabels[2].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColorUnselect);
            btnLabels[3].Background = new SolidColorBrush(HistoryWall.BtnSelectCollect.BackgroundColorUnselect);
            btnLabels[3].Foreground = new SolidColorBrush(HistoryWall.BtnSelectCollect.TextColorUnselect);
            langTextBlocks[2].Text = "第一語系，第一語系，第一語系，第一語系，第一語系，第一語系";
        }

        //判斷字串是不是只有中文字
        public bool IsUseDefaultString(string sourceStr)
        {
            bool result = false;
            int dRange = 0;
            int dstringMax = Convert.ToInt32("9fff", 16);
            int dstringMin = Convert.ToInt32("4e00", 16);

            for (int i = 0; i < sourceStr.Length; i++)
            {
                dRange = Convert.ToInt32(Convert.ToChar((sourceStr.Substring(i, 1))));
                if (dRange >= dstringMin && dRange < dstringMax)
                {
                    result = true;
                    break;
                }
            }

            return result;
        }

        private void uxFourLangText_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (IsUseDefaultString(uxFourLangText.Text))
            {
                uxFourLangText.MaxLength = 4;
            }
            else
            {
                uxFourLangText.MaxLength = 8;
            }

            btnLabels[3].Content = uxFourLangText.Text;
            if (uxFourLangText.Text.Trim() == string.Empty)
            {
                uxFourLangRbtn.IsEnabled = false;
            }
            else
            {
                uxFourLangRbtn.IsEnabled = true;
            }
        }

        private void uxThirdLangText_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (IsUseDefaultString(uxThirdLangText.Text))
            {
                uxThirdLangText.MaxLength = 4;
            }
            else
            {
                uxThirdLangText.MaxLength = 8;
            }

            btnLabels[2].Content = uxThirdLangText.Text;
            if (uxThirdLangText.Text.Trim() == string.Empty)
            {
                uxThirdLangRbtn.IsEnabled = false;
            }
            else
            {
                uxThirdLangRbtn.IsEnabled = true;
            }
        }

        private void uxSecondLangText_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (IsUseDefaultString(uxSecondLangText.Text))
            {
                uxSecondLangText.MaxLength = 4;
            }
            else
            {
                uxSecondLangText.MaxLength = 8;
            }

            btnLabels[1].Content = uxSecondLangText.Text;
            if (uxSecondLangText.Text.Trim() == string.Empty)
            {
                uxSecondLangRbtn.IsEnabled = false;
            }
            else
            {
                uxSecondLangRbtn.IsEnabled = true;
            }
        }

        private void uxDefaultLangText_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (IsUseDefaultString(uxDefaultLangText.Text))
            {
                uxDefaultLangText.MaxLength = 4;
            }
            else
            {
                uxDefaultLangText.MaxLength = 8;
            }

            btnLabels[0].Content = uxDefaultLangText.Text;
            if (uxDefaultLangText.Text.Trim() == string.Empty)
            {
                uxDefaultLangRbtn.IsEnabled = false;
            }
            else
            {
                uxDefaultLangRbtn.IsEnabled = true;
            }
        }

        private void uxCloseBtn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Grid gd = this.Parent as Grid;
            DialogWindow window = gd.Parent as DialogWindow;
            window.Close();
        }

        #endregion

        #region Methods

        public void SetLangControl(StackPanel textPanel, Grid btnPanel)
        {
            List<TextBlock> textblocks = new List<TextBlock>();
            for (int i = 0; i < textPanel.Children.Count; i++)
            {
                TextBlock tb = textPanel.Children[i] as TextBlock;
                textblocks.Add(tb);
            }
            langTextBlocks = textblocks;

            List<Label> labels = new List<Label>();
            for (int i = 0; i < btnPanel.Children.Count; i++)
            {
                Label lbl = btnPanel.Children[i] as Label;
                labels.Add(lbl);
            }
            btnLabels = labels;
            LoadComponent();
        }

        private void LoadComponent()
        {
            string[] text = HistoryWall.MainBackCollect.LangText;
            FlowDirection[] dir = HistoryWall.MainBackCollect.LangFlowDir;
            uxDefaultLangText.TextChanged += new TextChangedEventHandler(uxDefaultLangText_TextChanged);
            uxSecondLangText.TextChanged += new TextChangedEventHandler(uxSecondLangText_TextChanged);
            uxThirdLangText.TextChanged += new TextChangedEventHandler(uxThirdLangText_TextChanged);
            uxFourLangText.TextChanged += new TextChangedEventHandler(uxFourLangText_TextChanged);
            uxDefaultLangRbtn.Checked += new System.Windows.RoutedEventHandler(uxDefaultLangRbtn_Checked);
            uxSecondLangRbtn.Checked += new System.Windows.RoutedEventHandler(uxSecondLangRbtn_Checked);
            uxThirdLangRbtn.Checked += new System.Windows.RoutedEventHandler(uxThirdLangRbtn_Checked);
            uxFourLangRbtn.Checked += new System.Windows.RoutedEventHandler(uxFourLangRbtn_Checked);
            uxDefaultLangText.Text = text[0];
            uxSecondLangText.Text = text[1];
            uxThirdLangText.Text = text[2];
            uxFourLangText.Text = text[3];

            if (HistoryWall.MainBackCollect.DefaultLangIndex == 0)
            {
                uxDefaultLangRbtn.IsChecked = true;
            }
            else if (HistoryWall.MainBackCollect.DefaultLangIndex == 1)
            {
                uxSecondLangRbtn.IsChecked = true;
            }
            else if (HistoryWall.MainBackCollect.DefaultLangIndex == 2)
            {
                uxThirdLangRbtn.IsChecked = true;
            }
            else if (HistoryWall.MainBackCollect.DefaultLangIndex == 3)
            {
                uxFourLangRbtn.IsChecked = true;
            }

            for (int i = 0; i < dir.Length; i++)
            {
                if (dir[i] == FlowDirection.RightToLeft)
                {
                    if (i == 0)
                    {
                        uxLangDirOneR.IsChecked = true;
                    }
                    else if (i == 1)
                    {
                        uxLangDirTwoR.IsChecked = true;
                    }
                    else if (i == 2)
                    {
                        uxLangDirThreeR.IsChecked = true;
                    }
                    else if (i == 3)
                    {
                        uxLangDirFourR.IsChecked = true;
                    }
                }
            }
        }

        #endregion

        #region Language FlowDirection Setting
        private FlowDirection flowOneDir = FlowDirection.LeftToRight;
        private FlowDirection flowTwoDir = FlowDirection.LeftToRight;
        private FlowDirection flowThreeDir = FlowDirection.LeftToRight;
        private FlowDirection flowFourDir = FlowDirection.LeftToRight;

        private void uxLangDirOne_Checked(object sender, RoutedEventArgs e)
        {
            if ((sender as RadioButton).Tag.ToString() == "L")
            {
                flowOneDir = FlowDirection.LeftToRight;
            }
            else
            {
                flowOneDir = FlowDirection.RightToLeft;
            }
            uxDefaultLangText.FlowDirection = flowOneDir;
        }

        private void uxLangDirTwo_Checked(object sender, RoutedEventArgs e)
        {
            if ((sender as RadioButton).Tag.ToString() == "L")
            {
                flowTwoDir = FlowDirection.LeftToRight;
            }
            else
            {
                flowTwoDir = FlowDirection.RightToLeft;
            }
            uxSecondLangText.FlowDirection = flowTwoDir;
        }

        private void uxLangDirThree_Checked(object sender, RoutedEventArgs e)
        {
            if ((sender as RadioButton).Tag.ToString() == "L")
            {
                flowThreeDir = FlowDirection.LeftToRight;
            }
            else
            {
                flowThreeDir = FlowDirection.RightToLeft;
            }
            uxThirdLangText.FlowDirection = flowThreeDir;
        }

        private void uxLangDirFour_Checked(object sender, RoutedEventArgs e)
        {
            if ((sender as RadioButton).Tag.ToString() == "L")
            {
                flowFourDir = FlowDirection.LeftToRight;
            }
            else
            {
                flowFourDir = FlowDirection.RightToLeft;
            }
            uxFourLangText.FlowDirection = flowFourDir;
        }
        #endregion

    }
}
