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

[System.Serializable]
public class WaveSet
{
    public string Name;
    public int Sector;
    public int Wave;
    public List<Transform> Spawns;
    public int AsteroidCount=0;
}

public class LevelGenerator : MonoBehaviour {

    public Transform BallPrefab;
    public Transform AsteroidPrefab;

    public PrefabLibrary ZoidLib;
    public List<WaveSet> WaveSets;
    public Transform EmperorPrefab;
    public Transform PalaceBlockPrefab;

    private Transform player;
	
	void Start () {
        
	}
	
	void Update () {
	    
	}

    public void GenerateWave(bool newScene)
    {
        player = GameObject.Find("Player").transform;
        int c = GetCount();

        Debug.Log("LevelGenerator: Generating Sector " + GameController.Instance.Sector + " Wave " + GameController.Instance.WaveNum + " with " + c + " items");

        //for (int i = 0; i < c; i++)
        //    PlaceItem(ZoidLib.GetEntry(GameController.Instance.Sector));

        WaveSet wave = WaveSets.Where(w => w.Sector == GameController.Instance.Sector && w.Wave == GameController.Instance.WaveNum).First();

        foreach (Transform prefab in wave.Spawns)
        {
            PlaceItem(prefab);
        }

        int assCount;
        if (newScene)
        {
            int ballcount = (GameController.Instance.Sector == 5) ? 5 : 6;
            for (int b = 0; b < ballcount; b++)
                PlaceItem(BallPrefab);

            if (GameController.Instance.Sector == 5)
                PlaceItem(PalaceBlockPrefab);

            assCount = WaveSets.Where(w => w.Sector == GameController.Instance.Sector && w.Wave <= GameController.Instance.WaveNum).Sum(wv => wv.AsteroidCount);
        }
        else
        {
            //if(GameController.Instance.WaveNum == 1)
            //{
            //    for (int b = 0; b < 6; b++)
            //        PlaceItem(BallPrefab);
            //}
            assCount = wave.AsteroidCount;            
        }

        for (int a = 0; a < assCount; a++)
            PlaceItem(AsteroidPrefab);

        if(GameController.Instance.Sector == 5 && GameController.Instance.WaveNum == 3)
        {
            Transform emp = GameObject.Instantiate(EmperorPrefab);
            emp.position = new Vector3(0, 6.2f, 0);
        }
    }

    private int GetCount()
    {
        return GameController.Instance.Sector + GameController.Instance.WaveNum + ((GameController.Instance.Sector == 1) ? 0 : 1);

        int w = GameController.Instance.WaveNum;
        int s = GameController.Instance.Sector;
        if (s == 1) {
            if (w == 1)
                return 1;
            else if (w == 2)
                return 2;
            else
                return 3;
        }
        else if (s == 2)
            return 4;
        else if (s == 3)
            return 5;
        else if (s == 4)
            return 6;
        else
            return 7;

    }

    private void PlaceItem(Transform prefab)
    {
        Vector3 pos = new Vector3(Random.Range(4f, 8f) * RandomSign(), Random.Range(4f, 8f) * RandomSign(), 0);

        if((player.position-pos).magnitude < 5.5f)
        {
            pos *= -1;
        }

        Transform t = GameObject.Instantiate(prefab);
        t.position = pos;
    }    


    private int RandomSign()
    {
        if (Random.Range(0, 2) == 0)
            return 1;
        else
            return -1;
    }
}
