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

public class MasterScript : MonoBehaviour {

    GameObject titleObj;

    public GameObject fogPrefab;
    public float fogDelayMultiplier;
    public float nextFogTime;
    public float fogTimer;

    public Sprite mikoSp1;
    public Sprite mikoSp2;
    SpriteRenderer mikoSr;
    SpriteRenderer calibSr;
    SpriteRenderer whiteSr;

    GameObject firstSaija;

    public SpriteRenderer victorySr;

    public float titleFadeRate;
    public float titleFadeTime;
    float titleTimer = 0f;

    public float victoryDelay;
    float victoryTimer = 0f;

    int gameState;

    public int cameraPlace = 0;


    public List<GameObject> saijaList;

    public bool camLerping = false;
    public float camLerpTime;
    public float camLerped = 0f;
    public Vector3 camLerpTarget;
    public Vector3 camInitiPos;

    // Use this for initialization
    void Start () {

        firstSaija = GameObject.Find("Saija");

        titleObj = GameObject.Find("Title_board");

        mikoSr = GameObject.Find("Miko").GetComponent<SpriteRenderer>();
        calibSr = GameObject.Find("Calibration/Collider").GetComponent<SpriteRenderer>();
        whiteSr = GameObject.Find("Valkotausta").GetComponent<SpriteRenderer>();


        SetState(0);

        nextFogTime = Random.Range(0f, 3f);

        victorySr.enabled = false;
	}
	
	// Update is called once per frame
	void Update () {
	
        switch(gameState)
        {
            case 0:

                if(calibSr.color.a > 0f)
                {
                    mikoSr.sprite = mikoSp2;
                } else
                {
                    mikoSr.sprite = mikoSp1;
                }

                if(calibSr.color.a > 0.85f)
                {
                    titleTimer += Time.deltaTime;
                }

                if(titleTimer > 0f) {
                    titleTimer += Time.deltaTime;
                    whiteSr.color = new Color(whiteSr.color.r, whiteSr.color.g, whiteSr.color.b, whiteSr.color.a + titleFadeRate);
                }

                if(titleTimer > titleFadeTime)
                {
                    Destroy(GameObject.Find("Title_board"));
                    SetState(1);
                }

                break;

            case 1:

                if(whiteSr.color.a > 0f)
                {
                    whiteSr.color = new Color(whiteSr.color.r, whiteSr.color.g, whiteSr.color.b, whiteSr.color.a - titleFadeRate);
                }
                Fog();
                UpdateCamera();

                if(firstSaija.transform.position.x > 8.3f)
                {
                    foreach (GameObject g in saijaList)
                    {
                        g.GetComponent<SaijaScript>().Victory();
                        gameState = 2;
                    }
                }
                break;

            case 2:

                if(victoryTimer < victoryDelay)
                {
                    victoryTimer += Time.deltaTime;
                } else
                {
                    victorySr.enabled = true;
                    if(Input.GetKeyDown(KeyCode.Escape))
                    {
                        Application.Quit();
                    }
                }

                break;
        }

	}

    void UpdateCamera()
    {
        switch(cameraPlace)
        {
            case 0:
                if(firstSaija.transform.position.x > -1.9f)
                {
                    cameraPlace += 1;
                    camLerping = true;
                    camLerped = 0f;
                    camLerpTarget = new Vector3(-0f, 0.02f, -10f);
                    camInitiPos = new Vector3(-5.5f, 0.02f, -10f);
                }
                break;

            case 1:
                if(firstSaija.transform.position.x > 3.8f)
                {
                    cameraPlace += 1;
                    camLerping = true;
                    camLerped = 0f;
                    camLerpTarget = new Vector3(5.4f, 0.02f, -10f);
                    camInitiPos = new Vector3(-0f, 0.02f, -10f);
                }
                break;

            case 2:

                break;
        }

        if(camLerping)
        {
            camLerped += Time.deltaTime / camLerpTime;
            Camera.main.transform.position = Vector3.Lerp(camInitiPos, camLerpTarget, camLerped);
            
        }
    }

    public void SetState(int i)
    {
        gameState = i;
        switch(i)
        {
            case 0:
                titleObj.SetActive(true);
                foreach (GameObject g in saijaList)
                {
                    g.GetComponent<SaijaScript>().saijaState = 0;
                }
                break;

            case 1:
                titleObj.SetActive(false);
                foreach (GameObject g in saijaList)
                {
                    g.GetComponent<SaijaScript>().saijaState = 1;
                }
                break;
        }
    }

    void Fog()
    {
        if (fogTimer < nextFogTime)
        {
            fogTimer += Time.deltaTime;
        }
        else
        {
            Vector3 fogPos = new Vector3(Camera.main.transform.position.x + Random.Range(-5f, 3f), Random.Range(-2.5f, 2.5f), 0f);
            Instantiate(fogPrefab, fogPos, transform.rotation);
            nextFogTime = Random.Range(2f, fogDelayMultiplier);
            fogTimer = 0f;
        }

    }
}
