// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using SelmoStudio.Hmi.Server.SelmoHmi.Helpers;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Component.Ids;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.WellKnownTypes;

namespace SelmoStudio.Hmi.Server.Shared.Components.BaseComponents
{
    public partial class ComponentBase : Microsoft.AspNetCore.Components.ComponentBase, IComponent
    {
        [Parameter]
        public ComponentId ComponentId { get; set; } = new(Guid.NewGuid());

        [Parameter]
        public string Id { get; set; }

        [Parameter]
        public int Width { get; set; }

        [Parameter]
        public int Height { get; set; }

        [Parameter]
        public WellKnownLengthUnits WidthUnit { get; set; }

        [Parameter]
        public WellKnownLengthUnits HeightUnit { get; set; }

        [Parameter]
        public bool Disabled { get; set; }

        [Parameter]
        public bool ReadOnly { get; set; }

        [Parameter]
        public string Color { get; set; }

        [Parameter]
        public string BackgroundColor { get; set; }

        [Parameter]
        public int Padding { get; set; }

        [Parameter]
        public int Margin { get; set; }

        [Parameter]
        public int OutlineWidth { get; set; }

        [Parameter]
        public string OutlineStyle { get; set; }

        [Parameter]
        public string OutlineColor { get; set; }

        [Parameter]
        public int BorderWidth { get; set; }

        [Parameter]
        public string BorderStyle { get; set; }

        [Parameter]
        public string BorderColor { get; set; }

        [Parameter]
        public string StyleString { get; set; }

        [Parameter]
        public string ClassString { get; set; }

        [Parameter]
        public string ContainerStyleString { get; set; }

        [Parameter]
        public int? X { get; set; }

        [Parameter]
        public int? Y { get; set; }

        [Parameter]
        public int ZIndex { get; set; }

        private string _widthStyle => @HtmlStyleHelper.GetWidthStyle(Width, WidthUnit);

        private string _heightStyle => @HtmlStyleHelper.GetHeightStyle(Height, HeightUnit);

        protected string DimensionStyle => $"{_widthStyle} {_heightStyle}";

        protected string ControlClass => $"{(!string.IsNullOrEmpty(ClassString) ? ClassString : string.Empty)}";

        protected string ControlStyle => HtmlStyleHelper.GetControlStyle(Color, BackgroundColor, Padding, Margin, OutlineWidth, OutlineStyle,
            OutlineColor, BorderWidth, BorderStyle, BorderColor, StyleString);

        protected Dictionary<string, object> GetContainerAttributes()
        {
            var attributes = new Dictionary<string, object>();

            var positionClass = HtmlStyleHelper.GetPositionClass();
            var positionStyle = HtmlStyleHelper.GetPositionStyle(X, Y);
            var zIndexStyle = HtmlStyleHelper.GetZIndexStyleString(ZIndex);
            var elementStyle = $"{(string.IsNullOrEmpty(positionStyle) ? "" : positionStyle)}"
                + $"{(string.IsNullOrEmpty(positionStyle) || string.IsNullOrEmpty(zIndexStyle) ? "" : $" {zIndexStyle}")}"
                    + $"{(string.IsNullOrEmpty(ContainerStyleString) ? "" : $" {ContainerStyleString}")}";

            if (!string.IsNullOrEmpty(positionClass))
            {
                attributes["class"] = positionClass;
            }

            if (!string.IsNullOrEmpty(elementStyle))
            {
                attributes["style"] = elementStyle;
            }

            return attributes;
        }
    }
}