using System;
using Core.Domain;
using Core.Domain.Status.Effects;
using KBCore.Refs;
using UnityEngine;

namespace Infrastructure.Unity
{
    public class PowerUpViewAdapter : MonoBehaviour
    {
        [SerializeField] private PowerUpType type;
        [SerializeField] private float duration = 10f;
        [SerializeField] private ParticleSystem pickupVfx;

        [Self][SerializeField] private MeshRenderer meshRenderer;

        private MaterialPropertyBlock _propBlock;
        private static readonly int ColorProperty = Shader.PropertyToID("_BaseColor");

        public event Action<PowerUpType, float> OnCollected;

        private void Awake()
        {
            _propBlock = new MaterialPropertyBlock();
            ConfigureVisuals();
        }

        private void ConfigureVisuals()
        {
            switch (type)
            {
                case PowerUpType.LightFooted:
                    SetColor(EffectColors.LightFootedColor);
                    break;
                case PowerUpType.SpeedBoost:
                    SetColor(EffectColors.SpeedBoostColor);
                    break;
                case PowerUpType.TimeSlow:
                    SetColor(EffectColors.TimeSlowColor);
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
        }

        private void OnTriggerEnter(Collider other)
        {
            if (other.TryGetComponent<PlayerController>(out _))
            {
                OnCollected?.Invoke(type, duration);

                if (pickupVfx)
                {
                    var vfx = Instantiate(pickupVfx, transform.position, Quaternion.identity);
                    var main = vfx.main;
                    meshRenderer.GetPropertyBlock(_propBlock);
                    main.startColor = _propBlock.GetColor(ColorProperty);
                    Destroy(vfx.gameObject, 2f);
                }

                Destroy(gameObject);
            }
        }

        public void Configure(PowerUpType newType)
        {
            type = newType;
            ConfigureVisuals();
        }

        private void SetColor(Color color)
        {
            meshRenderer.GetPropertyBlock(_propBlock);
            _propBlock.SetColor(ColorProperty, color);
            meshRenderer.SetPropertyBlock(_propBlock);
        }
    }
}