using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YourBeatingHeart.Main.States.Play.Entities;
using YourBeatingHeart.Main.Graphics;

namespace YourBeatingHeart.Main.States.Play.Data
{
    public class PlayerHeart
    {
        public float DurationLeft { get; protected set; }
        public Player OriginalPlayer { get; protected set; }
        public bool Alive { get { return DurationLeft > 0; } }

        public PlayerHeart(Player player)
        {
            DurationLeft = Constants.HEART_BEATING_DURATION;
            OriginalPlayer = player;
        }

        public void Update(float elapsedTime)
        {
            if (!Alive)
                return;

            DurationLeft -= elapsedTime;
            if (DurationLeft < 0)
            {
                DurationLeft = 0;
                HeartStoppedBeating();
            }
        }

        private void HeartStoppedBeating()
        {
            Destroy();
        }

        public void Destroy()
        {
            OriginalPlayer.Respawn(false);
        }

        public float CurrentHeartBeatSpeed { get { return CalculateHeartBeatSpeed(PercentLeft); } }
        public float CurrentHeartBeatStrength { get { return CalculateHeartBeatStrength(PercentLeft); } }

        public float PercentLeft { get { return DurationLeft / Constants.HEART_BEATING_DURATION; } }

        public static float CalculateHeartBeatSpeed(float percentLeft)
        {
            const float lowLevel = 0.5f;
            return lowLevel + percentLeft * (1-lowLevel);
        }

        public static float CalculateHeartBeatStrength(float percentLeft)
        {
            return percentLeft;
        }
    }
}
