using UnityEngine;
using System.Collections;

public class GameManager : MonoBehaviour
{
    CamControl cam;
    MainBody player;
    public Generator generator;
    [SerializeField]
    float one2twoTime = 21;
    [SerializeField]
    float two2threeTime = 68;
    [SerializeField]
    float three2overTime = 122;
    float timer;
    Coroutine flowCor;
    Coroutine startGameCheckCor;
    new AudioSource audio;
    [SerializeField]
    AudioClip bgMusic;
    [SerializeField]
    M_UICanvas ui;
    int score;

    private void Awake()
    {
        audio = GetComponent<AudioSource>();

    }
    private void Start()
    {
        Application.targetFrameRate = 60;
        cam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<CamControl>();
        player = GameObject.FindGameObjectWithTag("Player").GetComponent<MainBody>();
        StartGame();
        //startGameCheckCor = StartCoroutine(StartGameCheckCoroutine());
    }
    private void Update()
    {
        DebugSwitch();
    }
    public void StartGameCheckor()
    {
        startGameCheckCor = StartCoroutine(StartGameCheckCoroutine());
    }
    IEnumerator StartGameCheckCoroutine()
    {
        while (true)
        {
            if (Input.GetMouseButtonDown(0))
            {
                StartGame();
                break;
            }
            yield return null;
        }
    }
    public void StartGame()
    {
        audio.clip = bgMusic;
        audio.Play();
        ChangeState(State.OneD);
        flowCor = StartCoroutine(FlowCoroutine());
    }
    IEnumerator FlowCoroutine()
    {
        yield return new WaitForSeconds(one2twoTime);
        ChangeState(State.TwoD);
        yield return new WaitForSeconds(two2threeTime - one2twoTime);
        ChangeState(State.ThreeD);
        yield return new WaitForSeconds(three2overTime - two2threeTime);
        //Destinate
        ui.Finish();
    }
    void ChangeState(State state)
    {
        switch (state)
        {
            case State.OneD:
                cam.ChangeState(CamControl.State.OneD);
                player.ChangeState(MainBody.State.OneD);
                generator.sm.SwitchTo<GaneratorStateOneD>();
                break;
            case State.TwoD:
                cam.ChangeState(CamControl.State.TwoD);
                player.ChangeState(MainBody.State.TwoD);
                generator.sm.SwitchTo<GeneratorStateTwoD>();
                break;
            case State.ThreeD:
                cam.ChangeState(CamControl.State.ThreeD);
                player.ChangeState(MainBody.State.ThreeD);
                generator.sm.SwitchTo<GeneratorStateThreeD>();
                break;
            default:
                break;
        }
    }
    void DebugSwitch()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            cam.ChangeState(CamControl.State.OneD);
            player.ChangeState(MainBody.State.OneD);
        }
        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            cam.ChangeState(CamControl.State.TwoD);
            player.ChangeState(MainBody.State.TwoD);
            generator.sm.SwitchTo<GeneratorStateTwoD>();
        }
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            cam.ChangeState(CamControl.State.ThreeD);
            player.ChangeState(MainBody.State.ThreeD);
            generator.sm.SwitchTo<GeneratorStateThreeD>();
        }
    }
    public void Score()
    {
        score++;
    }
    public int GetScore()
    {
        return score;
    }
    public float GetBGMTime()
    {
        return audio.time;
    }
    enum State
    {
        OneD, TwoD, ThreeD,
    }
}
