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

namespace YourBeatingHeart.Main.States.Play.Levels
{
    class TempleBlood : Entity
    {
        private Texture texture;

        private Sprite[] sprites;
        private const int numSprites = 15;
        private const int spriteHeight = 5;

        private int position = 0;
        private int width = 893;
        private int height = 626;

        public PlayState PlayState;

        public TempleBlood(Game game, Vector2f position, String levelName)
            : base(game, position)
        {
            texture = Game.LoadTexture(levelName + "_Blood.png");

            sprites = new Sprite[numSprites];
            for (int i = 0; i < numSprites; ++i)
            {
                sprites[i] = new Sprite(texture);
                float alpha = 255 * ((float)(numSprites - i) / (float)(numSprites));
                sprites[i].Color = new Color(255, 255, 255, (byte)alpha);
                sprites[i].Position = position;
            }

            Layer = PlayState.Layers.TempleBlood;
        }

        public override void Update(float elapsedTime)
        {
            position = PlayState.totalScore * 50;
        }

        public override void Draw(RenderTarget renderTarget)
        {
            for (int i = 0; i < numSprites; ++i)
            {
                int top = position - (numSprites - i) * spriteHeight;
                int bottom = top + spriteHeight;

                top = Math.Max(0, top);
                top = Math.Min(height, top);
                bottom = Math.Max(0, bottom);
                bottom = Math.Min(height, bottom);

                // alpha 100 layer always starts at the top
                if (i == 0)
                    top = 0;

                sprites[i].Position = new Vector2f(Position.X, Position.Y + top);
                sprites[i].TextureRect = new IntRect(0, top, width, bottom - top);
                renderTarget.Draw(sprites[i]);
            }
        }
    }
}
