// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using Novus.Connectivity.Common;
using SelmoStudio.Hmi.Common.SelmoProject;
using SelmoStudio.Hmi.Common.SelmoProject.ProcessValue;
using SelmoStudio.Hmi.Server.ProcessValue.Services;
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.Service;
using SelmoStudio.Hmi.Server.SelmoHmi.Shared.WellKnownTypes;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Plant.Components
{
    public partial class GlobalOptionsComponent : IBaseProcessValueComponent, IAsyncDisposable
    {
        private WellKnownContentFlexDirection _contentFlexDirection = WellKnownContentFlexDirection.Column;

        [Inject]
        private IProjectService _projectService { get; set; }

        private HmiSettings _hmiSettings => _projectService.GetHmiSettings();

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

        public bool ShowGlobalSequenceStepReset { get => _hmiSettings.ShowGlobalSequenceStepReset; }

        [Inject]
        private IProcessValueInfoService _processValueInfoService { get; set; }

        private ProcessValuesPlant _plantValues;

        private bool _isRendered;

        public bool IsRendered
        {
            get
            {
                return _isRendered;
            }
        }

        public event EventHandler<ComponentEventArgs> ComponentChangeEvent;

        protected override void OnInitialized()
        {
        }

        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
            {
                _isRendered = true;
            }
        }

        public List<IProcessValueInfo> GetValueInfos()
        {
            _plantValues = _processValueInfoService.PlantProcessInfo;

            var values = _processValueInfoService.CreatePlantRegistration().ValueInfos;
            var valueList = values.ToList();
            return valueList;
        }

        public void Update(IDictionary<ProcessValueId, IProcessValue> updatedValues)
        {
        }

        public void TriggerComponentChangeEvent(ButtonEventArgs e, ProcessValueId id, bool val)
        {
            if (e == null)
                return;
            var processValue = GetValueInfo(id);
            if (processValue == null)
                return;

            var processValueHandle = new ProcessValueHandleInfo()
            {
                ProcessValueHandle = ProcessValueHandle.SetValue,
                ProcessValueInfo = processValue,
                Value = val
            };

            ComponentChangeEvent?.Invoke(this, ComponentEventArgs.Create(e, processValueHandle));
        }

        public void TriggerStepResetChangeEvent(ButtonEventArgs e, ProcessValuesPlant plant, bool val)
        {
            if (e == null)
                return;

            foreach (var hwz in plant.HardwareZones)
            {
                foreach (var seq in hwz.Value.Sequences)
                {
                    var pvId = seq.Value.TriggerStepReset.Id;

                    var processValue = GetValueInfo(pvId);
                    if (processValue == null)
                        return;

                    var processValueHandle = new ProcessValueHandleInfo()
                    {
                        ProcessValueHandle = ProcessValueHandle.SetValue,
                        ProcessValueInfo = processValue,
                        Value = val
                    };

                    ComponentChangeEvent?.Invoke(this, ComponentEventArgs.Create(e, processValueHandle));
                }
            }
        }

        private IProcessValueInfo GetValueInfo(ProcessValueId id)
        {
            if (_plantValues.GlobalReset.Id.Equals(id))
                return _plantValues.GlobalReset;
            if (_plantValues.LampTest.Id.Equals(id))
                return _plantValues.LampTest;
            if (_plantValues.GlobalAutomaticRelease.Id.Equals(id))
                return _plantValues.GlobalAutomaticRelease;
            if (_plantValues.GlobalSequenceStepReset.Id.Equals(id))
                return _plantValues.GlobalSequenceStepReset;

            foreach (var hwz in _plantValues.HardwareZones)
            {
                foreach (var seq in hwz.Value.Sequences)
                {
                    var pvId = seq.Value.TriggerStepReset.Id;

                    if (pvId.Equals(id))
                        return seq.Value.TriggerStepReset;
                }
            }

            return null;
        }

        public async ValueTask DisposeAsync()
        {
            //await BaseDisposeAsync();
        }
    }
}