// Copyright © Selmo Technology GmbH. All Rights Reserved

using Novus.Shared.Types.IssueHandling;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.WellKnownConstants;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.WellKnownTypes;
using System.Text;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Helpers
{
    internal class HtmlStyleHelper
    {
        internal static string GetToggleButtonBackgroundColor(bool isActive)
        {
            return isActive ? WellKnownStyleConstants.ActiveToggleButtonBackgroundColor
                : WellKnownStyleConstants.InactiveToggleButtonBackgroundColor;
        }

        internal static string GetSequenceOverviewContainerStyle()
        {
            return GetLargeElementHeightStyle();
        }

        internal static string GetGroupsContainerStyle()
        {
            return GetLargeElementHeightStyle();
        }

        internal static string GetGroupsLabelContainerStyle()
        {
            return GetSmallElementHeightStyle();
        }

        internal static string GetParameterContainerStyle()
        {
            return $"{GetMediumElementHeightStyle()} align-items: center;";
        }

        internal static string GetAutomaticModeContainerStyle()
        {
            return GetLargeElementHeightStyle();
        }

        private static string GetLargeElementHeightStyle()
        {
            return $"height: {WellKnownStyleConstants.LargeElementHeight}px;";
        }

        private static string GetExtraLargeElementHeightStyle()
        {
            return $"height: {WellKnownStyleConstants.ExtraLargeElementHeight}px;";
        }

        private static string GetMediumElementHeightStyle()
        {
            return $"height: {WellKnownStyleConstants.MediumElementHeight}px;";
        }

        private static string GetSmallElementHeightStyle()
        {
            return $"height: {WellKnownStyleConstants.SmallElementHeight}px;";
        }

        internal static string GetControlGroupsHeadlineStyle()
        {
            return $"{GetMediumElementHeightStyle()} gap:{WellKnownStyleConstants.GlobalSpacing / 2}px; align-items: center;";
        }

        internal static string GetControlGroupsHeadlineLabelStyle()
        {
            return "width: max-content;";
        }

        internal static string GetOverviewButtonClass(bool isAccent)
        {
            return $"{(WellKnownStyleConstants.ResponsiveItemWidthClass)} {(isAccent ? "button-accent-background medium" : "button-background medium")}";
        }

        internal static string GetManualControlClassString()
        {
            return WellKnownStyleConstants.ResponsiveItemWidthClass;
        }

        internal static string GetSidePanelClass()
        {
            return "col-md-3 col-xl-2 column-padding dark-gray-background";
        }

        internal static string GetContentPanelClass(bool showGlobalOptions)
        {
            return (showGlobalOptions ? "col-md-7 col-xl-8" : "col-md-9 col-xl-10") + " d-flex flex-column p-0";
        }

        internal static string GetGlobalOptionsPanelClass(bool showGlobalOptions)
        {
            return showGlobalOptions ? "col-md-2 col-xl-2" : "";
        }

        internal enum HardwareZoneButtonColor
        {
            Foreground,
            Background
        }

        internal static string GetHardwareZoneButtonColor(bool isCurrentZone, HardwareZoneButtonColor hardwareZoneButtonColor)
        {
            return hardwareZoneButtonColor switch
            {
                HardwareZoneButtonColor.Background => isCurrentZone ? WellKnownStyleConstants.CurrentHardwareZoneButtonBackgroundColor
                    : WellKnownStyleConstants.HardwareZoneButtonBackgroundColor,
                HardwareZoneButtonColor.Foreground => isCurrentZone ? WellKnownStyleConstants.CurrentHardwareZoneButtonForegroundColor
                    : WellKnownStyleConstants.HardwareZoneButtonForegroundColor,
                _ => throw new NotImplementedException("Hardware zone button color is not supported")
            };
        }

        internal static string GetContentInfoScrollableContainerStyle()
        {
            return "flex-grow: 1; overflow-y: auto; height: 0;";
        }

        internal enum StepDisplayType
        {
            Previous,
            Current,
            Next,
            WaitingFor
        }

        internal static string GetStepStyleString(StepDisplayType stepDisplayType, string color = "lighgray")
        {
            StringBuilder sb = new StringBuilder();

            var heightStyle = GetLargeElementHeightStyle();
            var backgroundStyle = "background-color ";

            switch (stepDisplayType)
            {
                case StepDisplayType.Previous:
                    sb.Append(GetLargeElementHeightStyle()).AppendLine($" {backgroundStyle}: {WellKnownStyleConstants.PreviousStepColor}");

                    break;

                case StepDisplayType.Current:
                    sb.Append(GetLargeElementHeightStyle()).AppendLine($" {backgroundStyle}: {WellKnownStyleConstants.CurrentStepColor}");
                    break;

                case StepDisplayType.Next:
                    sb.Append(GetExtraLargeElementHeightStyle()).AppendLine($" {backgroundStyle}: {WellKnownStyleConstants.NextStepColor}");
                    break;

                case StepDisplayType.WaitingFor:
                    sb.Append(GetExtraLargeElementHeightStyle()).AppendLine($" {backgroundStyle}:  {color}");
                    break;

                default:
                    throw new NotSupportedException("Step display type is not supported");
            }

            return sb.ToString();
        }

        internal static string GetActiveOutputZoneStyleString()
        {
            var heightStyle = GetExtraLargeElementHeightStyle();
            var backgroundStyle = $"background-color: {WellKnownStyleConstants.ActiveOutZoneButtonBackgroundColor}";

            return $"{heightStyle} {backgroundStyle}";
        }

        internal static string GetSeverityStyleAttributeContent(Severity severity, bool useConditionalFormatting)
        {
            if (useConditionalFormatting)
            {
                if (severity == Severity.Warning)
                {
                    return GetColorStyleAttributes($"{WellKnownStyleConstants.IssueWarningColor}");
                }
                else if (severity == Severity.Error)
                {
                    return GetColorStyleAttributes($"{WellKnownStyleConstants.IssueErrorColor}");
                }
                else if (severity == Severity.Fatal)
                {
                    return GetColorStyleAttributes($"{WellKnownStyleConstants.IssueFatalColor}");
                }
            }

            return GetColorStyleAttributes($"{WellKnownStyleConstants.IssueUnformattedColor}");
        }

        private static string GetColorStyleAttributes(string color)
        {
            return $"color: {color};";
        }

        internal static string GetMarginForUnknownErrors(bool showMargin)
        {
            return showMargin ? $"{WellKnownStyleConstants.UnknownErrorMarginSize}px" : "0px";
        }

        internal static string GetButtonIconTextMargin(WellKnownContentFlexDirection contentFlexDirection, WellKnownIconPosition iconPosition,
            int iconMargin)
        {
            var rowMargin = iconPosition == WellKnownIconPosition.Left ?
                $"margin-right: {iconMargin}px;" : $"margin-left: {iconMargin}px;";

            var columnMargin = iconPosition == WellKnownIconPosition.Left ?
                $"margin-bottom: {iconMargin}px;" : $"margin-top: {iconMargin}px;";

            return contentFlexDirection == WellKnownContentFlexDirection.Row ? rowMargin : columnMargin;
        }

        internal static string GetButtonContainerStyle(WellKnownContentFlexDirection contentFlexDirection)
        {
            return $"flex-direction: {contentFlexDirection}; align-items: center; justify-content: center; flex: 1; line-height: 1rem;";
        }

        internal static string GetWidthStyle(int width, WellKnownLengthUnits widthUnit)
        {
            return $"{(widthUnit == WellKnownLengthUnits.Auto ? $"width: {widthUnit.Symbol()}; " : width > -1 ? $"width: {width}{widthUnit.Symbol()}; " : string.Empty)}";
        }

        internal static string GetHeightStyle(int height, WellKnownLengthUnits heightUnit)
        {
            return $"{(heightUnit == WellKnownLengthUnits.Auto ? $"height: {heightUnit.Symbol()};" : height > -1 ? $"height: {height}{heightUnit.Symbol()};" : string.Empty)}";
        }

        internal static string GetPositionStyle(int? x, int? y) =>
            (x.HasValue && y.HasValue) ? $"position: absolute; left: {x}px; top: {y}px;" : string.Empty;

        internal static string GetZIndexStyleString(int zIndex)
        {
            return $"{(zIndex > -1 ? $"z-index: {zIndex}; " : string.Empty)}";
        }

        internal static string GetPositionClass() => $"p-0 m-0";

        internal static string GetControlStyle(string color, string backgroundColor, int padding, int margin, int outlineWidth, string outlineStyle, string outlineColor,
            int borderWidth, string borderStyle, string borderColor, string additionalStyleString)
        {
            return $"{(!string.IsNullOrEmpty(color) ? $"color: {color}; " : string.Empty)}" +
            $"{(!string.IsNullOrEmpty(backgroundColor) ? $"background-color: {backgroundColor}; " : string.Empty)}" +
            $"{(padding > -1 ? $"padding: {padding}px; " : string.Empty)}{(margin > -1 ? $"margin: {margin}px; " : string.Empty)}" +
            $"{(outlineWidth > -1 ? $"outline-width: {outlineWidth}px; " : string.Empty)}" +
            $"{(!string.IsNullOrEmpty(outlineStyle) ? $"outline-style: {outlineStyle}; " : string.Empty)}" +
            $"{(!string.IsNullOrEmpty(outlineColor) ? $"outline-color: {outlineColor}; " : string.Empty)}" +
            $"{(borderWidth > -1 ? $"border-width: {borderWidth}px; " : string.Empty)}" +
            $"{(!string.IsNullOrEmpty(borderStyle) ? $"border-style: {borderStyle}; " : string.Empty)}" +
            $"{(!string.IsNullOrEmpty(borderColor) ? $"border-color: {borderColor}; " : string.Empty)}" +
            $"{(!string.IsNullOrEmpty(additionalStyleString) ? $"{additionalStyleString}" : string.Empty)}";
        }

        internal static string GetControlWithLabelStyle(int fontSize, string fontFamily, string labelStyleString)
        {
            return $"{(fontSize > 0 ? $"font-size: {fontSize}px; " : string.Empty)} " +
            $"{(!string.IsNullOrEmpty(fontFamily) ? $"font-family: {fontFamily}; " : string.Empty)}" +
            $"{(!string.IsNullOrEmpty(labelStyleString) ? $"{labelStyleString}" : string.Empty)}";
        }

        internal static string GetDropDownComponentStyle()
        {
            return $"min-height: {WellKnownStyleConstants.MediumElementHeight}px; line-height: {WellKnownStyleConstants.MediumElementHeight}px;";
        }

        internal static string GetNotificationMessageStyle()
        {
            return "width: 100%; height: 10%; opacity: 0.5; text-align: center; position: fixed; z-index: 99;left: 0; bottom: 0; top: 90%;";
        }

        internal static string GetSwitchLabelGapStyle()
        {
            return "width: 5px; height: auto;";
        }

        internal static string GetSequenceBackgroundColor(bool isCurrent, bool showOverview)
        {
            return isCurrent && !showOverview ? WellKnownStyleConstants.CurrentSequenceButtonBackgroundColor
                : WellKnownStyleConstants.SequenceButtonBackgroundColor;
        }

        internal static string GetResponsiveWidthItemContainerClass()
        {
            return WellKnownStyleConstants.ResponsiveItemWidthClass;
        }

        internal static string GetPaginationItemClasses()
        {
            return $"{WellKnownStyleConstants.PaginationItemTextWrappingClass} heavy-font-weight button-label";
        }

        internal static string GetPaginationItemContainerStyle()
        {
            return "width: 100%";
        }

        internal static string GetSequenceSelectionStyle()
        {
            return $"gap: {WellKnownStyleConstants.GlobalSpacing}px;";
        }

        internal static string GetAlarmListBackgroundColor(Alarms.Model.Severity severity, bool adjustOpacity = true)
        {
            return $"Background-color: {GetAlarmSeverityColor(severity)}{(adjustOpacity ? WellKnownStyleConstants.AlarmSeverityOpacity : "")}";
        }

        private static string GetAlarmSeverityColor(Alarms.Model.Severity severity)
        {
            return severity switch
            {
                Alarms.Model.Severity.None => WellKnownStyleConstants.AlarmSeverityNoneColor,
                Alarms.Model.Severity.Info => WellKnownStyleConstants.AlarmSeverityInfoColor,
                Alarms.Model.Severity.Warning => WellKnownStyleConstants.AlarmSeverityWarningColor,
                Alarms.Model.Severity.Error => WellKnownStyleConstants.AlarmSeverityErrorColor,
                Alarms.Model.Severity.Fatal => WellKnownStyleConstants.AlarmSeverityFatalColor,
                _ => throw new NotSupportedException($"Severity {nameof(severity)} is not supported")
            };
        }

        internal static string GetDefaultButtonBackgroundColor()
        {
            return WellKnownStyleConstants.DefaultButtonBackgroundColor;
        }

        internal static string GetActiveInOutZoneButtonBackgroundColor()
        {
            return WellKnownStyleConstants.ActiveInOutZoneButtonBackgroundColor;
        }

        internal static string GetActiveOutZoneButtonBackgroundColor()
        {
            return WellKnownStyleConstants.ActiveOutZoneButtonBackgroundColor;
        }

        internal static string GetButtonBaseStyle()
        {
            return $"display: flex; align-items: center; justify-content: center;";
        }

        internal static string GetFlashingButtonStyle()
        {
            return $"animation: flashOpacity 1s infinite;";
        }
    }
}