﻿using System.Collections;
using UnityEngine;

public class WindOS_Indication : MonoBehaviour {

    [SerializeField]
    WindOSI_Canvas indicationCanvasPrefab;
    WindOnSpline wind;
    int delayBeforeAlert = 1;
    string enteredSplineText = "Good! \nKeep the wind!";
    string leftSplineText = "Bad! \nEnter the wind!";


    private void Start()
    {
        // React to the boat's actions.
        wind = GetComponent<WindOnSpline>();
        wind.BoatEnteredSpline += GotOnSpline;
        wind.BoatLeftSpline += LeftSpline;
        // Spawn the canvas.
        indicationCanvasPrefab = Instantiate(indicationCanvasPrefab);
    }

    private void Update()
    {
        if (!Boat.instance.enabled)
            indicationCanvasPrefab.gameObject.SetActive(false);
    }

    void GotOnSpline()
    {
        // Stop leaving text coroutine, and show congrats text.
        StopAllCoroutines();
        indicationCanvasPrefab.StartOnEnteredText(enteredSplineText, 3.5f);
    }

    void LeftSpline()
    {
        // Show "bad!" text after some time.
        StartCoroutine(WaitThenAlert());
    }

    IEnumerator WaitThenAlert()
    {
        yield return new WaitForSeconds(delayBeforeAlert);
        indicationCanvasPrefab.StartOnLeftText(leftSplineText, 2);
    }
}
