using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
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;
namespace SPC.Kiosk.Common
{
    /// <summary>
    /// MessageBoxForm.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MessageBoxForm : Window 
    {
        #region [ Members ]
        private DispatcherTimer ShowTimer;
        /// <summary>
        /// MessageBox 응답 결과
        /// </summary>
        public MessageBoxResult BoxResult { get; set; }
        /// <summary>
        /// MessageBox Button 구성 종류
        /// </summary>
        public MessageBoxButton BoxButton { get; set; }
        /// <summary>
        /// MessageBox Open Animation Type
        /// </summary>
        public OpenCloseAnimationType BoxOpenAnimationType { get; set; } 
        /// <summary>
        /// MessageBox Close Animation Type
        /// </summary>
        public OpenCloseAnimationType BoxCloseAnimationType { get; set; }
        /// <summary>
        /// 시작 종료 투명도 
        /// </summary>
        public double StartEndOpacity { get; set; } 
        /// <summary>
        /// Animation 시강(초) 
        /// </summary>
        public double AnimationSeconds { get; set; } 
        /// <summary>
        /// 표시 대기 시간 (초) 0= 무한대
        /// </summary>
        public double ShowWaitTime { get; set; }
        private int ScreenNo { get; set; }
        private Size PopupSize { get; set; }

        public SupportLanguageType LanguageType { get; set; }
        #endregion Members

        #region [ Ctor  / Etc ]
        /// <summary>
        /// MesssageBox 초기화 (for ShowMesssageBox)
        /// </summary>
        /// <param name="_screenNo"></param>
        /// <param name="_size"></param>
        /// <param name="_boxButton"></param>
        /// <param name="_openAnimationType"></param>
        /// <param name="_closeAnimationType"></param>
        /// <param name="_startEndOpacity"></param>
        /// <param name="animationSeconds"></param>
        /// <param name="_waitTime"></param>
        public MessageBoxForm(int _screenNo, 
                              Size _size,
                              MessageBoxButton _boxButton,
                              OpenCloseAnimationType _openAnimationType , 
                              OpenCloseAnimationType _closeAnimationType , 
                              double _startEndOpacity , 
                              double animationSeconds ,
                              double _waitTime)
        {
            ScreenNo = _screenNo;
            PopupSize = _size;
            BoxButton = _boxButton;
            BoxOpenAnimationType = _openAnimationType;
            BoxCloseAnimationType = _closeAnimationType;
            StartEndOpacity = _startEndOpacity;
            AnimationSeconds = animationSeconds;
            ShowWaitTime = _waitTime;
            InitializeComponent();
            if (ShowWaitTime > 0)
            {
                ShowTimer = new DispatcherTimer
                {
                    Interval = TimeSpan.FromSeconds(ShowWaitTime),
                    IsEnabled = true
                };
                ShowTimer.Tick += ShowTimer_Tick;
            }
            BaseInit();
            this.Yes_Button.MouseClicked += Yes_Button_MouseClicked;
            this.No_Button.MouseClicked += No_Button_MouseClicked;
            this.Cancel_Button.MouseClicked += Cancel_Button_MouseClicked;
        }
        /// <summary>
        ///  MesssageBox 초기화 (for TimeMesssageBox)
        /// </summary>
        /// <param name="_screenNo"></param>
        /// <param name="_size"></param>
        /// <param name="_openAnimationType"></param>
        /// <param name="_closeAnimationType"></param>
        /// <param name="_startEndOpacity"></param>
        /// <param name="animationSeconds"></param>
        /// <param name="_waitTime"></param>
        public MessageBoxForm(int _screenNo,
                              Size _size,
                              OpenCloseAnimationType _openAnimationType,
                              OpenCloseAnimationType _closeAnimationType,
                              double _startEndOpacity,
                              double animationSeconds,
                              double _waitTime)
        {
            ScreenNo = _screenNo;
            PopupSize = _size;
            BoxOpenAnimationType = _openAnimationType;
            BoxCloseAnimationType = _closeAnimationType;
            StartEndOpacity = _startEndOpacity;
            AnimationSeconds = animationSeconds;
            InitializeComponent();
            ShowWaitTime = _waitTime.Equals(0) ? 5 :  _waitTime;
            ShowTimer = new DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(ShowWaitTime),
                IsEnabled = false
            };
            ShowTimer.Tick += ShowTimer_Tick;
            BaseInit();
            this.ButtonStackHight.Height = new GridLength(0);
        }
        private void BaseInit()
        {
            this.Yes_Button.DisplayLanguage= CommonValue.CommonLanguageType;
            this.No_Button.DisplayLanguage = CommonValue.CommonLanguageType;
            this.Cancel_Button.DisplayLanguage = CommonValue.CommonLanguageType;
            this.MessageHeader.DisplayLanguage = CommonValue.CommonLanguageType;
            this.Message.DisplayLanguage = CommonValue.CommonLanguageType;
            this.Left = Screen.AllScreens[ScreenNo].WorkingArea.Left;
            this.Top = Screen.AllScreens[ScreenNo].WorkingArea.Top;
            this.Width = PopupSize.Width.Equals(0) ? Screen.AllScreens[ScreenNo].Bounds.Width : PopupSize.Width;
            this.Height = PopupSize.Height.Equals(0) ? Screen.AllScreens[ScreenNo].Bounds.Height : PopupSize.Height;
            this.IsEnabled = false;
            this.Loaded += MessageBoxForm_Loaded;
        }
        #endregion Ctor  / Etc

        #region [ Methods ]
        private void CloseBase()
        {
            if (ShowTimer != null)
            {
                ShowTimer.Stop();
                ShowTimer.Tick -= ShowTimer_Tick;
                ShowTimer = null;
            }
            this.Loaded -= MessageBoxForm_Loaded;
            this.Yes_Button.MouseClicked -= Yes_Button_MouseClicked;
            this.No_Button.MouseClicked -= No_Button_MouseClicked;
            this.Cancel_Button.MouseClicked -= Cancel_Button_MouseClicked;

            if (!BoxCloseAnimationType.Equals(OpenCloseAnimationType.None))
            {
                var animationVector = BoxCloseAnimationType.Equals(OpenCloseAnimationType.GotoLeft) || BoxCloseAnimationType.Equals(OpenCloseAnimationType.GotoRight)
                                        ? this.BoxBase.Width : this.BoxBase.Height;
                var CloseAnimation = Animations.GetCloseAnimation(this.BoxBase, BoxCloseAnimationType, animationVector, StartEndOpacity, AnimationSeconds);
                if (CloseAnimation != null)
                {
                    CloseAnimation.Completed += CloseAnimation_Completed;
                    CloseAnimation.Begin();
                }
            }
            else
            {
                this.Close();
            }
        }

        #endregion Methods

        #region [  Event Handlers  ]
        private void ShowTimer_Tick(object sender, EventArgs e)
        {
            ShowTimer.Stop();
            BoxResult = MessageBoxResult.None;
            CloseBase();
        }

        private void MessageBoxForm_Loaded(object sender, RoutedEventArgs e)
        {
            if (!BoxOpenAnimationType.Equals(OpenCloseAnimationType.None))
            {
                var animationVector = BoxOpenAnimationType.Equals(OpenCloseAnimationType.GotoLeft) || BoxOpenAnimationType.Equals(OpenCloseAnimationType.GotoRight)
                                        ? this.BoxBase.Width : this.BoxBase.Height;
                var OpenAnimations = Animations.GetOpenAnimation(this.BoxBase, BoxOpenAnimationType, animationVector, StartEndOpacity, AnimationSeconds);
                if (OpenAnimations != null)
                {
                    OpenAnimations.Completed += OpenAnimations_Completed;
                    OpenAnimations.Begin();
                }
            }
            else
            {
                if (ShowTimer != null) ShowTimer.Start();
                this.IsEnabled = true;
            }
        }

        private void OpenAnimations_Completed(object sender, EventArgs e)
        {
            if (ShowTimer != null) ShowTimer.Start();
            this.IsEnabled = true;
        }

        private void CloseAnimation_Completed(object sender, EventArgs e)
        {
            
            this.Close();
        }

        private void Cancel_Button_MouseClicked(object sender, RoutedEventArgs e)
        {
            if (ShowTimer != null) ShowTimer.Stop();
            switch (BoxButton)
            {
                case MessageBoxButton.OKCancel:
                case MessageBoxButton.YesNoCancel:
                    BoxResult = MessageBoxResult.Cancel;
                    break;
                default:
                    BoxResult = MessageBoxResult.None;
                    break;
            }
            this.Cancel_Button.Switch = true;
            CloseBase();
        }

        private void No_Button_MouseClicked(object sender, RoutedEventArgs e)
        {
            if (ShowTimer != null) ShowTimer.Stop();
            switch (BoxButton)
            {
                case MessageBoxButton.YesNo:
                case MessageBoxButton.YesNoCancel:
                    BoxResult = MessageBoxResult.No;
                    break;
                default:
                    BoxResult = MessageBoxResult.None;
                    break;
            }
            this.No_Button.Switch = true;
            CloseBase();
        }

        private void Yes_Button_MouseClicked(object sender, RoutedEventArgs e)
        {
            if (ShowTimer != null) ShowTimer.Stop();
            switch (BoxButton)
            {
                case MessageBoxButton.OK:
                case MessageBoxButton.OKCancel:
                    BoxResult = MessageBoxResult.OK;
                    break;
                case MessageBoxButton.YesNo:
                case MessageBoxButton.YesNoCancel:
                    BoxResult = MessageBoxResult.Yes;
                    break;
                default:
                    BoxResult = MessageBoxResult.None;
                    break;
            }
            this.Yes_Button.Switch = true;
            CloseBase();
        }


        #endregion Event Handlers 
    }
}
