using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using SPC.Kiosk.Base;
using SPC.Kiosk.Common;
using SPC.Kiosk.Popup.Model;
namespace SPC.Kiosk.Popup.ViewModel
{
    /// <summary>
    /// OptionSelect.xaml 의 ViewModel
    /// </summary>
    public class VmOptionSelect : PopupViewModelBase
    {
        #region [ Members ]
        /// <summary>
        /// Return Data
        /// </summary>
        public M_OptionReturn OptionReturn { get; set; }

        private double itemPrice;
        public double ItemPrice
        {
            get { return itemPrice; }
            set { itemPrice = value; PropertyChange("ItemPrice"); }
        }
        private List<M_ItemOptionGroup> options;
        /// <summary>
        /// Option Data
        /// </summary>
        public List<M_ItemOptionGroup> Options
        {
            get { return options; }
            set { options = value; PropertyChange("Options"); }
        }

        private List<M_Language> headerText;
        /// <summary>
        /// Top Header Text
        /// </summary>
        public List<M_Language> HeaderText
        {
            get { return headerText; }
            set { headerText = value; PropertyChange("HeaderText"); }
        }
        private List<M_Language> scanGuidText;
        /// <summary>
        /// Second Sub Header Text
        /// </summary>
        public List<M_Language> ScanGuidText
        {
            get { return scanGuidText; }
            set { scanGuidText = value; PropertyChange("ScanGuidText"); }
        }

        private string itemImageBrush;
        /// <summary>
        /// Item Image
        /// </summary>
        public string ItemImageBrush
        {
            get { return itemImageBrush; }
            set { itemImageBrush = value; PropertyChange("ItemImageBrush"); }
        }
        private M_AnimationButton leftButtonData= null;
        /// Combo Right Left Data
        public M_AnimationButton LeftButtonData
        {
            get { return leftButtonData; }
            set { leftButtonData = value; PropertyChange("LeftButtonData"); }
        }
        private M_AnimationButton rightButtonData = null;
        /// <summary>
        /// Combo Right Button Data
        /// </summary>
        public M_AnimationButton RightButtonData
        {
            get { return rightButtonData; }
            set { rightButtonData = value; PropertyChange("RightButtonData"); }
        }


        private List<M_Language> itemName;
        /// <summary>
        /// Second Sub Header Text
        /// </summary>
        public List<M_Language> ItemName
        {
            get { return itemName; }
            set { itemName = value; PropertyChange("ItemName"); }
        }
        private int itemCount = 1;
        /// <summary>
        /// Item Count
        /// </summary>
        public int ItemCount
        {
            get { return itemCount; }
            set { itemCount = value; PropertyChange("ItemCount"); }
        }
        private double unitPrice;
        public double UnitPrice
        {
            get { return unitPrice; }
            set { unitPrice = value; PropertyChange("UnitPrice"); }
        }
        private List<M_Language> totalPriceLabel;
        /// <summary>
        /// Second Sub Header Text
        /// </summary>
        public List<M_Language> TotalPriceLabel
        {
            get { return totalPriceLabel; }
            set { totalPriceLabel = value; PropertyChange("TotalPriceLabel"); }
        }

        private double totalPrice;
        public double TotalPrice
        {
            get { return totalPrice; }
            set { totalPrice = value; PropertyChange("TotalPrice"); }
        }
        private List<M_Language> cancelButtonText;
        /// <summary>
        /// Cancel Button Text
        /// </summary>
        public List<M_Language> CancelButtonText
        {
            get { return cancelButtonText; }
            set { cancelButtonText = value; PropertyChange("CancelButtonText"); }
        }
        private List<M_Language> okButtonText;
        /// <summary>
        /// OK Button Text
        /// </summary>
        public List<M_Language> OkButtonText
        {
            get { return okButtonText; }
            set { okButtonText = value; PropertyChange("OkButtonText"); }
        }
        /// <summary>
        /// OK Click Command
        /// </summary>
        public ICommand OkCommand { get; protected set; }
        /// <summary>
        /// Cancel Click Command
        /// </summary>
        public ICommand CancelCommand { get; protected set; }
        /// <summary>
        /// Option Changed Command
        /// </summary>
        public ICommand OptionCommand { get; protected set; }
        private bool editMode = false;
        /// <summary>
        /// Edit Mode
        /// </summary>
        public bool EditMode
        {
            get { return editMode; }
            set { editMode = value; PropertyChange("EditMode"); }
        }
        #endregion Members

        #region [ Ctor ]
        /// <summary>
        /// Ctor
        /// </summary>
        public VmOptionSelect()
        {
            OkCommand = new Command(OkCommandHandler);
            CancelCommand = new Command(CancelCommandHandler);
            OptionCommand = new Command(OptionCommandHandler);
            LeftButtonData = new M_AnimationButton
            {
                NormalBrush = ResourceManager.GetNximagePathAdd("btn_count_minus_n.png", CommonValue.PBdesignImagesPath),
                DownBrush = ResourceManager.GetNximagePathAdd("btn_count_minus_p.png", CommonValue.PBdesignImagesPath),
                BrushStretch = Stretch.Fill,
                DragEnable = false,
                Width = 36,
                Height = 36,
                Switch = false,
                AutoToggle = false,
                TextVisible = Visibility.Collapsed,
                ClickAnimationType = ButtonAnimationType.None,
                CircleBackBrush = "Transparent",
                CircleTextBruch = "Transparent",
                CircleText = string.Empty,
            };
            RightButtonData = new M_AnimationButton
            {
                NormalBrush = ResourceManager.GetNximagePathAdd("btn_count_plus_n.png", CommonValue.PBdesignImagesPath),
                DownBrush = ResourceManager.GetNximagePathAdd("btn_count_plus_p.png", CommonValue.PBdesignImagesPath),
                BrushStretch = Stretch.Fill,
                DragEnable = false,
                Width = 36,
                Height = 36,
                Switch = false,
                AutoToggle = false,
                TextVisible = Visibility.Collapsed,
                ClickAnimationType = ButtonAnimationType.None,
                CircleBackBrush = "Transparent",
                CircleTextBruch = "Transparent",
                CircleText = string.Empty,
            };
            this.PropertyChanged += VmOptionSelect_PropertyChanged;
            HeaderText = Languages.GetMessages("LBL0115");
            TotalPriceLabel = Languages.GetMessages("LBL0132");
            ScanGuidText = Languages.GetMessages("LBL0068");
            ShowLanguageType = CommonValue.CommonLanguageType;
            CancelButtonText = Languages.GetMessages("BTN0033");
            OkButtonText = Languages.GetMessages("BTN0024");
        }


        ~VmOptionSelect()
        {
            this.PropertyChanged -= VmOptionSelect_PropertyChanged;
        }
        #endregion Ctor

        #region [ Methods ]
        private void CalcUnitPrice(object _param)
        {
            if (_param is List<M_ItemOptionGroup> changedOption)
            {

                var optionPrice = ItemPrice;
                foreach (var aGroup in changedOption)
                {
                    foreach (var aOption in aGroup.Options)
                    {
                        if ((aOption.Kind.Equals(OptionKind.Discount)
                            || aOption.Kind.Equals(OptionKind.UpCharge))
                            && aOption.IsSelected)
                        {
                            optionPrice += aOption.SelectValue;
                        }
                    }
                }
                UnitPrice = optionPrice; 
            }
        }
        #endregion Methods

        #region [ Event Handlers ]
        private void OptionCommandHandler(object _param)
        {
            CalcUnitPrice(_param);
        }
        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)
        {
            TimerEnabled = false;
            var newOptionReturn = new M_OptionReturn
            { 
                 ItemCount = ItemCount,
                 PriceWithOption = UnitPrice,
                 Options = Options
            };
            ReturnValue = new M_PopupReturn
            {
                OKAnswer = true,
                TimeOut = false,
                ReturnLanguage = ShowLanguageType,
                PopupArgs = newOptionReturn
            };
            CanWindowClose = true;
        }

        private void VmOptionSelect_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            try
            {
                switch (e.PropertyName)
                {
                    case "IsTimeout":
                        if (IsTimeout)
                        {
                            ReturnValue = new M_PopupReturn
                            {
                                OKAnswer = false,
                                TimeOut = true,
                                ReturnLanguage = ShowLanguageType,
                                PopupArgs = null
                            };
                            CanWindowClose = true;
                        }
                        break;
                    case "ItemPrice":
                        CalcUnitPrice(Options);
                        break;
                    case "ItemCount":
                    case "UnitPrice":
                        if (ItemCount > 0 && UnitPrice > 0)
                        {
                            TotalPrice = ItemCount * UnitPrice;
                        }
                        break;
                }
            }
            catch (Exception ex)
            {

                CommonLog.ErrorLogWrite(this, "VmOptionSelect_PropertyChanged()", "Fail !!", string.Format("{0}\n{1}", ex.Message, ex.StackTrace));
            }
        }


        #endregion Event Handlers
    }
}