using UnityEngine;
using System.Collections;

public class Powerup : MonoBehaviour 
{
    public PlayerMovement playerScript;

    private float WaveAmplitude = 0.5f;
    private float WaveFrequency = 4.0f;
    public float fWaveTimer;
    public float fStartingYValue;

    public float HealthRestore;
    public float EthicsRestore;

    // Use this for initialization
    void Start()
    {
        fWaveTimer = Random.value;
        fStartingYValue = transform.position.y;
    }

    // Update is called once per frame
    public void Update()
    {
        fWaveTimer += Time.smoothDeltaTime;
        if (fWaveTimer > WaveFrequency)
            fWaveTimer -= WaveFrequency;

        Vector3 vNewPosition = transform.position;
        vNewPosition.y = fStartingYValue + Mathf.Sin(6.28f * (fWaveTimer / WaveFrequency)) * WaveAmplitude;

        transform.position = vNewPosition;

    }

    protected virtual bool GetPlayerScript()
    {
        GameObject go = GameObject.Find("PlayerMans");
        if (go != null)
        {
            playerScript = go.GetComponent<PlayerMovement>();
        }

        return playerScript != null;
    }

    public void OnTriggerEnter2D(Collider2D other)
    {
        if (playerScript == null && !GetPlayerScript())
            return;

        if (other.gameObject == playerScript.gameObject)
        {
            if( EthicsRestore != 0.0f )
            {
                playerScript.ChangeEthics(EthicsRestore);
                playerScript.BarkFoundJournalisms();
            }
            else if( HealthRestore != 0.0f )
            {
                playerScript.GiveHealth(HealthRestore);
            }

            Destroy(gameObject);
        }
    }
}