// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Model;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.WellKnownConstants;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Shared.Component
{
    public partial class StatusComponent
    {
        [Parameter]
        public List<string> Icons { get; set; } = new();

        [Parameter]
        public OperationMode Led { get; set; }

        protected override void OnParametersSet()
        {
            StateHasChanged();
        }

        private string GetIconPriority()
        {
            var icon = Icons.FirstOrDefault(i => !i.Contains("ghost"));
            if (icon == null)
            {
                return "";
            }
            return icon;
        }

        private string GetGhostIcon()
        {
            var icon = Icons.FirstOrDefault(i => i.Contains("ghost"));
            if (icon != null)
            {
                return icon;
            }
            return "";
        }

        private string GetStatusIconStyle()
        {
            return "height: 100%; align-content: center; align-self: center;";
        }

        private bool _blinkOn;

        private string GetStatusContainerStyle()
        {
            var baseStyle = "margin-left:6px; min-width:30px; border-radius:4px; height:100%;";

            if (Led.Equals(OperationMode.Error))
            {
                baseStyle += $"background-color:{WellKnownStyleConstants.ErrorColor}";
            }
            else if (Led.Equals(OperationMode.AutomationSelected))
            {
                if (_blinkOn)
                {
                    baseStyle += $"background-color:{WellKnownStyleConstants.AutomationColor}";
                }
                else
                {
                    baseStyle += $"background-color:{WellKnownStyleConstants.ManualColor}";
                }
                _blinkOn = !_blinkOn;
            }
            else if (Led.Equals(OperationMode.Automation))
            {
                baseStyle += $"background-color:{WellKnownStyleConstants.AutomationColor}";
            }
            else if (Led.Equals(OperationMode.Manual))
            {
                baseStyle += $"background-color:{WellKnownStyleConstants.ManualColor}";
            }
            else if (Led.Equals(OperationMode.None))
            {
                baseStyle += "background-color:light-gray";
            }
            return baseStyle;
        }
    }
}