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

public class EndPanel : MonoBehaviour
{
    public Text escapeText, withCatText, noCatText, timeText, blackHoleText, engineText, noEngineText;
    CanvasGroup canvasGroup;

    private void Awake()
    {
        canvasGroup = GetComponent<CanvasGroup>();
        Hide();
    }

    private void Hide()
    {
        canvasGroup.alpha = 0f;
        canvasGroup.interactable = false;
        canvasGroup.blocksRaycasts = false;
    }

    private void Show()
    {
        canvasGroup.alpha = 1f;
        canvasGroup.interactable = true;
        canvasGroup.blocksRaycasts = true;
    }

    public void WinWithCat()
    {
        Show();
        escapeText.gameObject.SetActive(true);
        blackHoleText.gameObject.SetActive(false);
        withCatText.gameObject.SetActive(true);
        noCatText.gameObject.SetActive(false);   

        engineText.gameObject.SetActive(GameManager.instance.engine.isRepaired);
        noEngineText.gameObject.SetActive(!GameManager.instance.engine.isRepaired);
        timeText.text = GetTime();
    }

    public void WinNoCat()
    {
        Show();
        escapeText.gameObject.SetActive(true);
        blackHoleText.gameObject.SetActive(false);
        withCatText.gameObject.SetActive(false);
        noCatText.gameObject.SetActive(true);

        SoundManager.instance.PlayCatFail();

        engineText.gameObject.SetActive(GameManager.instance.engine.isRepaired);
        noEngineText.gameObject.SetActive(!GameManager.instance.engine.isRepaired);
        timeText.text = GetTime();
    }

    public void BlackHole()
    {
        Show();
        escapeText.gameObject.SetActive(false);
        blackHoleText.gameObject.SetActive(true);
        withCatText.gameObject.SetActive(false);
        noCatText.gameObject.SetActive(false);
        engineText.gameObject.SetActive(false);
        timeText.text = "";
    }

    private string GetTime()
    {
        float timeInSeconds = Time.timeSinceLevelLoad;

        int min = Mathf.FloorToInt(timeInSeconds / 60);
        int sec = Mathf.FloorToInt(timeInSeconds % 60);

        return min + ":" + sec.ToString("00");
    }
}
