using Intrepid.Gameplay.Codex.Visual;
using Intrepid.Utilities;
using Sirenix.OdinInspector;
using UnityEngine;

namespace Intrepid.Gameplay.Codex.UI.Components
{
    public class CodexSurfaceComponentUI : CodexComponentUI
    {
        [GroupConfigurations, SerializeField] private PictorialSurfaceType backgroundType = PictorialSurfaceType.Base;
        [GroupConfigurations, SerializeField] private bool replaceAlpha;
        [GroupConfigurations, SerializeField, Range(0, 1), ShowIf("replaceAlpha")] private float alpha = .5f;
        
        [GroupInjections, SerializeField] private CodexColorEffectTargetUI background;

        public PictorialSurfaceType BackgroundType
        {
            get => backgroundType;
            set => backgroundType = value;
        }

        public override void ApplyCodexVisuals(CodexVisualContext ctx)
        {
            var sc = ctx.Protocol.GetSurfaceColor(backgroundType);
            if (replaceAlpha) sc = new Color(sc.r, sc.g, sc.b, alpha);
            background?.Apply(sc);
        }

        public override void BeforeClose()
        {
            base.BeforeClose();
            background?.Stop();
        }
    }
}