using Stylet;
using System.Collections.ObjectModel;

namespace MainShell.Resources.CustomControl
{
    public class MagazineMapViewModel : PropertyChangedBase
    {
        private string _title = "MAGAZINE MAP";
        public string Title
        {
            get { return _title; }
            set { SetAndNotify(ref _title, value); }
        }

        private int _totalSlots;
        public int TotalSlots
        {
            get { return _totalSlots; }
            set { SetAndNotify(ref _totalSlots, value); }
        }

        public ObservableCollection<MagazineSlotState> Slots { get; } = new ObservableCollection<MagazineSlotState>();

        public static MagazineMapViewModel CreateDefault(int slotCount)
        {
            var vm = new MagazineMapViewModel { TotalSlots = slotCount };

            for (var i = slotCount; i >= 1; i--)
            {
                var status = MagazineSlotStatus.Empty;
                if (i <= 11 || i == 18)
                {
                    status = MagazineSlotStatus.Present;
                }

                if (i == 10 || i == 9)
                {
                    status = MagazineSlotStatus.Processing;
                }

                if (i == 8 || i == 7)
                {
                    status = MagazineSlotStatus.Completed;
                }

                vm.Slots.Add(new MagazineSlotState
                {
                    SlotName = $"L{i:D2}",
                    SlotStatus = status
                });
            }

            return vm;
        }
    }

    public enum MagazineSlotStatus
    {
        Empty,
        Present,
        Processing,
        Completed,
        Target,
        Alarm
    }

    public class MagazineSlotState : PropertyChangedBase
    {
        private string _slotName;
        public string SlotName
        {
            get { return _slotName; }
            set { SetAndNotify(ref _slotName, value); }
        }

        private bool _isPresent;
        public bool IsPresent
        {
            get { return _isPresent; }
            set
            {
                if (SetAndNotify(ref _isPresent, value))
                {
                    SlotStatus = value ? MagazineSlotStatus.Present : MagazineSlotStatus.Empty;
                    OnPropertyChanged(nameof(StatusText));
                }
            }
        }

        private MagazineSlotStatus _slotStatus = MagazineSlotStatus.Empty;
        public MagazineSlotStatus SlotStatus
        {
            get { return _slotStatus; }
            set
            {
                if (SetAndNotify(ref _slotStatus, value))
                {
                    _isPresent = value == MagazineSlotStatus.Present;
                    OnPropertyChanged(nameof(IsPresent));
                    OnPropertyChanged(nameof(StatusText));
                }
            }
        }

        public string StatusText
        {
            get
            {
                switch (SlotStatus)
                {
                    case MagazineSlotStatus.Present:
                        return "PRES";
                    case MagazineSlotStatus.Processing:
                        return "PROC";
                    case MagazineSlotStatus.Completed:
                        return "DONE";
                    case MagazineSlotStatus.Target:
                        return "PROC";
                    case MagazineSlotStatus.Alarm:
                        return "ALM";
                    default:
                        return "---";
                }
            }
        }
    }
}
