package squidpony.squidgrid.gui.gdx;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.math.MathUtils;

import javax.swing.event.MouseInputListener;
import java.awt.event.MouseEvent;

/**
 * This mouse processor allows for easy conversion to a grid based system. This
 * class covers all aspects of mouse movement and clicking, passing those off
 * to a given InputProcessor after converting to cell-based grid coordinates
 * instead of pixel-based screen coordinates. It also passes off scroll events
 * to the InputProcessor without additional changes.
 *
 * This class is meant to be used as a wrapper to your own mouse InputProcessor,
 * it simply converts the coordinates from UI Component x,y to Grid based x,y
 *
 * @author Eben Howard - http://squidpony.com - howard@squidpony.com
 * @author Tommy Ettinger
 */
public class SquidMouse implements InputProcessor {

    protected int cellWidth, cellHeight, offsetX, offsetY, gridWidth, gridHeight;
    protected InputProcessor processor;

    /**
     * Sets the size of the cell so that all mouse input can be evaluated as
     * relative to the grid. All input is passed to the provided InputProcessor once
     * it has had its coordinates translated to grid coordinates.
     *
     * Offsets are initialized to 0 here, and the grid is assumed to take up the
     * full game window.
     *
     * @param cellWidth
     * @param cellHeight
     * @param processor an InputProcessor that implements some of touchUp(), touchDown(), touchDragged(), mouseMoved(), or scrolled().
     */
    public SquidMouse(int cellWidth, int cellHeight, InputProcessor processor) {
        this.cellWidth = cellWidth;
        this.cellHeight = cellHeight;
        this.processor = processor;
        this.offsetX = 0;
        this.offsetY = 0;
        this.gridWidth = Gdx.graphics.getWidth() / cellWidth;
        this.gridHeight = Gdx.graphics.getHeight() / cellHeight;
    }

    /**
     * Sets the size of the cell so that all mouse input can be evaluated as
     * relative to the grid. Offsets can be specified for x and y if the grid
     * is displayed at a position other than the full screen. Instead of
     * specifying an offset from the bottom and right edges, specify the
     * width and height in grid cells of the area to receive input. All input
     * is passed to the provided InputProcessor once it's had its coordinates
     * translated to grid coordinates.
     *
     * If the input is not within the bounds of the grid as determined by
     * gridWidth, gridHeight, offsetX, and offsetY, the input will be clamped.
     *
     * @param cellWidth
     * @param cellHeight
     * @param gridWidth
     * @param gridHeight
     * @param offsetX
     * @param offsetY
     * @param processor an InputProcessor that implements some of touchUp(), touchDown(), touchDragged(), mouseMoved(), or scrolled().
     */
    public SquidMouse(int cellWidth, int cellHeight, int gridWidth, int gridHeight, int offsetX, int offsetY, InputProcessor processor) {
        this.cellWidth = cellWidth;
        this.cellHeight = cellHeight;
        this.processor = processor;
        this.offsetX = offsetX;
        this.offsetY = offsetY;
        this.gridWidth = gridWidth;
        this.gridHeight = gridHeight;
    }

    public int getCellWidth() {
        return cellWidth;
    }

    public int getCellHeight() {
        return cellHeight;
    }

    public int getOffsetX() {
        return offsetX;
    }

    public int getOffsetY() {
        return offsetY;
    }

    public int getGridWidth() {
        return gridWidth;
    }

    public int getGridHeight() {
        return gridHeight;
    }

    public void setCellWidth(int cellWidth) {
        this.cellWidth = cellWidth;
    }

    public void setCellHeight(int cellHeight) {
        this.cellHeight = cellHeight;
    }

    public void setOffsetX(int offsetX) {
        this.offsetX = offsetX;
    }

    public void setOffsetY(int offsetY) {
        this.offsetY = offsetY;
    }

    public void setGridWidth(int gridWidth) {
        this.gridWidth = gridWidth;
    }

    public void setGridHeight(int gridHeight) {
        this.gridHeight = gridHeight;
    }


    public void reinitialize(int cellWidth, int cellHeight)
    {
        this.cellWidth = cellWidth;
        this.cellHeight = cellHeight;
        this.offsetX = 0;
        this.offsetY = 0;
        this.gridWidth = Gdx.graphics.getWidth() / cellWidth;
        this.gridHeight = Gdx.graphics.getHeight() / cellHeight;
    }
    public void reinitialize(int cellWidth, int cellHeight, int gridWidth, int gridHeight, int offsetX, int offsetY)
    {
        this.cellWidth = cellWidth;
        this.cellHeight = cellHeight;
        this.offsetX = offsetX;
        this.offsetY = offsetY;
        this.gridWidth = gridWidth;
        this.gridHeight = gridHeight;
    }

    /**
     * Gets the InputProcessor this object uses to handle mouse input.
     * @return the wrapped InputProcessor.
     */
    public InputProcessor getProcessor() {
        return processor;
    }

    /**
     * Sets the InputProcessor this object uses to handle mouse input.
     * @param processor an InputProcessor that implements some of touchUp(), touchDown(), touchDragged(), mouseMoved(), or scrolled().
     */
    public void setProcessor(InputProcessor processor) {
        this.processor = processor;
    }

    protected int translateX(int screenX) {
        return MathUtils.clamp((screenX - offsetX) / cellWidth, 0, gridWidth - 1);
    }
    protected int translateY(int screenY) {
        return MathUtils.clamp((screenY - offsetY) / cellHeight, 0, gridHeight - 1);
    }

    @Override
    public boolean keyDown(int keycode) {
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        return processor.touchDown(translateX(screenX), translateY(screenY), pointer, button);
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        return processor.touchUp(translateX(screenX), translateY(screenY), pointer, button);
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        return processor.touchDragged(translateX(screenX), translateY(screenY), pointer);
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        return processor.mouseMoved(translateX(screenX), translateY(screenY));
    }

    @Override
    public boolean scrolled(int amount) {
        return processor.scrolled(amount);
    }
}

@@ -89,24 +89,22 @@
 dx.Input
-Processo
+Adapte
 r;%0Aimpor
@@ -126,102 +126,62 @@
 gdx.
-math.MathUtils;%0A%0Aimport javax.swing.event.MouseInputListener;%0Aimport java.awt.event.MouseEvent
+InputProcessor;%0Aimport com.badlogic.gdx.math.MathUtils
 ;%0A%0A/
@@ -842,32 +842,27 @@
 use 
-implement
+extend
 s Input
-Processo
+Adapte
 r %7B%0A
@@ -5640,265 +5640,8 @@
  %7D%0A%0A
-    @Override%0A    public boolean keyDown(int keycode) %7B%0A        return false;%0A    %7D%0A%0A    @Override%0A    public boolean keyUp(int keycode) %7B%0A        return false;%0A    %7D%0A%0A    @Override%0A    public boolean keyTyped(char character) %7B%0A        return false;%0A    %7D%0A%0A
     

extend InputAdapter instead of implementing interface