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

class UIController : Utils.Singleton<UIController> {
    public Animator fadeAnimator;
    public GameObject leapPanel;
    public Sprite revealedGerm;
    public Sprite skinGerm;
    private Animator leapAnimator;

    private void Start()
    {
        leapAnimator = leapPanel.GetComponent<Animator>();
    }

    public void FadeOut()
    {
        FadeOut(1);
    }

    public void FadeOut(float speed)
    {
        print("out " + speed);
        fadeAnimator.speed = 1/speed;
        fadeAnimator.SetTrigger("out");
    }

    public void FadeIn()
    {
        FadeOut(1);
    }

    public void FadeIn(float speed)
    {
        print("in " + speed);
        fadeAnimator.speed = 1/speed;
        fadeAnimator.SetTrigger("in");
    }

    public void PlayLeap(bool left2right)
    {
        StartCoroutine(Leap(left2right));
    }

    IEnumerator Leap(bool toRight)
    {
        // do wiggle stuff
        leapPanel.SetActive(true);

        leapAnimator.SetTrigger(toRight ? "right" : "left");

        Transform germinator = leapPanel.transform.GetChild(1); // Are you sure that is fucking germinator?
        Image germinatorImage = germinator.GetComponent<Image>();

        if (Progress.LatestDefeat() == null)
        {
            germinatorImage.sprite = skinGerm;
        }
        else
        {
            germinatorImage.sprite = revealedGerm;
        }


        if (!toRight) germinator.localScale = new Vector3(-1, 1, 1);
        else germinator.localScale = new Vector3(1, 1, 1);

        yield return new WaitForSeconds(1.5f); // animation lenght, hardcoded since animation events are for pussies

        leapPanel.SetActive(false);

        CameraController.Instance.ReportPhaseCompleted(CameraController.Phase.LEAP);
    }
}
