namespace ConfigFileManager.ViewModels.Dialog;

internal partial class MigrateViewModel : ObservableObject, IDialogAware
{
    public MigrateViewModel(DeviceCollection deviceCollection)
    {
        this.Sources = [];
        this.Running = Visibility.Collapsed;
        this.Progress = 0;
        foreach (DeviceInfo_VM deviceInfos in deviceCollection.DeviceList.Values.OrderBy(t=>t.DeviceModel))
            this.Sources.Add(deviceInfos);
    }

    [ObservableProperty]
    private string? _Title;

    [ObservableProperty]
    private ObservableCollection<DeviceInfo_VM> _Sources;

    [ObservableProperty]
    private DeviceInfo_VM? _Source;

    public DialogCloseListener RequestClose { get; set; }

    public bool CanCloseDialog()
    {
        return true;
    }

    public void OnDialogClosed()
    {
    }

    public void OnDialogOpened(IDialogParameters parameters)
    {
        this.Title = "文件迁移";
        if (!parameters.TryGetValue("Device", out DeviceInfo_VM? deviceInfo) || deviceInfo is null)
        {
            MessageBox.Show("Config Device Error");
            this.RequestClose.Invoke();
            return;
        }
        this.Sources.Remove(deviceInfo);
        this.Source = deviceInfo;
    }

    [ObservableProperty]
    private Visibility _Running;

    [ObservableProperty]
    private int _progress;

    [RelayCommand]
    private async Task Migrate(DeviceInfo_VM device)
    {
        if (this.Source is null)
            return;

        if (Source.SoftwareVersion != device.SoftwareVersion)
            MessageBox.Show($"{this.Source.DeviceName} 与 {device.DeviceName} 间存在软件版本差异，请确认无误后再进行迁移！", "配置迁移", MessageBoxButton.OK, MessageBoxImage.Warning);

        MessageBoxResult result = MessageBox.Show($"确认将 {this.Source.DeviceName} Config文件迁移至 {device.DeviceName}?", "配置迁移", MessageBoxButton.YesNo, MessageBoxImage.Question);

        if (result != MessageBoxResult.Yes)
            return;
        this.Running = Visibility.Visible;
        this.Progress = 0;
        for (int i = 1; i <= 10; i++)
        {
            await Task.Delay(500);
            this.Progress = 10 * i;
        }

        MessageBox.Show($"配置及Recipe文件迁移成功", "配置迁移", MessageBoxButton.OK, MessageBoxImage.Question);
        this.Running = Visibility.Collapsed;
    }

    [RelayCommand]
    private async Task MigrateRecipe(DeviceInfo_VM device)
    {
        if (this.Source is null)
            return;

        if (Source.SoftwareVersion != device.SoftwareVersion)
            MessageBox.Show($"{this.Source.DeviceName} 与 {device.DeviceName} 间存在软件版本差异，请确认无误后再进行迁移！", "配置迁移", MessageBoxButton.OK, MessageBoxImage.Warning);

        MessageBoxResult result = MessageBox.Show($"确认将 {this.Source.DeviceName} Recipe文件迁移至 {device.DeviceName}?", "配置迁移", MessageBoxButton.YesNo, MessageBoxImage.Question);

        if (result != MessageBoxResult.Yes)
            return;
        this.Running = Visibility.Visible;
        this.Progress = 0;
        for (int i = 1; i <= 10; i++)
        {
            await Task.Delay(500);
            this.Progress = 10 * i;
        }

        MessageBox.Show($"配置及Recipe文件迁移成功", "配置迁移", MessageBoxButton.OK, MessageBoxImage.Question);
        this.Running = Visibility.Collapsed;
    }

    [RelayCommand]
    private void Close()
    {
        this.RequestClose.Invoke();
    }
}