using System;
using System.Collections;
using Intrepid.GameManagers;
using Intrepid.Gameplay.Codex.UI.Components;
using Intrepid.Gameplay.Codex.UI.Controllers;
using Intrepid.Gameplay.Codex.Visual;
using Intrepid.Utilities;
using UnityEngine;

namespace Intrepid.Gameplay.Entities.Player.ActionBelt.UI
{
    public class CodexActionBeltControllerUI : CodexControllerUI
    {
        [GroupConfigurations, SerializeField] private float animationDuration = 0.5f;
        [GroupConfigurations, SerializeField] private float animationSpeed = 4.5f;

        [GroupInjections, SerializeField] private CodexPanelComponentUI panel;
        [GroupInjections, SerializeField] private CodexIconComponentUI icon;
        [GroupInjections, SerializeField] private CodexTextComponentUI text;
        
        private Coroutine BlinkRoutine { get; set; }
        
        private static string MapActionCategoryRUNNING(ActionCategory actionCategory)
        {
            return actionCategory switch
            {
                ActionCategory.General => "General RUNNING",
                ActionCategory.Terminal => "Terminal RUNNING",
                _ => throw new ArgumentOutOfRangeException(nameof(actionCategory), actionCategory, null)
            };
        }
        
        public void BlinkBorderActionTaken()
        {
            if (BlinkRoutine is not null)
            {
                StopCoroutine(BlinkRoutine);
            }
            BlinkRoutine = StartCoroutine(BlinkBorderCoroutine(PictorialSignalType.Focus));
        }
        
        private void BlinkBorderSelected()
        {
            if (BlinkRoutine is not null)
            {
                StopCoroutine(BlinkRoutine);
            }
            BlinkRoutine = StartCoroutine(BlinkBorderCoroutine(PictorialSignalType.Selected));
        }
        
        private void BlinkBorderDropped()
        {
            if (BlinkRoutine is not null)
            {
                StopCoroutine(BlinkRoutine);
            }
            BlinkRoutine = StartCoroutine(BlinkBorderCoroutine(PictorialSignalType.Denied));
        }
        
        private IEnumerator BlinkBorderCoroutine(PictorialSignalType target)
        {
            var previous = panel.BorderType;
            panel.SetBorder(target);            
            
            float time = 0;
            while (time < animationDuration)
            {
                yield return null;
                time += Timer.UnscaledTime * animationSpeed;
            }
            
            panel.SetBorder(previous);
            BlinkRoutine = null;
        }

        public void UpdateContent(ActionEnvelope envelope)
        {
            icon.ChangeAnimation(MapActionCategoryRUNNING(envelope.ActionCategory));
            text.Value = Proxy.Instance.I18N.Localise(envelope.ActionLocalization);
            // if (envelope.DescriptionLocalization.IsNull())
            // {
            //     local.gameObject.SetActive(false);    
            // }
            // else
            // {
            //     local.gameObject.SetActive(true);
            //     var message = Proxy.Instance.I18N.Localise(envelope.DescriptionLocalization, envelope.DescriptionParams);
            //     localNewsTicker.Operational(message);
            // }
        }

        public void ActionTaken()
        {
            BlinkBorderActionTaken();
            // if (local.gameObject.activeSelf) local.BlinkBorderActionTaken();
        }
        
        public void Selected()
        {
            BlinkBorderSelected();
            // if (local.gameObject.activeSelf) local.BlinkBorderSelected();

        }

        public void Drop()
        {
            BlinkBorderDropped();
            // if (local.gameObject.activeSelf) local.BlinkBorderDropped();
        }

        public void Reset()
        {
            icon.AnimationReset();
        }
    }
}