// Copyright © Selmo Technology GmbH. All Rights Reserved

using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Localization;
using SelmoStudio.Hmi.Common.SelmoProject.Ids;
using SelmoStudio.Hmi.Server.SelmoHmi.Model.Dtos;
using SelmoStudio.Hmi.Server.SelmoHmi.Sequence.Model.Dto;
using SelmoStudio.Hmi.Server.SelmoHmi.Sequence.Model.Ids;
using SelmoStudio.Hmi.Server.Shared.Resources;

namespace SelmoStudio.Hmi.Server.SelmoHmi.Sequence.Components
{
    public partial class SequenceShowStepComponent
    {
        [Inject]
        private IStringLocalizer<HmiResource> _localizer { get; set; }

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

        [Parameter]
        public StepCollectionDto StepCollection { get; set; }

        [Parameter]
        public Dictionary<SelmoHmiStepId, int> StepOrder { get; set; }

        [Parameter]
        public SequenceId SequenceId { get; set; }

        [Parameter]
        public int CurrentStepNumber { get; set; } = -1;

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

        [Parameter]
        public string WaitingForColor { get; set; } = string.Empty;

        [Parameter]
        public List<ZoneDto> ActiveOutputZones { get; set; }

        public int _currentStepNumber { get; set; } = 0;

        #region Init/Lifecycle

        protected override async Task OnParametersSetAsync()
        {
            if (!_currentStepNumber.Equals(CurrentStepNumber))
            {
                _currentStepNumber = CurrentStepNumber;
            }

            //TestCode();

            StateHasChanged();
        }

        #endregion Init/Lifecycle

        private string GetCorrectStepName(int count)
        {
            var stepId = StepOrder.FirstOrDefault(x => x.Value == count).Key;
            if (stepId == null || stepId.Equals(Guid.Empty))
                return "No step available";
            var correctStep = StepCollection.Steps.FirstOrDefault(s => s.Id.Guid.Equals(stepId.Guid));
            if (correctStep == null)
                return "No step available";

            if (!string.IsNullOrEmpty(correctStep.HmiDisplayText))
                return $"{count}: {correctStep.HmiDisplayText}";

            return $"{count}: {correctStep.Name}";
        }

        private string GetPreviousStep()
        {
            if (StepCollection == null)
                return "";
            return _currentStepNumber <= 1
                ? $"{_localizer[nameof(HmiResource.NoPreviousStep)]}" : $"{GetCorrectStepName(_currentStepNumber - 1)}";
        }

        private string GetCurrentStep()
        {
            if (StepCollection == null)
                return "";
            return _currentStepNumber > StepCollection.Steps.Count
                ? $"{_localizer[nameof(HmiResource.NoCurrentStep)]}" : $"{GetCorrectStepName(_currentStepNumber)}";
        }

        private string GetNextStep()
        {
            if (StepCollection == null)
                return "";

            var nextStepNumber = _currentStepNumber + 1;
            if (_currentStepNumber + 1 > StepCollection.Steps.Count)
                nextStepNumber = 1;

            var stepWaitingForMessage = GetCorrectStepName(nextStepNumber);
            return stepWaitingForMessage;
        }
    }
}