using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SFML.Window;

namespace YourBeatingHeart.Main.States.Play
{
    public class ScreenShake
    {
        readonly Vector2f OFFSET_1 = -new Vector2f(10, 20);
        readonly Vector2f OFFSET_2 = new Vector2f(10, 20);
        readonly Vector2f OFFSET_3 = new Vector2f(-5, 15);
        readonly Vector2f OFFSET_4 = new Vector2f(-5, -10);

        public Vector2f CurrentOffset { get; private set; }
        float strength = 0;
        float shakeTimer = 0;
        float shakeTime = 0;

        public ScreenShake()
        {
            CurrentOffset = new Vector2f(0, 0);
        }

        public void Shake(float _strength, float _shakeTime)
        {
            strength = _strength;
            shakeTime = _shakeTime;
            shakeTimer = _shakeTime;
        }

        public void Update(float _elapsedTime)
        {
            if (shakeTimer > 0)
            {
                shakeTimer -= _elapsedTime;

                Vector2f pos1 = OFFSET_1 * strength;
                Vector2f pos2 = OFFSET_2 * strength;
                Vector2f pos3 = OFFSET_3 * strength;
                Vector2f pos4 = OFFSET_4 * strength;

                float percent = 1.0f - shakeTimer / shakeTime;
                if (percent < 0.2f)
                    CurrentOffset = pos1 * percent / 0.2f * percent / 0.2f;
                else if (percent < 0.4f)
                    CurrentOffset = pos1 + (pos2 - pos1) * (percent - 0.2f) / 0.2f * (percent - 0.2f) / 0.2f;
                else if (percent < 0.6f)
                    CurrentOffset = pos2 + (pos3 - pos2) * (percent - 0.4f) / 0.2f * (percent - 0.4f) / 0.2f;
                else if (percent < 0.8f)
                    CurrentOffset = pos3 + (pos4 - pos3) * (percent - 0.6f) / 0.2f * (percent - 0.6f) / 0.2f;
                else
                    CurrentOffset = pos4 + (-pos4) * (percent - 0.8f) / 0.2f * (percent - 0.8f) / 0.2f;

                if (shakeTimer <= 0)
                {
                    shakeTimer = 0;
                    CurrentOffset = new Vector2f(0, 0);
                }
            }
            
        }

    }
}
