using UnityEngine;

public enum CardStatus
{
    show_back = 0,
    show_front,
    rotating_to_back,
    rotating_to_front
}

public class Card : MonoBehaviour
{

    [SerializeField] private CardStatus status;
    [SerializeField] private float turnTargetTime;

    [SerializeField] private SpriteRenderer frontRenderer;
    [SerializeField] private SpriteRenderer backRenderer;

    private float turnTimer;

    private Quaternion startRotation;
    private Quaternion targetRotation;

    [SerializeField] AudioSource flipSFX;

    Game game;

    private void Awake()
    {
        flipSFX = GameObject.Find("Flip").GetComponent<AudioSource>();
        status = CardStatus.show_back;
        GetFrontAndBackSpriteRenderers();
        game = FindFirstObjectByType<Game>();
    }

    void Update()
    {
        if (status == CardStatus.rotating_to_front || status == CardStatus.rotating_to_back)
        {
            turnTimer += Time.deltaTime;
            float percentage = turnTimer / turnTargetTime;

            transform.rotation = Quaternion.Slerp(startRotation, targetRotation,percentage);

            if (percentage >= 1f)
            {
                if (status == CardStatus.rotating_to_back)
                {
                    status = CardStatus.show_back;
                }
                else if (status == CardStatus.rotating_to_front)
                {
                    status = CardStatus.show_front;
                }
            }
        }
    }

    private void OnMouseUp()
    {
        if (game.AllowedToSelectCard(this) == true)
        {
            if (status == CardStatus.show_back)
            {
                game.SelectCard(gameObject);
                TurnToFront();
            }
            else if (status == CardStatus.show_front)
            {
                TurnToBack();
            }
        }
    }

    private void TurnToFront()
    {
        status = CardStatus.rotating_to_front;
        turnTimer = 0f;
        startRotation = transform.rotation;
        targetRotation = Quaternion.Euler(0,180,0);
        PlayFlipSFX();
    }

    public void TurnToBack()
    {
        status = CardStatus.rotating_to_back;
        turnTimer = 0f;
        startRotation = transform.rotation;
        targetRotation = Quaternion.Euler(0, 0, 0);
        PlayFlipSFX();
    }

    private void PlayFlipSFX()
    {
        flipSFX.pitch = Random.Range(0.9f, 1.1f);
        flipSFX.Play();
    }

    public void GetFrontAndBackSpriteRenderers()
    {
        foreach(Transform t in transform)
        {
            if (t.name == "Front")
            {
                frontRenderer = t.GetComponent<SpriteRenderer>();
            }
            else if (t.name == "Back")
            {
                backRenderer = t.GetComponent<SpriteRenderer>();
            }
        }
    }

    public void SetFront(Sprite sprite)
    {
        if (frontRenderer != null)
        {
            frontRenderer.sprite = sprite;
        }
    }

    public void SetBack(Sprite sprite)
    {
        if (backRenderer != null)
        {
            backRenderer.sprite = sprite;
        }
    }

    public Vector2 GetFrontSize()
    {
        if (frontRenderer == null)
        {
            Debug.LogError("Geen frontrenderer gevonden");
        }
        return frontRenderer.bounds.size;
    }

    public Vector2 GetBackSize()
    {
        if (backRenderer == null)
        {
            Debug.LogError("Geen backrenderer gevonden");
        }
        return backRenderer.bounds.size;
    }

}