using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerDeathVFXManager : MonoBehaviour
{
    [Header("TextOverlay:")]
    [SerializeField] private GameObject cheatedDeathText;
    [SerializeField] private GameObject revivedText;
    [SerializeField] private GameObject faintedText;
    [SerializeField] private GameObject permaDeathText;

    [Header("Volumes:")]
    [SerializeField] private PlayerDeathVFX cheatedDeathVFX;
    [SerializeField] private PlayerDeathVFX faintedVFX;
    [SerializeField] private PlayerDeathVFX permaDeathVFX;

    [Header("Listeners:")]

    [SerializeField] private GameEventListener onLocalPlayerCheatedDeath;
    [SerializeField] private GameEventListener onLocalPlayerFainted;
    [SerializeField] private GameEventListener onLocalPlayerPermaDeath;
    [SerializeField] private GameEventListener onLocalPlayerRevived;
    [SerializeField] private GameEventListener_ZoneData onGameSceneLoaded;

    private void Awake()
    {
        onLocalPlayerCheatedDeath.Response.AddListener(OnLocalCheatedDeath);
        onLocalPlayerFainted.Response.AddListener(OnLocalFainted);
        onLocalPlayerPermaDeath.Response.AddListener(OnLocalPermaDeath);
        onLocalPlayerRevived.Response.AddListener(OnLocalPlayerRevived);
        onGameSceneLoaded.Response.AddListener((x) => ResetAll());
    }

    private void ResetAll()
    {
        cheatedDeathVFX.ResetWeight();
        faintedVFX.ResetWeight();
        permaDeathVFX.ResetWeight();
        cheatedDeathText.SetActive(false);
        faintedText.SetActive(false);
        permaDeathText.SetActive(false);
        revivedText.SetActive(false);
    }

    private void OnLocalCheatedDeath()
    {
        EnableCheatedDeathVFX();
        faintedVFX.ResetWeight();
        permaDeathVFX.ResetWeight();
        faintedText.SetActive(false);
        permaDeathText.SetActive(false);
        revivedText.SetActive(false);

        StartCoroutine(FallbackCheatedDeathVFX());
    }
    IEnumerator FallbackCheatedDeathVFX()
    {
        yield return new WaitForSeconds(GameConstants.CharacterBalancing.SoloCheatDeathInvulnerabilityDuration);

        DisableCheatedDeathVFX();
    }

    private void OnLocalFainted()
    {
        cheatedDeathVFX.ResetWeight();
        EnableFaintedVFX();
        permaDeathVFX.ResetWeight();

        cheatedDeathText.SetActive(false);
        permaDeathText.SetActive(false);
        revivedText.SetActive(false);
    }
    private void OnLocalPermaDeath()
    {
        cheatedDeathVFX.ResetWeight();
        faintedVFX.ResetWeight();
        EnablePermaDeathVFX();

        cheatedDeathText.SetActive(false);
        faintedText.SetActive(false);
        revivedText.SetActive(false);
    }
    private void OnLocalPlayerRevived()
    {
        EnableCheatedDeathVFX();
        faintedVFX.ResetWeight();
        permaDeathVFX.ResetWeight();

        revivedText.SetActive(true);

        cheatedDeathText.SetActive(false);
        faintedText.SetActive(false);
        permaDeathText.SetActive(false);

        StartCoroutine(FallbackCheatedDeathVFX());
    }

    public void EnableCheatedDeathVFX()
    {
        cheatedDeathVFX.BlendIn();
        cheatedDeathText.SetActive(true);
    }
    public void DisableCheatedDeathVFX()
    {
        cheatedDeathVFX.BlendOut();
        cheatedDeathText.SetActive(false);
        revivedText.SetActive(false);
    }

    public void EnableFaintedVFX()
    {
        faintedVFX.BlendIn();
        faintedText.SetActive(true);
    }
    public void DisableFaintedVFX()
    {
        faintedVFX.BlendOut();
        faintedText.SetActive(false);
    }

    public void EnablePermaDeathVFX()
    {
        permaDeathVFX.BlendIn();
        permaDeathText.SetActive(true);
    }
    public void DisablePermaDeathVFX()
    {
        permaDeathVFX.BlendOut();
        permaDeathText.SetActive(false);
    }
}
