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

public class LevelGeneratorScript : MonoBehaviour {

    public GameObject chair;
    public GameObject chair_2;
    public GameObject table;
    public GameObject background;
    public GameObject floor;
    public GameObject kingchair_1;
    public GameObject kingchair_2;
    public GameObject collider_prefab;

    public int numberOfChairsPerSide = 0;
    public float bgWidth;
    public List<GameObject> weapons;

    private float chairWidth = 1.2f;
	// Use this for initialization
	void Start () {
        
        GameObject clone;
        if (numberOfChairsPerSide == 0) numberOfChairsPerSide = 10;
        for (int i = 0; i < numberOfChairsPerSide; i++)
        {
            float x = i * chairWidth + chairWidth / 2;
            //frontside chairs
            clone = Instantiate(chair_2, Vector3.zero, Quaternion.identity) as GameObject;
            clone.transform.position = new Vector3(x, -0.5f, -0.5f);
            clone = Instantiate(chair, Vector3.zero, Quaternion.identity) as GameObject;
            clone.transform.position = new Vector3(-x, -0.5f, -0.5f);
            //backside chairs
            clone = Instantiate(chair_2, Vector3.zero, Quaternion.identity) as GameObject;
            clone.transform.position = new Vector3(x, 0.6f, 1.5f);
            clone = Instantiate(chair, Vector3.zero, Quaternion.identity) as GameObject;
            clone.transform.position = new Vector3(-x, 0.6f, 1.5f);
            //table
            clone = Instantiate(table, Vector3.zero, Quaternion.identity) as GameObject;
            clone.transform.position = new Vector3(x, -0.5f, 0.5f);
            clone = Instantiate(table, Vector3.zero, Quaternion.identity) as GameObject;
            clone.transform.position = new Vector3(-x, -0.5f, 0.5f);
            generate_item(x);
            generate_item(-x);
        }
        // making floor
        float curPos = -numberOfChairsPerSide * chairWidth * 2;
        float maxPos = numberOfChairsPerSide * chairWidth * 2;
        while (curPos <= maxPos)
        {
            curPos += 4;
            clone = Instantiate(floor, Vector3.zero, Quaternion.identity) as GameObject;
            clone.transform.position = new Vector3(curPos, -1f, 0.5f);
        }

        int bGtilesPerSite = (int)numberOfChairsPerSide / 10 + 1;
        Vector3 pos = new Vector3(bgWidth / 2, 4, 10);
        for (int i = 0; i < bGtilesPerSite; i++)
        {
            Vector3 posNeg = pos;
            posNeg.x = -pos.x;
            Instantiate(background, pos, Quaternion.identity);
            Instantiate(background, posNeg, Quaternion.identity);
            pos.x += bgWidth;
        }

        //kingschairs
        clone = Instantiate(kingchair_1, Vector3.zero, Quaternion.identity) as GameObject;
        clone.transform.position = new Vector3(numberOfChairsPerSide * chairWidth + chairWidth / 2, -0.5f, 0);
        clone = Instantiate(kingchair_2, Vector3.zero, Quaternion.identity) as GameObject;
        clone.transform.position = new Vector3(-1 * numberOfChairsPerSide * chairWidth - chairWidth / 2, -0.5f, 0);
        //kings cup *LOL*
        //prefab = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Prefabs/cup.prefab", typeof(GameObject));
        //clone = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;
        //clone.transform.position = new Vector3(numberOfChairsPerSide * chairWidth, 0, 0);
        //clone = Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject;
        //clone.transform.position = new Vector3(-1 * numberOfChairsPerSide * chairWidth, 0, 0);
        // table collider
        
        GameObject collider = (GameObject)Instantiate(collider_prefab, new Vector3(0, 0, 0), Quaternion.identity);
        collider.transform.localScale = new Vector2((numberOfChairsPerSide + 1) * chairWidth * 2, 0.2f);
    }

    void generate_item(float x)
    {
        if (weapons.Count == 0) return;
        int number = Random.Range(0, weapons.Count);
        GameObject clone = Instantiate(weapons[number], Vector3.zero, Quaternion.identity) as GameObject;
        clone.transform.position = new Vector2(x+ Random.Range(-0.5F, 0.5F), 0.5f);
    }
	
	// Update is called once per frame
	void Update () {
	
	}
}
