using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class GameManager : MonoBehaviour {

    public static GameManager Instance;
    public FirstPersonController Controler;
    public Camera Cam;
    public Plane[] LastCameraPlanes;

    public Color FogColor;
    public bool AllowChanges = true;
    public bool FullPatate = false;

    public List<StartPoint> Checkpoints;
    private StartPoint LastCheckpoint;

    public GUISkin MySkin;

    public int SlowUpdateCurrent = 0;
    private int BallTotal = 5;
    private int BallRest = 5;
    private float LastBallTime = -1.0f;
    private bool IsFinish = false;
    private float FinishTime = 0.0f;

    // Use this for initialization
    void Awake () {
        Instance = this;
        RenderSettings.fogColor = FogColor;
        Cam.backgroundColor = FogColor;
    }

    void Start() {
        //IsFinish = true;
    }

    public void SetFullPatate() {
        FullPatate = true;
        Light Spot = Controler.transform.GetComponentInChildren<Light>();
        Spot.enabled = false;
    }

    public void RegisterCheckpoint(StartPoint Point, bool IsStart) {
        Checkpoints.Add(Point);
        if(IsStart) {
            LastCheckpoint = Point;
            ResetPlayer();
        }
    }

    public void SetCheckpoint(StartPoint Point) {
        LastCheckpoint = Point;
    }

    public void ResetPlayer() {
        Controler.transform.position = LastCheckpoint.transform.position;
        //Controler.transform.rotation = LastCheckpoint.transform.rotation;
    }

    public Vector3 GetPlayerPosition()
    {
        return Controler.transform.position;
    }

    public void GotBall() {
        BallRest -= 1;
        LastBallTime = 5.0f;
        if (BallRest<=0) {
            IsFinish = true;
        }
        Controler.PlayPickSound();
    }
	
	// Update is called once per frame
	void Update () {
        if(Input.GetButtonDown("Fire3") || Input.GetKeyDown(KeyCode.R)) {
            ResetPlayer();
        }
        ++SlowUpdateCurrent;
        LastCameraPlanes = GeometryUtility.CalculateFrustumPlanes(Cam);

        if(LastBallTime>0.0f) {
            LastBallTime -= Time.deltaTime;
        }

        if (IsFinish) {
            FinishTime += Time.deltaTime * (FullPatate ? 0.2f : 1.0f);
            if (FinishTime>4.0f && !FullPatate) {
                SetFullPatate();
            }
        }
    }

    public void OnGUI() {
        GUI.skin = MySkin;
        GUIStyle Style = GUI.skin.GetStyle("Label");
        Style.fontSize = 30;
        Style.alignment = TextAnchor.MiddleCenter;

        if (IsFinish) {
            Style.normal.textColor = Color.black;
            float Offset = Mathf.Max(-FinishTime * 80.0f,-Screen.height * 0.9f);
            //float Offset = -FinishTime * 80.0f;
            Rect BoxLocation = new Rect(Screen.width * 0.5f - 200, Screen.height + Offset - 10, 400, 210);
            Rect TextLocation = new Rect(Screen.width * 0.5f - 200, Screen.height + Offset, 400, 40);
            GUI.Box(BoxLocation, "");
            GUI.Label(TextLocation, "Thank you for playing");
            TextLocation.y += 60.0f;
            GUI.Label(TextLocation, "Made in 48h for LD38");
            TextLocation.y += 30.0f;
            GUI.Label(TextLocation, "NuSan - 2017");
            TextLocation.y += 60.0f;
            GUI.Label(TextLocation, "Now, lets turn off the light");
        }

        if (LastBallTime>0.0f) {
            Style.normal.textColor = Color.black;
            Rect TextLocation = new Rect(Screen.width * 0.5f - 50, Screen.height * 0.15f, 100, 60);
            GUI.Box(TextLocation, "");
            GUI.Label(TextLocation, "" + (BallTotal-BallRest) + "/" + BallTotal);
        }
        /*
        if (!IsInCine && !InMenu && ShowCrosshair) {
            float TexSize = 16;
            GUI.DrawTexture(new Rect(Screen.width * 0.5f - TexSize, Screen.height * 0.5f - TexSize, TexSize * 2, TexSize * 2), TargetTexture);
        }
        */
        /*TextLocation = new Rect(Screen.width * 0.5f - 300, Screen.height * 0.75f - 25, 600, 50);
        GUI.Box(TextLocation, "");
        GUI.Label(TextLocation, DebugText1);*/
    }
}
