using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using SPC.Kiosk.Base;
using SPC.Kiosk.Common;
using SPC.Kiosk.Popup.Model;
using SPC.Kiosk.Popup.ViewModel;
namespace SPC.Kiosk.Popup
{
    /// <summary>
    /// LanguageSelector.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class LanguageSelector : Window
    {
        #region [ Members ]
        private int ScreenNo { get; set; } = 0;
        private VmLanguageSelector ViewModel { get; set; }
        private SupportLanguageType GetLanguageType { get; set; }
        #endregion Members

        #region [ Ctor  / Etc ]
        /// <summary>
        /// Ctor
        /// </summary>
        public LanguageSelector(SupportLanguageType _supportLanguageType)
        {
            GetLanguageType = _supportLanguageType;
            InitializeComponent();
            this.Left = Screen.AllScreens[ScreenNo].WorkingArea.Left;
            this.Top = Screen.AllScreens[ScreenNo].WorkingArea.Top;
            this.Width = Screen.AllScreens[ScreenNo].Bounds.Width;
            this.Height = Screen.AllScreens[ScreenNo].Bounds.Height;
            this.Loaded += LanguageSelector_Loaded;
            this.ContentRendered += LanguageSelector_ContentRendered;
        }

        #endregion Ctor  / Etc

        #region [ Methods ]
        private void ChangeSwitch()
        {
            foreach (var aObj in this.LanguageButtonStack.Children)
            {
                if (aObj is AnimationButton aButton)
                {
                    if (aButton.Tag is SupportLanguageType languageType)
                    {
                        aButton.Switch = this.ViewModel.ShowLanguageType.Equals(languageType);
                        aButton.Text = languageType.GetLanguageNativeName();
                    }
                }
            }
        }
        private void CreateLanguageButtons()
        {
            foreach (var aLanguage in Enum.GetValues(typeof(SupportLanguageType)).Cast<SupportLanguageType>())
            {
                var visible = Visibility.Visible;
                switch (aLanguage)
                {
                    case SupportLanguageType.en:
                        if (this.ViewModel.OptionValue813 == "0") visible = Visibility.Collapsed;
                        break;
                    case SupportLanguageType.ja:
                        if (this.ViewModel.OptionValue814 == "0") visible = Visibility.Collapsed;
                        break;
                    case SupportLanguageType.zh:
                        if (this.ViewModel.OptionValue815 == "0") visible = Visibility.Collapsed;
                        break;
                    default:
                        break;
                }

                var ButtonData = new M_AnimationButton
                {
                    Width = 180,
                    Height = 180,
                    BrushStretch = Stretch.Fill,
                    ClickAnimationType = ButtonAnimationType.SizeDown,
                    CircleBackBrush = "Transparent",
                    CircleTextBruch = "Transparent",
                    CircleText = string.Empty,
                    Switch = this.ViewModel.ShowLanguageType.Equals(aLanguage),
                    TextVisible = Visibility.Collapsed,
                    Margin = new Thickness(33,0,33,0),                    
                };
                switch (aLanguage)
                {
                    case SupportLanguageType.ko:
                        ButtonData.NormalBrush = ResourceManager.GetNximagePathAdd("btn_pop_kor_n.png", CommonValue.PBdesignImagesPath);
                        ButtonData.SwitchOnBrush = ResourceManager.GetNximagePathAdd("btn_pop_kor_s.png", CommonValue.PBdesignImagesPath);
                        break;
                    case SupportLanguageType.en:
                        ButtonData.NormalBrush = ResourceManager.GetNximagePathAdd("btn_pop_eng_n.png", CommonValue.PBdesignImagesPath);
                        ButtonData.SwitchOnBrush = ResourceManager.GetNximagePathAdd("btn_pop_eng_s.png", CommonValue.PBdesignImagesPath);
                        break;
                    case SupportLanguageType.zh:
                        ButtonData.NormalBrush = ResourceManager.GetNximagePathAdd("btn_pop_chi_n.png", CommonValue.PBdesignImagesPath);
                        ButtonData.SwitchOnBrush = ResourceManager.GetNximagePathAdd("btn_pop_chi_s.png", CommonValue.PBdesignImagesPath);
                        break;
                    case SupportLanguageType.ja:
                        ButtonData.NormalBrush = ResourceManager.GetNximagePathAdd("btn_pop_jap_n.png", CommonValue.PBdesignImagesPath);
                        ButtonData.SwitchOnBrush = ResourceManager.GetNximagePathAdd("btn_pop_jap_s.png", CommonValue.PBdesignImagesPath);
                        break;
                }
                var newLanauageButton = new AnimationButton
                {
                    Name = string.Format("Button_{0}", aLanguage),
                    DataParameter = ButtonData,
                    Tag = aLanguage,
                    Visibility = visible
                };

                newLanauageButton.MouseClicked += NewLanauageButton_MouseClicked;
                this.LanguageButtonStack.Children.Add(newLanauageButton);
                this.ReregisterName(string.Format("Button_{0}", aLanguage), newLanauageButton);
            }
        }

        private void CloseWindow()
        {
            var CloseAnimation = Animations.GetCloseAnimation(this.FrameBase
                                                            , OpenCloseAnimationType.FullSizeDown
                                                            , this.FrameBase.Height
                                                            , 0.5
                                                            , 0.2);
            if (CloseAnimation != null)
            {
                CloseAnimation.Completed += CloseAnimation_Completed;
                CloseAnimation.Begin();
            }
        }

        private void OpenWindow()
        {
            var OpenAnimations = Animations.GetOpenAnimation(this.FrameBase
                                                           , OpenCloseAnimationType.FullSizeUp
                                                           , this.FrameBase.Height
                                                           , 0.5
                                                           , 0.2);
            if (OpenAnimations != null)
            {
                OpenAnimations.Completed += OpenAnimations_Completed;
                OpenAnimations.Begin();
            }
        }
        #endregion Methods

        #region [  Event Handlers  ]


        private void CloseAnimation_Completed(object sender, EventArgs e)
        {
            this.Loaded -= LanguageSelector_Loaded;
            this.ContentRendered -= LanguageSelector_ContentRendered;
            this.ViewModel.Dispose();
            this.Close();
        }
        private void OpenAnimations_Completed(object sender, EventArgs e)
        {
        }

        private void NewLanauageButton_MouseClicked(object sender, RoutedEventArgs e)
        {
            if (sender is AnimationButton clickedButton)
            {
                if (clickedButton.Tag is SupportLanguageType changeLanguage)
                {
                    if (!this.ViewModel.ShowLanguageType.Equals(changeLanguage))
                    {
                        this.ViewModel.ShowLanguageType = changeLanguage;
                        ChangeSwitch();
                    }
                }
            }
        }
        private void LanguageSelector_ContentRendered(object sender, EventArgs e)
        {
            this.FrameBase.Width = 1300;
            this.FrameBase.Height = 1010;
            OpenWindow();
        }

        private void LanguageSelector_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.DataContext is VmLanguageSelector viewModel)
            {
                this.ViewModel = viewModel;
                this.ViewModel.PropertyChanged += ViewModel_PropertyChanged;
                this.ViewModel.ShowLanguageType = GetLanguageType;
                this.ViewModel.IsTimeoutPopup = false;
            }
            CreateLanguageButtons();
            this.UpdateLayout();
            this.FrameBase.Width = 0;
            this.FrameBase.Height = 0;
        }

        private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "CanWindowClose":
                    if (this.ViewModel.CanWindowClose)
                    {
                        CloseWindow();
                    }
                    break;

            }
        }
        #endregion Event Handlers

    }
}
