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

public class PlayerController : MonoBehaviour
{
    public VirtualJoystickController joyLeft;
    public VirtualJoystickController joyRight;
    public VirtualJoystickController joyLefts;
    public VirtualJoystickController joyRights;

    public enum Weight {light, medium, heavy};
    public Weight weight = Weight.light;

    public GameObject clawsGameObject;

    public KeyCode keyFlapLeft = KeyCode.A;
    public KeyCode keyFlapRight = KeyCode.D;

    public float forceUp = 550;
    public float forceSide = 250;

    [Range(0, 100)]
    public float stamina = 100;

    [Range(0, 100)]
    public float hunger = 100;

    [Range(0, 100)]
    public float stress = 100;

    [Range(1, 10)]
    public float staminaWeakness = 1;

    private Rigidbody2D body = null;
    private Rigidbody2D clawsBody = null;

    public bool isGrounded = false;

    private Texture2D guiTextureBackground;
    private Texture2D guiTextureFill;
    private Texture2D guiTextureFillRed;
    private Texture2D guiTextureFillStress;


    private bool staminaTired = false;
    private TimerHelper staminaTiredTimer;

    private bool bar_show = true;
    private TimerHelper bar_timer;

    public TimerHelper pickupTimer;

    static public PlayerController instance;
    
    public SpriteRenderer clawSpriteRenderer;
    public SpriteRenderer clawSpriteRenderer_idle;
    public SpriteRenderer clawSpriteRenderer_extended;

    private JointLineRenderer2D jointLineRenderer;

    public Rigidbody2D carriedObject;

    public static PlayerController Get() {
        return(instance);
    }
    
    void Start()
    {
        jointLineRenderer = GetComponent<JointLineRenderer2D>();
        body = gameObject.GetComponent<Rigidbody2D>();

        clawsBody = gameObject.GetComponent<Rigidbody2D>();

        guiTextureBackground = Resources.Load<Texture2D>("Sprites/Hud/GUI Texture");
        guiTextureFill = Resources.Load<Texture2D>("Sprites/Hud/GUI Texture Fill");
        guiTextureFillRed = Resources.Load<Texture2D>("Sprites/Hud/GUI Texture Fill Red");
        guiTextureFillStress = Resources.Load<Texture2D>("Sprites/Hud/GUI Texture Sress");

        staminaTiredTimer = TimerHelper.Create();
        
        bar_timer = TimerHelper.Create();

        instance = this;

        pickupTimer = TimerHelper.Create();

    }

    void Update()
    {
        UpdateInput();

        UpdateStamina();

        UpdatePhysics();

        if (hunger > 0) {
            hunger -= Time.deltaTime * 1;
        }
        
        if (hunger <= 0) {
            Application.LoadLevel(Application.loadedLevel);
        }

        // Claw Animation
        if (isGrounded) {
            jointLineRenderer.enabled = false;
            clawSpriteRenderer.enabled = false;

            clawSpriteRenderer_idle.enabled = false;

            clawSpriteRenderer_extended.enabled = true;

        } else if (clawsGameObject.transform.childCount > 0) {
            jointLineRenderer.enabled = true;
            clawSpriteRenderer.enabled = true;

            clawSpriteRenderer_idle.enabled = false;

            clawSpriteRenderer_extended.enabled = false;
        } else {
            jointLineRenderer.enabled = false;
            clawSpriteRenderer.enabled = false;

            clawSpriteRenderer_idle.enabled = true;

            clawSpriteRenderer_extended.enabled = false;
        }

        switch(weight) {
            case Weight.light:
                carriedObject.mass = 1;
                break;

            case Weight.medium:
                carriedObject.mass = 2;
                break;

            case Weight.heavy:
                carriedObject.mass = 3;
                break;
        }
    }

    public void UpdateStamina() {
        if (stamina < 100) {
            stamina += Time.deltaTime * 15; // 10 stamina per second
        }

        // Bar Animation
        if (bar_timer.GetMillisecs() > 300) {
            bar_show = !bar_show;
            bar_timer.Reset();
        }

        if (staminaTiredTimer.GetMillisecs() > 3000) {
            staminaTired = false;
        }
    }

    public void ReduceStamina(float value) {
        stamina -= value;

        if (stamina <= 0) {
            stamina = 0;
            staminaTiredTimer = TimerHelper.Create();

            staminaTired = true;
        }
    }

    public void UpdateInput() {
        if (staminaTired) {
            return;
        }


        if (Input.GetKeyDown(keyFlapLeft)) {
            body.AddForce(new Vector2(-forceSide, forceUp));

            if (transform.localScale.x != -1) {
            }

            transform.localScale = new Vector3(-1, 1, 1);

            ReduceStamina((staminaWeakness + 4)* 1);
        }

          if (Input.GetKeyDown(keyFlapRight)) {
            body.AddForce(new Vector2(forceSide, forceUp));

            if (transform.localScale.x != 1) {
            }

            transform.localScale = new Vector3(1, 1, 1);

            ReduceStamina((staminaWeakness + 4) * 1);
        }
    }

    public void UpdatePhysics() {
       Vector2 velocity = body.velocity;

        if (velocity.y > 4) {
            velocity.y = 4;
        }

        if (velocity.y < -4) {
            velocity.y = -4;
        }

        if (velocity.x > 4) {
            velocity.x = 4;
        }

        if (velocity.x < -4) {
            velocity.x = -4;
        }
       
        velocity.x *= 0.95f;

        body.velocity = velocity;

        /*
        if (isGrounded) {
            Vector3 pos = transform.position;
            pos.y += -0.38f;

            clawsGameObject.transform.position = pos;

            clawsGameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
            
            clawsBody.velocity = Vector2.zero;
        }  */
    }

    public void OnCollisionEnter2D(Collision2D collision) {
        if (collision.gameObject.layer != 9) {
            
            isGrounded = true;
        }

        if (collision.gameObject.layer == 10) {
            if (clawsGameObject.transform.childCount > 0) {
                foreach(Transform t in clawsGameObject.transform) {
                    t.parent = PickablesManager.Get().transform;

                    pickupTimer.Reset();
                }
            }
        }
    }

    public void OnCollisionExit2D(Collision2D collision){
        if (collision.gameObject.layer != 9) {
            isGrounded = false;
        }
    }
    
    // Debug Stuff
    public void OnGUI() {
        
       // GUI.Label(new Rect(0, 0, 1000, 100), "Velocity: " + body.velocity);
        //GUI.Label(new Rect(0, 20, 1000, 100), "Is Grounded: " + isGrounded);

        Rect rect = UIHelper.ConvertRect(new Rect(1 + 5, 1 + 5, 25, 5));
        GUI.DrawTexture(rect, guiTextureBackground);

        rect = UIHelper.ConvertRect(new Rect(2+ 5, 2+ 5, 23 * GetStamina(), 3));
        
        if (staminaTired == false) {
            GUI.DrawTexture(rect, guiTextureFill);
        } else {
            if (bar_show) {
                GUI.DrawTexture(rect, guiTextureFillRed);
            }
        }

        rect = UIHelper.ConvertRect(new Rect(1+ 5, 10+ 5, 25, 5));
        GUI.DrawTexture(rect, guiTextureBackground);

        rect = UIHelper.ConvertRect(new Rect(2+ 5, 11+ 5, 23 * GetHunger(), 3));
        
        GUI.DrawTexture(rect, guiTextureFillRed);



        rect = UIHelper.ConvertRect(new Rect(1+ 5, 20+ 5, 25, 5));
        GUI.DrawTexture(rect, guiTextureBackground);

        rect = UIHelper.ConvertRect(new Rect(2+ 5, 21+ 5, 23 * GetStress(), 3));
        
        GUI.DrawTexture(rect, guiTextureFillStress);
    }

    public float GetStamina() {
        return(stamina / 100);
    }

    public float GetHunger() {
        return(hunger / 100);
    }

    public float GetStress() {
        return(stress / 100);
    }
}
