using ITNexusAgent.Models;
using ITNexusAgent.Services;
using System.Windows;
using System.Windows.Media;

namespace ITNexusAgent.UI;

public partial class NotificationWindow : Window
{
    private readonly Announcement _ann;
    private readonly ApiService? _api;

    public NotificationWindow(Announcement ann)
    {
        InitializeComponent();
        _ann = ann;

        var (hex, label, icon) = ann.Type switch
        {
            "warning"     => ("#DC3545", "WICHTIGE WARNUNG",    "⚠️"),
            "maintenance" => ("#E67E22", "WARTUNGSANKÜNDIGUNG",  "🔧"),
            _             => ("#5865F2", "INFORMATION",          "ℹ️"),
        };

        var color = (System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString(hex);
        var brush = new SolidColorBrush(color);
        var iconBg = new SolidColorBrush(System.Windows.Media.Color.FromArgb(40, color.R, color.G, color.B));

        TopAccent.Background   = brush;
        IconBox.Background     = iconBg;
        IconBox.BorderBrush    = new SolidColorBrush(System.Windows.Media.Color.FromArgb(80, color.R, color.G, color.B));
        IconBox.BorderThickness = new Thickness(1);
        IconText.Text          = icon;
        TypeLabel.Text         = label;
        TypeLabel.Foreground   = brush;
        TitleText.Text         = ann.Title;
        MessageText.Text       = ann.Message;
        ConfirmButton.Background = brush;
        MetaText.Text          = $"IT Nexus Mitteilung";

        try
        {
            var cfg = AgentConfig.Load(@"C:\ProgramData\IT Nexus Agent\config.json");
            _api = new ApiService(cfg.ServerUrl, cfg.AgentKey);
        }
        catch { }
    }

    private async void ConfirmButton_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            if (_api != null)
                await _api.AckAnnouncementAsync(_ann.Id, Environment.MachineName);
        }
        catch { }
        Close();
    }
}
