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

namespace YourBeatingHeart.Main.Graphics
{
    public class Animation
    {
        int currentFrame;
        List<IntRect> frameRects;
        List<Vector2f> offsets;
        int frameCount;
        float frameTime;
        float frameTimer;
        bool isAnimating = true;

        public IntRect CurrentFrameRect { get { return frameRects[currentFrame]; } }
        public Vector2f CurrentOffset { get { return offsets[currentFrame]; } }
        public bool IsAnimating { get { return isAnimating; } }

        public Animation(List<AnimatedSprite.FrameInfo> _frames, int fps)
        {
            currentFrame = 0;
            frameCount = _frames.Count;
            frameTime = 1f / fps;
            frameRects = new List<IntRect>();
            offsets = new List<Vector2f>();

            foreach (AnimatedSprite.FrameInfo info in _frames)
            {
                frameRects.Add(info.FrameRect);
                offsets.Add(info.Offset);
            }

            /*int pX = 0;
            int pY = 0;

            for (int i = _offset; i < _offset + _frameCount; i++)
            {
                pX = (i * _frameWidth) % _textureWidth;
                pY = ((i * _frameWidth) / _textureWidth) * _frameHeight;
                frameRects.Add(new IntRect(pX, pY, _frameWidth, _frameHeight));
            }*/
        }

        public void Pause()
        {
            isAnimating = false;
        }

        public void Play()
        {
            isAnimating = true;
        }

        public void SetFrame(int _index)
        {
            if (_index >= 0 && _index < frameCount)
            {
                currentFrame = _index;
                frameTimer = 0;
            }
        }

        public int GetFrame()
        {
            return currentFrame;
        }

        public void Reset()
        {
            currentFrame = 0;
            frameTimer = 0;
        }

        public void Update(float _elapsedTime)
        {
            if (isAnimating)
            {
                frameTimer += _elapsedTime;
                if (frameTimer >= frameTime)
                {
                    frameTimer = 0;
                    currentFrame = (currentFrame + 1) % frameCount;
                }
            }
        }
    }
}
