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;

namespace YourBeatingHeart.Main.States.Play.Entities
{
    public class PlayerPointDisplay : Drawable
    {
        List<AnimatedSprite> pointSprites;
        Sprite nameSprite;
        Game game;
        const int FIVE_WIDTH = 97;
        const int FIVE_HEIGHT = 55;
        string file;
        int currentPointCount = 0;

        Vector2f offset;
        
        public PlayerPointDisplay(Game _game, string _file, Vector2f _offset, string _nameFile)
        {
            offset = _offset;
            game = _game;
            file = _file;
            pointSprites = new List<AnimatedSprite>();
            nameSprite = new Sprite(_game.LoadTexture(_nameFile));
            nameSprite.Position = _offset - new Vector2f(nameSprite.Texture.Size.X + 10, 0);
        }

        public void Update(int _currScore, float _elapsedTime)
        {
            foreach (AnimatedSprite s in pointSprites)
                s.Update(_elapsedTime);

            if (currentPointCount != _currScore)
            {
                currentPointCount = _currScore;
                UpdateSprites();
            }
        }

        private void UpdateSprites()
        {
            int count = (currentPointCount - 1)/ 5 + 1;
            if (count > pointSprites.Count)
            {
                if(count > 1)
                    pointSprites[count - 2].SetFrame(4);

                pointSprites.Add(new AnimatedSprite(file, game));
                pointSprites[count - 1].Position = offset + new Vector2f(FIVE_WIDTH * ((count - 1) % 3), FIVE_HEIGHT * ((count - 1) / 3)) -  new Vector2f(0, 7);
                pointSprites[count - 1].Play("strike_");
                pointSprites[count - 1].Pause();
            }

           pointSprites[count - 1].SetFrame((currentPointCount - 1) % 5);
        }

        public void Draw(RenderTarget target, RenderStates states)
        {
            foreach (AnimatedSprite s in pointSprites)
                target.Draw(s, states);
            target.Draw(nameSprite);
        }
    }
}