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



public class GuyScript : MonoBehaviour
{
    public Transform cheese;

    float delay = 0;
    int animstep = 0;
    int Frame = 0;
    public int facing = 1;
    float walkspeed = 1.7f;
    public Sprite[] guypic;
    bool walking = false;
    bool jumping = false;
    bool nohead = false;
    int badpress = 0;
    float badtimer = 0.5f;
    float presstimer = 0f;
    bool newduh = false;
    bool leftpress = false;
    bool rightpress = false;
    float dyingtimer = 3f;
    float stucktimer = 0f;

    // Use this for initialization
    void Start()
    {

        if (PlayerPrefs.GetInt("HasHead") == 0) nohead = true;

    }

    // Update is called once per frame
    void Update()
    {
        rightpress = false;
        leftpress = false;

        if (PlayerPrefs.GetInt("Dying") == 1)
        {
            dyingtimer -= Time.deltaTime;
            if (dyingtimer < 0) SceneManager.LoadScene("Death");
            Vector3 p = this.transform.position;
            p.x += Random.Range(-0.3f, 0.3f);
            p.y += Random.Range(-0.3f, 0.3f);
            this.transform.rotation = Quaternion.identity;
            this.transform.Rotate(0,0,Random.Range(-70f, 70f));
            return;
        }



        if (nohead)
        {
            badtimer -= Time.deltaTime;
            if (badtimer < 0)
            { // Make a bad move
                badpress = Random.Range(1, 5);
                Debug.Log(badpress);
                badtimer = Random.Range(0.5f, 3.5f);
                presstimer = Random.Range(0.2f, 0.8f);
                newduh = true;
            }
        }


        // Regardless of legs, check jetpack and cheese

        // jetpack
        if (Input.GetKeyDown(KeyCode.W) || badpress == 1)
        {
            if (PlayerPrefs.GetInt("Fuel") <= 0) return;
            if (PlayerPrefs.GetInt("HasPack") == 0) return;

            if (badpress == 1 && newduh)
            {
                PlayerPrefs.SetInt("NeedDuh", 1);
                presstimer = 0;
            }

            animstep = 10;
            Rigidbody2D rb = GetComponent<Rigidbody2D>();
            Vector3 v = rb.velocity;
            v.y += 5;
            rb.velocity = v;
            jumping = true;
            PlayerPrefs.SetInt("NeedJetSound", 1);

            // start clouds
            JetPackScript jps;
            jps = gameObject.GetComponentInChildren<JetPackScript>();
            jps.StartCloud();

            int f = PlayerPrefs.GetInt("Fuel");
            PlayerPrefs.SetInt("Fuel", f - 1);
        }

        // cheese
        if (Input.GetKeyDown(KeyCode.S) || badpress == 2) // make cheese
        {

            if (badpress == 2 && newduh)
            {
                PlayerPrefs.SetInt("NeedDuh", 1);
                badtimer *= 0.3f; // shorter for cheese
            }

            Transform t = Instantiate(cheese, new Vector3(this.transform.position.x + Random.Range(-0.1f, 0.1f), this.transform.position.y - 0.4f, -3), Quaternion.identity);
            t.Rotate(new Vector3(0, 0, Random.Range(-90, 90)));
            Vector3 v = this.transform.position;
            v.y += 0.1f;
            this.transform.position = v;
            PlayerPrefs.SetInt("NeedCheeseSound", 1);
        }


        if (PlayerPrefs.GetInt("NumLegs") == 2) // 2-legged walking
        {

            if (Input.GetKey(KeyCode.D)) rightpress = true;
            if (Input.GetKey(KeyCode.A)) leftpress = true;

            if (nohead && presstimer > 0) // doing a bad move, so cancel original
            {
                rightpress = false;
                leftpress = false;
            }

            // return to rest if I'm not doing anything
            if (!walking && !jumping)
            {
                animstep = 0;
                delay = 0;
                Rigidbody2D rb = GetComponent<Rigidbody2D>();
                Vector3 v = rb.velocity;
                v.x = 0;
                rb.velocity = v;
            }

            // preemptively set walking to false; it will get re-set to true if we are walking later in this function
            walking = false;




            if ((rightpress || badpress == 3) && !jumping)
            {
                {
                    Rigidbody2D rb = GetComponent<Rigidbody2D>();
                    Vector3 v = rb.velocity;
                    v.x = walkspeed;
                    rb.velocity = v;
                    walking = true;
                    facing = 1;
                }

                if (badpress == 3 && newduh) PlayerPrefs.SetInt("NeedDuh", 1);
            }

            if ((leftpress || badpress == 4) && !jumping)
            {
                {
                    Rigidbody2D rb = GetComponent<Rigidbody2D>();
                    Vector3 v = rb.velocity;
                    v.x = -walkspeed;
                    rb.velocity = v;
                    walking = true;
                    facing = -1;
                }
                if (badpress == 4 && newduh) PlayerPrefs.SetInt("NeedDuh", 1);

            }

            if (jumping)
            {
                this.GetComponent<SpriteRenderer>().sprite = guypic[5];
            }
            else
            { // ground movement
                delay -= Time.deltaTime;
                if (delay < 0)
                { // next step in animation
                    switch (animstep)
                    {
                        // walking
                        case 0: // base pose 1
                            animstep = 1;
                            delay = 0.05f;
                            Frame = 0;
                            break;
                        case 1: // walk pose 2
                            animstep = 2;
                            delay = 0.05f;
                            Frame = 1;
                            break;
                        case 2: // walk pose 3
                            animstep = 3;
                            delay = 0.05f;
                            Frame = 2;
                            break;
                        case 3: // walk pose 4
                            animstep = 4;
                            delay = 0.05f;
                            Frame = 3;
                            break;
                        case 4: // walk pose 5
                            animstep = 0;
                            delay = 0.05f;
                            Frame = 4;
                            break;

                    }

                    this.GetComponent<SpriteRenderer>().sprite = guypic[Frame];

                }
                Vector3 theScale = transform.localScale;
                theScale.x = 0.3f * facing;
                transform.localScale = theScale;
            }
        }

        if (PlayerPrefs.GetInt("NumLegs") == 1)             // 1-legged hopping
        {
            if (stucktimer > 0f)
            {
                stucktimer -= Time.deltaTime;
                if (stucktimer <= 0f) jumping = false;
            }

            if (Input.GetKeyDown(KeyCode.D)) rightpress = true;
            if (Input.GetKeyDown(KeyCode.A)) leftpress = true;

            if (nohead && presstimer > 0) // doing a bad move, so cancel original
            {
                rightpress = false;
                leftpress = false;
            }

            // return to rest if I'm not doing anything
            if (!walking && !jumping)
            {
                animstep = 20;
                delay = 0;
                Rigidbody2D rb = GetComponent<Rigidbody2D>();
                Vector3 v = rb.velocity;
                v.x = 0;
                rb.velocity = v;


                if (rightpress || badpress == 3) // right 
                {
                    delay = 0;
                    animstep = 21;
                    walking = true;
                    facing = 1;
                    if (badpress == 3 && newduh) PlayerPrefs.SetInt("NeedDuh", 1);

                }

                if (leftpress || badpress == 4) // left
                {
                    delay = 0;
                    animstep = 21;
                    walking = true;
                    facing = -1;
                    if (badpress == 4 && newduh) PlayerPrefs.SetInt("NeedDuh", 1);
                }
            }

            if (!jumping)
            {
                delay -= Time.deltaTime;
                if (delay < 0)
                { // next step in animation
                  //                    Debug.Log("S:" + animstep + walking + jumping);
                    switch (animstep)
                    {
                        // walking
                        case 20: // base pose 1
                            delay = 0.05f;
                            Frame = 0;
                            break;
                        case 21: // about to squat
                            animstep = 22;
                            delay = 0.05f;
                            Frame = 1;
                            break;
                        case 22: // lowest squat
                            animstep = 23;
                            delay = 0.05f;
                            Frame = 2;
                            break;
                        case 23: // mid squat 
                            animstep = 24;
                            delay = 0.05f;
                            Frame = 3;
                            break;
                        case 24: // leaping
                            Rigidbody2D rb = GetComponent<Rigidbody2D>();
                            Vector3 v = rb.velocity;
                            v.x = walkspeed * facing;
                            v.y += 2.5f;
                            jumping = true;
                            rb.velocity = v;
                            walking = false;
                            stucktimer = 1f;
                            break;
                    }
                    this.GetComponent<SpriteRenderer>().sprite = guypic[Frame];
                }

                // set direction
                Vector3 theScale = transform.localScale;
                theScale.x = 0.3f * facing;
                transform.localScale = theScale;
            }

        }

        if (PlayerPrefs.GetInt("NumLegs") == 0)             // 0-legged crawling
        {

            if (stucktimer > 0f)
            {
                stucktimer -= Time.deltaTime;
                if (stucktimer <= 0f) jumping = false;
            }



            if (Input.GetKeyDown(KeyCode.D)) rightpress = true;
            if (Input.GetKeyDown(KeyCode.A)) leftpress = true;

            if (nohead && presstimer > 0) // doing a bad move, so cancel original
            {
                rightpress = false;
                leftpress = false;
            }

            // return to rest if I'm not doing anything
            if (!walking && !jumping)
            {
                animstep = 30;
                delay = 0;
                Rigidbody2D rb = GetComponent<Rigidbody2D>();
                Vector3 v = rb.velocity;
                v.x = 0;
                rb.velocity = v;

                if (rightpress || badpress == 3) // right 
                {
                    delay = 0;
                    animstep = 31;
                    walking = true;
                    facing = 1;
                    if (badpress == 3 && newduh) PlayerPrefs.SetInt("NeedDuh", 1);

                }

                if (leftpress || badpress == 4) // left
                {
                    delay = 0;
                    animstep = 31;
                    walking = true;
                    facing = -1;
                    if (badpress == 4 && newduh) PlayerPrefs.SetInt("NeedDuh", 1);
                }
            }

            if (!jumping)
            {
                delay -= Time.deltaTime;
                if (delay < 0)
                { // next step in animation
                  //                    Debug.Log("S:" + animstep + walking + jumping);
                    switch (animstep)
                    {
                        // walking
                        case 30: // base pose 1
                            delay = 0.15f;
                            break;
                        case 31: // about to leap
                            animstep = 32;
                            delay = 0.05f;
                            break;
                        case 32: // leap
                            Rigidbody2D rb = GetComponent<Rigidbody2D>();
                            Vector3 v = rb.velocity;
                            v.x = 0.5f * walkspeed * facing;
                            v.y += 1.5f;
                            jumping = true;
                            rb.velocity = v;
                            walking = false;
                            stucktimer = 1f;
                            break;
                    }
                }

                // set direction
                Vector3 theScale = transform.localScale;
                theScale.x = 0.3f * facing;
                transform.localScale = theScale;
            }

        }

        // Check victory state
        if (transform.position.x > 8.4f)
        { // made it
            PlayerPrefs.SetInt("Level", PlayerPrefs.GetInt("Level") + 1);
            if (PlayerPrefs.GetInt("Level") == 6)
                SceneManager.LoadScene("Victory");
            else
                SceneManager.LoadScene("ScrapChoice");
        }

        // clear bad press if any and if timer is expired
        presstimer -= Time.deltaTime;
        if (presstimer < 0) badpress = 0;
        newduh = false;
    }

    void OnCollisionEnter2D(Collision2D theCollision)
    {
            foreach (ContactPoint2D contact in theCollision.contacts)
            {
                if (contact.normal.y > 0.5)
                {
                    jumping = false;
                    break;
                }
            }
    }

/*
    void OnCollisionEnter(Collision collision)
    {
                foreach (ContactPoint contact in collision.contacts)
                {
                    Debug.DrawRay(contact.point, contact.normal, Color.white);
                }
                if (collision.relativeVelocity.magnitude > 2)
                    audioSource.Play();
    }
 */   
}
