using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using SPC.Kiosk.Base;
using SPC.Kiosk.Common;
using SPC.Kiosk.Popup.Model;
using SPC.Kiosk.Payments;
using SPC.Kiosk.DataHelper;

namespace SPC.Kiosk.Popup.ViewModel
{
    /// <summary>
    /// PaymentsEnd.xaml에 대한 ViewModel
    /// </summary>
    public class VmPaymentsEnd : PopupViewModelBase
    {
        #region [ Members ]
        private PosDataService posDataService = null;
        public WaitWindow WaitWindowFrom { get; set; }
        private List<M_BasketItem> basketData;
        public List<M_BasketItem> BasketData
        {
            get { return basketData; }
            set { basketData = value; PropertyChange("BasketData"); }
        }
        private string titileImage;
        /// <summary>
        /// Cash Image
        /// </summary>
        public string TitileImage
        {
            get { return titileImage; }
            set { titileImage = value; PropertyChange("TitileImage"); }
        }
        private List<M_Language> headerText;
        /// <summary>
        /// Top Header Text (first Grid)
        /// </summary>
        public List<M_Language> HeaderText
        {
            get { return headerText; }
            set { headerText = value; PropertyChange("HeaderText"); }
        }
        private List<M_Language> scanGuidText;
        /// <summary>
        /// Second Header Text (first Grid)
        /// </summary>
        public List<M_Language> ScanGuidText
        {
            get { return scanGuidText; }
            set { scanGuidText = value; PropertyChange("ScanGuidText"); }
        }
        private List<M_Language> infoGuid1Text;
        /// <summary>
        /// Bootom first infomation text (first Grid)
        /// </summary>
        public List<M_Language> InfoGuid1Text
        {
            get { return infoGuid1Text; }
            set { infoGuid1Text = value; PropertyChange("InfoGuid1Text"); }
        }
        private List<M_Language> infoGuid2Text;
        /// <summary>
        /// Bottom second infomation text (first Grid)
        /// </summary>
        public List<M_Language> InfoGuid2Text
        {
            get { return infoGuid2Text; }
            set { infoGuid2Text = value; PropertyChange("InfoGuid2Text"); }
        }

        private List<M_Language> totalPayHeaderText;
        /// <summary>
        /// Payments Total Header Text
        /// </summary>
        public List<M_Language> TotalPayHeaderText
        {
            get { return totalPayHeaderText; }
            set { totalPayHeaderText = value; PropertyChange("TotalPayHeaderText"); }
        }
        private List<M_Language> queryPointsHeaderText;
        /// <summary>
        /// Member Point Header Text
        /// </summary>
        public List<M_Language> QueryPointsHeaderText
        {
            get { return queryPointsHeaderText; }
            set { queryPointsHeaderText = value; PropertyChange("QueryPointsHeaderText"); }
        }
        private double cashPayments;
        /// <summary>
        /// Payments Total
        /// </summary>
        public double CashPayments
        {
            get { return cashPayments; }
            set { cashPayments = value; PropertyChange("CashPayments"); }
        }
        private double cashTax;
        /// <summary>
        /// Payments Total
        /// </summary>
        public double CashTax
        {
            get { return cashTax; }
            set { cashTax = value; PropertyChange("CashTax"); }
        }


        private List<M_Language> cancelButtonText;
        /// <summary>
        /// Cancel Button Text
        /// </summary>
        public List<M_Language> CancelButtonText
        {
            get { return cancelButtonText; }
            set { cancelButtonText = value; PropertyChange("CancelButtonText"); }
        }
        private double cancelButtonWidth = 650d;
        /// <summary>
        /// Cancel Button Width
        /// </summary>
        public double CancelButtonWidth
        {
            get { return cancelButtonWidth; }
            set { cancelButtonWidth = value; PropertyChange("CancelButtonWidth"); }
        }
        private double okButtonWidth = 650d;
        /// <summary>
        /// OK Button Width
        /// </summary>
        public double OKButtonWidth
        {
            get { return okButtonWidth; }
            set { okButtonWidth = value; PropertyChange("OKButtonWidth"); }
        }
        private List<M_Language> okButtonText;
        /// <summary>
        /// OK Button Text
        /// </summary>
        public List<M_Language> OkButtonText
        {
            get { return okButtonText; }
            set { okButtonText = value; PropertyChange("OkButtonText"); }
        }
        private bool oKButtonEnabled = false;
        /// <summary>
        /// OK Button Enabled
        /// </summary>
        public bool OKButtonEnabled
        {
            get { return oKButtonEnabled; }
            set { oKButtonEnabled = value; PropertyChange("OKButtonEnabled"); }
        }
        private bool talkGetButtonEnabled = false;
        /// <summary>
        /// OK Button Enabled
        /// </summary>
        public bool TalkGetButtonEnabled
        {
            get { return talkGetButtonEnabled; }
            set { talkGetButtonEnabled = value; PropertyChange("TalkGetButtonEnabled"); }
        }
        private bool storeAgreeView;
        public bool StoreAgreeView
        {
            get { return storeAgreeView; }
            set { storeAgreeView = value; PropertyChange("StoreAgreeView"); }
        }

        private M_StoreAgree storeAgreeData;
        /// <summary>
        /// Sotre Agree Data
        /// </summary>
        public M_StoreAgree StoreAgreeData
        {
            get { return storeAgreeData; }
            set { storeAgreeData = value; PropertyChange("StoreAgreeData"); }
        }

        #region CashReceipt

        private bool cashReceiptVisible = false;
        /// <summary>
        /// 인증 시작 화면 Visible
        /// </summary>
        public bool CashReceiptVisible
        {
            get { return cashReceiptVisible; }
            set { cashReceiptVisible = value; PropertyChange("CashReceiptVisible"); }
        }
        private string cashReceipTitileImage;
        /// <summary>
        /// CashReceip Titile Image
        /// </summary>
        public string CashReceipTitileImage
        {
            get { return cashReceipTitileImage; }
            set { cashReceipTitileImage = value; PropertyChange("CashReceipTitileImage"); }
        }
        private string cashReceipButtonBackground;
        /// <summary>
        /// CashReceip Button Background
        /// </summary>
        public string CashReceipButtonBackground
        {
            get { return cashReceipButtonBackground; }
            set { cashReceipButtonBackground = value; PropertyChange("CashReceipButtonBackground"); }
        }
        private string cashTypePersonButtonNormal;
        /// <summary>
        /// CashType Person Button Normal
        /// </summary>
        public string CashTypePersonButtonNormal
        {
            get { return cashTypePersonButtonNormal; }
            set { cashTypePersonButtonNormal = value; PropertyChange("CashTypePersonButtonNormal"); }
        }
        private string cashTypePersonButtonDisable;
        /// <summary>
        /// CashType Person Button Disable
        /// </summary>
        public string CashTypePersonButtonDisable
        {
            get { return cashTypePersonButtonDisable; }
            set { cashTypePersonButtonDisable = value; PropertyChange("CashTypePersonButtonDisable"); }
        }
        private bool cashTypePersonEnable = false;
        /// <summary>
        /// CashType Serson Enabled
        /// </summary>
        public bool CashTypePersonEnable
        {
            get { return cashTypePersonEnable; }
            set { cashTypePersonEnable = value; PropertyChange("CashTypePersonEnable"); }
        }
        private List<M_Language> cashTypePersonText;
        /// <summary>
        /// CashType Serson  Button text
        /// </summary>
        public List<M_Language> CashTypePersonText
        {
            get { return cashTypePersonText; }
            set { cashTypePersonText = value; PropertyChange("CashTypePersonText"); }
        }
        private string cashTypeCompanyButtonNormal;
        /// <summary>
        /// CashType Company Button Normal
        /// </summary>
        public string CashTypeCompanyButtonNormal
        {
            get { return cashTypeCompanyButtonNormal; }
            set { cashTypeCompanyButtonNormal = value; PropertyChange("CashTypeCompanyButtonNormal"); }
        }
        private string cashTypeCompanyButtonDisable;
        /// <summary>
        /// CashType Company Button Disable
        /// </summary>
        public string CashTypeCompanyButtonDisable
        {
            get { return cashTypeCompanyButtonDisable; }
            set { cashTypeCompanyButtonDisable = value; PropertyChange("CashTypeCompanyButtonDisable"); }
        }
        public bool cashTypeCompanyEnable = true;
        /// <summary>
        /// CashType Serson Enabled
        /// </summary>
        public bool CashTypeCompanyEnable
        {
            get { return cashTypeCompanyEnable; }
            set { cashTypeCompanyEnable = value; PropertyChange("CashTypeCompanyEnable"); }
        }
        private List<M_Language> cashTypeCompanyText;
        /// <summary>
        /// All Point Using  Button text
        /// </summary>
        public List<M_Language> CashTypeCompanyText
        {
            get { return cashTypeCompanyText; }
            set { cashTypeCompanyText = value; PropertyChange("CashTypeCompanyText"); }
        }
        private string inputNumber = string.Empty;
        /// <summary>
        /// Input Numner Point Text
        /// </summary>
        public string InputNumber
        {
            get { return inputNumber; }
            set { inputNumber = value; PropertyChange("InputNumber"); }
        }
        private List<M_Language> numPadGuidText;
        /// <summary>
        /// Number Pad Input Guide Text
        /// </summary>
        public List<M_Language> NumPadGuidText
        {
            get { return numPadGuidText; }
            set { numPadGuidText = value; PropertyChange("NumPadGuidText"); }
        }

        private int numPadMaxLength;
        /// <summary>
        /// Point Pad Input Max Length
        /// </summary>
        public int NumPadMaxLength
        {
            get { return numPadMaxLength; }
            set { numPadMaxLength = value; PropertyChange("NumPadMaxLength"); }
        }
        private bool numPadFormated = false;
        /// <summary>
        /// NumPad Formated
        /// </summary>
        public bool NumPadFormated
        {
            get { return numPadFormated; }
            set { numPadFormated = value; PropertyChange("NumPadFormated"); }
        }
        private bool numPadInputVisible = false;
        /// <summary>
        /// Number Pad Points Visible
        /// </summary>
        public bool NumPadInputVisible
        {
            get { return numPadInputVisible; }
            set { numPadInputVisible = value; PropertyChange("NumPadInputVisible"); }
        }
        private bool numPadGuideVisible = true;
        /// <summary>
        /// Nummber Pad Guid Visible
        /// </summary>
        public bool NumPadGuideVisible
        {
            get { return numPadGuideVisible; }
            set { numPadGuideVisible = value; PropertyChange("NumPadGuideVisible"); }
        }

        #endregion

        #region Processing
        private bool processingVisible = false;
        /// <summary>
        /// 처리 진행 화면 Visible
        /// </summary>
        public bool ProcessingVisible
        {
            get { return processingVisible; }
            set { processingVisible = value; PropertyChange("ProcessingVisible"); }
        }

        private string processingGIF;
        /// <summary>
        /// Processing GIF
        /// </summary>
        public string ProcessingGIF
        {
            get { return processingGIF; }
            set { processingGIF = value; PropertyChange("ProcessingGIF"); }
        }
        private List<M_Language> processingMessageText;
        /// <summary>
        /// Processing Message Text
        /// </summary>
        public List<M_Language> ProcessingMessageText
        {
            get { return processingMessageText; }
            set { processingMessageText = value; PropertyChange("ProcessingMessageText"); }
        }
        private List<M_Language> processingGuide;
        /// <summary>
        /// Processing Guide Text
        /// </summary>
        public List<M_Language> ProcessingGuide
        {
            get { return processingGuide; }
            set { processingGuide = value; PropertyChange("ProcessingGuide"); }
        }
        #endregion

        #region BellOrNotifyTalk
        private bool bellOrNotifyTalkVisible = false;
        /// <summary>
        /// 처리 진행 화면 Visible
        /// </summary>
        public bool BellOrNotifyTalkVisible
        {
            get { return bellOrNotifyTalkVisible; }
            set { bellOrNotifyTalkVisible = value; PropertyChange("BellOrNotifyTalkVisible"); }
        }
        private string bellOrNotifyTalkTitileImage;
        /// <summary>
        /// BellOrNotifyTalk Titile Image
        /// </summary>
        public string BellOrNotifyTalkTitileImage
        {
            get { return bellOrNotifyTalkTitileImage; }
            set { bellOrNotifyTalkTitileImage = value; PropertyChange("BellOrNotifyTalkTitileImage"); }
        }
        private List<M_Language> bellOrNotifyTalkText;
        /// <summary>
        /// Bell Button Text
        /// </summary>
        public List<M_Language> BellOrNotifyTalkText
        {
            get { return bellOrNotifyTalkText; }
            set { bellOrNotifyTalkText = value; PropertyChange("BellOrNotifyTalkText"); }
        }
        private bool bellButtonEnable = false;
        /// <summary>
        /// Bell Button Enable
        /// </summary>
        public bool BellButtonEnable
        {
            get { return bellButtonEnable; }
            set { bellButtonEnable = value; PropertyChange("BellButtonEnable"); }
        }
        private List<M_Language> bellButtonText;
        /// <summary>
        /// Bell Button Text
        /// </summary>
        public List<M_Language> BellButtonText
        {
            get { return bellButtonText; }
            set { bellButtonText = value; PropertyChange("BellButtonText"); }
        }
        private bool talkButtonEnable = true;
        /// <summary>
        /// Talk Button Enable
        /// </summary>
        public bool TalkButtonEnable
        {
            get { return talkButtonEnable; }
            set { talkButtonEnable = value; PropertyChange("TalkButtonEnable"); }
        }
        private List<M_Language> talkButtonText;
        /// <summary>
        /// Talk Button Text
        /// </summary>
        public List<M_Language> TalkButtonText
        {
            get { return talkButtonText; }
            set { talkButtonText = value; PropertyChange("TalkButtonText"); }
        }
        private string talkNumPadNumber = string.Empty;
        /// <summary>
        /// TalkNumPad Number
        /// </summary>
        public string TalkNumPadNumber
        {
            get { return talkNumPadNumber; }
            set { talkNumPadNumber = value; PropertyChange("TalkNumPadNumber"); }
        }
        private List<M_Language> talkNumPadGuidText;
        /// <summary>
        /// Number Pad Input Guide Text
        /// </summary>
        public List<M_Language> TalkNumPadGuidText
        {
            get { return talkNumPadGuidText; }
            set { talkNumPadGuidText = value; PropertyChange("TalkNumPadGuidText"); }
        }

        private int talkNumPadMaxLength;
        /// <summary>
        /// Point Pad Input Max Length
        /// </summary>
        public int TalkNumPadMaxLength
        {
            get { return talkNumPadMaxLength; }
            set { talkNumPadMaxLength = value; PropertyChange("TalkNumPadMaxLength"); }
        }
        private bool talkNumPadFormated = true;
        /// <summary>
        /// TalkNumPad  Guide Visible
        /// </summary>
        public bool TalkNumPadFormated
        {
            get { return talkNumPadFormated; }
            set { talkNumPadFormated = value; PropertyChange("TalkNumPadFormated"); }
        }
        private bool talkNumPadInputVisible = false;
        /// <summary>
        /// TalkNumPad  InputNumber Visible
        /// </summary>
        public bool TalkNumPadInputVisible
        {
            get { return talkNumPadInputVisible; }
            set { talkNumPadInputVisible = value; PropertyChange("TalkNumPadInputVisible"); }
        }
        private bool talkNumPadGuideVisible = true;
        /// <summary>
        /// TalkNumPad  Guide Visible
        /// </summary>
        public bool TalkNumPadGuideVisible
        {
            get { return talkNumPadGuideVisible; }
            set { talkNumPadGuideVisible = value; PropertyChange("TalkNumPadGuideVisible"); }
        }

        #endregion

        #region PaymentSuccess
        private bool paymentSuccessVisible = false;

        /// <summary>
        /// 처리 완료 화면 Visible
        /// </summary>
        public bool PaymentSuccessVisible
        {
            get { return paymentSuccessVisible; }
            set { paymentSuccessVisible = value; PropertyChange("PaymentSuccessVisible"); }
        }
        private string successImage;
        /// <summary>
        /// Sucess Icon
        /// </summary>
        public string SuccessImage
        {
            get { return successImage; }
            set { successImage = value; PropertyChange("SuccessImage"); }
        }
        private List<M_Language> successText;
        /// <summary>
        /// Sucess Guide Text
        /// </summary>
        public List<M_Language> SuccessText
        {
            get { return successText; }
            set { successText = value; PropertyChange("SuccessText"); }
        }

        #endregion

        #region TalkSuccess
        private bool talkSuccessVisible = false;

        /// <summary>
        /// 처리 완료 화면 Visible
        /// </summary>
        public bool TalkSuccessVisible
        {
            get { return talkSuccessVisible; }
            set { talkSuccessVisible = value; PropertyChange("TalkSuccessVisible"); }
        }
        private string talkSuccessImage;
        /// <summary>
        /// Sucess Icon
        /// </summary>
        public string TalkSuccessImage
        {
            get { return talkSuccessImage; }
            set { talkSuccessImage = value; PropertyChange("TalkSuccessImage"); }
        }
        private List<M_Language> talkSuccessText;
        /// <summary>
        /// Sucess Guide Text
        /// </summary>
        public List<M_Language> TalkSuccessText
        {
            get { return talkSuccessText; }
            set { talkSuccessText = value; PropertyChange("TalkSuccessText"); }
        }
        private List<M_Language> talkSuccessInfoText;
        /// <summary>
        /// Sucess Guide Text
        /// </summary>
        public List<M_Language> TalkSuccessInfoText
        {
            get { return talkSuccessInfoText; }
            set { talkSuccessInfoText = value; PropertyChange("TalkSuccessInfoText"); }
        }
        #endregion

        #region WaitNumberSuccess
        private bool waitNumberSuccessVisible = false;

        /// <summary>
        /// 처리 완료 화면 Visible
        /// </summary>
        public bool WaitNumberSuccessVisible
        {
            get { return waitNumberSuccessVisible; }
            set { waitNumberSuccessVisible = value; PropertyChange("WaitNumberSuccessVisible"); }
        }
        private List<M_Language> waitNumberHeaderText;
        /// <summary>
        /// Sucess Guide Text
        /// </summary>
        public List<M_Language> WaitNumberHeaderText
        {
            get { return waitNumberHeaderText; }
            set { waitNumberHeaderText = value; PropertyChange("WaitNumberHeaderText"); }
        }
        private string waitNumberText;
        /// <summary>
        /// Sucess Guide Text
        /// </summary>
        public string WaitNumberText
        {
            get { return waitNumberText; }
            set { waitNumberText = value; PropertyChange("WaitNumberText"); }
        }
        private List<M_Language> successInfoText;
        /// <summary>
        /// Sucess Guide Text
        /// </summary>
        public List<M_Language> SuccessInfoText
        {
            get { return successInfoText; }
            set { successInfoText = value; PropertyChange("SuccessInfoText"); }
        }

        /// <summary>
        /// Point Saving Button OK
        /// </summary>
        private bool pointSavingButtonOk = false;

        public bool PointSavingButtonOk
        {
            get { return pointSavingButtonOk; }
            set { pointSavingButtonOk = value; PropertyChange("PointSavingButtonOk"); }
        }

        /// <summary>
        /// Point Saving Button Background
        /// </summary>
        private string pointSavingButtonBackground;

        public string PointSavingButtonBackground
        {
            get { return pointSavingButtonBackground; }
            set { pointSavingButtonBackground = value; PropertyChange("PointSavingButtonBackground"); }
        }

        /// <summary>
        /// Point Image
        /// </summary>
        private string waitNumberSuccessPointImage;

        public string WaitNumberSuccessPointImage
        {
            get { return waitNumberSuccessPointImage; }
            set { waitNumberSuccessPointImage = value; PropertyChange("WaitNumberSuccessPointImage"); }
        }

        #endregion

        private PaymentEndWindowEndType endWindow = PaymentEndWindowEndType.None;
        public PaymentEndWindowEndType EndWindow
        {
            get { return endWindow; }
            set { endWindow = value; PropertyChange("EndWindow"); }
        }
        private PaymentEndWindowType paymentEndWindow = PaymentEndWindowType.None;
        /// <summary>
        /// Current  View Window
        /// </summary>
        public PaymentEndWindowType PaymentEndWindow
        {
            get { return paymentEndWindow; }
            set { paymentEndWindow = value; PropertyChange("PaymentEndWindow"); }
        }
        private bool bellAnsTalkToggleVisible = true;
        public bool BellAnsTalkToggleVisible
        {
            get { return bellAnsTalkToggleVisible; }
            set { bellAnsTalkToggleVisible = value; PropertyChange("BellAnsTalkToggleVisible"); }
        }


        private double popupWidth = 1300;
        /// <summary>
        /// Popup Whidth (For Calculate OK Button Width)
        /// </summary>
        public double PopupWidth
        {
            get { return popupWidth; }
            set { popupWidth = value; PropertyChange("PopupWidth"); }
        }
        private M_PaymentEndReturn paymentEndReturn;
        /// <summary>
        /// HappyPoint 인증 정보
        /// </summary>
        public M_PaymentEndReturn PaymentEndReturn
        {
            get { return paymentEndReturn; }
            set { paymentEndReturn = value; PropertyChange("PaymentEndReturn"); }
        }
        /// <summary>
        /// OK Click Command
        /// </summary>
        public ICommand OkCommand { get; protected set; }
        /// <summary>
        /// Cancel Click Command
        /// </summary>
        public ICommand CancelCommand { get; protected set; }
        /// <summary>
        /// Cash Type Person Command
        /// </summary>
        public ICommand CashTypePersonCommand { get; protected set; }
        /// <summary>
        /// Cash Type Company Command
        /// </summary>
        public ICommand CashTypeCompanyCommand { get; protected set; }
        /// <summary>
        /// Bell Button Command
        /// </summary>
        public ICommand BellButtonCommand { get; protected set; }
        /// <summary>
        /// Talk Button Command
        /// </summary>
        public ICommand TalkButtonCommand { get; protected set; }
        /// <summary>
        /// Store Agree Command
        /// </summary>
        public ICommand StoreAgreeCommand { get; protected set; }
        /// <summary>
        /// Point Saving Button Command
        /// </summary>
        public ICommand PointSavingButtonCommand { get; protected set; }
        /// <summary>
        /// Talk Get Button Command
        /// </summary>
        public ICommand TalkGetButtonCommand { get; protected set; }
        /// <summary>
        /// All Points Using Click Command
        /// </summary>
        public bool ImmediatelyClose { get; protected set; } = false;
        /// <summary>
        /// Happy Point Reserve
        /// </summary>
        public bool HappyPointReserve { get; set; }
        /// <summary>
        /// OkCashBag Reserve
        /// </summary>
        public bool OkCashBagReserve { get; set; }
        private M_HappyPointReturn happyPointReturn;
        /// <summary>
        /// Happy Point Return
        /// </summary>
        public M_HappyPointReturn HappyPointReturn
        {
            get { return happyPointReturn; }
            set { happyPointReturn = value; PropertyChange("HappyPointReturn"); }
        }

        private bool ProcessStart = false;
        private posLastPayments LastPayments = null;
        private Task ProcessTask  = null;
        private string certifyKey = string.Empty;
        private string divType = "0"; // "0"=개인(소득공제) ,"1"=사업자(증빙제출)

        #endregion  Members


        #region [ Ctor ]
        /// <summary>
        /// Ctor
        /// </summary>
        public VmPaymentsEnd()
        {
            posDataService = new PosDataService();
            WaitWindowFrom = WaitWindow.GetInstance;
            WaitWindowFrom.CloseWaitWindow();
            OkCommand = new Command(OkCommandHandler);
            CancelCommand = new Command(CancelCommandHandler);
            CashTypePersonCommand = new Command(CashTypePersonCommandHandler);
            CashTypeCompanyCommand = new Command(CashTypeCompanyCommandHandler);
            BellButtonCommand = new Command(BellButtonCommandHandler);
            TalkButtonCommand = new Command(TalkButtonCommandHandler);
            StoreAgreeCommand = new Command(StoreAgreeCommandHandler);
            PointSavingButtonCommand = new Command(PointSavingCommandHandler);
            TalkGetButtonCommand = new Command(TalkGetCommandHandler);
            this.PropertyChanged += VmPaymentsEnd_PropertyChanged;
            //TitileImage = ResourceManager.GetNximagePathAdd("img_gif_barcode_happy.gif", CommonValue.PBdesignImagesPath);
            CashReceipTitileImage = ResourceManager.GetNximagePathAdd("img_cashreceipt.png", CommonValue.PBdesignImagesPath);
            CashReceipButtonBackground = ResourceManager.GetNximagePathAdd("img_check_bg.png", CommonValue.PBdesignImagesPath);
            CashTypePersonButtonNormal = ResourceManager.GetNximagePathAdd("btn_check_n.png", CommonValue.PBdesignImagesPath);
            CashTypePersonButtonDisable = ResourceManager.GetNximagePathAdd("btn_check_s.png", CommonValue.PBdesignImagesPath);
            CashTypeCompanyButtonNormal = ResourceManager.GetNximagePathAdd("btn_check_n.png", CommonValue.PBdesignImagesPath);
            CashTypeCompanyButtonDisable = ResourceManager.GetNximagePathAdd("btn_check_s.png", CommonValue.PBdesignImagesPath);
            ProcessingGIF = ResourceManager.GetNximagePathAdd("ic_alert_ing2.gif", CommonValue.PBdesignImagesPath);
            SuccessImage = ResourceManager.GetNximagePathAdd("img_receipt.png", CommonValue.PBdesignImagesPath);
            TalkSuccessImage = ResourceManager.GetNximagePathAdd("ic_talk.png", CommonValue.PBdesignImagesPath);
            
            OkButtonText = Languages.GetMessages("BTN0033");
            CancelButtonText = Languages.GetMessages("BTN0024");
            CashTypePersonText = Languages.GetMessages("BTN0002");
            CashTypeCompanyText = Languages.GetMessages("BTN0017");
            SuccessText = Languages.GetMessages("LBL0030");
            SuccessInfoText = Languages.GetMessages("LBL0076");
            TalkSuccessText = Languages.GetMessages("LBL0075");
            TalkSuccessInfoText = Languages.GetMessages("LBL0076");
            ProcessingMessageText = Languages.GetMessages("LBL0087");
            ProcessingGuide = Languages.GetMessages("LBL0028");
            NumPadFormated = true;
            NumPadMaxLength = 11;
            NumPadGuidText = Languages.GetMessages("LBL0091");

            BellOrNotifyTalkTitileImage = ResourceManager.GetNximagePathAdd("img_bell.png", CommonValue.PBdesignImagesPath);
            BellOrNotifyTalkText = Languages.GetMessages("LBL0078");
            TalkNumPadFormated = false;
            TalkNumPadMaxLength = 3;
            TalkNumPadGuidText = Languages.GetMessages("LBL0104");
            TotalPayHeaderText = Languages.GetMessages("PriceForPayment");
            LastPayments = new posLastPayments();

            WaitNumberSuccessPointImage = ResourceManager.GetNximagePathAdd("ic_point.png", CommonValue.PBdesignImagesPath);
            PointSavingButtonBackground = ResourceManager.GetNximagePathAdd("btn_store.png", CommonValue.PBdesignImagesPath);            
        }

        /// <summary>
        /// Dispose
        /// </summary>
        public new void Dispose()
        {
            TitileImage = string.Empty; 
            ProcessingGIF = string.Empty;
            this.PropertyChanged -= VmPaymentsEnd_PropertyChanged;
            if (posDataService != null)
            {
                posDataService.LastBarcodeDelete();
                posDataService.Dispose();
                posDataService = null;
            }
            if (ProcessTask != null)
            {
                ProcessTask.Dispose();
                ProcessTask = null;
            }
            if (LastPayments != null)
            {
                CashReceiptRemoveEvents();
                LastPayments.Dispose();
                LastPayments = null;
            }
            base.Dispose();
        }
        #endregion Ctor


        #region [ Methods ]
        private void SetStoreAgreeData()
        {
            var isFavoriteStore = HappyPointReturn is M_HappyPointReturn
                                ? HappyPointReturn.IsFavoriteStore
                                : false;
            StoreAgreeData = new M_StoreAgree
            {
                IsFavoriteStore = isFavoriteStore,
                DisplayLanguage = ShowLanguageType,
                Header = isFavoriteStore
                       ? new List<M_Language>
                         {
                             new M_Language { Type=SupportLanguageType.ko, LanguageData="단골매장"}
                         }
                       : Languages.GetMessages("LBL0039"),
                BackgroundImage = ResourceManager.GetNximagePathAdd("store_bg.png", CommonValue.PBdesignImagesPath),
                StoreImage = ResourceManager.GetNximagePathAdd("ic_store.png", CommonValue.PBdesignImagesPath),
                StoreName = posDataService != null ? posDataService.StoreName() : string.Empty,
                FirstInfoText = Languages.GetMessages("LBL0040"),
                SecondInfoText = Languages.GetMessages("LBL0019"),
                WelcomeInfoText = new List<M_Language>
                {
                    new M_Language { Type=SupportLanguageType.ko, LanguageData="고객님 저희 매장을 방문해 주셔서 감사합니다."}
                },
                ButtonNormalBrush = ResourceManager.GetNximagePathAdd("btn_store_agree_n2.png", CommonValue.PBdesignImagesPath),
                ButtonSwitchOnBrush = "Transparent",
                ButtonText = Languages.GetMessages("BTN0010"),
            };
        }
        private void TestDelay()
        {
            Thread.Sleep(1000);
        }


        private void CashReceiptCreateEvents()
        {
            if (LastPayments != null)
            {
                LastPayments.ReadStart += LastPayments_ReadStart;
                LastPayments.ReadEnd += LastPayments_ReadEnd;
            }
        }


        private void CashReceiptRemoveEvents()
        {
            if (LastPayments != null)
            {
                LastPayments.ReadStart -= LastPayments_ReadStart;
                LastPayments.ReadEnd -= LastPayments_ReadEnd;
            }
        }


        private void DoCashReceiptProcessing()
        {

            ProcessStart = true;
            TimerEnabled = false;
            CashReceiptCreateEvents();
            var processTask = new Task(() => LastPayments.StartCashBill(InputNumber,divType,CashPayments,CashTax));
            processTask.Start();
            processTask.Wait();
            DoBeforeReceiptPrint();
            CashReceiptRemoveEvents();
            processTask = null;
            ProcessStart = false;
            TimerEnabled = true;
        }
        private void DoNewMemberRequest()
        {
            ProcessStart = true;
            TimerEnabled = false;
            WaitWindowFrom.ShowWaitWindow();

            var korItemName = BasketData.FirstOrDefault().ItemName.GetLanguageData(SupportLanguageType.ko);
            var itemCount = BasketData.Sum(r => r.Count).ToString();
            //TODO:비회원 입력 전화 번호 처리  (InputNumber)
            using (var commonDataHelper = new DataHelper.CommonDataHelper())
            {
                commonDataHelper.Send_NoticeTalk(InputNumber, korItemName, itemCount);
            }

            // test
            //DoReceiptPrint();

            //TestDelay();
            WaitWindowFrom.CloseWaitWindow();
            ProcessStart = false;
            CanWindowClose = true;

        }
        private void DoRegFavoriteStore()//DoRegFavoriteStore
        {
            ProcessStart = true;
            TimerEnabled = false;
            WaitWindowFrom.ShowWaitWindow();
            using (var PosHappyPoint = new posHappyPoint())
            {
                PosHappyPoint.StartFavoriteStoreProcess(happyPointReturn.MemberCode, true);
            }
            WaitWindowFrom.CloseWaitWindow();
            ProcessStart = false;
            CanWindowClose = true;
        }

        private void DoBeforeReceiptPrint()
        {
            switch (EndWindow)
            {
                case PaymentEndWindowEndType.Talk:
                    TalkButtonEnable = false;
                    PaymentEndWindow = PaymentEndWindowType.BellOrNotifyTalk;
                    break;
                case PaymentEndWindowEndType.Bell:
                    BellButtonEnable = false;
                    PaymentEndWindow = PaymentEndWindowType.BellOrNotifyTalk;
                    break;
                default:
                    DoReceiptPrint();
                    break;
            }
        }


        private void DoAfterReceiptPrint()
        {
            switch (EndWindow)
            {
                case PaymentEndWindowEndType.None:
                    PaymentEndWindow = PaymentEndWindowType.PaymentSuccess;
                    break;
                case PaymentEndWindowEndType.Bell:
                    PaymentEndWindow = PaymentEndWindowType.WaitNumberSuccess;
                    break;
                case PaymentEndWindowEndType.Talk:
                    if (string.IsNullOrEmpty(TalkNumPadNumber))
                    {
                        PaymentEndWindow = PaymentEndWindowType.WaitNumberSuccess;
                    }
                    else
                    {
                        PaymentEndWindow = PaymentEndWindowType.TalkSuccess;
                    }
                    break;
                case PaymentEndWindowEndType.WaitNumber:
                    PaymentEndWindow = PaymentEndWindowType.WaitNumberSuccess;
                    break;
                default:
                    PaymentEndWindow = PaymentEndWindowType.PaymentSuccess;
                    break;
            }
        }


        private void DoReceiptPrint()
        {
            var waitNumber = string.Empty;
            var bellNo = EndWindow.Equals(PaymentEndWindowEndType.Bell)
                       ? TalkNumPadNumber
                       : string.Empty;
            var isTalk = EndWindow.Equals(PaymentEndWindowEndType.Talk);
            using (var ps = new PosDataService()) waitNumber=ps.PrintReceipt(bellNo, isTalk, HappyPointReserve,OkCashBagReserve);
            if (!string.IsNullOrEmpty(waitNumber)) WaitNumberText = waitNumber;
            DoAfterReceiptPrint();
        
        }


        #endregion Methods


        #region [ Event Handlers ]
        private void CashTypePersonCommandHandler(object obj)
        {
            CashTypePersonEnable = false;
        }
        private void CashTypeCompanyCommandHandler(object obj)
        {
            CashTypeCompanyEnable = false;
        }
        private void BellButtonCommandHandler(object obj)
        {
            
            BellButtonEnable = false;

        }
        private void TalkButtonCommandHandler(object obj)
        {
            TalkButtonEnable = false;
        }

        private void PointSavingCommandHandler(object obj)
        {
            PointSavingButtonOk = true;
        }

        private void TalkGetCommandHandler(object obj)
        {
            //Console.WriteLine("알림톡 받기");
            DoNewMemberRequest();
            CanWindowClose = true;
        }        

        private void CancelCommandHandler(object obj)
        {
            switch (PaymentEndWindow)
            {
                case PaymentEndWindowType.CashReceipt:
                    certifyKey = InputNumber;
                    divType = CashTypeCompanyEnable ? "0" : "1";
                    PaymentEndWindow = PaymentEndWindowType.Processing;
                    break;
                case PaymentEndWindowType.PaymentSuccess:
                case PaymentEndWindowType.WaitNumberSuccess:
                case PaymentEndWindowType.TalkSuccess:
                    CanWindowClose = true;
                    break;
            }
        }
        private void OkCommandHandler(object obj)
        {
            switch (PaymentEndWindow)
            {
                case PaymentEndWindowType.CashReceipt:
                    PaymentEndWindow = PaymentEndWindowType.Processing;
                    break;
                case PaymentEndWindowType.BellOrNotifyTalk:
                    DoReceiptPrint();
                    break;
                case PaymentEndWindowType.PaymentSuccess:
                case PaymentEndWindowType.WaitNumberSuccess:
                case PaymentEndWindowType.TalkSuccess:
                    if (ProcessTask != null) ProcessTask.Wait();
                    CanWindowClose = true;
                    
                    break;
            }
        }


        private void StoreAgreeCommandHandler(object obj)
        {
            if (ProcessTask != null) ProcessTask.Wait();
            if (HappyPointReturn is M_HappyPointReturn)
            {
                DoRegFavoriteStore();
            }
            else
            {
                CanWindowClose = true;
            }
        }

        private void LastPayments_ReadStart(object sender)
        {
            TimerEnabled = false;
            ProcessStart = true;
        }
        private void LastPayments_ReadEnd(object sender)
        {
            ProcessStart = false;
        }
        private void VmPaymentsEnd_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            try
            {
                switch (e.PropertyName)
                {

                    case "IsTimeout":
                        if (IsTimeout)
                        {
                            switch (PaymentEndWindow)
                            {
                                case PaymentEndWindowType.CashReceipt:
                                    InputNumber = string.Empty;
                                    PaymentEndWindow = PaymentEndWindowType.Processing;
                                    break;
                                case PaymentEndWindowType.BellOrNotifyTalk:
                                    TalkNumPadNumber = string.Empty;
                                    DoReceiptPrint();
                                    break;
                                case PaymentEndWindowType.PaymentSuccess:
                                case PaymentEndWindowType.WaitNumberSuccess:
                                case PaymentEndWindowType.TalkSuccess:
                                    if (ProcessTask != null) ProcessTask.Wait();
                                    CanWindowClose = true;
                                    break;
                            }
                        }
                        break;
                    case "HappyPointReturn":
                        StoreAgreeView = HappyPointReturn is M_HappyPointReturn;                        
                        SetStoreAgreeData();
                        break;
                    case "EndWindow":
                        switch (EndWindow)
                        {
                            case PaymentEndWindowEndType.Bell:
                                BellAnsTalkToggleVisible = !CommonValue.DisableEndType.Equals(PaymentEndWindowEndType.Talk);
                                break;
                            case PaymentEndWindowEndType.Talk:
                                BellAnsTalkToggleVisible = !CommonValue.DisableEndType.Equals(PaymentEndWindowEndType.Bell);
                                break;
                        }
                        break;
                    case "PaymentEndWindow":
                        CashReceiptVisible = false;
                        ProcessingVisible = false;
                        BellOrNotifyTalkVisible = false;
                        PaymentSuccessVisible = false;
                        WaitNumberSuccessVisible = false;
                        TalkSuccessVisible = false;
                        switch (PaymentEndWindow)
                        {
                            case PaymentEndWindowType.CashReceipt:
                                CashReceiptVisible = true;
                                break;
                            case PaymentEndWindowType.Processing:
                                ProcessingVisible = true;
                                break;
                            case PaymentEndWindowType.BellOrNotifyTalk:
                                BellOrNotifyTalkVisible = true;
                                break;
                            case PaymentEndWindowType.PaymentSuccess:
                                PaymentSuccessVisible = true;
                                break;
                            case PaymentEndWindowType.WaitNumberSuccess:
                                WaitNumberSuccessVisible = true;
                                break;
                            case PaymentEndWindowType.TalkSuccess:
                                TalkSuccessVisible = true;
                                break;
                        }
                        break;
                    case "CashReceiptVisible":
                        if (CashReceiptVisible)
                        {
                            using (var ps = new PosDataService()) ps.GetCashBill(ref cashPayments, ref cashTax);
                            if (CashPayments.Equals(0))
                            {
                                DoBeforeReceiptPrint();
                            }
                            else
                            {
                                HeaderText = Languages.GetMessages("LBL0139");
                                ScanGuidText = Languages.GetMessages("LBL0053");
                                InfoGuid1Text = Languages.GetMessages("LBL0009");
                                InfoGuid2Text = Languages.GetMessages("LBL0010");
                                CancelButtonWidth = PopupWidth / 2;
                                OKButtonWidth = PopupWidth / 2;
                                OKButtonEnabled = false;
                                CancelButtonText = Languages.GetMessages("BTN0033");
                                OkButtonText = Languages.GetMessages("BTN0024");
                                TimeOutSeconds = CommonValue.TimeOutSeconds;
                            }
                        }
                        break;

                    case "ProcessingVisible":
                        if (ProcessingVisible)
                        {
                            HeaderText = Languages.GetMessages("LBL0139");
                            ScanGuidText = new List<M_Language>();
                            InfoGuid1Text = new List<M_Language>();
                            InfoGuid2Text = new List<M_Language>();
                            CancelButtonWidth = 0d;
                            OKButtonWidth = 0d;
                            if (ProcessTask != null)
                            {
                                ProcessTask.Dispose();
                                ProcessTask = null;
                            }
                            ProcessTask = new Task(DoCashReceiptProcessing);
                            ProcessTask.Start();
                        }
                        break;
                    case "BellOrNotifyTalkVisible":
                        if (BellOrNotifyTalkVisible)
                        {
                            HeaderText = Languages.GetMessages("LBL0029");
                            ScanGuidText = Languages.GetMessages("LBL0074");
                            InfoGuid1Text = Languages.GetMessages("LBL0011");
                            InfoGuid2Text = Languages.GetMessages("LBL0005");
                            CancelButtonWidth = 0d;
                            OKButtonWidth = PopupWidth;
                            OKButtonEnabled = false;
                            OkButtonText = Languages.GetMessages("BTN0024");
                            TimeOutSeconds = CommonValue.TimeOutSeconds;
                        }
                        break;
                    case "PaymentSuccessVisible":
                        if (PaymentSuccessVisible)
                        {
                            InputNumber = string.Empty;
                            HeaderText = Languages.GetMessages("LBL0029");
                            ScanGuidText = new List<M_Language>();
                            InfoGuid1Text = new List<M_Language>();
                            InfoGuid2Text = new List<M_Language>();
                            CancelButtonWidth = 0d;
                            OKButtonWidth = PopupWidth;
                            OKButtonEnabled = true;
                            OkButtonText = Languages.GetMessages("BTN0039");
                            TimeOutSeconds =  CommonValue.TimeOutSeconds;
                        }
                        break;
                    case "WaitNumberSuccessVisible":
                        if (WaitNumberSuccessVisible)
                        {
                            InputNumber = string.Empty;
                            HeaderText = Languages.GetMessages("LBL0029");
                            ScanGuidText = Languages.GetMessages("LBL0093");
                            WaitNumberHeaderText = Languages.GetMessages("LBL0041");
                            if (EndWindow.Equals(PaymentEndWindowEndType.Bell)
                                && !string.IsNullOrEmpty(TalkNumPadNumber))
                            {
                                WaitNumberHeaderText = Languages.GetMessages("LBL0103");

                                WaitNumberText = TalkNumPadNumber;
                            }
                            InfoGuid1Text = new List<M_Language>();
                            InfoGuid2Text = new List<M_Language>();
                            CancelButtonWidth = 0d;
                            OKButtonWidth = PopupWidth;
                            OKButtonEnabled = true;
                            OkButtonText = Languages.GetMessages("BTN0039");
                            TimeOutSeconds =  CommonValue.TimeOutSeconds;

                        }
                        break;
                    case "TalkSuccessVisible":
                        if (TalkSuccessVisible)
                        {
                            InputNumber = string.Empty;
                            HeaderText = Languages.GetMessages("LBL0029");
                            ScanGuidText = Languages.GetMessages("LBL0093");
                            InfoGuid1Text = new List<M_Language>();
                            InfoGuid2Text = new List<M_Language>();
                            CancelButtonWidth = 0d;
                            OKButtonWidth = PopupWidth;
                            OKButtonEnabled = true;
                            OkButtonText = Languages.GetMessages("BTN0039");
                            TimeOutSeconds =  CommonValue.TimeOutSeconds;
                        }
                        break;

                    case "NumPadInputVisible":
                        NumPadGuideVisible = !NumPadInputVisible;
                        break;
                    case "InputNumber":
                        NumPadInputVisible = InputNumber.Length > 0;
                        switch (PaymentEndWindow)
                        {
                            case PaymentEndWindowType.CashReceipt:
                                if (!CashTypePersonEnable)
                                {
                                    OKButtonEnabled = InputNumber.Length.Equals(10) || InputNumber.Length.Equals(11);
                                }
                                if (!CashTypeCompanyEnable)
                                {
                                    OKButtonEnabled = InputNumber.Length.Equals(10);
                                }
                                break;
                            case PaymentEndWindowType.PaymentSuccess:
                            case PaymentEndWindowType.WaitNumberSuccess:
                            case PaymentEndWindowType.TalkSuccess:
                                TalkGetButtonEnabled = InputNumber.Length.Equals(10) || InputNumber.Length.Equals(11);
                                break;
                            default:
                                OKButtonEnabled = InputNumber.Length.Equals(10) || InputNumber.Length.Equals(11);
                                break;
                        }
                        break;
                    case "TalkNumPadInputVisible":
                        TalkNumPadGuideVisible = !TalkNumPadInputVisible;
                        break;
                    case "TalkNumPadNumber":
                        TalkNumPadInputVisible = TalkNumPadNumber.Length > 0;
                        if (!BellButtonEnable)
                        {
                            OKButtonEnabled = TalkNumPadNumber.Length > 0;
                        }
                        if (!TalkButtonEnable)
                        {
                            OKButtonEnabled = TalkNumPadNumber.Length.Equals(10) || TalkNumPadNumber.Length.Equals(11);
                        }
                        break;
                    case "CashTypePersonEnable":
                        if (!CashTypePersonEnable)
                        {
                            NumPadFormated = true;
                            NumPadMaxLength = 11;
                            NumPadGuidText = Languages.GetMessages("LBL0091");
                            InputNumber = string.Empty;
                            CashTypeCompanyEnable = true;
                        }
                        break;
                    case "CashTypeCompanyEnable":
                        if (!CashTypeCompanyEnable)
                        {
                            NumPadFormated = false;
                            NumPadMaxLength = 10;
                            NumPadGuidText = Languages.GetMessages("LBL0056");
                            InputNumber = string.Empty;
                            CashTypePersonEnable = true;
                        }
                        break;
                    case "BellButtonEnable":
                        if (!BellButtonEnable)
                        {
                            EndWindow = PaymentEndWindowEndType.Bell;
                            TalkNumPadFormated = true;
                            TalkNumPadMaxLength = 3;
                            TalkNumPadGuidText = Languages.GetMessages("LBL0104");
                            BellOrNotifyTalkTitileImage = ResourceManager.GetNximagePathAdd("img_bell.png", CommonValue.PBdesignImagesPath);
                            BellOrNotifyTalkText = Languages.GetMessages("LBL0078");
                            TalkNumPadNumber = string.Empty;
                            TalkButtonEnable = true;
                        }
                        break;
                    case "TalkButtonEnable":
                        if (!TalkButtonEnable)
                        {
                            EndWindow = PaymentEndWindowEndType.Talk;
                            TalkNumPadFormated = true;
                            TalkNumPadMaxLength = 11;
                            TalkNumPadGuidText = Languages.GetMessages("LBL0091");
                            BellOrNotifyTalkTitileImage = ResourceManager.GetNximagePathAdd("img_talk.png", CommonValue.PBdesignImagesPath);
                            BellOrNotifyTalkText = Languages.GetMessages("LBL0036");
                            TalkNumPadNumber = string.Empty;
                            BellButtonEnable = true;
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                CommonLog.ErrorLogWrite(this, "VmPaymentsEnd_PropertyChanged()", "Fail", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
            }
        }

        
        #endregion Event Handlers
    }
}