using ActionGame.GameArtifacts.Models.Characters;
using ActionGame.GameResources.Models;


namespace ActionGame.GameArtifacts.Operators
{
    public class CharacterSetAnimator
    {
        private readonly ICharacter character;


        public CharacterSetAnimator(ICharacter character) 
        {
            this.character = character;
        }


        public void Update()
        {
            MovementPhysics physics = character.MovementPhysics;
            CharacterSet.Frame currentFrame = character.CharacterSet.CurrentFrame;
            CharacterSet.Frame newFrame = currentFrame;

            if(currentFrame == CharacterSet.Frame.Down)
            {
                if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
                else if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
            }
            else if (currentFrame == CharacterSet.Frame.Down_1)
            {
                if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_2;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
                else if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
                else newFrame = CharacterSet.Frame.Down;
            }
            else if (currentFrame == CharacterSet.Frame.Down_2)
            {
                if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_3;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
                else if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
                else newFrame = CharacterSet.Frame.Down;
            }
            else if (currentFrame == CharacterSet.Frame.Down_3)
            {
                if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
                else if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
                else newFrame = CharacterSet.Frame.Down;
            }
            else if (currentFrame == CharacterSet.Frame.Up)
            {
                if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
                else if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
            }
            else if (currentFrame == CharacterSet.Frame.Up_1)
            {
                if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_2;
                else if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
                else newFrame = CharacterSet.Frame.Up;
            }
            else if (currentFrame == CharacterSet.Frame.Up_2)
            {
                if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_3;
                else if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
                else newFrame = CharacterSet.Frame.Up;
            }
            else if (currentFrame == CharacterSet.Frame.Up_3)
            {
                if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
                else if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
                else newFrame = CharacterSet.Frame.Up;
            }
            else if (currentFrame == CharacterSet.Frame.Left)
            {
                if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
                else if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
            }
            else if (currentFrame == CharacterSet.Frame.Left_1)
            {
                if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_2;
                else if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
                else newFrame = CharacterSet.Frame.Left;
            }
            else if (currentFrame == CharacterSet.Frame.Left_2)
            {
                if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_3;
                else if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
                else newFrame = CharacterSet.Frame.Left;
            }
            else if (currentFrame == CharacterSet.Frame.Left_3)
            {
                if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
                else if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
                else newFrame = CharacterSet.Frame.Left;
            }
            else if (currentFrame == CharacterSet.Frame.Right)
            {
                if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
                else if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
            }
            else if (currentFrame == CharacterSet.Frame.Right_1)
            {
                if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_2;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
                else if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
                else newFrame = CharacterSet.Frame.Right;
            }
            else if (currentFrame == CharacterSet.Frame.Right_2)
            {
                if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_3;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
                else if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
                else newFrame = CharacterSet.Frame.Right;
            }
            else if (currentFrame == CharacterSet.Frame.Right_3) 
            {
                if (physics.VelocityX > 0) newFrame = CharacterSet.Frame.Right_1;
                else if (physics.VelocityX < 0) newFrame = CharacterSet.Frame.Left_1;
                else if (physics.VelocityY > 0) newFrame = CharacterSet.Frame.Down_1;
                else if (physics.VelocityY < 0) newFrame = CharacterSet.Frame.Up_1;
                else newFrame = CharacterSet.Frame.Right;
            }

            character.CharacterSet.CurrentFrame = newFrame;
        }

    }
}
