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


public class ScrapScript : MonoBehaviour {

    public int PartID;
    bool falling = false;
    float vel = 0;
    public AudioSource ScrapSound;
    static bool chosen = false;


	// Use this for initialization
	void Start () {

        chosen = false;

        if (PlayerPrefs.GetInt("NumLegs") == 0)
        {
            if (PartID == 1) Destroy(gameObject);
        }

        if (PlayerPrefs.GetInt("NumLegs") < 2)
        {
            if (PartID == 2) Destroy(gameObject);
        }

        if (PlayerPrefs.GetInt("HasArm") == 0)
        {
            if (PartID == 3) Destroy(gameObject);
        }

        if (PlayerPrefs.GetInt("HasPack") == 0)
        {
            if (PartID == 4) Destroy(gameObject);
        }

        if (PlayerPrefs.GetInt("HasHead") == 0)
        {
            if (PartID == 5) Destroy(gameObject);
        }

    }

    // Update is called once per frame
    void Update () {
        if(falling)
        {
            vel += 2*Time.deltaTime;
            Vector3 p = this.transform.position;
            p.y -= vel;
            this.transform.position = p;
            if (p.y < -160f) SceneManager.LoadScene("Game");
        }
		
	}

    void OnMouseDown()
    {
        // make sure we don't pick twice
        if (chosen) return;

        switch (PartID)
        {
            case 1: // Leg
            case 2: // Other Leg
                PlayerPrefs.SetInt("NumLegs",PlayerPrefs.GetInt("NumLegs") - 1);
                break;

            case 3: // arm
                PlayerPrefs.SetInt("HasArm",0);
                break;

            case 4: // Jetpack
                PlayerPrefs.SetInt("HasPack",0);
                break;

            case 5: // Head 
                PlayerPrefs.SetInt("HasHead",0);
                break;
        }

        ScrapSound.Play();
        falling = true;
        chosen = true;
    }

}
