using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Media;
using ZB.MOM.WW.OtOpcUa.Client.Shared.Models;
using ZB.MOM.WW.OtOpcUa.Client.UI.ViewModels;

namespace ZB.MOM.WW.OtOpcUa.Client.UI.Views;

public partial class ShelveAlarmWindow : Window
{
    private readonly AlarmsViewModel _alarmsVm;
    private readonly AlarmEventViewModel _alarm;

    /// <summary>Designer ctor.</summary>
    public ShelveAlarmWindow()
    {
        InitializeComponent();
        _alarmsVm = null!;
        _alarm = null!;
    }

    /// <summary>Creates the shelve dialog for an alarm.</summary>
    /// <param name="alarmsVm">The view model used to issue the shelve/unshelve command.</param>
    /// <param name="alarm">The alarm event being shelved.</param>
    public ShelveAlarmWindow(AlarmsViewModel alarmsVm, AlarmEventViewModel alarm)
    {
        InitializeComponent();
        _alarmsVm = alarmsVm;
        _alarm = alarm;

        var sourceText = this.FindControl<TextBlock>("SourceText");
        if (sourceText != null) sourceText.Text = alarm.SourceName;

        var conditionText = this.FindControl<TextBlock>("ConditionText");
        if (conditionText != null) conditionText.Text = $"{alarm.ConditionName} (Severity: {alarm.Severity})";

        var kindCombo = this.FindControl<ComboBox>("KindCombo");
        if (kindCombo != null) kindCombo.SelectionChanged += OnKindChanged;

        var shelveButton = this.FindControl<Button>("ShelveButton");
        if (shelveButton != null) shelveButton.Click += OnShelveClicked;

        var cancelButton = this.FindControl<Button>("CancelButton");
        if (cancelButton != null) cancelButton.Click += OnCancelClicked;
    }

    private void OnKindChanged(object? sender, SelectionChangedEventArgs e)
    {
        var durationPanel = this.FindControl<StackPanel>("DurationPanel");
        var kindCombo = this.FindControl<ComboBox>("KindCombo");
        var shelveButton = this.FindControl<Button>("ShelveButton");
        if (durationPanel != null && kindCombo != null)
            durationPanel.IsEnabled = kindCombo.SelectedIndex == 1; // Timed
        // Relabel the action button so selecting "Unshelve" doesn't read "Shelve".
        if (shelveButton != null)
            shelveButton.Content = kindCombo?.SelectedIndex == 2 ? "Unshelve" : "Shelve";
    }

    private async void OnShelveClicked(object? sender, RoutedEventArgs e)
    {
        var kindCombo = this.FindControl<ComboBox>("KindCombo");
        var durationInput = this.FindControl<NumericUpDown>("DurationInput");
        var resultText = this.FindControl<TextBlock>("ResultText");
        if (kindCombo == null || durationInput == null || resultText == null) return;

        var kind = kindCombo.SelectedIndex switch
        {
            1 => ShelveKind.Timed,
            2 => ShelveKind.Unshelve,
            _ => ShelveKind.OneShot
        };
        var duration = (double)(durationInput.Value ?? 0m);

        resultText.Foreground = Brushes.Gray;
        resultText.Text = "Working...";

        var (success, message) = await _alarmsVm.ShelveAlarmAsync(_alarm, kind, duration);
        if (success)
        {
            Close();
        }
        else
        {
            resultText.Foreground = Brushes.Red;
            resultText.Text = message;
        }
    }

    private void OnCancelClicked(object? sender, RoutedEventArgs e) => Close();
}
