using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Runtime.InteropServices;
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.Animation;
using System.Threading.Tasks;
using SPC.Kiosk.Common;
using SPC.Kiosk.PB.Model;
using SPC.Kiosk.PB.ViewModel;
using SPC.Kiosk.Popup.Model;
namespace SPC.Kiosk.PB
{
    /// <summary>
    /// PBPayments.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class PBPaymentsWindow : Window
    {
        #region [ Members ]
        private PBPaymentPageType ShowPage { get; set; }
        private int ScreenNo { get; set; } = 0;

        private PBPaymentsViewModel ViewModel { get; set; }
        private M_HappyPointReturn GetHappyPointReturn { get; set; }
        private ObservableCollection<M_BasketItem> GetBasketItems { get; set; }
        private double GetPayments { get; set; }

        #endregion Members

        #region [ Ctor/Etc ]
        /// <summary>
        /// Ctor
        /// </summary>
        public PBPaymentsWindow()
        {
            StartInitialize();
        }
        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="_getBasketItems"></param>
        /// <param name="_getHappyPointReturn"></param>
        /// <param name="_startPBPaymentPage"></param>
        public PBPaymentsWindow(ObservableCollection<M_BasketItem> _getBasketItems
                              , M_HappyPointReturn _getHappyPointReturn
                              , PBPaymentPageType _startPBPaymentPage)
        {
            GetHappyPointReturn = _getHappyPointReturn;
            GetBasketItems = _getBasketItems;
            StartInitialize();
            ShowPage = _startPBPaymentPage;
            switch (_startPBPaymentPage)
            {
                //case PBPaymentPageType.PaymentsSelect:
                //    ContentGrid.Visibility = Visibility.Hidden;
                //    PaymentSelectGrid.RenderTransform = new TranslateTransform(0, 0);
                //    break;
                case PBPaymentPageType.PointsDisCounts:
                    //PaymentSelectGrid.Visibility = Visibility.Hidden;
                    DetailBasketGrid.Visibility = Visibility.Hidden;
                    PaymentsGrid.Visibility = Visibility.Visible;
                    PointsDisCountsGrid.Visibility = Visibility.Visible;
                    CouponUsePointsGrid.Visibility = Visibility.Hidden;
                    PaymentExcuteGrid.Visibility = Visibility.Hidden;
                    PointsDisCountsGrid.RenderTransform = new TranslateTransform(0, 0);
                    CouponUsePointsGrid.RenderTransform = new TranslateTransform(CouponUsePointsGrid.Width, 0);
                    PaymentExcuteGrid.RenderTransform = new TranslateTransform(PaymentExcuteGrid.Width, 0);
                    break;
                case PBPaymentPageType.CouponUsePoints:
                    //PaymentSelectGrid.Visibility = Visibility.Hidden;
                    DetailBasketGrid.Visibility = Visibility.Hidden;
                    PaymentsGrid.Visibility = Visibility.Visible;
                    PointsDisCountsGrid.Visibility = Visibility.Hidden;
                    CouponUsePointsGrid.Visibility = Visibility.Visible;
                    PaymentExcuteGrid.Visibility = Visibility.Hidden;
                    PointsDisCountsGrid.RenderTransform = new TranslateTransform(PointsDisCountsGrid.Width, 0);
                    CouponUsePointsGrid.RenderTransform = new TranslateTransform(0, 0);
                    PaymentExcuteGrid.RenderTransform = new TranslateTransform(PaymentExcuteGrid.Width, 0);
                    break;
                case PBPaymentPageType.PaymentExcute:
                    //PaymentSelectGrid.Visibility = Visibility.Hidden;
                    DetailBasketGrid.Visibility = Visibility.Hidden;
                    PaymentsGrid.Visibility = Visibility.Visible;
                    PointsDisCountsGrid.Visibility = Visibility.Hidden;
                    CouponUsePointsGrid.Visibility = Visibility.Hidden;
                    PaymentExcuteGrid.Visibility = Visibility.Visible;
                    PointsDisCountsGrid.RenderTransform = new TranslateTransform(PointsDisCountsGrid.Width, 0);
                    CouponUsePointsGrid.RenderTransform = new TranslateTransform(CouponUsePointsGrid.Width, 0);
                    PaymentExcuteGrid.RenderTransform =  new TranslateTransform(0, 0);
                    break;
                case PBPaymentPageType.DetailBasket:
                    //PaymentSelectGrid.Visibility = Visibility.Hidden;
                    DetailBasketGrid.Visibility = Visibility.Visible;
                    PaymentsGrid.Visibility = Visibility.Hidden;
                    PointsDisCountsGrid.RenderTransform = new TranslateTransform(0, 0);
                    //PaymentSelectGrid.RenderTransform = new TranslateTransform(PaymentSelectGrid.Width, 0);
                    break;

            }
        }

        #endregion

        #region [ Methods ]
        private void StartInitialize()
        {
            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 += PBPayments_Loaded;
        }
        private void SetPage()
        {
            if (!ShowPage.Equals(ViewModel.DefaultPage))
            {
                switch (ShowPage)
                {
                    case PBPaymentPageType.DetailBasket:
                        switch (ViewModel.DefaultPage)
                        {
                            case PBPaymentPageType.PointsDisCounts:
                                PaymentsGrid.Visibility = Visibility.Visible;
                                PaymentsGrid.RenderTransform = new TranslateTransform(PaymentsGrid.Width, 0);
                                OpenAndClose(this.DetailBasketGrid, this.PaymentsGrid, OpenCloseAnimationType.GotoLeft);
                                break;
                        }
                        break;
                    case PBPaymentPageType.PointsDisCounts:
                        switch (ViewModel.DefaultPage)
                        {
                            case PBPaymentPageType.DetailBasket:
                                OpenAndClose(this.PaymentsGrid, this.DetailBasketGrid, OpenCloseAnimationType.GotoRight);
                                break;
                            case PBPaymentPageType.CouponUsePoints:
                                OpenAndClose(this.PointsDisCountsGrid, this.CouponUsePointsGrid, OpenCloseAnimationType.GotoLeft);
                                break;
                            case PBPaymentPageType.PaymentExcute:
                                OpenAndClose(this.PointsDisCountsGrid, this.PaymentExcuteGrid, OpenCloseAnimationType.GotoLeft);
                                break;
                        }
                        break;
                    case PBPaymentPageType.CouponUsePoints:
                        switch (ViewModel.DefaultPage)
                        {
                            case PBPaymentPageType.PointsDisCounts:
                                OpenAndClose(this.CouponUsePointsGrid, this.PointsDisCountsGrid, OpenCloseAnimationType.GotoRight);
                                break;
                            case PBPaymentPageType.PaymentExcute:
                                OpenAndClose(this.CouponUsePointsGrid, this.PaymentExcuteGrid, OpenCloseAnimationType.GotoLeft);
                                break;
                            case PBPaymentPageType.MoreVIewPoints:
                                OpenAndClose(this.CouponUsePointsGrid, this.MoreViewPointGrid, OpenCloseAnimationType.GotoRight);
                                break;
                        }
                        break;

                    case PBPaymentPageType.MoreVIewPoints:
                        switch (ViewModel.DefaultPage)
                        {

                            case PBPaymentPageType.PointsDisCounts:
                                OpenAndClose(this.MoreViewPointGrid, this.PointsDisCountsGrid, OpenCloseAnimationType.GotoRight);
                                break;
                            case PBPaymentPageType.PaymentExcute:
                                OpenAndClose(this.MoreViewPointGrid, this.PaymentExcuteGrid, OpenCloseAnimationType.GotoLeft);
                                break;
                            case PBPaymentPageType.CouponUsePoints:
                                OpenAndClose(this.MoreViewPointGrid, this.CouponUsePointsGrid, OpenCloseAnimationType.GotoLeft);
                                break;
                        }
                        break;
                    case PBPaymentPageType.PaymentExcute:
                        switch (ViewModel.DefaultPage)
                        {
                            case PBPaymentPageType.PointsDisCounts:
                                OpenAndClose(this.PaymentExcuteGrid, this.PointsDisCountsGrid, OpenCloseAnimationType.GotoRight);
                                break;
                            case PBPaymentPageType.CouponUsePoints:
                                OpenAndClose(this.PaymentExcuteGrid, this.CouponUsePointsGrid, OpenCloseAnimationType.GotoRight);
                                break;
                        }
                        break;

                }
            }
        }
        private void OpenBase()
        {
            this.PaymentsMainGrid.IsEnabled = false;
            var OpenAnimations = Animations.GetOpenAnimation(this.TopGrid, OpenCloseAnimationType.FullSizeUp, this.TopGrid.Width, 0.5);
            if (OpenAnimations != null)
            {
                OpenAnimations.Completed += OpenAnimations_Completed;
                OpenAnimations.Name = this.PaymentsMainGrid.Name;
                OpenAnimations.Begin();
            }

        }
        private void CloseBase()
        {
            var CloseAnimation = Animations.GetCloseAnimation(this.TopGrid, OpenCloseAnimationType.FullSizeDown, this.TopGrid.Width, 0.2);
            if (CloseAnimation != null)
            {
                CloseAnimation.Completed += CloseAnimation_Completed;
                CloseAnimation.Begin();
            }
        }
        private void OpenAndClose(Grid _closeGrid, Grid _openGrid, OpenCloseAnimationType _animationType)
        {
            var OpenAnimations = Animations.GetOpenAndCloseAnimation(_closeGrid, _openGrid, _animationType, _closeGrid.ActualWidth, 0.3);
            if (OpenAnimations != null)
            {
                OpenAnimations.Completed += OpenAnimations_Completed;
                OpenAnimations.Name = _openGrid.Name;
                OpenAnimations.Begin();
            }
        }
        #endregion Methods

        #region [ Event Handlers ]
        private void PBPayments_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.DataContext is PBPaymentsViewModel viewModel)
            {
                ViewModel = viewModel;
                ViewModel.PropertyChanged += ViewModel_PropertyChanged;
                ViewModel.HappyPointReturn = GetHappyPointReturn;
                ViewModel.DetailBasketData = GetBasketItems;
                ViewModel.DefaultPage = ShowPage;
                if (GetHappyPointReturn is M_HappyPointReturn)
                {
                    ViewModel.MemberLoginOK = true;
                    ViewModel.GmbMemberName = GetHappyPointReturn.IsCardNoUsed ? GetHappyPointReturn.MemberName : "고객";
                }
                ViewModel.IsTimeoutPopup = true;
            }
            OpenBase();
        }
        private void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (sender is PBPaymentsViewModel viewModel)
            {
                switch (e.PropertyName)
                {
                    case "CanWindowClose":
                        if (this.ViewModel.CanWindowClose)
                        {
                            CloseBase();
                        }
                        break;
                    case "DefaultPage":
                        SetPage();
                        break;
                    default:
                        break;
                }
            }
        }

        private void OpenAnimations_Completed(object sender, EventArgs e)
        {
            var senderStoryboard = (sender as ClockGroup).Timeline as Storyboard;
            var getName = senderStoryboard.Name;
            switch (senderStoryboard.Name)
            {
                case "PaymentsMainGrid":
                    break;   
                default:
                    this.ShowPage = ViewModel.DefaultPage;
                    break;
            }
            if (ShowPage.Equals(PBPaymentPageType.PointsDisCounts) 
             && this.ViewModel.FirstLoaded)
            {
                 this.ViewModel.OpenFistPopup();
            }
            if (this.FindName(getName) is Grid findGrid) findGrid.IsEnabled = true;
            senderStoryboard = null;

        }
        private void CloseAnimation_Completed(object sender, EventArgs e)
        {
            this.ViewModel.Dispose();
            this.Close();
        }
        #endregion Event Handlers




    }
}
