using Singleton;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine;

public class GameManager : Singleton<GameManager> {

    public bool IsRunning = false;
    public bool Clicked = false;

    public bool First = false;
    public bool Second = false;
    public bool minigameTwo = false;

    public bool minigameOne = false;

    [SerializeField] private GameObject cannon;
    [SerializeField] private GameObject oldTermContent;
    [SerializeField] private GameObject newTermContent;

    private void OnEnable() {
        AudioManager.Instance.PlayAtmosphere("Theme", 0.25f);
    }

    public void StartGame() {
        IsRunning = true;
        StartCoroutine(Game());
    }

    public void SetClicked() {
        Clicked = true;
    }

    public IEnumerator Game() {
        // intro
        Ship.Instance.Activate();
        yield return new WaitForSeconds(1);
        Subtitles.Instance.DisplayTextFirst("Let me tell you a story of honorable seamen...");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("...sailing to find the treasures of their dreams.");
        yield return WaitForInputClicked();
        Subtitles.Instance.EndText();
        yield return new WaitForSeconds(5);

        // nessie
        Subtitles.Instance.DisplayTextFirst("They sailed beyond the sunset...");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("...nothing could have disturbed their peace.");
        Nessie.Instance.Activate();
        yield return new WaitForSeconds(3f);
        Subtitles.Instance.DisplayText("Wait! What's that?!", 0.05f);
        IsRunning = false;
        yield return new WaitForSeconds(2f);
        Subtitles.Instance.DisplayText("Oh no, it was the living nightmare! The epic NESSIE!");
        yield return new WaitForSeconds(3f);
        // lose head
        Nessie.Instance.LoseHead();
        yield return new WaitForSeconds(4f);
        Subtitles.Instance.DisplayText("Uh... This is not epic.");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("Seems that our programmer got something wrong there...");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("Would you mind repairing the code little quick please?");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("You can access the code window in the top right corner!");
        TerminalManager.Instance.ShowActivateTerminalButton();
        yield return new WaitForSeconds(4f);
        Subtitles.Instance.EndText(); // ended!!

        // coding first
        First = true; // first quest began
        while (First) {
            yield return new WaitForEndOfFrame(); // quest commanded from outside
        }
        cannon.SetActive(true);
        Cannon.Instance.Enable();
        yield return new WaitForSeconds(2f);
        Subtitles.Instance.DisplayText("I think we got that! Nice!");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("Back to the story...");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("The mighty monster Nessie blocked their way to the treasure!");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("Luckiy they've got their cannon with them! Shoot, little fellows, shoot!");

        minigameOne = true;
        while (minigameOne) {
            yield return new WaitForEndOfFrame(); // quest commanded from outside
        }

        yield return new WaitForSeconds(2f);
        Cannon.Instance.Disable();

        // nessie slayed
        yield return new WaitForSeconds(3f);
        Destroy(Cannon.Instance.gameObject);

        IsRunning = true;
        Subtitles.Instance.DisplayText("Ha! What a heroic fight!");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("After that exhausting fight, they carried on living normal lives.");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("...trying to take possession of the sea's treasures!");
        yield return new WaitForSeconds(7f);
        Subtitles.Instance.DisplayText("So they continued to sail.");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("Through the most beautiful seas the knew.");
        yield return new WaitForSeconds(5f);
        Subtitles.Instance.DisplayText("...I mean come on, it was such a nice view!");
        yield return new WaitForSeconds(1f);

        // windmills appear#
        Kraken.Instance.EnableWindrad();
        yield return new WaitForSeconds(2f);
        IsRunning = false;
        Subtitles.Instance.DisplayText("Wait... what is this?");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("Okay. Uh. I guess the artist had not enough sleep.");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("Or it was just some kind of... misunderstanding?");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("Whatever... Could you? You know... Fix it again?");
        yield return new WaitForSeconds(1f);
        TerminalManager.Instance.ShowActivateTerminalButton();
        TerminalManager.Instance.Enable();
        oldTermContent.SetActive(false);
        newTermContent.SetActive(true);

        Second = true;
        while (Second) {
            yield return new WaitForEndOfFrame(); // quest commanded from outside
        }
        TerminalManager.Instance.HideActivateTerminalButton();
        TerminalManager.Instance.Disable();
        Subtitles.Instance.DisplayText("And yet again, an strong enemy blocked their way!");
        yield return WaitForInputClicked();

        
        Subtitles.Instance.DisplayText("But they found out that the mighty kraken's weak spot...");
        yield return new WaitForSeconds(4f);
        Subtitles.Instance.DisplayText("...was it being ticklish!");

        minigameTwo = true;
        while (minigameTwo) {
            yield return new WaitForEndOfFrame(); // quest commanded from outside
        }
        IsRunning = true;

        Subtitles.Instance.DisplayText("So they managed to tickle it away!");
        yield return new WaitForSeconds(4f);
        SunDown.Instance.Execute();
        Subtitles.Instance.DisplayText("After all those adventures, the crew sat together...");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("...drank some wine and celebrated all...");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("...the thrilling time they had together.");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("This is the end of the story.");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("Probably they are still out there...");
        yield return WaitForInputClicked();
        Subtitles.Instance.DisplayText("...battling with sea... or C... #.");

        yield return new WaitForSeconds(5f);
        Subtitles.Instance.DisplayText("No. The story does not end, because there was no time left.");
        yield return new WaitForSeconds(3f);
        Subtitles.Instance.DisplayText("Definitely not.");

        Curtains.Instance.Close();
        yield return  new WaitForSeconds(3f);
        SceneManager.LoadScene(0);
    }




    IEnumerator WaitForInputClicked() {
        Clicked = false;
        yield return new WaitForSeconds(1f);
        Subtitles.Instance.ShowProgressButton();
        while (!Clicked) {
            yield return new WaitForEndOfFrame();
        }

        Clicked = false;
        Subtitles.Instance.HideProgressButton();
    }
}
