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


public class GameController : MonoBehaviour
{

    public float CamDist = 5f;

    public GameObject MCamera;

    List<Vector3> ShapePoints = new List<Vector3>();
    List<Vector3> ShapePoints2 = new List<Vector3>();
    List<GameObject> TargetShape = new List<GameObject>();
    List<GameObject> ObjShape = new List<GameObject>();
    //Audio
    //AudioSource[] Tracks = gameObject.GetComponents<AudioSource>();

    float RadiusMin = 1f;
    float RadiusMax = 2f;

    public Transform outlineList;
    public Transform shapeList;

    public Transform fieldTrans;

    //Music Stuff
    float BPM = 120f;
    float BeatsPerSection = 4.0f;
    float PrevUpdateTime = 0.0f;
    Text text;
    int CurrentAudio = 0;
    //int Completed

    public bool levelComplete = false;
    public int score = 0;
    GameObject shape;

    void Start()
    {
        gameObject.GetComponents<AudioSource>()[0].loop = true;
        gameObject.GetComponents<AudioSource>()[0].Play();
        //LevelStartTime = Time.time;
        transform.position = fieldTrans.position - new Vector3(0, 0, CamDist);
        GenerateShape();
        shape = GameObject.Find("ShapeList");
        

    }

    public void GenerateShape()
    {

        float Ang = 0f;
        int PointCount = Random.Range(Variables.DifficultyMin, Variables.DifficultyMax + 1);
        for (int i = 0; i < PointCount; i++)
        {
            float Radius = Random.Range(RadiusMin, RadiusMax);
            Ang += (360.0f / (float)PointCount);//Random.Range((float)PointCount*0.8f,(float)PointCount));
            ShapePoints.Add(fieldTrans.position + new Vector3(Radius * Mathf.Cos(Mathf.Deg2Rad * Ang), Radius * Mathf.Sin(Mathf.Deg2Rad * Ang), 0.049f));
            ShapePoints2.Add(fieldTrans.position + new Vector3(Radius * Mathf.Cos(Mathf.Deg2Rad * Ang), Radius * Mathf.Sin(Mathf.Deg2Rad * Ang), -1.0f));
        }
        for (int i = 0; i < PointCount; i++)
        {
            GameObject TempCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            TempCube.renderer.material.color = new Color(0.0f, 0.0f, 0.0f, 0.0f);
            if (i == ShapePoints.Count - 1)
            {
                TempCube.transform.position = Vector3.Lerp(ShapePoints[i], ShapePoints[0], 0.5f);
                TempCube.transform.LookAt(ShapePoints[0]);
                TempCube.transform.localScale = new Vector3(0.1f, 0.025f, (ShapePoints[i] - ShapePoints[0]).magnitude);
            }
            else
            {
                TempCube.transform.position = Vector3.Lerp(ShapePoints[i], ShapePoints[i + 1], 0.5f);
                TempCube.transform.LookAt(ShapePoints[i + 1]);
                TempCube.transform.localScale = new Vector3(0.1f, 0.025f, (ShapePoints[i] - ShapePoints[i + 1]).magnitude);

            }
            GameObject TempCube2 = GameObject.CreatePrimitive(PrimitiveType.Cube);
            if (i == ShapePoints.Count - 1)
            {
                TempCube2.transform.position = Vector3.Lerp(ShapePoints2[i], ShapePoints2[0], 0.5f);
                TempCube2.transform.LookAt(ShapePoints2[0]);
                TempCube2.transform.localScale = new Vector3(2.0f, 0.0f, (ShapePoints2[i] - ShapePoints2[0]).magnitude);
            }
            else
            {
                TempCube2.transform.position = Vector3.Lerp(ShapePoints2[i], ShapePoints2[i + 1], 0.5f);
                TempCube2.transform.LookAt(ShapePoints2[i + 1]);
                TempCube2.transform.localScale = new Vector3(2.0f, 0.0f, (ShapePoints2[i] - ShapePoints2[i + 1]).magnitude);

            }
            //Debug.Log(i);
            TempCube.renderer.material.color = new Color(0.0f, 0.0f, 0.0f, 0.0f);
            TempCube2.renderer.material.color = new Color(1.0f, 1.0f, 1.0f, 0.0f);
            //TempCube2.renderer.material.shader = Shader.Find("Self-Illumin/Diffuse");
            TempCube.transform.parent = outlineList;
            TempCube2.transform.parent = shapeList;
            TargetShape.Add(TempCube);
            ObjShape.Add(TempCube2);
        }
        //Debug.Log(ShapePoints);
        shapeList.Rotate(new Vector3(0, 0, Random.Range(90, 270)));
    }
    public void ClearList()
    {
        ShapePoints.Clear();
        ShapePoints2.Clear();
        TargetShape.Clear();
        ObjShape.Clear();
        foreach (Transform child in shapeList)
        {
            GameObject.Destroy(child.gameObject);
        }
    }

    public void DeleteOutline()
    {
        foreach (Transform child in outlineList)
        {
            GameObject.Destroy(child.gameObject);
        }
    }

    void FixedUpdate()
    {
        transform.position = fieldTrans.position - new Vector3(0, 0, CamDist);

        if (MCamera.GetComponent<CameraMovementScript>().locked)
        {
            /*float modifier = score / 16;
            CurrentAudio++;
            MCamera.GetComponent<GameController>().DifficultyMin +=(int)modifier;
            MCamera.GetComponent<GameController>().DifficultyMax +=(int)modifier;*/
        }
        if (Time.time - PrevUpdateTime >= (60.0f / BPM) * BeatsPerSection)
        //if (Time.time % (60.0f / BPM)*BeatsPerSection <= 0.1)
        {
            Debug.Log("Time = " + gameObject.GetComponents<AudioSource>()[CurrentAudio].time);
            if (MCamera.GetComponent<CameraMovementScript>().locked)
            {

                if (score % 16 == 0)
                {
                    if (CurrentAudio < 4)
                    {
                        CurrentAudio++;
                        gameObject.GetComponents<AudioSource>()[CurrentAudio - 1].Stop();
                        gameObject.GetComponents<AudioSource>()[CurrentAudio].loop = true;
                        gameObject.GetComponents<AudioSource>()[CurrentAudio].Play();
                    }
                    Variables.DifficultyMin += 1;
                    Variables.DifficultyMax += 1;
                    RadiusMin = 1 + Random.Range(-0.2f, 0.2f);
                    RadiusMax = 2 + Random.Range(-0.2f, 0.2f);
                }
            }
            else
            {
                if (Variables.lives > 0)
                {
                    Variables.lives--;
                }
                if (Variables.lives == 0 && !Variables.endless)
                {
                    Application.LoadLevel("Game Over");
                }
            }
            MCamera.GetComponent<CameraMovementScript>().locked = false;
            MCamera.GetComponent<GameController>().DeleteOutline();
            MCamera.GetComponent<GameController>().ClearList();
            MCamera.GetComponent<GameController>().GenerateShape();
            gameObject.GetComponent<Throb>().Rot = 0;
            GameObject.Find("MainField").GetComponent<ColourChange>().count = 0f;
            PrevUpdateTime = Time.time;
            //Tracks[CurrentAudio].Play();
        }
    }

}
