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 SPC.Kiosk.Common;
using System.Windows.Threading;
using System.Text.RegularExpressions;
using SPC.Kiosk.Popup.Model;
using SPC.Kiosk.Popup.ViewModel;
namespace SPC.Kiosk.Popup
{
    /// <summary>
    /// PkCashBagCertify.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class OkCashBagUsing : Window 
    {
        #region [  Members  ]
        private int ScreenNo { get; set; } = 0;
        private VmOkCashBagUsing ViewModel { get; set; }
        private double GetPayments { get; set; }
        private bool GetReserveEnabled { get; set; }
        #endregion Members

        #region [ Ctor ]
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="_totalPayment"></param>
        /// <param name="_reserveEnabled"></param>
        public OkCashBagUsing(double _totalPayment = 0, bool _reserveEnabled = true)
        {

            GetPayments = _totalPayment;
            GetReserveEnabled = _reserveEnabled;
            InitializeComponent();
            this.ContentRendered += OkCashBagUsing_ContentRendered;
            this.Loaded += OkCashBagCertify_Loaded;
            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;

        }

        #endregion Ctor

        #region [  Methods  ]
        private void OpenWindow()
        {
            this.AfterBase.Visibility = Visibility.Hidden;
            var OpenAnimations = Animations.GetOpenAnimation(this.FrameBase
                                                           , OpenCloseAnimationType.FullSizeUp
                                                           , this.FrameBase.Height
                                                           , 0.5
                                                           , 0.2);
            if (OpenAnimations != null)
            {
                OpenAnimations.Completed += OpenAnimations_Completed;
                OpenAnimations.Begin();
            }
        }
        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();
            }
        }
        
        #endregion Methods

        #region [ Event Handlers ]
        private void OpenAnimations_Completed(object sender, EventArgs e)
        {
            this.BarCode.Focus();
        }
        private void CloseAnimation_Completed(object sender, EventArgs e)
        {
            this.ContentRendered -= OkCashBagUsing_ContentRendered;
            this.Loaded -= OkCashBagCertify_Loaded;
            this.BarCode.PreviewKeyDown -= BarCode_PreviewKeyDown;
            this.BarCode.LostFocus -= BarCode_LostFocus;
            this.Close();
        }
        private void OkCashBagUsing_ContentRendered(object sender, EventArgs e)
        {
            this.FrameBase.Width = 1300;
            this.FrameBase.Height = 1010;
            OpenWindow();
        }

        private void OkCashBagCertify_Loaded(object sender, RoutedEventArgs e)
        {



            if (this.DataContext is VmOkCashBagUsing viewModel)
            {
                this.ViewModel = viewModel;
                this.ViewModel.PropertyChanged += ViewModel_PropertyChanged;
                this.ViewModel.Payments = GetPayments;
                this.ViewModel.ReservePoint = GetReserveEnabled;
                this.ViewModel.IsTimeoutPopup = false;
            }
            this.BarCode.PreviewKeyDown += BarCode_PreviewKeyDown;
            this.BarCode.LostFocus += BarCode_LostFocus;
            this.AfterBase.IsEnabled = false;
            this.FrameBase.Width = 0;
            this.FrameBase.Height = 0;
        }
        private void BarCode_LostFocus(object sender, RoutedEventArgs e)
        {
            this.BarCode.Focus();
        }
        private void BarCode_PreviewKeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (sender is System.Windows.Controls.TextBox textBox
                && (e.Key.Equals(Key.Enter) || e.Key.Equals(Key.Return))
                && !string.IsNullOrEmpty(textBox.Text))
            {
                this.ViewModel.ReadBarCode = textBox.Text.Trim();
                textBox.Text = string.Empty;
            }
        }
        private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "CanWindowClose":
                    if (this.ViewModel.CanWindowClose)
                    {
                        CloseWindow();
                    }
                    break;
                case "ToAfterBase":
                    if (this.ViewModel.ToAfterBase)
                    {
                        this.FrameBase.IsEnabled = false;
                        var AfterBaseOpenAnimations = Animations.GetOpenAndCloseAnimation(this.FrameBase
                                                                                        , this.AfterBase
                                                                                        , OpenCloseAnimationType.GotoRight
                                                                                        , this.FrameBase.Width, 0, 0.3);
                        if (AfterBaseOpenAnimations != null)
                        {
                            AfterBaseOpenAnimations.Completed += AfterBaseOpenAnimations_Completed;
                            AfterBaseOpenAnimations.Begin();
                        }
                    }
                    break;
            }
        }
        private void AfterBaseOpenAnimations_Completed(object sender, EventArgs e)
        {
            this.AfterBase.IsEnabled = true;
        }
        #endregion Event Handlers
    }

}
