package com.udacity.gamedev.gigagal.overlays;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.udacity.gamedev.gigagal.entities.GigaGal;
import com.udacity.gamedev.gigagal.util.Assets;
import com.udacity.gamedev.gigagal.util.Constants;
import com.udacity.gamedev.gigagal.util.Utils;


public class OnscreenControls extends InputAdapter {

    public static final String TAG = OnscreenControls.class.getName();

    public final Viewport viewport;
    public GigaGal gigaGal;
    private Vector2 moveLeftCenter;
    private Vector2 moveRightCenter;
    private Vector2 shootCenter;
    private Vector2 jumpCenter;
    private int moveLeftPointer;
    private int moveRightPointer;
    private int jumpPointer;

    public OnscreenControls() {
        this.viewport = new ExtendViewport(
                Constants.ONSCREEN_CONTROLS_VIEWPORT_SIZE,
                Constants.ONSCREEN_CONTROLS_VIEWPORT_SIZE);


        moveLeftCenter = new Vector2();
        moveRightCenter = new Vector2();
        shootCenter = new Vector2();
        jumpCenter = new Vector2();

        recalculateButtonPositions();
    }


    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {

        Vector2 viewportPosition = viewport.unproject(new Vector2(screenX, screenY));

        if (viewportPosition.dst(shootCenter) < Constants.BUTTON_RAIDUS) {
            gigaGal.shoot();
        } else if (viewportPosition.dst(jumpCenter) < Constants.BUTTON_RAIDUS) {
            jumpPointer = pointer;
            gigaGal.jumpButtonPressed = true;
        } else if (viewportPosition.dst(moveLeftCenter) < Constants.BUTTON_RAIDUS) {
            moveLeftPointer = pointer;
            gigaGal.leftButtonPressed = true;
        } else if (viewportPosition.dst(moveRightCenter) < Constants.BUTTON_RAIDUS) {
            moveRightPointer = pointer;
            gigaGal.rightButtonPressed = true;
        }

        return super.touchDown(screenX, screenY, pointer, button);
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        Vector2 viewportPosition = viewport.unproject(new Vector2(screenX, screenY));

        if (pointer == moveLeftPointer && viewportPosition.dst(moveRightCenter) < Constants.BUTTON_RAIDUS) {
            gigaGal.leftButtonPressed = false;
            moveLeftPointer = 0;

            moveRightPointer = pointer;
            gigaGal.rightButtonPressed = true;
        }

        if (pointer == moveRightPointer && viewportPosition.dst(moveLeftCenter) < Constants.BUTTON_RAIDUS) {
            gigaGal.rightButtonPressed = false;
            moveRightPointer = 0;

            moveLeftPointer = pointer;
            gigaGal.leftButtonPressed = true;
        }

        return super.touchDragged(screenX, screenY, pointer);
    }

    public void render(SpriteBatch batch) {

        viewport.apply();
        batch.setProjectionMatrix(viewport.getCamera().combined);
        batch.begin();

        if (!Gdx.input.isTouched(jumpPointer)) {
            gigaGal.jumpButtonPressed = false;
            jumpPointer = 0;
        }

        if (!Gdx.input.isTouched(moveLeftPointer)) {
            gigaGal.leftButtonPressed = false;
            moveLeftPointer = 0;
        }

        if (!Gdx.input.isTouched(moveRightPointer)) {
            gigaGal.rightButtonPressed = false;
            moveRightPointer = 0;
        }

        Utils.drawTextureRegion(
                batch,
                Assets.instance.onscreenControlsAssets.moveLeft,
                moveLeftCenter,
                Constants.BUTTON_CENTER
        );

        Utils.drawTextureRegion(
                batch,
                Assets.instance.onscreenControlsAssets.moveRight,
                moveRightCenter,
                Constants.BUTTON_CENTER
        );

        Utils.drawTextureRegion(
                batch,
                Assets.instance.onscreenControlsAssets.shoot,
                shootCenter,
                Constants.BUTTON_CENTER
        );

        Utils.drawTextureRegion(
                batch,
                Assets.instance.onscreenControlsAssets.jump,
                jumpCenter,
                Constants.BUTTON_CENTER
        );
        batch.end();

    }

    public void recalculateButtonPositions() {


        moveLeftCenter.set(Constants.BUTTON_RAIDUS * 3 / 4, Constants.BUTTON_RAIDUS);
        moveRightCenter.set(Constants.BUTTON_RAIDUS * 2, Constants.BUTTON_RAIDUS * 3 / 4);

        shootCenter.set(
                viewport.getWorldWidth() - Constants.BUTTON_RAIDUS * 2f,
                Constants.BUTTON_RAIDUS * 3 / 4
        );

        jumpCenter.set(
                viewport.getWorldWidth() - Constants.BUTTON_RAIDUS * 3 / 4,
                Constants.BUTTON_RAIDUS
        );

    }

}

@@ -1566,34 +1566,34 @@
 stants.BUTTON_RA
-I
 D
+I
 US) %7B%0A          
@@ -1676,34 +1676,34 @@
 stants.BUTTON_RA
-I
 D
+I
 US) %7B%0A          
@@ -1842,34 +1842,34 @@
 stants.BUTTON_RA
-I
 D
+I
 US) %7B%0A          
@@ -2013,34 +2013,34 @@
 stants.BUTTON_RA
-I
 D
+I
 US) %7B%0A          
@@ -2468,34 +2468,34 @@
 stants.BUTTON_RA
-I
 D
+I
 US) %7B%0A          
@@ -2764,18 +2764,18 @@
 UTTON_RA
-I
 D
+I
 US) %7B%0A  
@@ -4531,34 +4531,34 @@
 stants.BUTTON_RA
-I
 D
+I
 US * 3 / 4, Cons
@@ -4572,18 +4572,18 @@
 UTTON_RA
-I
 D
+I
 US);%0A   
@@ -4622,26 +4622,26 @@
 ts.BUTTON_RA
-I
 D
+I
 US * 2, Cons
@@ -4647,34 +4647,34 @@
 stants.BUTTON_RA
-I
 D
+I
 US * 3 / 4);%0A%0A  
@@ -4754,26 +4754,26 @@
 ts.BUTTON_RA
-I
 D
+I
 US * 2f,%0A   
@@ -4796,34 +4796,34 @@
 stants.BUTTON_RA
-I
 D
+I
 US * 3 / 4%0A     
@@ -4915,18 +4915,18 @@
 UTTON_RA
-I
 D
+I
 US * 3 /
@@ -4968,10 +4968,10 @@
 N_RA
-I
 D
+I
 US%0A 

Update OnscreenControls.java