using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using UnityEngine.Playables;

public class GameplayManager : MonoBehaviour
{
    public EndingStoryControl EndingStoryControl;
    public CutsceneControl cutsceneControl;

    public List<AudioClip> BGMs;

    private static GameplayManager _instance;

    public bool Loading;

    public bool IsTutorial
    {
        get { return currCycle.currDay == 0; }
    }

    public int currDay;
    public int currCycleVal;
    public Cycle currCycle;
    public int AllowedCycle;

    private int maxCyclePerDay = 1;
    private int totalCycles  = 6;
    private int totalDays = 6;

    public List<string> EndGameTextLines;
    public List<Sprite> EndGameImages;

    public void Start() {
        Input.multiTouchEnabled = false;
    }
    public void Awake() {
        if(_instance == null) {
            _instance = this;
        } else if(Instance != this) {
            Destroy(gameObject);
        }
    }
    public static GameplayManager Instance {
        get
        {
            return _instance;
        }
    }

    public void Init() {

        EndGameTextLines = new List<string>();
        EndGameImages = new List<Sprite>();

        //Tutorial
        currDay = 0;
        currCycleVal = 1;
        currCycle = new Cycle(currCycleVal, currDay);
        Debug.Log("before StartCycle");
        AudioManager.Instance.TutorialAudioSource.Play();
        currCycle.StartCycle();
    }

    public void UpdateGameplayManager() {
        Debug.Log("UpdateGameplayManager");
        currCycleVal++;
        if (currCycleVal > maxCyclePerDay)
        {
            currDay++;
            currCycleVal = 1;
            if (currDay <= AllowedCycle)
            {
                cutsceneControl.gameObject.SetActive(true);
                cutsceneControl.Init();
            }
        }
        if (currDay > AllowedCycle)
        {
            EndingStoryControl.gameObject.SetActive(true);
            EndingStoryControl.StartEndingStory();
        }
        else
        {
            //currCycle = new Cycle(currCycleVal, currDay);
            //currCycle.StartCycle();
            StartCoroutine(delayStartCycle());
        }
    }
    IEnumerator delayStartCycle() {
        yield return new WaitForSeconds(2f);
        currCycle = new Cycle(currCycleVal, currDay);
        currCycle.StartCycle();
    }

    void Update()
    {
        if (!AudioManager.Instance.BGMAudioSource.isPlaying &&
            BGMs.Count > 0)
        {
            AudioManager.Instance.PlayBGM(BGMs[Random.Range(0, BGMs.Count)]);
        }
    }


}