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

public class LandMaker : MonoBehaviour {

    public Transform brick;
    public Transform heart;

    public Transform TwoLegs;
    public Transform OneLeg;
    public Transform NoLegs;

    public Transform Saucer, Crate;

    public AudioSource BoomSound;
    public AudioSource OuchSound;
    public AudioSource ShotSound;
    public AudioSource ShotPuffSound;
    public AudioSource JetSound, CheeseSound;
    public AudioSource Duh1Sound, Duh2Sound, Duh3Sound;
    public AudioSource BombSound;
    public AudioSource AlienHitSound;
    public AudioSource Thump;


    public Color ocol;
    public float coltimer = 0;


    // Use this for initialization
    void Start() {

        for (float y = -4; y < -3; y++)
        {
            for (float x = -12; x < 10; x += 0.3f)
            {
                float pos = x + Random.Range(-0.1f, 0.1f);
                Transform t = Instantiate(brick, new Vector3(pos, y, 0), Quaternion.identity);
                t.Rotate(new Vector3(0, 0, Random.Range(-45, 45)));
            }
        }


        // initiate if this is the first time we're running a level
        if (PlayerPrefs.GetInt("JustStarting") == 1)
        {
            InitiateGame();
            PlayerPrefs.SetInt("JustStarting", 0);
        }

        int level = PlayerPrefs.GetInt("Level");
        bool[] used = new bool[8];
        for (int i = 0; i < 8; i++) used[i] = false;

        for (int i = 0; i < level * (0.75f); i++) 
        {
            int j;
            do {
                j = Random.Range(0, 8);
            }
            while (used[j]);
            used[j] = true;
            Instantiate(Crate, new Vector3(-1.5f*(j-3.5f), -3.33f, -2.9f+0.1f*j), Quaternion.identity); 
        }

        for (int i = 0; i < level; i++)
        {
            Instantiate(Saucer, new Vector3(i*2f, 3f, -2f), Quaternion.identity);
        }



        // store health for a replay
        PlayerPrefs.SetInt("LevelStartHealth", PlayerPrefs.GetInt("Health"));
        PlayerPrefs.SetInt("LevelStartFuel", PlayerPrefs.GetInt("Fuel"));

        DoHealth();

        ocol = GetComponent<SpriteRenderer>().color;

        switch (PlayerPrefs.GetInt("NumLegs")) {
            case 0:
                Instantiate(NoLegs, new Vector3(-7.2f, -3.493f, -5f), Quaternion.identity);
                break;
            case 1:
                Instantiate(OneLeg, new Vector3(-7.2f, -3.2f, -5f), Quaternion.identity);
                break;
            case 2:
                Instantiate(TwoLegs, new Vector3(-7.2f, -3.2f, -5f), Quaternion.identity);
                break;
        }


        GameObject k;

        if (PlayerPrefs.GetInt("HasPack") == 0)
        {
            KillBar(); // no need for fuel bar
            k = GameObject.Find("Jetpack");
            Destroy(k);
        }
        if (PlayerPrefs.GetInt("HasArm") == 0)
        {
            k = GameObject.Find("Arm");
            Destroy(k);
        }
        if (PlayerPrefs.GetInt("HasHead") == 0)
        {
            k = GameObject.Find("Head");
            Destroy(k);
        }


    }

    // Update is called once per frame
    void Update() {

        if (PlayerPrefs.GetInt("NeedBoom") == 1)
        {
            BoomSound.Play();
            PlayerPrefs.SetInt("NeedBoom", 0);
        }

        if (PlayerPrefs.GetInt("NeedShot") == 1)
        {
            ShotSound.Play();
            PlayerPrefs.SetInt("NeedShot", 0);
        }

        if (PlayerPrefs.GetInt("NeedShotPuff") == 1)
        {
            ShotPuffSound.Play();
            PlayerPrefs.SetInt("NeedShotPuff", 0);
        }

        if (PlayerPrefs.GetInt("NeedOuch") == 1)
        {
            OuchSound.Play();
            PlayerPrefs.SetInt("NeedOuch", 0);
            coltimer = 0.2f;
            GetComponent<SpriteRenderer>().color = new Color(1, 0, 0);
        }


        if (PlayerPrefs.GetInt("NeedCheeseSound") == 1)
        {
            CheeseSound.Play();
            PlayerPrefs.SetInt("NeedCheeseSound", 0);
        }

        if (PlayerPrefs.GetInt("NeedJetSound") == 1)
        {
            JetSound.Play();
            PlayerPrefs.SetInt("NeedJetSound", 0);
        }

        if (PlayerPrefs.GetInt("NeedThump") == 1)
        {
            Thump.Play();
            PlayerPrefs.SetInt("NeedThump", 0);
        }

        if (PlayerPrefs.GetInt("NeedAlienHit") == 1)
        {
            AlienHitSound.Play();
            PlayerPrefs.SetInt("NeedAlienHit", 0);
        }

        if (PlayerPrefs.GetInt("NeedBomb") == 1)
        {
            BombSound.Play();
            PlayerPrefs.SetInt("NeedBomb", 0);
        }


        if (PlayerPrefs.GetInt("NeedDuh") == 1)
        {
            switch (Random.Range(0, 3))
            {
                case 1:
                    Duh1Sound.Play();
                    break;
                case 2:
                    Duh2Sound.Play();
                    break;
                case 0:
                    Duh3Sound.Play();
                    break;
            }
            PlayerPrefs.SetInt("NeedDuh", 0);
        }





        if (coltimer > 0)
        {
            coltimer -= Time.deltaTime;
            if (coltimer < 0) GetComponent<SpriteRenderer>().color = ocol;
//            Debug.Log(ocol);
        }

    }

    public void DoHealth() {

        // erase old hearts
        GameObject[] gameObjects;
        gameObjects = GameObject.FindGameObjectsWithTag("Heart");

        for (var i = 0; i < gameObjects.Length; i++)
            Destroy(gameObjects[i]);

        // replace hearts

        int health = PlayerPrefs.GetInt("Health");
        for (float x = 0; x < health; x++)
        {
            float pos = -9.5f + 0.7f * x;
            Instantiate(heart, new Vector3(pos, 5, -0.1f), Quaternion.identity);
        }
    }

    public void InitiateGame()
    {
        PlayerPrefs.SetInt("Fuel",10);
        PlayerPrefs.SetInt("HasPack", 1);
        PlayerPrefs.SetInt("HasHead", 1);
        PlayerPrefs.SetInt("HasArm", 1);
        PlayerPrefs.SetInt("NumLegs", 2);
        PlayerPrefs.SetInt("Health", 10);
        PlayerPrefs.SetInt("Level", 1);
        PlayerPrefs.SetInt("Dying", 0);
    }

    public void KillBar()
    {
        GameObject k;
        k = GameObject.Find("JetpackIcon");
        if (k != null) Destroy(k);
        k = GameObject.Find("BarFill");
        if (k != null) Destroy(k);
        k = GameObject.Find("BarEmpty");
        if (k != null) Destroy(k);
    }

}


