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.Threading;
using SPC.Kiosk.Base;
using SPC.Kiosk.Common;
namespace SPC.Kiosk.Popup.ViewModel
{
    /// <summary>
    /// Popup View Model Base
    /// </summary>
    public class PopupViewModelBase : ViewModelBase 
    {
        #region [ Members ]
        public ICommand ToExecuteNothing { get; protected set; } = null;
        private SupportLanguageType showLanguageType;
        /// <summary>
        /// 화면 언어
        /// </summary>
        public SupportLanguageType ShowLanguageType
        {
            get { return showLanguageType; }
            set { showLanguageType = value; PropertyChange("ShowLanguageType"); }
        }
        /// <summary>
        /// 팝업 응답 정보
        /// </summary>
        public M_PopupReturn ReturnValue { get; set; }
        private bool canWindowClose = false;
        /// <summary>
        /// 화면 닫기 (true 이면 바로 닫음)
        /// </summary>
        public bool CanWindowClose
        {
            get { return canWindowClose; }
            set { canWindowClose = value; PropertyChange("CanWindowClose"); }
        }
        private bool timerEnabled = false;
        /// <summary>
        /// 대기시간 타이머 동작 유무
        /// </summary>
        public bool TimerEnabled
        {
            get { return timerEnabled; }
            set { timerEnabled = value; PropertyChange("TimerEnabled"); }
        }
        private bool isTimeout = false;
        /// <summary>
        /// 대기시간 초과 유무 (타이머 동작 중지후 IsTimeOut = True 발생)
        /// </summary>
        public bool IsTimeout
        {
            get { return isTimeout; }
            set { isTimeout = value; PropertyChange("IsTimeout"); }
        }
        private double timeOutSeconds = 30d;
        /// <summary>
        /// 대기시간 (초)
        /// </summary>
        public double TimeOutSeconds
        {
            get { return timeOutSeconds; }
            set { timeOutSeconds = value; PropertyChange("TimeOutSeconds"); }
        }
        private bool isTimeoutPopup = false;
        public bool IsTimeoutPopup
        {
            get { return isTimeoutPopup; }
            set { isTimeoutPopup = value; PropertyChange("IsTimeoutPopup"); }
        }
        /// <summary>
        /// 최종 엑세스 시간 PropertyChanged 가 발생 할때 마다 업데이트됨
        /// </summary>
        public DateTime LastAccessTime { get; set; } = DateTime.Now;
        private DispatcherTimer ShowTimer;
        #endregion

        #region [ Ctor ]
        /// <summary>
        /// 생성자
        /// </summary>
        public PopupViewModelBase()
        {
            ToExecuteNothing = new Command(ToExecuteNothingHandler);
            this.PropertyChanged += PopupViewModelBase_PropertyChanged;
            TimeOutSeconds = CommonValue.TimeOutSeconds;
            ShowLanguageType = CommonValue.CommonLanguageType;
            TimerEnabled = true;
        }
        /// <summary>
        /// Dispose
        /// </summary>
        public void Dispose()
        {
            this.PropertyChanged -= PopupViewModelBase_PropertyChanged;
            if (this.ShowTimer != null)
            {
                ShowTimer.Stop();
                ShowTimer.Tick -= ShowTimer_Tick;
                ShowTimer = null;
            }
        }
        #endregion Ctor

        #region [ Event Handlers ]
        private void ToExecuteNothingHandler(object obj)
        {

        }
        private void ShowTimer_Tick(object sender, EventArgs e)
        {
            if ((DateTime.Now - LastAccessTime) > TimeSpan.FromSeconds(TimeOutSeconds))
            {

                TimerEnabled = false;
                if (IsTimeoutPopup)
                {
#if !TESTMODE
                    var popupReturn = PopupMessageBox.ShowMessageBox(ShowLanguageType
                                                                        , Languages.GetMessages("LBL0067")
                                                                        , Languages.GetMessages("LBL0021")
                                                                        , MessageBoxButton.YesNo
                                                                        , OpenCloseAnimationType.FullSizeUp
                                                                        , OpenCloseAnimationType.FullSizeDown
                                                                        , 0.5, 0.2, 10);
                    if (popupReturn.Equals(MessageBoxResult.Yes))
                    {
                        LastAccessTime = DateTime.Now;
                        TimerEnabled = true;
                    }
                    else
                    {
                        IsTimeout = true;
                    }
#endif
                }
                else
                {
                    IsTimeout = true;
                }
            }
        }
        private void PopupViewModelBase_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            LastAccessTime = DateTime.Now;
            switch (e.PropertyName)
            {
                case "TimerEnabled":
                    if (TimerEnabled)
                    {
                        if (this.ShowTimer != null)
                        {
                            this.ShowTimer.Start();
                        }
                        else
                        {
                            this.ShowTimer = new DispatcherTimer
                            {
                                Interval = TimeSpan.FromSeconds(1),
                            };
                            ShowTimer.Tick += ShowTimer_Tick;
                            ShowTimer.Start();
                        }
                        
                    }
                    else
                    {
                        if (this.ShowTimer != null)
                        {
                            this.ShowTimer.Stop();
                        }
                    }
                    break;
                case "CanWindowClose":
                    if (this.ShowTimer != null)
                    {
                        this.ShowTimer.Stop();
                        this.ShowTimer.Tick -= ShowTimer_Tick;
                        this.ShowTimer = null;
                    }
                    break;
            }
        }



        #endregion Event Handlers
    }
}