using System;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using HistoryWallEditTool.Helper;
using System.IO;
using HistoryWallEditTool.ControlsForData;
using System.Windows.Markup;
using System.Windows;

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

        private string imgLogo = string.Empty;

        private Image[] imgControl = null;

        #endregion

        #region Properties

        public Image[] ImgControl
        {
            get { return imgControl; }
            set
            {
                imgControl = value;
            }
        }

        public string ImgLogo
        {
            get { return imgLogo; }
            set
            {
                imgLogo = value;
                if (imgLogo != String.Empty)
                {
                    uxSelectLogoRbtn.IsChecked = true;
                    uxLogoTextBox.Text = GlobalFunction.thumbPath + imgLogo;
                }
            }
        }

        #endregion

        #region Consturctor

        public static CurrentDisplayLanguage cdl;

        public SelectDialogControl()
        {
            InitializeComponent();

            SysLanChange();
            uxSelectLogoRbtn.Checked += new System.Windows.RoutedEventHandler(uxSelectLogoRbtn_Checked);
            uxNoLogoRbtn.Checked += new System.Windows.RoutedEventHandler(uxNoLogoRbtn_Checked);
        }

        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

        private void uxNoLogoRbtn_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            uxLogoTextBox.IsEnabled = false;
            uxBrowseBtn.IsEnabled = false;
            if (imgControl != null)
            {
                for (int i = 0; i < imgControl.Length; i++)
                {
                    imgControl[i].Source = null;
                }
            }
        }

        private void uxSelectLogoRbtn_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            uxLogoTextBox.IsEnabled = true;
            uxBrowseBtn.IsEnabled = true;
            if (imgControl != null)
            {
                BitmapImage img = UtilityFunctions.LoadBitmapImage(uxLogoTextBox.Text, (int)imgControl[0].Width);
                for (int i = 0; i < imgControl.Length; i++)
                {
                    imgControl[i].Source = img;
                }
            }
        }

        private void btnBrowse_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
            openFileDialog.Multiselect = false;
            openFileDialog.DefaultExt = ".jpg";
            openFileDialog.Filter = @"Image documents (*.jpg, *.jpeg, *.bmp, *.png)|*.jpg; *.jpeg; *.bmp; *.png";
            bool? result = openFileDialog.ShowDialog();
            if (result == true)
            {
                uxLogoTextBox.Text = MoveFileToThumbFolder(openFileDialog.FileName,
                    GlobalFunction.thumbPath + "logoMap" + Path.GetExtension(openFileDialog.FileName));

                if (imgControl != null)
                {
                    BitmapImage img = UtilityFunctions.LoadBitmapImage(uxLogoTextBox.Text, (int)imgControl[0].Width);
                    for (int i = 0; i < imgControl.Length; i++)
                    {
                        imgControl[i].Source = img;
                    }
                }
            }
        }

        private string MoveFileToThumbFolder(string fromFile, string toFile)
        {
            if (File.Exists(fromFile))
            {
                File.Copy(fromFile, toFile, true);
            }
            return toFile;
        }

        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
    }
}
