// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using SelmoStudio.Hmi.Server.Alarms.Model;
using SelmoStudio.Hmi.Server.Alarms.Service;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.WellKnownTypes;

namespace SelmoStudio.Hmi.Server.Alarms.Pages
{
    public partial class CurrentProcessAlarms
    {
        private IList<Alarm> _alarms = new List<Alarm>();

        private WellKnownContentFlexDirection _contentFlexDirection = WellKnownContentFlexDirection.Column;

        [Inject]
        private IAlarmService _alarmService { get; set; }

        protected override Task OnInitializedAsync()
        {
            _alarmService.OnUpdate += async () => await OnUpdate();

            return base.OnInitializedAsync();
        }

        protected override async Task OnParametersSetAsync()
        {
            await UpdateAlarmList();

            await base.OnParametersSetAsync();
        }

        private async Task OnUpdate()
        {
            await UpdateAlarmList();

            await InvokeAsync(() => StateHasChanged());
        }

        private async Task UpdateAlarmList()
        {
            _alarms.Clear();
            _alarms = await _alarmService.GetActive();
        }

        #region TestData

        private void AddTestAlarm()
        {
            Alarm rndAlarm = CreateRandomTestAlarm();

            _alarmService.Add(rndAlarm);
        }

        private static Dictionary<int, Severity> _severities = new()
        {
            {1 , Severity.Info },
            {2 , Severity.Warning },
            {3 , Severity.Error },
            {4 , Severity.Fatal },
        };

        private int _severityCount = 1;

        private Alarm CreateRandomTestAlarm()
        {
            Alarm test = new();

            test.Id = Guid.NewGuid();
            test.Title = "Test Title";
            test.Message = "Test Message bla bla lorem ipsum";
            test.Created = DateTimeOffset.Now;
            test.NavigateUrl = "selmoDiagnose";
            _severityCount++;

            if (_severityCount >= 4)
                _severityCount = 1;

            test.Severity = _severities[_severityCount];

            return test;
        }

        #endregion TestData
    }
}