using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.VFX;
using UnityEngine.VFX.Utility;


public enum MaskType
{
    None = 0,

    Red = 1,
    Green = 2,
    Blue = 3,
    Cyan = 4,
    Purple = 5,

    Rainbow = 99,
}

public class MagicMask : MonoBehaviour
{
    [SerializeField]
    private VisualEffect m_maskEffect;

    [SerializeField]
    private BoxCollider m_collider;

    [SerializeField]
    private Rigidbody m_rb;

    private ExposedProperty m_baseColor = "Mask Color";
    private ExposedProperty m_color = "Mask Emission";

    public MaskType MaskType => m_maskType;
    private MaskType m_maskType = MaskType.None;

    public void ActivateMask(MaskType mask)
    {
        m_maskType = mask;
        var _col = Color.white;
        var _emi = Color.white;
        var _pow = 2.2f;
        switch (mask)
        {
            case MaskType.Red:
                _col = Color.red;
                _emi = Color.red;
                _pow = 4.1f;
                break;

            case MaskType.Blue:
                _col = Color.blue;
                _emi = Color.blue;
                _pow = 5.1f;
                break;

            case MaskType.Green:
                _col = Color.green;
                _emi = Color.green;
                _pow = 4.1f;
                break;

            case MaskType.Cyan:
                _col = Color.cyan;
                _emi = Color.cyan;
                _pow = 3.5f;
                break;

            case MaskType.Purple:
                _col = new Color32((byte)65, (byte)0, (byte)255, (byte)255);
                _emi = _col;
                _pow = 4.1f;
                break;
        }

        var _fac = Mathf.Pow(2, _pow);
        _emi = new Color(_emi.r * _fac, _emi.g * _fac, _emi.b * _fac);
        m_maskEffect.SetVector4(m_baseColor, _col);
        m_maskEffect.SetVector4(m_color, _emi);
    }

    public void DropMask()
    {
        m_collider.enabled = true;
        m_rb.isKinematic = false;
        m_fadeMaskAway = true;
    }

    public void PickUpMask()
    {
        Destroy(gameObject);
    }

    private bool m_fadeMaskAway = false;
    private float m_maskStayTimer = 5f;

    private void Update()
    {
        if (m_fadeMaskAway == false) return;

        m_maskStayTimer -= Time.deltaTime;
        if (m_maskStayTimer < 0)
        {
            Destroy(gameObject);
        }
    }
}
