using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Threading;
using SPC.Kiosk.Base;
using SPC.Kiosk.Common;
using SPC.Kiosk.Popup.Model;
namespace SPC.Kiosk.Popup.ViewModel
{
    /// <summary>
    /// PkCashBagCertify.xaml에 대한 ViewModel
    /// </summary>
    public class VmOkCashBagUsing : PopupViewModelBase
    {
        #region [ Members ]
        private List<M_Language> headerText;
        /// <summary>
        /// Top Header Text (Memebr login)
        /// </summary>
        public List<M_Language> HeaderText
        {
            get { return headerText; }
            set { headerText = value; PropertyChange("HeaderText"); }
        }
        private List<M_Language> afterHeaderText;
        /// <summary>
        /// Top Header Text (Using Points)
        /// </summary>
        public List<M_Language> AfterHeaderText
        {
            get { return afterHeaderText; }
            set { afterHeaderText = value; PropertyChange("AfterHeaderText"); }
        }
        private List<M_Language> scanGuidText;
        /// <summary>
        /// Second Header Text (Memebr login)
        /// </summary>
        public List<M_Language> ScanGuidText
        {
            get { return scanGuidText; }
            set { scanGuidText = value; PropertyChange("ScanGuidText"); }
        }
        private List<M_Language> infoGuid1Text;
        /// <summary>
        /// Bottom first Infomation Text (Memebr login)
        /// </summary>
        public List<M_Language> InfoGuid1Text
        {
            get { return infoGuid1Text; }
            set { infoGuid1Text = value; PropertyChange("InfoGuid1Text"); }
        }
        private List<M_Language> infoGuid2Text;
        /// <summary>
        /// Bottom second Infomation Text (Memebr login)
        /// </summary>
        public List<M_Language> InfoGuid2Text
        {
            get { return infoGuid2Text; }
            set { infoGuid2Text = value; PropertyChange("InfoGuid2Text"); }
        }
        private List<M_Language> infoGuid3Text;
        /// <summary>
        /// Bottom first Infomation Text (Using Points)
        /// </summary>
        public List<M_Language> InfoGuid3Text
        {
            get { return infoGuid3Text; }
            set { infoGuid3Text = value; PropertyChange("InfoGuid3Text"); }
        }
        private List<M_Language> infoGuid4Text;
        /// <summary>
        /// Bottom second Infomation Text (Using Points)
        /// </summary>
        public List<M_Language> InfoGuid4Text
        {
            get { return infoGuid4Text; }
            set { infoGuid4Text = value; PropertyChange("InfoGuid4Text"); }
        }
        public int numPadMaxLength;
        /// <summary>
        /// Number Pad Max Length
        /// </summary>
        public int NumPadMaxLength
        {
            get { return numPadMaxLength; }
            set { numPadMaxLength = value; PropertyChange("NumPadMaxLength"); }
        }
        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 List<M_Language> numPadCancelText;
        /// <summary>
        /// Number Pad Cancel Button Text
        /// </summary>
        public List<M_Language> NumPadCancelText
        {
            get { return numPadCancelText; }
            set { numPadCancelText = value; PropertyChange("NumPadCancelText"); }
        }
        private bool pointPadVisible;
        /// <summary>
        /// Point Pad Visible
        /// </summary>
        public bool PointPadVisible
        {
            get { return pointPadVisible; }
            set { pointPadVisible = value; PropertyChange("PointPadVisible"); }
        }
        private int pointPadMaxLength;
        /// <summary>
        /// Point Pad Max Length
        /// </summary>
        public int PointPadMaxLength
        {
            get { return pointPadMaxLength; }
            set { pointPadMaxLength = value; PropertyChange("PointPadMaxLength"); }
        }
        private List<M_Language> pointPadGuidText;
        /// <summary>
        /// Point Pad Input Guide Text
        /// </summary>
        public List<M_Language> PointPadGuidText
        {
            get { return pointPadGuidText; }
            set { pointPadGuidText = value; PropertyChange("PointPadGuidText"); }
        }
        private List<M_Language> pointPadCancelText;
        /// <summary>
        /// Point Pad Cancel Button Text
        /// </summary>
        public List<M_Language> PointPadCancelText
        {
            get { return pointPadCancelText; }
            set { pointPadCancelText = value; PropertyChange("PointPadCancelText"); }
        }
        private List<M_Language> cancelButtonText;
        /// <summary>
        /// Cancel Button Text (Member Login)
        /// </summary>
        public List<M_Language> CancelButtonText
        {
            get { return cancelButtonText; }
            set { cancelButtonText = value; PropertyChange("CancelButtonText"); }
        }
        private List<M_Language> okButtonText;
        /// <summary>
        /// OK Button  Text (Member Login)
        /// </summary>
        public List<M_Language> OkButtonText
        {
            get { return okButtonText; }
            set { okButtonText = value; PropertyChange("OkButtonText"); }
        }
        public bool oKButtonEnabled = false;
        /// <summary>
        /// OK Button Enabled (Member Login)
        /// </summary>
        public bool OKButtonEnabled
        {
            get { return oKButtonEnabled; }
            set { oKButtonEnabled = value; PropertyChange("OKButtonEnabled"); }
        }
        private List<M_Language> cancelButton2Text;
        /// <summary>
        /// Cancel Button Text (Point Using)
        /// </summary>
        public List<M_Language> CancelButton2Text
        {
            get { return cancelButton2Text; }
            set { cancelButton2Text = value; PropertyChange("CancelButton2Text"); }
        }
        private List<M_Language> okButton2Text;
        /// <summary>
        /// OK Button Text (Point Using)
        /// </summary>
        public List<M_Language> OkButton2Text
        {
            get { return okButton2Text; }
            set { okButton2Text = value; PropertyChange("OkButton2Text"); }
        }
        public bool oKButton2Enabled = false;
        /// <summary>
        /// OK Button Enabled (Point using)
        /// </summary>
        public bool OKButton2Enabled
        {
            get { return oKButton2Enabled; }
            set { oKButton2Enabled = value; PropertyChange("OKButton2Enabled"); }
        }
        private List<M_Language> switchAnswer1Text;
        /// <summary>
        /// Point Reserve Check Button Text
        /// </summary>
        public List<M_Language> SwitchAnswer1Text
        {
            get { return switchAnswer1Text; }
            set { switchAnswer1Text = value; PropertyChange("SwitchAnswer1Text"); }
        }
        public bool switchAnswer1Switch = false;
        /// <summary>
        /// Point Reserve Check Button Switch
        /// </summary>
        public bool SwitchAnswer1Switch
        {
            get { return switchAnswer1Switch; }
            set { switchAnswer1Switch = value; PropertyChange("SwitchAnswer1Switch"); }
        }
        private List<M_Language> switchAnswer2Text;
        /// <summary>
        /// Point using Check Button Text
        /// </summary>
        public List<M_Language> SwitchAnswer2Text
        {
            get { return switchAnswer2Text; }
            set { switchAnswer2Text = value; PropertyChange("SwitchAnswer2Text"); }
        }
        public bool switchAnswer2Switch = false;
        /// <summary>
        /// Point Using Button Switch
        /// </summary>
        public bool SwitchAnswer2Switch
        {
            get { return switchAnswer2Switch; }
            set { switchAnswer2Switch = value; PropertyChange("SwitchAnswer2Switch"); }
        }
        private List<M_Language> allUseButtonText;
        /// <summary>
        /// All using Points Button Text
        /// </summary>
        public List<M_Language> AllUseButtonText
        {
            get { return allUseButtonText; }
            set { allUseButtonText = value; PropertyChange("AllUseButtonText"); }
        }
        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 string formatedBarCode;
        /// <summary>
        /// Formated Read Barcode
        /// </summary>
        public string FormatedBarCode
        {
            get { return formatedBarCode; }
            set { formatedBarCode = value; PropertyChange("FormatedBarCode"); }
        }
        private string readBarCode;
        /// <summary>
        /// Read Barcode
        /// </summary>
        public string ReadBarCode
        {
            get { return readBarCode; }
            set { readBarCode = value; PropertyChange("ReadBarCode"); }
        }
        /// <summary>
        /// Point Reserve Check Flag
        /// </summary>
        private bool reservepoint;
        public bool ReservePoint
        {
            get { return reservepoint; }
            set { reservepoint = value; PropertyChange("ReservePoint"); }
        }
        private double payments;
        /// <summary>
        /// Total Payments
        /// </summary>
        public double Payments
        {
            get { return payments; }
            set { payments = value; PropertyChange("Payments"); }
        }
        private string showPayment;
        /// <summary>
        /// Total Payments Show Text
        /// </summary>
        public string ShowPayment
        {
            get { return showPayment; }
            set { showPayment = value; PropertyChange("ShowPayment"); }
        }
        private string memberName;
        /// <summary>
        /// Login Memeber Name
        /// </summary>
        public string MemberName
        {
            get { return memberName; }
            set { memberName = value; PropertyChange("MemberName"); }
        }
        private double queryPoints;
        /// <summary>
        /// Member Useable All Points
        /// </summary>
        public double QueryPoints
        {
            get { return queryPoints; }
            set { queryPoints = value; PropertyChange("QueryPoints"); }
        }
        private List<M_Language> errorText;
        /// <summary>
        /// Error Text (Member login)
        /// </summary>
        public List<M_Language> ErrorText
        {
            get { return errorText; }
            set { errorText = value; PropertyChange("ErrorText"); }
        }
        private bool isErrorBlink = false;
        /// <summary>
        /// Is Error Text Blink  (Member login)
        /// </summary>
        public bool IsErrorBlink
        {
            get { return isErrorBlink; }
            set { isErrorBlink = value; PropertyChange("IsErrorBlink"); }
        }
        private List<M_Language> errorText2;
        /// <summary>
        /// Error Text (Point Using)
        /// </summary>
        public List<M_Language> ErrorText2
        {
            get { return errorText2; }
            set { errorText2 = value; PropertyChange("ErrorText2"); }
        }
        private bool isErrorBlink2 = false;
        /// <summary>
        /// Is Error Text Blink (Point using)
        /// </summary>
        public bool IsErrorBlink2
        {
            get { return isErrorBlink2; }
            set { isErrorBlink2 = value; PropertyChange("IsErrorBlink2"); }
        }
        private bool toAfterBase = false;
        /// <summary>
        /// Change Grid to Point Using Grid Flag (true => to to Point Using)
        /// </summary>
        public bool ToAfterBase
        {
            get { return toAfterBase; }
            set { toAfterBase = value; PropertyChange("ToAfterBase"); }
        }
        private List<M_Language> memberInfo;
        /// <summary>
        /// Login Memner Info
        /// </summary>
        public List<M_Language> MemberInfo
        {
            get { return memberInfo; }
            set { memberInfo = value; PropertyChange("MemberInfo"); }
        }
        private string inputNumbers = string.Empty;
        /// <summary>
        /// Number pad input Numbers
        /// </summary>
        public string InputNumbers
        {
            get { return inputNumbers; }
            set { inputNumbers = value; PropertyChange("InputNumbers"); }
        }
        private string inputPoints = string.Empty;
        /// <summary>
        /// Point pad Input Points
        /// </summary>
        public string InputPoints
        {
            get { return inputPoints; }
            set { inputPoints = value; PropertyChange("InputPoints"); }
        }
        private M_OkCashBagReturn okCashBagReturn;
        /// <summary>
        /// Ok Chash bag Return Value
        /// </summary>
        public M_OkCashBagReturn OkCashBagReturn
        {
            get { return okCashBagReturn; }
            set { okCashBagReturn = value; PropertyChange("OkCashBagReturn"); }
        }
        /// <summary>
        /// OK Click Command (Member Login)
        /// </summary>
        public ICommand OkCommand { get; protected set; }
        /// <summary>
        /// Cancel Click Command (Member Login & Point Using)
        /// </summary>
        public ICommand CancelCommand { get; protected set; }
        /// <summary>
        /// Ok Click Command (Point Using)
        /// </summary>
        public ICommand Ok2Command { get; protected set; }
        /// <summary>
        /// Point Reserve Click Command
        /// </summary>
        public ICommand SwitchAnswer1Command { get; protected set; }
        /// <summary>
        /// Point Using Click Command
        /// </summary>
        public ICommand SwitchAnswer2Command { get; protected set; }
        /// <summary>
        /// All Using Points Click Command
        /// </summary>
        public ICommand AllUseCommand { get; protected set; }

        private List<M_Language> PointUsingGuid3 = new List<M_Language>
                                        {
                                            new M_Language
                                            {
                                                Type = SupportLanguageType.ko,
                                                LanguageData = "* 해피포인트 적립시 OK캐쉬백은 적립할 수 없습니다."
                                            },
                                            new M_Language
                                            {
                                                Type = SupportLanguageType.en,
                                                LanguageData = "en* 해피포인트 적립시 OK캐쉬백은 적립할 수 없습니다."
                                            },
                                        };
        private List<M_Language> PointUsingGuid4 = new List<M_Language>
                                        {
                                            new M_Language
                                            {
                                                Type = SupportLanguageType.ko,
                                                LanguageData = "* 1000P 이상 10P 단위 사용가능."
                                            },
                                            new M_Language
                                            {
                                                Type = SupportLanguageType.en,
                                                LanguageData = "en* * 1000P 이상 10P 단위 사용가능."
                                            },
                                        };
        private List<M_Language> ReservePoint3 = new List<M_Language>
                                        {
                                            new M_Language
                                            {
                                                Type = SupportLanguageType.ko,
                                                LanguageData = "* 최종 결제후 자동 적립됩니다."
                                            },
                                            new M_Language
                                            {
                                                Type = SupportLanguageType.en,
                                                LanguageData = "en* 최종 결제후 자동 적립됩니다."
                                            },
                                        };
        private List<M_Language> ReservePoint4 = new List<M_Language>();
        private bool MemberProcessStart = false;
        private Task ProcessTask = null;
        #endregion Members

        #region [ Ctor ]
        /// <summary>
        /// Ctor
        /// </summary>
        public VmOkCashBagUsing()
        {
            OkCommand = new Command(OkCommandHandler);
            CancelCommand = new Command(CancelCommandHandler);
            Ok2Command = new Command(Ok2CommandHandler);
            SwitchAnswer1Command = new Command(SwitchAnswer1CommandHandler);
            SwitchAnswer2Command = new Command(SwitchAnswer2CommandHandler);
            AllUseCommand = new Command(AllUseCommandHandler);
            this.PropertyChanged += VmOkCashBagCertify_PropertyChanged;
            HeaderText = new List<M_Language>
                        {
                            new M_Language
                            {
                                Type = SupportLanguageType.ko,
                                LanguageData = "OK 캐쉬백 회원인증"
                            },
                            new M_Language
                            {
                                Type = SupportLanguageType.en,
                                LanguageData = "OK CASHBAG Member Certify"
                            },
                        };
            AfterHeaderText = new List<M_Language>
                        {
                            new M_Language
                            {
                                Type = SupportLanguageType.ko,
                                LanguageData = "OK 캐쉬백"
                            },
                            new M_Language
                            {
                                Type = SupportLanguageType.en,
                                LanguageData = "OK CASHBAG"
                            },
                        };
            ScanGuidText = new List<M_Language>
                        {
                            new M_Language
                            {
                                Type = SupportLanguageType.ko,
                                LanguageData = "바코드 스켄 또는 전화 번호를 입력하세요."
                            },
                            new M_Language
                            {
                                Type = SupportLanguageType.en,
                                LanguageData = "en바코드 스켄 또는 전화 번호를 입력하세요."
                            },
                        };
            InfoGuid1Text = new List<M_Language>
                                        {
                                            new M_Language
                                            {
                                                Type = SupportLanguageType.ko,
                                                LanguageData = "* 해피포인트 적립시 OK캐쉬백은 적립할 수 없습니다."
                                            },
                                            new M_Language
                                            {
                                                Type = SupportLanguageType.en,
                                                LanguageData = "en* 해피포인트 적립시 OK캐쉬백은 적립할 수 없습니다."
                                            },
                                        };
            InfoGuid2Text = new List<M_Language>
                                        {
                                            new M_Language
                                            {
                                                Type = SupportLanguageType.ko,
                                                LanguageData = "* 수집된 개인정보는 활용 후 자동 폐기 됩니다."
                                            },
                                            new M_Language
                                            {
                                                Type = SupportLanguageType.en,
                                                LanguageData = "en* 수집된 개인정보는 활용 후 자동 폐기 됩니다."
                                            },
                                        };
            NumPadMaxLength = 11;
            NumPadGuidText = Languages.GetMessages("MobileNumberMessage");
            NumPadCancelText = Languages.GetMessages("CommonClear");
            PointPadMaxLength = 9;
            PointPadGuidText = Languages.GetMessages("InputUsingPoints");
            PointPadCancelText = Languages.GetMessages("CommonClear");
            CancelButtonText = Languages.GetMessages("CommonCancel");
            OkButtonText = Languages.GetMessages("CommonOK");
            CancelButton2Text = Languages.GetMessages("CommonCancel");
            OkButton2Text = Languages.GetMessages("CommonOK");
            SwitchAnswer1Text = Languages.GetMessages("CommonReserve");
            SwitchAnswer2Text = Languages.GetMessages("CommonUsing");
            AllUseButtonText = Languages.GetMessages("CommonUsingAll");
            TotalPayHeaderText = Languages.GetMessages("PriceForPayment");
            if (!ReservePoint) PointPadVisible = true;
        }

        ~VmOkCashBagUsing()
        {
            this.PropertyChanged -= VmOkCashBagCertify_PropertyChanged;
        }
        #endregion Ctor

        #region [ Methods ]
        private bool PointValueCheck()
        {
            bool result = false;
            try
            {
                if (!Payments.Equals(0) && QueryPoints >= 1000)
                {
                    var checkValue = int.Parse(InputPoints);
                    if (checkValue >= 1000)
                    {
                        result = (checkValue % 10).Equals(0)
                              && checkValue <= QueryPoints
                              && checkValue <= Payments;
                    }
                }

            }
            catch
            {
                result = false;
            }
            return result;
        }
        private void DoOkCashBagCertify()
        {
            TimerEnabled = false;
            MemberProcessStart = true;
            //TODO : OK CASHBAG Point 조회
            OkCashBagReturn = new M_OkCashBagReturn
            {
                MemberName = "홍길동",
                MemberPoint = 120501,
                MemberCode = ReadBarCode.Equals(string.Empty) ? InputNumbers : ReadBarCode,
            };
            MemberName = OkCashBagReturn.MemberName;
            QueryPoints = OkCashBagReturn.MemberPoint;
            ToAfterBase = true;
            TimerEnabled = true;
        }
        #endregion Methods

        #region  [ Event Handlers ]
        private void AllUseCommandHandler(object obj)
        {
            if (QueryPoints > 1000)
            {
                var canUseAllPoints = QueryPoints - QueryPoints % 10;
                if (!Payments.Equals(0))
                {
                    if (Payments < canUseAllPoints)
                    {
                        InputPoints = Payments.ToString();
                    }
                    else
                    {
                        InputPoints = canUseAllPoints.ToString();
                    }
                }
            }
        }

        private void CancelCommandHandler(object obj)
        {
            TimerEnabled = false;
            ReturnValue = new M_PopupReturn
            {
                OKAnswer = false,
                TimeOut = false,
                ReturnLanguage = ShowLanguageType,
                PopupArgs = null
            };
            CanWindowClose = true;
        }

        private void OkCommandHandler(object obj)
        {
            DoOkCashBagCertify();
        }

        private void Ok2CommandHandler(object obj)
        {
            TimerEnabled = false;
            OkCashBagReturn.ReservePoint = SwitchAnswer1Switch;
            OkCashBagReturn.PointUse = SwitchAnswer2Switch;
            OkCashBagReturn.UsingPoints = string.IsNullOrEmpty(InputPoints)
                                             ? 0 : int.Parse(InputPoints);
            ReturnValue = new M_PopupReturn
            {
                OKAnswer = true,
                TimeOut = false,
                ReturnLanguage = ShowLanguageType,
                PopupArgs = OkCashBagReturn
            };
            if (ProcessTask != null) ProcessTask.Wait();
            CanWindowClose = true;
        }

        private void SwitchAnswer1CommandHandler(object obj)
        {
            if (SwitchAnswer2Switch)
            {
                OKButton2Enabled = PointValueCheck();

            }
            else
            {
                OKButton2Enabled = ReservePoint ? SwitchAnswer1Switch : false;
            }
        }

        private void SwitchAnswer2CommandHandler(object obj)
        {
            if (!ReservePoint) SwitchAnswer2Switch = true;
            if (SwitchAnswer2Switch)
            {
                InfoGuid3Text = PointUsingGuid3;
                InfoGuid4Text = PointUsingGuid4;
                if (!Payments.Equals(0))
                {
                    PointPadVisible = true;
                    OKButton2Enabled = PointValueCheck();
                }
                else
                {
                    SwitchAnswer2Switch = false;
                }

            }
            else
            {
                InfoGuid3Text = ReservePoint3;
                InfoGuid4Text = ReservePoint4;
                InputPoints = string.Empty;
                PointPadVisible = false;
                OKButton2Enabled = ReservePoint ? SwitchAnswer1Switch : false;
            }
        }

        private void VmOkCashBagCertify_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "IsTimeout":
                    if (IsTimeout)
                    {
                        ReturnValue = new M_PopupReturn
                        {
                            OKAnswer = false,
                            TimeOut = true,
                            ReturnLanguage = ShowLanguageType,
                            PopupArgs = null
                        };
                        if (ProcessTask != null) ProcessTask.Wait();
                        CanWindowClose = true;
                    }
                    break;
                case "ReservePoint":
                    if (ReservePoint)
                    {
                        InfoGuid3Text = ReservePoint3;
                        InfoGuid4Text = ReservePoint4;
                        PointPadVisible = false;
                    }
                    else
                    {
                        SwitchAnswer2Switch = true;
                        OKButton2Enabled = false;
                        InfoGuid3Text = PointUsingGuid3;
                        InfoGuid4Text = PointUsingGuid4;
                    }
                    break;
                case "InputNumbers":
                    if (InputNumbers.Length > 0) ReadBarCode = string.Empty;
                    OKButtonEnabled = NumPadMaxLength.Equals(InputNumbers.Length);
                    break;
                case "InputPoints":
                    OKButton2Enabled = PointValueCheck();
                    break;
                case "Payments":
                    ShowPayment = string.Format(@"\{0}", Payments.ToString("#,##0"));
                    break;
                case "MemberName":
                case "QueryPoints":
                    var memberInfo = string.Format("{0}({1}P)"
                                    , MemberName.GetEncriptName(), QueryPoints.ToString("#,###0"));
                    MemberInfo = new List<M_Language>
                    {
                        new M_Language { Type=SupportLanguageType.ko, LanguageData = memberInfo }
                    };
                    break;
                case "ReadBarCode":
                    if (!MemberProcessStart)
                    {
                        switch (ReadBarCode.Length)
                        {
                            case 16:
                                InputNumbers = string.Empty;
                                FormatedBarCode = ReadBarCode.GetEncriptCardNo();
                                DoOkCashBagCertify();
                                break;
                            default:
                                FormatedBarCode = string.Empty;
                                OKButtonEnabled = false;
                                break;
                        }
                    }
                    break;
            }
        }
        #endregion Event Handlers
    }
}