using MainShell.Resources.CustomControl;
using Stylet;
using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Linq;

namespace MainShell.Manual.ViewModel.State
{
    public class LogisticsOperationState : PropertyChangedBase
    {
        private SectionState _substrateLoad;
        public SectionState SubstrateLoad
        {
            get { return _substrateLoad; }
            set { SetAndNotify(ref _substrateLoad, value); }
        }

        private SectionState _chipLoad;
        public SectionState ChipLoad
        {
            get { return _chipLoad; }
            set { SetAndNotify(ref _chipLoad, value); }
        }

        private SectionState _substrateOut;
        public SectionState SubstrateOut
        {
            get { return _substrateOut; }
            set { SetAndNotify(ref _substrateOut, value); }
        }

        private SectionState _chipUnload;
        public SectionState ChipUnload
        {
            get { return _chipUnload; }
            set { SetAndNotify(ref _chipUnload, value); }
        }

        private MagazineMapViewModel _magazineMap;
        public MagazineMapViewModel MagazineMap
        {
            get { return _magazineMap; }
            set { SetAndNotify(ref _magazineMap, value); }
        }

        private string _feedbackTitle;
        public string FeedbackTitle
        {
            get { return _feedbackTitle; }
            set { SetAndNotify(ref _feedbackTitle, value); }
        }

        private bool _isScanning;
        public bool IsScanning
        {
            get { return _isScanning; }
            set { SetAndNotify(ref _isScanning, value); }
        }

        private int _scanProgress;
        public int ScanProgress
        {
            get { return _scanProgress; }
            set { SetAndNotify(ref _scanProgress, value); }
        }

        private bool _isManualWithoutEfem;
        public bool IsManualWithoutEfem
        {
            get { return _isManualWithoutEfem; }
            set { SetAndNotify(ref _isManualWithoutEfem, value); }
        }

        public string LastFeedbackTime
        {
            get
            {
                var last = FeedbackRecords.LastOrDefault();
                return last == null ? "--:--:--" : last.Time.ToString("HH:mm:ss");
            }
        }

        public ObservableCollection<FeedbackRecord> FeedbackRecords { get; } = new ObservableCollection<FeedbackRecord>();

        public LogisticsOperationState()
        {
            FeedbackRecords.CollectionChanged += OnFeedbackRecordsChanged;
            AddFeedback("内部物流动作已独立触发并完成。");
        }

        public void AddFeedback(string message, string level = "DEBUG")
        {
            FeedbackRecords.Add(new FeedbackRecord
            {
                Time = DateTime.Now,
                Level = level,
                Message = message
            });

            while (FeedbackRecords.Count > 3)
            {
                FeedbackRecords.RemoveAt(0);
            }
        }

        private void OnFeedbackRecordsChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            OnPropertyChanged(nameof(LastFeedbackTime));
        }
    }

    public class SectionState : PropertyChangedBase
    {
        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetAndNotify(ref _title, value); }
        }

        private string _status;
        public string Status
        {
            get { return _status; }
            set { SetAndNotify(ref _status, value); }
        }

        private string _primaryActionText;
        public string PrimaryActionText
        {
            get { return _primaryActionText; }
            set { SetAndNotify(ref _primaryActionText, value); }
        }

        private string _secondaryActionText;
        public string SecondaryActionText
        {
            get { return _secondaryActionText; }
            set { SetAndNotify(ref _secondaryActionText, value); }
        }

        public ObservableCollection<IndicatorState> Indicators { get; } = new ObservableCollection<IndicatorState>();

        public ObservableCollection<PhaseStepState> PhaseSteps { get; } = new ObservableCollection<PhaseStepState>();
    }

    public class PhaseStepState : PropertyChangedBase
    {
        private string _title;
        public string Title
        {
            get { return _title; }
            set { SetAndNotify(ref _title, value); }
        }

        private string _status;
        public string Status
        {
            get { return _status; }
            set { SetAndNotify(ref _status, value); }
        }

        private string _actionText;
        public string ActionText
        {
            get { return _actionText; }
            set { SetAndNotify(ref _actionText, value); }
        }

        private bool _isEnabled;
        public bool IsEnabled
        {
            get { return _isEnabled; }
            set { SetAndNotify(ref _isEnabled, value); }
        }

        private string _controlTag;
        public string ControlTag
        {
            get { return _controlTag; }
            set { SetAndNotify(ref _controlTag, value); }
        }

        private bool _requiresEfemCommunication;
        public bool RequiresEfemCommunication
        {
            get { return _requiresEfemCommunication; }
            set { SetAndNotify(ref _requiresEfemCommunication, value); }
        }

        private int _targetLayer = 1;
        public int TargetLayer
        {
            get { return _targetLayer; }
            set { SetAndNotify(ref _targetLayer, value); }
        }
    }

    public class IndicatorState : PropertyChangedBase
    {
        private string _name;
        public string Name
        {
            get { return _name; }
            set { SetAndNotify(ref _name, value); }
        }

        private bool _isOn;
        public bool IsOn
        {
            get { return _isOn; }
            set { SetAndNotify(ref _isOn, value); }
        }
    }

    public class FeedbackRecord : PropertyChangedBase
    {
        private DateTime _time;
        public DateTime Time
        {
            get { return _time; }
            set { SetAndNotify(ref _time, value); }
        }

        private string _level;
        public string Level
        {
            get { return _level; }
            set { SetAndNotify(ref _level, value); }
        }

        private string _message;
        public string Message
        {
            get { return _message; }
            set { SetAndNotify(ref _message, value); }
        }
    }
}
