using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SFML.Graphics;
using SFML.Window;
using YourBeatingHeart.Main.Graphics;
using YourBeatingHeart.Main.Sound;

namespace YourBeatingHeart.Main.States.Play.Entities
{
    public class HeartEater : PlayStateEntity
    {
        private AnimatedSprite sprite;
        float announcerTimer = 0;

        public HeartEater(PlayState playState, Vector2f position)
            : base(playState, position)
        {
            sprite = new AnimatedSprite("PyramidTop.xml", playState.Game);
            sprite.Position = position;
            sprite.Play("PyramidTop");
            sprite.Pause();
            Layer = PlayState.Layers.HeartEater;
        }

        public void Play()
        {
            sprite.SetFrame(1);
            sprite.Resume();
        }

        public override void Update(float elapsedTime)
        {
            sprite.Update(elapsedTime);
            if (sprite.GetFrame() == 2)
            {
                SoundManager.Instance.PlaySound(SoundEvent.TowerCrush);
                playState.ScreenShake.Shake(1.3f, 0.2f);
            }

            if (announcerTimer > 0)
            {
                announcerTimer -= elapsedTime;
                if (announcerTimer <= 0)
                {
                    announcerTimer = 0;
                    int index = Game.Random(2);
                    if (index == 0)
                        SoundManager.Instance.PlaySound(SoundEvent.AnnouncerBlood1);
                    else if (index == 1)
                        SoundManager.Instance.PlaySound(SoundEvent.AnnouncerBlood2);
                }
            }
        }

        public override void Draw(RenderTarget renderTarget)
        {
            if (sprite.GetFrame() == 0 && sprite.IsPlaying)
            {
                sprite.Pause();
                SoundManager.Instance.PlaySound(SoundEvent.TowerBurp);
                announcerTimer = 1.5f;
                
            }

            renderTarget.Draw(sprite);
        }
    }
}
