using System;
using System.ComponentModel;
using System.Windows.Threading;

namespace SWRIS.Models
{
    public class DateTimeModel : INotifyPropertyChanged
    {
        private readonly DispatcherTimer _timer;
        private DateTime systemTime;
        private string date = DateTime.Now.ToString("yyyy-MM-dd");
        private string time = DateTime.Now.ToString("HH:mm");

        public DateTimeModel()
        {
            _timer = new DispatcherTimer
            {
                Interval = TimeSpan.FromSeconds(1) // 1秒更新一次保持精准
            };
            _timer.Tick += Timer_Tick;

            // 启动定时器
            _timer.Start();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            systemTime = DateTime.Now;
            if (Time != systemTime.ToString("HH:mm"))
            {
                Time = systemTime.ToString("HH:mm");
                if (Date != systemTime.ToString("yyyy-MM-dd"))
                {
                    Date = systemTime.ToString("yyyy-MM-dd");
                }
            }
        }

        public string Date { get => date; set { date = value; OnPropertyChanged("Date"); } }
        public string Time { get => time; set { time = value; OnPropertyChanged("Time"); } }
        public event PropertyChangedEventHandler PropertyChanged;
        protected internal virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
