package com.mygdx.mount.game.manager.game.services;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.mygdx.mount.MountGame;
import com.mygdx.mount.game.actors.Hero;
import com.mygdx.mount.game.manager.GameManager;


public class MoveService {
    GameManager manager;
    float delta;
    boolean isAccelerated;

    public MoveService(GameManager manager) {
        this.manager = manager;
    }

    public void act(Hero hero) {
        isAccelerated = true;
        delta = Gdx.graphics.getDeltaTime();
        TouchService.REALM currentTouch = manager.getCurrentTouch();
        if (currentTouch != null) {
            if (currentTouch.equals(TouchService.REALM.LEFT)) {
                isAccelerated = false;
                slow(hero, delta);
            } else {
                if (currentTouch.equals(TouchService.REALM.RIGHT_UPPER)) {
                    jump(hero);
                } else {
                    doNothing(hero, delta);
                }
            }
        }
        updateHero(hero, delta, manager);
        moveRight(hero, delta, isAccelerated, manager);

    }

    private static void moveRight(Hero hero, float delta, boolean isAccelerated, GameManager manager) {
        float distance = hero.getSpeed() * delta;
        if (hero.getX() + distance > GameManager.SCREEN_WIDTH) {
            hero.setX(0);
        }
        hero.setX(hero.getX() + distance);

        /*if (!manager.getCollisionService().isHeroCollide(hero, manager.getWalls())) {
            hero.coveredDistance += distance;
            if (hero.coveredDistance > Hero.STEP_SIZE) {
                hero.runSpriteNumber = ((hero.runSpriteNumber) % 5) + 1;
                hero.setCurrentSprite(Hero.heroSprites[hero.runSpriteNumber]);
                hero.coveredDistance = 0;
            }
        }*/


        if (!isAccelerated) {
            return;
        }
        hero.setSpeed(hero.getSpeed() + 4 * Hero.MAX_SPEED / (Hero.ACCELERATION_TIME / delta));
        if (hero.getSpeed() > Hero.MAX_SPEED) hero.setSpeed(Hero.MAX_SPEED);

    }

    private static void slow(Hero hero, float delta) {
        if (hero.getSpeed() > 0) {
            hero.setSpeed(hero.getSpeed() - (Hero.SLOW_CONSTANT * delta * 5));
        }
        if (hero.getSpeed() < 0) {
            hero.setSpeed(0);
        }

    }

    private static void jump(Hero hero) {
        if (hero.getState().equals(Hero.State.Standing)) {
            hero.setState(Hero.State.Ascending);
            hero.setHeroJumpHeight(0);
        }
    }

    private static void doNothing(Hero hero, float delta) {

    }

    private static void updateHero(Hero hero, float delta, GameManager manager) {
        if (hero.getState().equals(Hero.State.Standing)) {
            if (!manager.getCollisionService().isHeroCollide(hero, manager.getWalls())) {
                hero.currentJumpSpeed = 0;
                hero.setState(Hero.State.Descending);
                hero.setHeroJumpHeight(0);
            }
            hero.setCurrentSprite(Hero.heroSprites[4]);
        }
        if (hero.getState().equals(Hero.State.Ascending)) {
            hero.setCurrentSprite(Hero.heroSprites[6]);
            float distance = (hero.MAX_SPEED * delta * (hero.currentJumpSpeed / 30));
            System.out.println("up distance:" + distance);
            hero.setY(hero.getY() + distance);
            hero.heroJumpHeight += distance;
            hero.currentJumpSpeed -= 5;
            if (hero.getHeroJumpHeight() > Hero.JUMP_MAX_HEIGHT) {
                hero.setY(hero.getY() + distance);
                hero.currentJumpSpeed = 0;
                hero.setState(Hero.State.Descending);
                hero.setHeroJumpHeight(hero.getY() + hero.getHeroJumpHeight());
                System.out.println("Set Descending");
            }
        }
        if (hero.getState().equals(Hero.State.Descending)) {
            hero.setCurrentSprite(Hero.heroSprites[5]);
            float distance = (hero.currentJumpSpeed * delta * (hero.currentJumpSpeed / 30));
            hero.setHeroJumpHeight(hero.getHeroJumpHeight() - distance);
            hero.currentJumpSpeed += 5;
            if (hero.getY() - distance < 0) {
                hero.setY(0);
                hero.setState(Hero.State.Standing);
                hero.currentJumpSpeed = Hero.JUMP_MAX_HEIGHT;
            } else {
                hero.setY(hero.getY() - distance);
                hero.setHeroJumpHeight(hero.getHeroJumpHeight() - distance);
            }
        }
    }

    public void moveCameraWithHero(Camera camera, Hero hero, Batch batch) {
        camera.position.set(hero.getX(), MountGame.CAMERA_HEIGHT / 2 - 20, 0);
        camera.update();
        batch.setProjectionMatrix(camera.combined);
    }
}

@@ -3276,25 +3276,25 @@
 distance = (
-h
+H
 ero.MAX_SPEE
@@ -3340,67 +3340,8 @@
 ));%0A
-            System.out.println(%22up distance:%22 + distance);%0A
     
@@ -3767,62 +3767,8 @@
 ));%0A
-                System.out.println(%22Set Descending%22);%0A
     

fix jump