namespace MinicsUI.AlarmHandler;

public class AlarmRealtime(Hardwares hardwares, Alarms alarms)
{
    public bool InsertAlarm(byte mini8Index, byte channelIndex, double current)
    {
        if (!hardwares.Mini8s.TryGetValue(mini8Index, out Mini8Info? mini8) || mini8 is null)
            return false;

        if (!hardwares.Mini8Channels.TryGetValue(mini8Index, out var channels) || channels is null)
            return false;

        if (!channels.TryGetValue(channelIndex, out Channel? channel) || channel is null)
            return false;

        AlarmInfo alarmInfo = new()
        {
            Mini8Index = mini8.Index,
            EventTime = DateTime.Now,
            AlarmLevel = AlarmLevel.Error,
            AlarmMini8 = mini8.Name,
            AlarmChannel = channel.Name,
            ChannelName = channel.Name,
            ChannelIndex = channel.ChannelIndex,
            AlarmDetail = $"Current Temperature: {current:0.00}℃   Caps: {channel.Caps}℃  Floor: {channel.Floor}℃"
        };
        try
        {
            Application.Current?.Dispatcher?.Invoke(() =>
            {
                if (alarms is null)
                    return;
                if (!alarms.DisplayAlarm.ContainsKey(mini8.Index))
                    alarms.DisplayAlarm[mini8.Index] = [];
                alarms.DisplayAlarm[mini8.Index][channel.ChannelIndex] = alarmInfo;
            });
        }
        catch
        {

        }

        return true;
    }

    public bool CancelAlarm(byte mini8, byte channel)
    {
        try
        {
            if (Application.Current is null)
                return false;
            Application.Current.Dispatcher.Invoke(() =>
            {
                if (!alarms.DisplayAlarm.TryGetValue(mini8, out var channels) || channels is null)
                    return;

                channels.Remove(channel);

                if (channels.Count == 0)
                    alarms.DisplayAlarm.Remove(mini8);
            });
        }
        catch
        {

        }
        return true;
    }


    public bool InsertTcBrocken(byte mini8Index, byte channelIndex)
    {
        if (!hardwares.Mini8s.TryGetValue(mini8Index, out Mini8Info? mini8) || mini8 is null)
            return false;

        if (!hardwares.Mini8Channels.TryGetValue(mini8Index, out var channels) || channels is null)
            return false;

        if (!channels.TryGetValue(channelIndex, out Channel? channel) || channel is null)
            return false;

        AlarmInfo alarmInfo = new()
        {
            Mini8Index = mini8.Index,
            EventTime = DateTime.Now,
            AlarmLevel = AlarmLevel.Error,
            AlarmMini8 = mini8.Name,
            AlarmChannel = channel.Name,
            ChannelName = channel.Name,
            ChannelIndex = channel.ChannelIndex,
            AlarmDetail = $"Tc Broken"
        };

        Application.Current.Dispatcher.Invoke(() =>
        {
            if (!alarms.DisplayAlarm.ContainsKey(mini8.Index))
                alarms.DisplayAlarm[mini8.Index] = [];
            alarms.DisplayAlarm[mini8.Index][channel.ChannelIndex] = alarmInfo;
        });

        return true;
    }
}
