// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using Novus.Connectivity.Common;
using SelmoStudio.Hmi.Common.SelmoProject.ProcessValue;
using SelmoStudio.Hmi.Server.ProcessValue;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Component;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Component.CustomArgs;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Component.Ids;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.Component.Types;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Sequence.Components
{
    public partial class SequenceStepTimeRowComponent
    {
        private ComponentId ComponentId { get; set; } = new(Guid.NewGuid());

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

        [Parameter]
        public List<IProcessValue> StepTimeMonitoringProcessValues { get; set; } = new();

        [Parameter]
        public ProcessValueMonitoringStep StepTimeMonitoringProcessValuesInfo { get; set; } = new();

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

        [Parameter]
        public EventHandler<ComponentEventArgs> ProcessValueChanged { get; set; }

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

        #region Init/Lifecycle

        public SequenceStepTimeRowComponent()
        {
        }

        #endregion Init/Lifecycle

        private string GetValueForProcessValueInfo(IProcessValueInfo info)
        {
            if (StepTimeMonitoringProcessValues == null)
                return "";

            try
            {
                var correctInfo = StepTimeMonitoringProcessValues.FirstOrDefault(s => s.ValueInfo.Id.Guid.Equals(info.Id.Guid));
                if (correctInfo == null)
                    return "";
                return correctInfo.Value.ToString();
            }
            catch (Exception ex)
            {
                return "";
            }
        }

        private bool GetProcessValue(IProcessValueInfo info)
        {
            if (StepTimeMonitoringProcessValues == null)
                return false;

            var correctInfo = StepTimeMonitoringProcessValues.FirstOrDefault(s => s.ValueInfo.Id.Guid.Equals(info.Id.Guid));
            if (correctInfo == null)
                return false;
            return Convert.ToBoolean(correctInfo.Value);
        }

        private void ChangedValue(IProcessValueInfo info, TextBoxEventArgs e)
        {
            var handleInfo = new ProcessValueHandleInfo()
            {
                //TODO after fix studio site ProcessValueConverter.Convert(info, e.Value),
                Value = ProcessValueConverter.Convert(typeof(uint), e.Value),
                ProcessValueInfo = info,
                ProcessValueHandle = ProcessValueHandle.TextInput
            };
            InvokeEvent(handleInfo, EventType.TextFieldChange, HmiElementType.TextBox);
        }

        private void ChangedValueBool(IProcessValueInfo info, bool value)
        {
            var handleInfo = new ProcessValueHandleInfo()
            {
                Value = value,
                ProcessValueInfo = info,
                ProcessValueHandle = ProcessValueHandle.Switch
            };
            InvokeEvent(handleInfo, EventType.SwitchChange, HmiElementType.Switch);
        }

        private void InvokeEvent(ProcessValueHandleInfo info, EventType eventType, HmiElementType buttonType)
        {
            ComponentEventInfo eventInfo = new()
            {
                ComponentId = this.ComponentId,
                Type = eventType,
                ButtonType = buttonType
            };
            ProcessValueChanged?.Invoke(this, ComponentEventArgs.Create(eventInfo, info));
        }
    }
}