namespace HistoryView.ViewModels.Regions.SettingSubs;

internal partial class ChannelSummaryViewModel : ObservableObject
{
    public ChannelSummaryViewModel(Hardwares hardwares, HubSender sender, IDialogService dialogService)
    {
        _hardwares = hardwares;
        _sender = sender;
        _dialogService = dialogService;
        this.Channels = [];
        this.Display = [];
        RefreshDisplay("All");
        this.Refresh();
    }

    private readonly HubSender _sender;
    private readonly Hardwares _hardwares;
    private readonly IDialogService _dialogService;

    [ObservableProperty]
    private ObservableDictionary<Mini8Info, ObservableCollection<Channel>> _Channels;

    [ObservableProperty]
    private ObservableDictionary<Mini8Info, DisplayDetail> _Display;

    [RelayCommand]
    private void Select()
    {
        this.Channels.Clear();
        foreach (var item in this.Display)
        {
            if (!item.Value.IsDisplay)
                continue;

            if (!this._hardwares.Mini8Channels.TryGetValue(item.Key.Index, out ObservableDictionary<byte, Channel>? channels) || channels is null)
                continue;
            this.Channels[item.Key] ??= [];
            this.Channels[item.Key].Clear();
            this.Channels[item.Key].AddRange(channels.Values);
        }
    }


    [RelayCommand]
    private void RefreshDisplay(string para)
    {
        foreach (var item in this._hardwares.Mini8s)
        {
            if (!this._hardwares.Mini8Channels.TryGetValue(item.Key, out ObservableDictionary<byte, Channel>? channels) || channels is null)
                continue;

            this.Display[item.Value] = para switch
            {
                "All" => new(true),
                "None" => new(false),
                _ => new(true)
            };
        }

        this.Select();
    }

    [RelayCommand]
    private void Refresh()
    {
        this.Channels.Clear();
        foreach (var item in this._hardwares.Mini8s)
        {
            if (!this._hardwares.Mini8Channels.TryGetValue(item.Key, out ObservableDictionary<byte, Channel>? channels) || channels is null)
                continue;
            this.Channels[item.Value] = [];
            this.Channels[item.Value].AddRange(channels.Values);
        }
    }
}
