using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using SPC.Kiosk.Base;
using SPC.Kiosk.Common;
using SPC.Kiosk.Popup.Model;
using SPC.Kiosk.Popup.ViewModel;
namespace SPC.Kiosk.Popup
{
    /// <summary>
    /// Popup Manager
    /// </summary>
    public sealed class PopupManager
    {
        private static PopupManager instance = null;
        private static readonly object padlock = new object();
        /// <summary>
        /// This Class Instance
        /// </summary>
        public static PopupManager GetInstance
        {
            get
            {
                lock (padlock)
                {
                    if (instance == null)
                    {
                        instance = new PopupManager();
                    }
                    return instance;
                }
            }
        }
        public void OpenPaymentsEnd(PaymentEndWindowEndType _paymentEndWindow
                                    , List<M_BasketItem> _getBasketItems
                                    , bool _happyPointReserve
                                    , bool _okCashBagReserve
                                    , M_HappyPointReturn _happyPointReturn=null)
        {
            try
            {
                var PaymentsEndWin = new PaymentsEnd(_paymentEndWindow
                                                , _getBasketItems
                                                , _happyPointReserve
                                                , _okCashBagReserve
                                                , _happyPointReturn);
                PaymentsEndWin.ShowDialog();
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenPaymentsEnd", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
            }
        }
        /// <summary>
        /// 번호 입력창
        /// </summary>
        /// <param name="_languageType"></param>
        /// <param name="_guideText"></param>
        /// <param name="_formated"></param>
        /// <param name="_isPassword"></param>
        /// <param name="_maxLength"></param>
        /// <param name="_checkLength"></param>
        /// <returns></returns>
        public M_PopupReturn OpenNumPadPopup(SupportLanguageType _languageType
                         , List<M_Language> _guideText
                         , bool _formated = false
                         , bool _isPassword = true
                         , int _maxLength = 4
                         , int _checkLength = 4)
        {
            M_PopupReturn result = null;
            try
            {
                var NumPadPopupWin = new NumPadPopup(_languageType
                                                , _guideText
                                                , _formated
                                                , _isPassword
                                                , _maxLength
                                                , _checkLength);
                NumPadPopupWin.ShowDialog();
                if (NumPadPopupWin.DataContext is VmNumPadPopup viewModel)
                {
                    result = viewModel.ReturnValue;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenNumPadPopup", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;

            }
            return result;
        }
        public M_PopupReturn OpenStepInfoPopup()
        {
            M_PopupReturn result = null;
            try
            {
                    var StepInfoPopupWin = new StepInfoPopup();
                    StepInfoPopupWin.ShowDialog();
                    if (StepInfoPopupWin.DataContext is VmStepInfoPopup viewModel)
                    {
                        result = viewModel.ReturnValue;
                    }

            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenStepInfoPopup", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;

            }
            return result;
        }
        /// <summary>
        /// Open MiniPopup
        /// </summary>
        /// <param name="_miniPopup"></param>
        /// <returns></returns>
        public M_PopupReturn OpenMiniPopup(M_MiniPopup _miniPopup)
        {
            M_PopupReturn result = null;
            try
            {
                if (_miniPopup is M_MiniPopup)
                {
                    var MiniPopupWin = new MiniPopup(_miniPopup);
                    MiniPopupWin.ShowDialog();
                    if (MiniPopupWin.DataContext is VmMiniPopup viewModel)
                    {
                        result = viewModel.ReturnValue;
                    }
                    //MiniPopupWin.DataContext = null;
                    //MiniPopupWin.Content = null;
                    //MiniPopupWin = null;
                }
                
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenMiniPopup", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;

            }
            return result;
        }
        /// <summary>
        /// Option Select
        /// </summary>
        /// <param name="_endWindow"></param>
        /// <param name="_totalPayment"></param>
        /// <param name="_cashPayment"></param>
        /// <returns></returns>
        public M_PopupReturn OpenOptionSelect(List<M_Language> _getItemName
                          , List<M_ItemOptionGroup> _getOptions
                          , string _getItemImage
                          , int _getItemCount
                          , double _getItemPrice
                          , bool _editMode = false)
        {
            M_PopupReturn result = null;
            try
            {
                var OptionSelectWin = new OptionSelect(_getItemName
                                                     , _getOptions
                                                     , _getItemImage
                                                     , _getItemCount
                                                     , _getItemPrice
                                                     , _editMode);
                OptionSelectWin.ShowDialog();
                if (OptionSelectWin.DataContext is VmOptionSelect viewModel)
                {
                    result = viewModel.ReturnValue;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenOptionSelect", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;
            }
            return result;
        }
        /// <summary>
        /// Last Payment
        /// </summary>
        /// <returns></returns>
        public M_PopupReturn OpenPaymentProcess(LastPaymentsType _paymentType, double _totalPayment )
        {
            M_PopupReturn result = null;
            try
            {
                var PaymentProcessWin = new PaymentProcess(_paymentType, _totalPayment);
                PaymentProcessWin.ShowDialog();
                if (PaymentProcessWin.DataContext is VmPaymentProcess viewModel)
                {
                    result = viewModel.ReturnValue;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenPaymentProcess", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;
            }
            return result;
        }
        /// <summary>
        /// Language Select
        /// </summary>
        /// <returns></returns>
        public M_PopupReturn OpenLanguageSelector(SupportLanguageType _supportLanguageType)
        {
            M_PopupReturn result = null;
            try
            {
                var LanguageSelectorWin = new LanguageSelector(_supportLanguageType);
                LanguageSelectorWin.ShowDialog();
                if (LanguageSelectorWin.DataContext is VmLanguageSelector viewModel)
                {
                    result = viewModel.ReturnValue;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenLanguageSelector()", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;

            }
            return result;
        }
        /// <summary>
        /// Call Employee
        /// </summary>
        /// <returns></returns>
        public M_PopupReturn OpenCallEmployee()
        {
            M_PopupReturn result = null;
            try
            {
                var CallEmployeeWin = new CallEmployee();
                CallEmployeeWin.ShowDialog();
                if (CallEmployeeWin.DataContext is VmCallEmployee viewModel)
                {
                    result = viewModel.ReturnValue;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenCallEmployee", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;
            }
            return result;
        }
        /// <summary>
        /// HappyPoint Certify
        /// </summary>
        /// <returns></returns>
        public M_PopupReturn OpenHappyPointCertify(bool _showLanguageButton=true, bool _showMemberHistoryItems = true, ParentPage _popupParentPage = ParentPage.EmptyPage)
        {
            M_PopupReturn result = null;
            try
            {                
                var HappyPointCertifyWin = new HappyPointCertify(_showLanguageButton, _showMemberHistoryItems, _popupParentPage);
                HappyPointCertifyWin.ShowDialog();
                if (HappyPointCertifyWin.DataContext is VmHappyPointCertify viewModel)
                {
                    result = new M_PopupReturn
                    {
                        OKAnswer = viewModel.ReturnValue.OKAnswer,
                        TimeOut = viewModel.ReturnValue.TimeOut,
                        ReturnLanguage = viewModel.ReturnValue.ReturnLanguage,
                        PopupArgs = viewModel.ReturnValue.PopupArgs
                    };
                }
                
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenHappyPointCertify", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;
            }
            return result;
        }
        /// <summary>
        /// HappyPoint Using
        /// </summary>
        /// <param name="_happyPointReturn">이전 인증 결과</param>
        /// <param name="_totalPayment">결제 대상 금액</param>
        /// <returns></returns>
        public M_PopupReturn OpenHappyPointUsing(M_HappyPointReturn _happyPointReturn, double _totalPayment = 0)
        {
            M_PopupReturn result = null;
            try
            {
                var HappyPointUsingWin = new HappyPointUsing(_happyPointReturn, _totalPayment);
                HappyPointUsingWin.ShowDialog();
                if (HappyPointUsingWin.DataContext is VmHappyPointUsing viewModel)
                {
                    result = viewModel.ReturnValue;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenHappyPointUsing", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;
            }
            return result;
        }

        public M_PopupReturn OpenHappyAppCoupon(M_HappyPointReturn _happyPointReturn, double _totalPayment = 0)
        {
            M_PopupReturn result = null;
            try
            {
                var HappyAppCouponWin = new HappyPointCoupon(_happyPointReturn, _totalPayment);
                HappyAppCouponWin.ShowDialog();
                if (HappyAppCouponWin.DataContext is VmHappyPointCoupon viewModel)
                {
                    result = viewModel.ReturnValue;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenHappyAppCoupon", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;
            }
            return result;
        }

        public M_PopupReturn OpenMobileHappyCoupon(List<M_BasketItem> _getBasketItems , double _totalPayment = 0)
        {
            M_PopupReturn result = null;
            try
            {
                var MobileHappyCouponWin = new MobileHappyCoupon(_getBasketItems, _totalPayment);
                MobileHappyCouponWin.ShowDialog();
                if (MobileHappyCouponWin.DataContext is VmMobileHappyCoupon viewModel)
                {
                    result = viewModel.ReturnValue;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenMobileHappyCoupon()", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;
            }
            return result;
        }
        /// <summary>
        /// OK CashBag Certify
        /// </summary>
        /// <param name="_totalPayment">결제 대상 금액</param>
        /// <param name="_reserveEnabled">적립 가능 유무</param>
        /// <returns></returns>
        public M_PopupReturn OpenOkCashBagCertify()
        {
            M_PopupReturn result = null;
            try
            {
                var OkCashBagCertifyWin = new OkCashBagCertify();
                OkCashBagCertifyWin.ShowDialog();
                if (OkCashBagCertifyWin.DataContext is VmOkCashBagCertify viewModel)
                {
                    result = viewModel.ReturnValue;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenOkCashBagCertify", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;
            }
            return result;
        }

        /// <summary>
        /// OK CashBag Using
        /// </summary>
        /// <param name="_totalPayment">결제 대상 금액</param>
        /// <param name="_reserveEnabled">적립 가능 유무</param>
        /// <returns></returns>
        public M_PopupReturn OpenOkCashBagUsing(double _totalPayment=0, bool _reserveEnabled = true)
        {
            M_PopupReturn result = null;
            try
            {
                var OkCashBagUsingWin = new OkCashBagUsing(_totalPayment, _reserveEnabled);
                OkCashBagUsingWin.ShowDialog();
                if (OkCashBagUsingWin.DataContext is VmOkCashBagUsing viewModel)
                {
                    result = viewModel.ReturnValue;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenOkCashBagUsing", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;
            }
            return result;
        }
        /// <summary>
        /// MobileCompany Certify && Discount
        /// </summary>
        /// <param name="_mobileCompany">대상 통신사</param>
        /// <param name="_totalPayment">경제 대상 금액</param>
        /// <returns></returns>
        public M_PopupReturn OpenMobileCompanyCertify(MobileCompanyType _mobileCompany, double _totalPayment = 0)
        {
            M_PopupReturn result = null;
            try
            {
                var MobileCompanyMemberCertifyWin = new MobileCompanyMemberCertify(_mobileCompany, _totalPayment);
                MobileCompanyMemberCertifyWin.ShowDialog();
                if (MobileCompanyMemberCertifyWin.DataContext is VmMobileCompanyMemberCertify viewModel)
                {
                    result = viewModel.ReturnValue;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "OpenMobileCompanyCertify", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
                result = null;
            }
            return result;
        }

    }
}
