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 SPC.Kiosk.Popup.Model;
using SPC.Kiosk.Popup.ViewModel;
using System.Windows.Media.Animation;

namespace SPC.Kiosk.Popup
{
    /// <summary>
    /// PaymentsEnd.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class PaymentsEnd : Window
    {
        #region [  Members  ]
        private int ScreenNo { get; set; } = 0;
        private VmPaymentsEnd ViewModel { get; set; }
        /// <summary>
        /// HappyPoint 인증 및 사용 정보
        /// </summary>
        private PaymentEndWindowEndType GetPaymentEndWindow { get; set; }
        private List<M_BasketItem> GetBasketItems { get; set; }
        private bool GetHappyPointReseve { get; set; }
        private bool GetOkCashBagReseve { get; set; }
        private M_HappyPointReturn GetHappyPointReturn { get; set; }

        #endregion Members

        #region [ Ctor ]
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="_happyPointReturn"></param>
        /// <param name="_totalPayment"></param>
        public PaymentsEnd(PaymentEndWindowEndType _paymentEndWindow
                         , List<M_BasketItem> _getBasketItems
                         , bool _happyPointReserve
                         , bool _okCashBagReserve
                         , M_HappyPointReturn _getHappyPointReturn = null)
        {
            GetPaymentEndWindow = _paymentEndWindow;
            GetBasketItems = _getBasketItems;
            GetHappyPointReseve = _happyPointReserve;
            GetOkCashBagReseve = _okCashBagReserve;
            GetHappyPointReturn = _getHappyPointReturn;
            InitializeComponent();
            this.ContentRendered += PaymentsEnd_ContentRendered;
            this.Loaded += PaymentsEnd_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()
        {
            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();
            }
        }
        private void ChangeWindow()
        {
            try
            {
                var _target = this.FrameData;
                var targetAnimation = new Storyboard();
                var centerX = _target.ActualWidth / 2;
                var centerY = _target.ActualHeight / 2;
                _target.RenderTransform = new ScaleTransform(1, 1, centerX, centerY);
                var flipXAni = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(0.2)));
                Storyboard.SetTarget(flipXAni, _target);
                Storyboard.SetTargetProperty(flipXAni, new PropertyPath("RenderTransform.ScaleX"));
                targetAnimation.Children.Add(flipXAni);
                targetAnimation.Completed += TargetAnimation_Completed;
                targetAnimation.Begin(); 
                
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "ChangeWindow()", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
            }
        }
        private void TargetAnimation_Completed(object sender, EventArgs e)
        {
        }
        #endregion Methods

        #region [ Event Handlers ]
        private void OpenAnimations_Completed(object sender, EventArgs e)
        {
        }
        private void CloseAnimation_Completed(object sender, EventArgs e)
        {
            this.ContentRendered -= PaymentsEnd_ContentRendered;
            this.Loaded -= PaymentsEnd_Loaded;
            this.ViewModel.Dispose();
            this.Close();
        }
        private void PaymentsEnd_ContentRendered(object sender, EventArgs e)
        {
            this.FrameBase.Width = 1300;
            this.FrameBase.Height = 1010;
            OpenWindow();

        }
        private void PaymentsEnd_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.DataContext is VmPaymentsEnd viewModel)
            {
                this.ViewModel = viewModel;
                this.ViewModel.PropertyChanged += ViewModel_PropertyChanged;
                this.ViewModel.EndWindow = GetPaymentEndWindow;
                this.ViewModel.BasketData = GetBasketItems;
                this.ViewModel.HappyPointReturn = GetHappyPointReturn;
                this.ViewModel.HappyPointReserve = GetHappyPointReseve;
                this.ViewModel.OkCashBagReserve = GetOkCashBagReseve;
                this.ViewModel.PopupWidth = this.FrameBase.Width;
                this.ViewModel.PaymentEndWindow = PaymentEndWindowType.CashReceipt;
                this.ViewModel.IsTimeoutPopup = false;
            }
            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)
                    {
                        if (this.ViewModel.ImmediatelyClose)
                        {
                            CloseAnimation_Completed(null, null);
                        }
                        else
                        {
                            CloseWindow();
                        }
                    }
                    break;
                case "PointUsingWindow":
                    ChangeWindow();
                    break;

            }
        }

        #endregion Event Handlers
    }

}
