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

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

        public Grid SetterGridBackground = null;
        private ColorPicker colorPicker = new ColorPicker(true);
        private Color selectColor;
        private Point selectPoint;
        private string imgPath;

        #endregion

        #region Properties

        public Color SelectColor
        {
            get { return selectColor; }
            set
            {
                selectColor = value;
                uxSelectColorRect.Fill = new SolidColorBrush(selectColor);
                colorPicker.SelectColor = selectColor;
            }
        }

        public Point SelectPoint
        {
            get { return selectPoint; }
            set
            {
                selectPoint = value;
                colorPicker.SelectPoint = selectPoint;
            }
        }

        public string ImgPath
        {
            get { return imgPath; }
            set
            {
                imgPath = value;
                uxImgSelectChkBox.IsChecked = true;
                uxSelectImgPath.Text = GlobalFunction.thumbPath + imgPath;
            }
        }

        #endregion

        #region Constructor
        public static CurrentDisplayLanguage cdl;

        public SettingControlVertical()
        {
            InitializeComponent();
            SysLanChange();

            this.Loaded += new System.Windows.RoutedEventHandler(SettingControlVertical_Loaded);
            colorPicker.OnColorChanged += new System.EventHandler<ColorEventArgs>(colorPicker_OnColorChanged);
            uxColorSelectChkBox.Checked += new RoutedEventHandler(uxColorSelectChkBox_Checked);
            uxImgSelectChkBox.Checked += new RoutedEventHandler(uxImgSelectChkBox_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 & EventHandler

        private void SettingControlVertical_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            uxColorPickerGrid.Children.Add(colorPicker);
            colorPicker.SelectColor = HistoryWall.MainBackCollect.BackgroundColor;
            colorPicker.SelectPoint = HistoryWall.MainBackCollect.BackgroundColorPoint.HasValue ?
                HistoryWall.MainBackCollect.BackgroundColorPoint.Value : new Point(0, 0);
            uxSelectColorRect.Fill = new SolidColorBrush(colorPicker.SelectColor);
            if (HistoryWall.MainBackCollect.ImgBackground != String.Empty)
            {
                ImgPath = HistoryWall.MainBackCollect.ImgBackground;
            }
        }

        private void uxImgSelectChkBox_Checked(object sender, RoutedEventArgs e)
        {
            uxSelectImgPath.IsEnabled = true;
            uxBrowseBtn.IsEnabled = true;
            uxColorPickerGrid.IsEnabled = false;
            if (File.Exists(uxSelectImgPath.Text))
            {
                BitmapImage img = UtilityFunctions.LoadBitmapImage(uxSelectImgPath.Text);
                SetterGridBackground.Background = new ImageBrush(img);
            }
        }

        private void uxColorSelectChkBox_Checked(object sender, RoutedEventArgs e)
        {
            uxSelectImgPath.IsEnabled = false;
            uxBrowseBtn.IsEnabled = false;
            SetterGridBackground.Background = uxSelectColorRect.Fill;
            uxColorPickerGrid.IsEnabled = true;
        }

        private void colorPicker_OnColorChanged(object sender, ColorEventArgs e)
        {
            uxSelectColorRect.Fill = new SolidColorBrush((e.SelectedColor));
            if (SetterGridBackground != null)
            {
                SetterGridBackground.Background = new SolidColorBrush(e.SelectedColor);
            }
        }

        private void CloseBtn_Click(object sender, RoutedEventArgs e)
        {
            if (uxImgSelectChkBox.IsChecked == true)
            {
                if (!File.Exists(uxSelectImgPath.Text))
                {
                    MessageBox.Show((string)FindResource("confirmFilePath"));
                    return;
                }
            }
            Grid gd = this.Parent as Grid;
            SettingWindow settingWindow = gd.Parent as SettingWindow;
            settingWindow.Close();
        }

        private void Button_Click(object sender, 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)
            {
                uxSelectImgPath.Text = MoveFileToThumbFolder(openFileDialog.FileName,
                    GlobalFunction.thumbPath + "bgMap" + Path.GetExtension(openFileDialog.FileName));
                BitmapImage img = UtilityFunctions.LoadBitmapImage(uxSelectImgPath.Text);
                SetterGridBackground.Background = new ImageBrush(img);
            }
        }

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

        #region Methods

        public Utilities.UtilityClass.ImagePointColor GetData()
        {
            Utilities.UtilityClass.ImagePointColor data = new Utilities.UtilityClass.ImagePointColor();
            if (uxColorSelectChkBox.IsChecked.Value == true)
            {
                data.ImgPath = String.Empty;
                data.SelectColor = colorPicker.SelectColor;
                data.SelectPoint = colorPicker.SelectPoint;
            }
            else
            {
                data.ImgPath = Path.GetFileName(uxSelectImgPath.Text);
                data.SelectColor = Colors.Transparent;
                data.SelectPoint = new Point(0, 0);
            }
            return data;
        }

        #endregion
    }
}
