﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

using UnityEngine.EventSystems;

[System.Serializable]
public class Health : MonoBehaviour {

    private float maxValue;

    //private int numberLives = 1;
    
    [SerializeField]
    private float amount = 10f;

    // an event to broadcast when the health changes
    [SerializeField, Header("Register to know when my health changes.")]
    private HealthChangedEvent healthChanged;

    // an event to broadcast when the health expires
    [SerializeField, Header("Register to know when my health expires.")]
    private HealthExpiredEvent healthExpired;

    /// <summary>
    /// Gets or sets the health value
    /// </summary>
    public float Amount
    {
        get { return amount; }
        set
        {
            amount = value;

            // Broadcast events
            if (healthChanged != null)
                healthChanged.Invoke(HealthPercent);

            if (HealthPercent <= 0f && healthExpired != null)
                healthExpired.Invoke();
        }
    }

    public float HealthPercent
    {
        get { return Amount / maxValue * 100; }
    }

    // Use this for initialization
    void Start () {
        maxValue = Amount;
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    public void AddBonusLife()
    {
        Amount = maxValue;
    }
}
