using UnityEngine;
using System.Collections;

public class Glyph : MonoBehaviour {

    public Sprite FireTexture;
    public Sprite EarthTexture;
    public Sprite WaterTexture;
    public Sprite WindTexture;

    public GameObject GlyphImage;
    public GameObject GlyphShadow;

    public float FollowForce = 2;
    public float FollowDistance = 2;
    public float CollectDistance = 1;

    public float HeightAboveGround = 0;
    public float HeightVelocity;
    SpriteRenderer ShadowSprite;

    public DamageType Type;

    void OnDrawGizmosSelected()
    {
        Gizmos.color = new Color(1, 1, 1, 0.5f);
        Gizmos.DrawSphere(transform.position, FollowDistance);
        Gizmos.DrawSphere(transform.position, CollectDistance);
    }

    CharacterData PlayerCharacter = null;

	// Use this for initialization
	void Start () {
        setType(Type);
	}

    void Awake()
    {
        ShadowSprite = GlyphShadow.GetComponent<SpriteRenderer>();
    }

    public void setType(DamageType type)
    {
        SpriteRenderer imageSprite = GlyphImage.GetComponent<SpriteRenderer>();
        switch (type)
        {
            case DamageType.Fire:
                imageSprite.sprite = FireTexture;
                ShadowSprite.sprite = FireTexture;
                break;
            case DamageType.Earth:
                imageSprite.sprite = EarthTexture;
                ShadowSprite.sprite = EarthTexture;
                break;
            case DamageType.Water:
                imageSprite.sprite = WaterTexture;
                ShadowSprite.sprite = WaterTexture;
                break;
            case DamageType.Wind:
                imageSprite.sprite = WindTexture;
                ShadowSprite.sprite = WindTexture;
                break;
        }
        Type = type;
    }

    void FixedUpdate()
    {
        HeightVelocity -= 0.1f * Time.fixedDeltaTime;
        HeightAboveGround += HeightVelocity;
        if (HeightAboveGround < 0)
        {
            if (Mathf.Abs(HeightVelocity) > 0.01f)
            {
                HeightAboveGround *= -1;
                HeightVelocity *= -0.7f;
            }
            else
            {
                HeightVelocity = 0;
                HeightAboveGround = 0;
            }
        }
        GlyphImage.transform.localPosition = new Vector3(0, HeightAboveGround, -0.1f);
        ShadowSprite.color = new Color(0, 0, 0, 0.8f * (1.0f - Mathf.Clamp01(HeightAboveGround / 3)));

        if (PlayerCharacter == null)
        {
            PlayerCharacter = CharacterManager.instance.PlayerCharacter;
        }

        if (PlayerCharacter != null)
        {
            Vector2 toPlayerVector = new Vector2(PlayerCharacter.transform.position.x - transform.position.x, PlayerCharacter.transform.position.y - transform.position.y);
            if (toPlayerVector.sqrMagnitude < FollowDistance * FollowDistance)
            {
                if (toPlayerVector.sqrMagnitude < CollectDistance * CollectDistance)
                {
                    PlayerCharacter.addGlyphToStack(Type);
					PlayerCharacter.audio.Play();
                    Destroy(gameObject);
                }
                else
                {
                    float distance = toPlayerVector.magnitude;
                    float force = (1.0f - distance / FollowDistance) * FollowForce;

                    toPlayerVector /= distance;
                    toPlayerVector *= force;
                    rigidbody2D.AddForce(toPlayerVector);
                }
            }
        }
	}
}
