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

public class Pickable : MonoBehaviour {
    public enum Type {Branch, Rock, Apple, Weapon};
    public Type type = Type.Branch;

    Rigidbody2D body;

    static private List<Pickable> list = new List<Pickable>();

    void OnEnable() {
		list.Add (this);
	}

	void OnDisable() {
		list.Remove (this);
	}

	static public List<Pickable> GetList() {
		return(new List<Pickable>(list));
	}

    void Start() {
        body = GetComponent<Rigidbody2D>();
    }

    void Update() {
        PlayerController player = PlayerController.Get();

        if (player == null || player.clawsGameObject == null) {
            return;
        }

        if (player.clawsGameObject.transform.childCount < 1) {
            if (player.pickupTimer.GetMillisecs() > 1000) { 
                if (Vector2.Distance(player.transform.position, transform.position) < 1f) {
                    transform.parent = player.clawsGameObject.transform;
                    switch(type) {
                        case Type.Branch:
                            player.weight = PlayerController.Weight.medium;
                            break;

                        case Type.Rock:
                            player.weight = PlayerController.Weight.heavy;
                            break;
                    }
                }
            }
        }

        if (transform.parent == player.clawsGameObject.transform) {
            transform.localPosition = Vector3.zero;
            if (body == null) {
                gameObject.AddComponent<PolygonCollider2D>();
                body = gameObject.AddComponent<Rigidbody2D>();
            }
            body.velocity = Vector2.zero;
        }

        if (Input.GetKeyDown(KeyCode.Space)) {
            if (transform.parent == player.clawsGameObject.transform) {
                transform.parent = PickablesManager.Get().transform;
                player.pickupTimer.Reset();

                if (body == null) {
                    gameObject.AddComponent<PolygonCollider2D>();
                    body = gameObject.AddComponent<Rigidbody2D>();
           
                }
                body.velocity = Vector2.zero;

                player.weight = PlayerController.Weight.light;
            }
            
        }

        if (Input.GetKeyDown(KeyCode.E)) {
            if (transform.parent == player.clawsGameObject.transform && type == Type.Apple) {
               
                player.hunger += 10;
                if (player.hunger > 100) {
                    player.hunger = 100;
                }


                transform.parent = PickablesManager.Get().transform;
                player.pickupTimer.Reset();

                if (body == null) {
                    gameObject.AddComponent<PolygonCollider2D>();
                    body = gameObject.AddComponent<Rigidbody2D>();
           
                }
                body.velocity = Vector2.zero;

                player.weight = PlayerController.Weight.light;

                Destroy(gameObject);
            }
        }
    }
}
