using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Timers;
using CafeTerrace.Models.DataBase;
using Plugin.Settings;
using Xamarin.Forms;

namespace CafeTerrace.ViewModels
{
    public class SuccessBuyDialogBoxViewModel : ViewModelBase
    {
        public ObservableCollection<OrderFood> Foods { get; set; } = new ObservableCollection<OrderFood>();
        public string OfferCode { get; set; }

        public bool IsShow { get; set; }

        public int PrapairingMinuets { get; set; }
        public DateTime SubmitBuyTime { get; set; }
        private TimeSpan _remainingTime;
        public TimeSpan RemainingTime
        {
            get { return _remainingTime; }
            set { SetProperty(ref _remainingTime, value); }
        }

        public bool IsEnded { get; set; } = false;
        public event EventHandler Ended;

        private int yekanSec;
        public int YekanSec
        {
            get { return yekanSec; }
            set { SetProperty(ref yekanSec, value); }
        }
        private int dahganSec;
        public int DahganSec
        {
            get { return dahganSec; }
            set { SetProperty(ref dahganSec, value); }
        }
        private int yekanMin;
        public int YekanMin
        {
            get { return yekanMin; }
            set { SetProperty(ref yekanMin, value); }
        }
        private int dahganMin;
        public int DahganMin
        {
            get { return dahganMin; }
            set { SetProperty(ref dahganMin, value); }
        }
        private Timer remainigTimer;
        public SuccessBuyDialogBoxViewModel()
        {
            RemainingTime = SubmitBuyTime.AddMinutes(PrapairingMinuets).Subtract(DateTime.Now);
        }

        public void Start()
        {
            remainigTimer = new Timer(1000);
            remainigTimer.Elapsed += RemainigTimer_Elapsed;
            remainigTimer.Start();
        }

        private void RemainigTimer_Elapsed(object sender, ElapsedEventArgs e)
        {
            RemainingTime = SubmitBuyTime.AddMinutes(PrapairingMinuets).Subtract(DateTime.Now);
            int min = RemainingTime.Minutes;
            int sec = RemainingTime.Seconds;
            if (min <= 0 && sec <= 0)
            {
                IsEnded = true;
                YekanMin = 0;
                DahganMin = 0;
                YekanSec = 0;
                DahganSec = 0;
                remainigTimer.Stop();
                CrossSettings.Current.Remove("Order");
                Ended?.Invoke(this,new EventArgs());
            }
            int fm = min % 10;
            int lm = min / 10;
            YekanMin = fm;
            DahganMin = lm;

            int fs = sec % 10;
            int ls = sec / 10;
            YekanSec = fs;
            DahganSec = ls;

        }
    }
}
