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

public class CutsceneControl : MonoBehaviour {
    public Image mask;
    private bool isCutScene;
    private bool nowDarkenScreen;
    private bool displayText;
    private bool fadeToNewDay;

    private float speed = 0.33f;
    private float multiplier = 5f;

    public Text dayText;
    public Text numText;
    public Text HappyFriday;

    // Use this for initialization
    void Start () {

    }
    public void Init()
    {
        GameplayManager.Instance.Loading = true;
        Cursor.visible = false;
        Debug.Log("in Cutscene Control");
        isCutScene = true;
        nowDarkenScreen = false;
        displayText = false;
        fadeToNewDay = false;
        mask.color = new Color(0f, 0f, 0f, 0f);
        numText.color = new Color(0f, 0f, 0f, 0f);
        dayText.color = new Color(0f, 0f, 0f, 0f);
    }
    // Update is called once per frame
    void Update () {
        //Flash to White
        if(isCutScene) {
            mask.color = new Color(mask.color.r + Time.deltaTime,
                mask.color.g + Time.deltaTime, 
                mask.color.b + Time.deltaTime, 
                mask.color.a + Time.deltaTime);
            if(mask.color.a > 1f) {
                Debug.Log("Phase 1");
                nowDarkenScreen = true;
                isCutScene = false;
            }
        }
        //Dim screen
        if(nowDarkenScreen) {
            mask.color = new Color(mask.color.r - Time.deltaTime,
                mask.color.g - Time.deltaTime, 
                mask.color.b - Time.deltaTime, 
                mask.color.a + Time.deltaTime);
            if(mask.color.r <= 0 && mask.color.g <= 0 && mask.color.b <= 0) {
                Debug.Log("Phase 2");

                nowDarkenScreen = false;
                displayText = true;
            }
        }
        if(displayText) {
            numText.gameObject.SetActive(true);
            dayText.gameObject.SetActive(true);
            numText.color = new Color(numText.color.r + Time.deltaTime, numText.color.g + Time.deltaTime, numText.color.b + Time.deltaTime, numText.color.a + Time.deltaTime);
            dayText.color = new Color(dayText.color.r + Time.deltaTime, dayText.color.g + Time.deltaTime, dayText.color.b + Time.deltaTime, dayText.color.a + Time.deltaTime);
            HappyFriday.color = new Color(HappyFriday.color.r + Time.deltaTime, HappyFriday.color.g + Time.deltaTime, HappyFriday.color.b + Time.deltaTime, HappyFriday.color.a + Time.deltaTime);
            if ((numText.color.r >= 1 && numText.color.g >= 1 && numText.color.b >= 1) && (dayText.color.r >= 1 && dayText.color.g >= 1 && dayText.color.b >= 1)) {
                Debug.Log("Phase 3");

                if (GameplayManager.Instance.currDay == GameplayManager.Instance.AllowedCycle)
                {
                    numText.gameObject.SetActive(false);
                    dayText.gameObject.SetActive(false);
                    HappyFriday.gameObject.SetActive(true);
                }
                else
                {
                    HappyFriday.gameObject.SetActive(false);
                }

                displayText = false;
                StartCoroutine(PauseSeconds(3f));
                numText.text = GameplayManager.Instance.currDay.ToString();
                StartCoroutine(PauseSeconds(10f));

                fadeToNewDay = true;
            }
        }
        if(fadeToNewDay) {
            numText.color = new Color(numText.color.r + Time.deltaTime, numText.color.g + Time.deltaTime, numText.color.b + Time.deltaTime, numText.color.a - Time.deltaTime);
            dayText.color = new Color(dayText.color.r + Time.deltaTime, dayText.color.g + Time.deltaTime, dayText.color.b + Time.deltaTime, dayText.color.a - Time.deltaTime);
            HappyFriday.color = new Color(HappyFriday.color.r + Time.deltaTime, HappyFriday.color.g + Time.deltaTime, HappyFriday.color.b + Time.deltaTime, HappyFriday.color.a - Time.deltaTime);
            mask.color = new Color(mask.color.r - Time.deltaTime, mask.color.g - Time.deltaTime, mask.color.b - Time.deltaTime, mask.color.a - Time.deltaTime);
            if(mask.color.a <= 0)
            {
                Debug.Log("Phase 4");
                EndCutScene();
            }
        }
    }

    public void EndCutScene()
    {
        Cursor.visible = true;
        fadeToNewDay = false;
        this.gameObject.SetActive(false);
        GameplayManager.Instance.Loading = false;
    }

    IEnumerator PauseSeconds(float secs) {
        yield return new WaitForSeconds(secs);
    }
}
