using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
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.Forms;
using System.Drawing;
using SPC.Kiosk.Base;
using System.Windows.Threading;

namespace SPC.Kiosk.Common
{
    /// <summary>
    /// PopupMessage.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class PopupMessage : 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; } = 0;
        /// <summary>
        /// LanguageType Code
        /// </summary>
        public SupportLanguageType LanguageType { get; set; }
        #endregion Members

        #region [ Ctor ]
        /// <summary>
        /// Ctor
        /// </summary>
        public PopupMessage(SupportLanguageType _languageType,
                            OpenCloseAnimationType _openAnimationType,
                            OpenCloseAnimationType _closeAnimationType,
                            double _startEndOpacity,
                            double animationSeconds,
                            double _waitTime)
        {
            LanguageType = _languageType;
            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;
            }
            InitializeComponent();
            BaseInit();
            this.Loaded += PopupMessage_Loaded;

        }
        #endregion Ctor

        #region [  Methods  ]
        private void BaseInit()
        {
            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.Yes_Button.DisplayLanguage = LanguageType;
            this.No_Button.DisplayLanguage = LanguageType;
            this.Cancel_Button.DisplayLanguage = LanguageType;
            this.Cancel_Button.DisplayLanguage = LanguageType;
            this.Message1.DisplayLanguage = LanguageType;
            this.Message2.DisplayLanguage = LanguageType;
            this.Yes_Button.MouseClicked += Yes_Button_MouseClicked;
            this.No_Button.MouseClicked += No_Button_MouseClicked;
            this.Cancel_Button.MouseClicked += Cancel_Button_MouseClicked;

            this.IsEnabled = false;
        }
        private void OpenWindow()
        {
            if (!BoxOpenAnimationType.Equals(OpenCloseAnimationType.None))
            {
                var animationVector = BoxOpenAnimationType.Equals(OpenCloseAnimationType.GotoLeft) || BoxOpenAnimationType.Equals(OpenCloseAnimationType.GotoRight)
                                ? this.FrameBase.Width : this.FrameBase.Height;

                var OpenAnimations = Animations.GetOpenAnimation(this.FrameBase
                                                               , BoxOpenAnimationType
                                                               , animationVector
                                                               , StartEndOpacity
                                                               , AnimationSeconds);
                if (OpenAnimations != null)
                {
                    OpenAnimations.Completed += OpenAnimations_Completed;
                    OpenAnimations.Begin();
                } 
            }
            else
            {
                if (ShowTimer != null) ShowTimer.Start();
                this.IsEnabled = true;
            }
        }
        private void CloseWindow()
        {
            if (ShowTimer != null)
            {
                ShowTimer.Stop();
                ShowTimer.Tick -= ShowTimer_Tick;
                ShowTimer = null;
            }
            this.Loaded -= PopupMessage_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.FrameBase.Width : this.FrameBase.Height;

                var CloseAnimation = Animations.GetCloseAnimation(this.FrameBase
                                                                , 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;
            CloseWindow();
        }
        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 PopupMessage_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
            {
                if (ShowTimer != null)
                {
                    ShowTimer.Stop();
                    ShowTimer.Tick -= ShowTimer_Tick;
                    ShowTimer = null;
                }
                this.Close();
            }
            else
            {
                OpenWindow();
            }
        }

        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;
            }
            CloseWindow();
        }

        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;
            }
            CloseWindow();
        }

        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;
            }
            CloseWindow();
        }





        #endregion Event Handlers
    }


}
