namespace HistoryView.ViewModels.Regions.SettingSubs;

internal partial class ConfigFileViewModel : ObservableObject
{
    public ConfigFileViewModel(MessageBoxHelper messageBox, IDialogService dialogService, TemperatureConfigs temperatureConfigs, UserInformation user, HubSender sender)
    {
        this._sender = sender;
        this._messageBox = messageBox;
        this._dialogService = dialogService;
        this.TemperatureConfigs = temperatureConfigs;
        this.UserInfo = user;
        this.IsEditable = true;
    }
    private readonly HubSender _sender;
    private readonly MessageBoxHelper _messageBox;
    private readonly IDialogService _dialogService;

    [ObservableProperty]
    private TemperatureConfigs _temperatureConfigs;

    [ObservableProperty]
    private TempConfig? _Selected;

    [ObservableProperty]
    private UserInformation? _UserInfo;

    [ObservableProperty]
    private bool _IsEditable;

    [RelayCommand]
    private void Load()
    {
        this._dialogService.Show("File", new DialogParameters(), GetPara);
    }

    partial void OnSelectedChanged(TempConfig? value)
    {
        if (value is null)
            return;

        if (this.TemperatureConfigs.CurrentConfigFile is null)
            return;

        this.IsEditable = (value != this.TemperatureConfigs.CurrentConfigFile.ConfigFile);
    }

    [RelayCommand]
    private async Task ReadCurrentAndSave()
    {
        if (!this._sender.QueryMini8Data(out TempConfig? config) || config is null)
        {
            await _messageBox.ShowAsync("Error", icon: MessageBoxImage.Error);
            return;
        }
        config.ConfigName += "-Saved";

        IDialogParameters para = new DialogParameters
        {
            { "ConfigEditor", config }
        };
        this._dialogService.Show("ConfigEditor", para, null);
    }


    [RelayCommand]
    private void SelectCurrent()
    {
        if (this.TemperatureConfigs is null || this.TemperatureConfigs.CurrentConfigFile is null)
            return;

        this.Selected = this.TemperatureConfigs.CurrentConfigFile.ConfigFile;
    }

    [RelayCommand]
    private void Edit()
    {
        if (this.Selected is null)
            return;
        IDialogParameters para = new DialogParameters
        {
            { "ConfigEditor", this.Selected }
        };
        this._dialogService.Show("ConfigEditor", para, null);
    }

    [RelayCommand]
    private void Remove()
    {
        if (this.Selected is null)
            return;
        if (string.IsNullOrEmpty(this.Selected.ConfigName))
            return;

        var t = this._messageBox.ShowAsync($"{Selected.ConfigName} ?", MessageBoxButton.YesNo, MessageBoxImage.Question).Result;
        if (t.Result != ButtonResult.Yes)
            return;

        _sender.RemoveFile(this.Selected.ConfigName);
    }

    [RelayCommand]
    private void SetAsCurrent()
    {
        if (this.Selected is null)
            return;
        if (_messageBox is null)
            return;

        var t = this._messageBox.ShowAsync($"{Selected.ConfigName} {App.Current.FindResource("SetConfigAsCurrent")}", MessageBoxButton.YesNo, MessageBoxImage.Question).Result;
        if (t.Result != ButtonResult.Yes)
            return;

        this._sender.SetConfigFile(this.Selected.ConfigName);
    }

    [RelayCommand]
    private void Compare()
    {
        IDialogParameters para = new DialogParameters
        {
            { "Source", this.Selected! }
        };
        this._dialogService.Show("Compare", para, GetPara);
    }


    private void GetPara(IDialogResult dialogResult)
    {
        if (!dialogResult.Parameters.TryGetValue<FileInfo>("File", out FileInfo? file) || file is null)
            return;

        _sender.LoadNewFile(file.FullName);
    }
}
