using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using HistoryWallEditTool.Controls.PreviewControl;

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

        private Button btnClicked = null;
        private Utilities.UtilityClass.DialogType selectType;
        private DetailPreview previewer = null;

        #endregion

        #region Properties

        public string LangString
        {
            set
            {
                LoadLanguageResource(value);
            }
        }

        #endregion

        #region Constructors

        public DialogWindow(string imgLogoPath)
        {
            InitializeComponent();
            selectType = Utilities.UtilityClass.DialogType.ImageSelect;
            SelectDialogControl control = new SelectDialogControl();
            control.ImgLogo = imgLogoPath;
            uxRootGrid.Children.Add(control);
            this.Width = control.Width;
            this.Height = control.Height;
            this.Closing += new System.ComponentModel.CancelEventHandler(DialogWindow_Closing);
            LangString = HistoryWall.LangString;
        }

        public DialogWindow()
        {
            InitializeComponent();
            selectType = Utilities.UtilityClass.DialogType.LangSelect;
            SelectLangControl control = new SelectLangControl();
            uxRootGrid.Children.Add(control);
            this.Width = control.Width;
            this.Height = control.Height;
            this.Closing += new System.ComponentModel.CancelEventHandler(DialogWindow_Closing);
            LangString = HistoryWall.LangString;
        }

        #endregion

        #region Events

        private void DialogWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            selectImage.Visibility = Visibility.Hidden;
            btnClicked.Visibility = Visibility.Visible;
            foreach (Button btn in hiddenBtn)
            {
                btn.Visibility = Visibility.Visible;
            }
            if (selectType == Utilities.UtilityClass.DialogType.ImageSelect)
            {
                SelectDialogControl control = uxRootGrid.Children[0] as SelectDialogControl;
                string imgLogoPath = control.uxLogoTextBox.Text;
                if (control.uxSelectLogoRbtn.IsChecked == true)
                {
                    if (!File.Exists(imgLogoPath))
                    {
                        MessageBox.Show(imgLogoPath + "\t" + (string)FindResource("confirmFilePath"));
                        e.Cancel = true;
                    }
                    else
                    {
                        HistoryWall.MainBackCollect.ImgLogo = Path.GetFileName(imgLogoPath);
                    }
                }
                else
                {
                    HistoryWall.MainBackCollect.ImgLogo = String.Empty;
                }
            }
            else
            {
                SelectLangControl control = uxRootGrid.Children[0] as SelectLangControl;
                HistoryWall.MainBackCollect.LangText = control.LangText;
                HistoryWall.MainBackCollect.LangFlowDir = control.LangDir;
                HistoryWall.MainBackCollect.DefaultLangIndex = control.DefaultLangIndex;

                if (control.LangText[0] == "")
                    previewer.uxLang1Lbl.Visibility = Visibility.Hidden;
                else
                    previewer.uxLang1Lbl.Visibility = Visibility.Visible;

                if (control.LangText[1] == "")
                    previewer.uxLang2Lbl.Visibility = Visibility.Hidden;
                else
                    previewer.uxLang2Lbl.Visibility = Visibility.Visible;

                if (control.LangText[2] == "")
                    previewer.uxLang3Lbl.Visibility = Visibility.Hidden;
                else
                    previewer.uxLang3Lbl.Visibility = Visibility.Visible;

                if (control.LangText[3] == "")
                    previewer.uxLang4Lbl.Visibility = Visibility.Hidden;
                else
                    previewer.uxLang4Lbl.Visibility = Visibility.Visible;
            }
        }

        private void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            base.DragMove();
        }

        private void mw_create_event(string state)
        {
            if (state == "zh-TW" || state == "en-US")
            {
                LoadLanguageResource(state);
            }
            else
            {
                //Close
            }
        }

        #endregion

        #region Methods

        public void SetLogoControl(Image[] controls)
        {
            SelectDialogControl control = uxRootGrid.Children[0] as SelectDialogControl;
            control.ImgControl = controls;
        }

        public void SetLangControl(StackPanel spText, Grid spBtn)
        {
            SelectLangControl control = uxRootGrid.Children[0] as SelectLangControl;
            control.SetLangControl(spText, spBtn);
        }

        private List<Button> hiddenBtn;
        private Button selectImage;

        public void SetDelegate(HistoryWall mw, Button btn, List<Button> otherBtn, Button img, DetailPreview dp)
        {
            mw.create_event += new HistoryWall.CallBackToUIThread(mw_create_event);
            btnClicked = btn;
            hiddenBtn = otherBtn;
            selectImage = img;
            previewer = dp;
        }

        private void LoadLanguageResource(string lang)
        {
            ResourceDictionary rd = null;
            try
            {
                rd = Application.LoadComponent(
                    new Uri(@"Controls\Localizing\" + lang + ".xaml", UriKind.Relative)) as ResourceDictionary;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

            if (rd != null)
            {
                if (Resources.MergedDictionaries.Count > 0)
                {
                    Resources.MergedDictionaries.Clear();
                }
                Resources.MergedDictionaries.Add(rd);
            }
        }

        #endregion
    }
}
