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

public class audience_member_spawner : MonoBehaviour {
    public Object chair;
    public Object audienceMember;

    public Transform midBack;
    public Transform leftBack;
    public Transform rightBack;

    public int percentOfEmptySeats = 10;
    public Vector3 characterScale = new Vector3(0.35f, 0.35f);


    public Sprite[] maleTorsoes;
    public Sprite[] maleFaces;
    public Sprite[] maleSkulls;

    public Sprite[] femaleTorsoes;
    public Sprite[] femaleFaces;
    public Sprite[] femaleSkulls;

    public Sprite UFO;


    void RandomCharacter(character chara)
    {
        bool male = Random.Range(0, 2) == 0;

        Sprite[] torsoes = null;
        Sprite[] faces = null;
        Sprite[] skulls = null;
        if (male)
        {
            torsoes = maleTorsoes;
            faces = maleFaces;
            skulls = maleSkulls;
        }
        else
        {
            torsoes = femaleTorsoes;
            faces = femaleFaces;
            skulls = femaleSkulls;
        }

        Sprite torso = torsoes[Random.Range(0, torsoes.Length)];
        Sprite skull = skulls[Random.Range(0, skulls.Length)];
        Sprite face = faces[Random.Range(0, faces.Length)];

        if (Random.Range(0, 250) == 0)
        {
            skull = UFO;
            face = null;
        }

        chara.MakeWith(
            male,
            torso,
            skull,
            face
            );

    }
    // Use this for initialization
    void Awake () {
        spawn_back_mid();
        spawn_back_left();
        spawn_back_right();
        spawn_yard();
	}
	

    void spawn_back_mid()
    {
        Vector3 baseCorner = new Vector3(-8.5f, 0.6f, 14.5f);
        Vector3 baseDir = Quaternion.Euler(-30, 0, 0) * Vector3.forward;
        Vector3 scale = new Vector3(0.35f, 0.35f);
        float x_spacing = 2.4f;
        float y_spacing = 2f;
        GameObject go;
        for (int y = 0; y < 10; y++)
        {
            for (int x = 0; x < 7 + (1 - y%2) ; x++)
            {
                MakeCharacter(
                    new Vector3(x * x_spacing + (x_spacing * (y % 2) / 2), 0, 0) + baseDir * y * y_spacing + baseCorner,
                    Quaternion.identity,
                    Vector3.zero
                    );
            }
        }
    }

    void spawn_back_left()
    {
        spawn_side_x(new Vector3(-10.5f, 0.4f, 15), leftBack, new Vector3(-1, 0, -0.73f));
    }

    void spawn_back_right()
    {
        spawn_side_x(new Vector3(10.5f, 0.4f, 15), rightBack, new Vector3(1, 0, -0.73f));
    }

    void spawn_side_x(Vector3 baseCorner, Transform terace, Vector3 sideShift)
    {
        Vector3 scale = new Vector3(0.35f, 0.35f);
        Vector3 upShift = terace.forward;
        float x_spacing = 2f;
        float y_spacing = 1.7f;
        int max_side_rows = 9;
        float positioning_multiplier = 1.5f;
        for (int y = 0; y < max_side_rows; y++)
        {
            for (int x = 1; x < 2 + (int)(y / 2); x++)
            {
                Vector3 pos = (-x) * sideShift * (x_spacing + (y % 2) / 2f) + // x positioning
                    y * upShift * y_spacing + // y positioning
                    baseCorner + sideShift * x_spacing * positioning_multiplier; // position adjustment
                MakeCharacter(
                    pos,
                    terace.rotation, // base rotation
                    pos + Vector3.Cross(Vector3.up, sideShift) // lookat
                    );
            }
        }
    }

    void spawn_yard()
    {
        Vector3 baseCorner = new Vector3(0, 0f, 7.5f);
        Vector3 baseDir = Vector3.forward;
        Vector3 scale = new Vector3(0.35f, 0.35f);
        float x_spacing = 2.5f;
        float y_spacing = 1.8f;
        for (int y = 0; y < 4; y++)
        {
            int xmax = 12 - (int)(y * 2f);
            for (int x = 0; x < xmax; x++)
            {
                MakeCharacter(
                    new Vector3((xmax / 2 - x) * x_spacing + (x_spacing * (y % 2) / 2), 0.15f * y, 0) + baseDir * y * y_spacing + baseCorner,
                    Quaternion.identity,
                    Vector3.zero
                    );
            }
        }
    }

   
    void MakeCharacter(Vector3 position, Quaternion rotation, Vector3 lookatPosition)
    {
        GameObject go = Instantiate(chair, position, rotation) as GameObject;
        go.transform.localScale = characterScale;
        if (lookatPosition != Vector3.zero)
        {
            go.transform.LookAt(lookatPosition);
        }

        if (Random.Range(0,100) < percentOfEmptySeats)
        {
            return;
        }

        go = Instantiate(audienceMember, position, rotation) as GameObject;

        if (lookatPosition != Vector3.zero)
        {
            go.transform.LookAt(lookatPosition);
        }

        RandomCharacter(go.GetComponent<character>());

        go.transform.localScale = characterScale;
    }
}
