
namespace HistoryView.ViewModels.Regions;

public partial class AlarmViewModel : ObservableObject
{
    public AlarmViewModel(MessageBoxHelper messageBox, UserInformation user, Alarms alarms, Hardwares hardwares, HistoryViewer historyViewer)
    {
        this._messageBox = messageBox;
        this.NoAlarm = alarms.DisplayAlarm.Count switch
        {
            0 => Visibility.Visible,
            _ => Visibility.Collapsed
        };
        alarms.DisplayAlarm.PropertyChanged += DisplayAlarm_PropertyChanged;
        this.Alarms = alarms;
        this.Hardwares = hardwares;
        this.UserInfo = user;
        this._history = historyViewer;
    }

    private void DisplayAlarm_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        this.Alarms.DisplayAlarm.CollectionChanged += DisplayAlarm_CollectionChanged;
    }

    private void DisplayAlarm_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        this.NoAlarm = this.Alarms.DisplayAlarm.Count switch
        {
            0 => Visibility.Visible,
            _ => Visibility.Collapsed
        };
    }

    private readonly HistoryViewer _history;
    private readonly MessageBoxHelper _messageBox;

    [ObservableProperty]
    private Hardwares _Hardwares;

    [ObservableProperty]
    private Alarms _Alarms;

    [ObservableProperty]
    private Visibility _NoAlarm;

    [ObservableProperty]
    ObservableCollection<AlarmInfo>? _DisplayHistoryAlarms;

    [ObservableProperty]
    private Mini8Info? _SelectedMini8;

    partial void OnSelectedMini8Changed(Mini8Info? value)
    {
        this.Mini8Channels ??= [];
        this.Mini8Channels.Clear();
        if (value is null)
            return;
        //this.Mini8Channels.AddRange(this.Hardwares.Mini8Channels.Where(t => t.Key == value.Index).FirstOrDefault().Value);
    }

    [ObservableProperty]
    public ObservableCollection<Channel>? _Mini8Channels;

    [ObservableProperty]
    private Channel? _SelectedChannel;

    [ObservableProperty]
    private UserInformation? _UserInfo;

    [RelayCommand]
    private void Filter()
    {
        if (this.Alarms.HistoryAlarms is null)
            return;

        this.DisplayHistoryAlarms ??= [];
        this.DisplayHistoryAlarms.Clear();

        List<AlarmInfo> alarms = [];
        alarms.AddRange(this.Alarms.HistoryAlarms);

        if (this.SelectedMini8 is not null)
            alarms = [.. alarms.Where(t => t.Mini8Index == this.SelectedMini8.Index)];

        if (this.SelectedChannel is not null)
            alarms = [.. alarms.Where(t => t.ChannelIndex == this.SelectedChannel.ChannelIndex)];

        this.DisplayHistoryAlarms.AddRange(alarms);
    }

    [RelayCommand]
    private void History()
    {
        this._history.StartAlarmHistroy();
    }

    [RelayCommand]
    private void ClearFilter()
    {
        this.SelectedMini8 = null;
        this.SelectedChannel = null;

        this.DisplayHistoryAlarms ??= [];
        this.DisplayHistoryAlarms.Clear();
        //this.DisplayHistoryAlarms.AddRange(this.Alarms.HistoryAlarms);
    }

    [RelayCommand]
    private void Clear()
    {
        IDialogResult result = this._messageBox.ShowAsync((string)App.Current.Resources["ConfirmClearAllAlarm"], MessageBoxButton.YesNo, MessageBoxImage.Question).Result;
        if (result.Result != ButtonResult.Yes)
            return;

        this.Alarms.DisplayAlarm?.Clear();
    }

    [RelayCommand]
    private void ClearSingle(AlarmInfo alarmInfo)
    {
        if (alarmInfo is null)
            return;
        if (this.Alarms.DisplayAlarm is null)
            return;
        if (!this.Alarms.DisplayAlarm.ContainsKey(alarmInfo.Mini8Index))
            return;

        IDialogResult result = this._messageBox.ShowAsync((string)App.Current.Resources["ConfirmClearThisAlarm"], MessageBoxButton.YesNo, MessageBoxImage.Question).Result;
        if (result.Result != ButtonResult.Yes)
            return;

        this.Alarms.DisplayAlarm.Remove(alarmInfo.Mini8Index);
    }
}
