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

public class PlayerVisuals : MonoBehaviour {

    public Color player1Color;
    public Color player2Color;

    public Color skinColorMin;
    public Color skinColorMax;

    public List<Color> hairColors;

    public Color LegColor;

    public Animator legsAnimator;
    public Animator torsoAnimator;

    private int itemTypeID;
    private ItemScript item;

    public SpriteRenderer legs;
    public SpriteRenderer skin;
    public SpriteRenderer hair;
    public SpriteRenderer torso;
    public GameObject hand;

    public void randomizeColors(int playerID)
    {
        legs.color = LegColor;
        hair.color = hairColors[(int)Random.Range(0,hairColors.Count)];
        float bla = Random.value;
        skin.color = new Color(skinColorMin.r + (skinColorMax.r - skinColorMin.r) * bla, skinColorMin.g + (skinColorMax.g - skinColorMin.g) * bla, skinColorMin.b + (skinColorMax.b - skinColorMin.b) * bla);
        if (playerID == 1)
        {
            torso.color = player1Color;
        }
        else
        {
            torso.color = player2Color;
        }
    }

	// Use this for initialization
	void Start () {

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

    public void move(bool moving)
    {
        legsAnimator.SetBool("moving",moving);
    }

    public void jump(bool jumping)
    {
        legsAnimator.SetBool("inAir",jumping);
    }

    public void collect(ItemScript item)
    {
        legsAnimator.SetTrigger("pickUp");
        torsoAnimator.SetTrigger("pickUp");
        updateItemTypeID(item);
        item.transform.parent = hand.transform;
        item.transform.localRotation = new Quaternion();
        item.transform.localPosition = Vector3.zero;
        item.transform.localScale = Vector3.one;
    }

    public void useItem()
    {
        if(item != null && itemTypeID == 2)
        {
            item.transform.parent = null;
            item = null;
        }
        torsoAnimator.SetTrigger("useItem");
        updateItemTypeID(this.item);
    }

    public void updateItemTypeID(ItemScript item)
    {
        this.item = item;
        if (item == null)
        {
            itemTypeID = 0;
            torsoAnimator.SetInteger("itemType", itemTypeID);
            return;
        }
        if (item.GetComponent<MeeleWapeon>() != null)
        {
            itemTypeID = 1;
            torsoAnimator.SetInteger("itemType", itemTypeID);
            return;
        }

        if (item.GetComponent<RangedWapeon>() != null)
        {
            itemTypeID = 2;
            torsoAnimator.SetInteger("itemType", itemTypeID);
            return;
        }
    }

    public void drop()
    {
        item.transform.parent = null;
        item = null;
        updateItemTypeID(this.item);
    }

}
