using UnityEngine;
using System.Collections;

[RequireComponent(typeof(ChapterTimer))]
public class ChapterManager : MonoBehaviour {

    public string xmlPath;

    private static ChapterManager cm;

    private static Chapter[] chapters;
    private static int chapterIndex = 0;
    private static int sentenceIndex = 0;

    private static TextController sentence;
    private static TextController chapter;

    private static HintController hintCont;

    private static ChapterTimer timer;
    private static float chapPause = 3.0f;

    void Awake() {
        DontDestroyOnLoad(gameObject);
        cm = this;

        // Build the chapters
        chapters = ChapterContainer.Load(xmlPath).chapters;
    }

    void Start() {
        // Get the hint controller
        hintCont = GameObject.FindGameObjectWithTag("Hints").GetComponent<HintController>();

        // Instantiate the sentence prefab
        sentence = GameObject.FindGameObjectWithTag("Sentence").GetComponent<TextController>();
        sentence.FadeInAction += StartHints;
        sentence.FadeOutAction += StopHints;

        // Instantiate the chapter prefab
        chapter = GameObject.FindGameObjectWithTag("Chapter").GetComponent<TextController>();

        // Get the timer
        timer = GetComponent<ChapterTimer>();

        // Start the chapter
        // StartChapter();
    }

    public static string Word {
        get { return chapters[chapterIndex].sentences[sentenceIndex].word; }
    }

    public static string Context {
        get { return chapters[chapterIndex].sentences[sentenceIndex].context; }
    }

    public static string Chapter {
        get { return chapters[chapterIndex].name; }
    }

    public static int Time {
        get { return chapters[chapterIndex].time; }
    }

    public static void Reset() {
        chapterIndex = 0;
        sentenceIndex = 0;
    }

    public static void GoToNextSentence() {
        Chapter chapter = chapters[chapterIndex];

        // Go to the next sentence
        if (sentenceIndex < chapter.sentences.Length - 1) {
            sentenceIndex++;
            sentence.FadeOut(false, true, Context);
        }

        // Go to the next chapter
        else GoToNextChapter();
    }

    public static void GoToNextChapter() {
        // Go to the next chapter
        if (chapterIndex < chapters.Length - 1) {
            TypingController.SetLocked(true);
            timer.StopCountdown();
            
            chapterIndex++; sentenceIndex = 0;
            
            sentence.FadeOut(false);

            chapter.FadeOutAction += StartChapterOnFade;
            chapter.FadeOut(false);
        }

        // End the game?
        else {
            timer.StopCountdown();
            GameManager.SetGameState(GameManager.State.Win);
        }
    }

    public static void StopChapter() {
        StopHints();
        TypingController.SetLocked(true);
        sentence.FadeOut(false);
        chapter.FadeOut(false);
    }

    public static void StartChapterOnFade() {
        chapter.FadeOutAction -= StartChapterOnFade;

        cm.StartCoroutine(cm.IStartChapter());
    }

    public static void StartChapter() {
        cm.StartCoroutine(cm.IStartChapter());
    }

    IEnumerator IStartChapter() {
        chapter.FadeIn(Chapter);
        yield return new WaitForSeconds(chapPause);

        // Unlock the typing controller
        TypingController.SetLocked(false);
        sentence.FadeIn(Context);
        timer.StartCountdown(Time);
    }

    static void StartHints() {
        hintCont.StartHints(Word);
    }

    static void StopHints() {
        hintCont.StopHints();
    }

}
