package studio.junk.jam.coloritum.entities.systems;

import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.EntitySystem;
import com.badlogic.ashley.core.Family;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Buttons;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

import studio.junk.jam.coloritum.GameState;
import studio.junk.jam.coloritum.entities.Families;
import studio.junk.jam.coloritum.entities.Pigment;
import studio.junk.jam.coloritum.entities.components.MirrorComponent;
import studio.junk.jam.coloritum.entities.components.MirrorComponent.Orientation;
import studio.junk.jam.coloritum.entities.components.Placable;
import studio.junk.jam.coloritum.entities.components.Source;
import studio.junk.jam.coloritum.entities.components.TilePosition;
import studio.junk.jam.coloritum.screen.InGame;

public class UserInputHandler extends EntitySystem {
	protected Texture highlightTex;


	protected InputProcessor inputProcessor = new MyInputProcessor();

	public UserInputHandler() {
		super();

		Pixmap pm = new Pixmap(120, 120, Format.RGBA8888);
		pm.setColor(new Color(255, 255, 255, 0.5f));
		pm.fill();

		highlightTex = new Texture(pm);

		Gdx.input.setInputProcessor(inputProcessor);
	}

	public UserInputHandler(int priority) {
		super(priority);

		Pixmap pm = new Pixmap(120, 120, Format.RGBA8888);
		pm.setColor(new Color(255, 255, 255, 0.5f));
		pm.fill();

		highlightTex = new Texture(pm);
	}

	class MyInputProcessor implements InputProcessor {

		@Override
		public boolean touchUp(int screenX, int screenY, int pointer, int button) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean touchDragged(int screenX, int screenY, int pointer) {
			// TODO Auto-generated method stub
			return false;
		}

		@SuppressWarnings("unchecked")
		@Override
		public boolean touchDown(int screenX, int screenY, int pointer, int button) {
			Vector3 mouseCoordinates = InGame.currentGame.camera.unproject(new Vector3(screenX, screenY, 0));


			int x = MathUtils.floor(mouseCoordinates.x);
			int y = MathUtils.floor(mouseCoordinates.y);

			Vector3 uiCoordinates = InGame.currentGame.cameraUI.unproject(new Vector3(screenX, screenY, 0));
			// MenuInteraction.showMenu(Menu.getMenu("mainMenu", 0), new Vector2(uiCoordinates.x, uiCoordinates.y));

			Entity entity = TilePosition.getEntityAt(x, y);

			//MenuInteraction.onTouch(uiCoordinates.x, uiCoordinates.y, button, entity);

			if (button == Buttons.LEFT) {
				if (entity == null) {
					GameState.state.types.get("placableLightSource").create("placableLightSource", new Vector2(x, y),
							GameState.MAX_EMISSION_PERIOD / 2, Pigment.Red);

				} else if (Families.placableMirror.matches(entity)) {
					GameState.state.types.get("placableMirror").create("placableMirror", new Vector2(x, y),
							MirrorComponent.mapper.get(entity).orientation.swap());
					GameState.state.rmEntity(entity);

				} else if (Families.pacableLightSource.matches(entity)) {
					Source lightSource = Source.mapper.get(entity);
					if (lightSource.emissionPeriod == GameState.MAX_EMISSION_PERIOD) {
						lightSource.emissionPeriod /= 2;
					} else {
						lightSource.emissionPeriod = GameState.MAX_EMISSION_PERIOD;
					}
				}
			} else if (button == Buttons.RIGHT) {
				if (entity == null) {
					GameState.state.types.get("placableMirror").create("placableMirror", new Vector2(x, y),
							Orientation.NorthEast);

				} else if (Families.placableMirror.matches(entity)) {
					GameState.state.rmEntity(entity);

				} else if (Families.pacableLightSource.matches(entity)) {
					GameState.state.rmEntity(entity);
				}
			}

			return true;
		}

		@Override
		public boolean scrolled(int amount) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean mouseMoved(int screenX, int screenY) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean keyUp(int keycode) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean keyTyped(char character) {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean keyDown(int keycode) {
			// TODO Auto-generated method stub
			return false;
		}

	};

	@SuppressWarnings("unchecked")
	@Override
	public void update(float delta) {
		Vector3 mouseCoordinates = InGame.currentGame.camera
				.unproject(new Vector3(Gdx.input.getX(), Gdx.input.getY(), 0));


		int x = MathUtils.floor(mouseCoordinates.x);
		int y = MathUtils.floor(mouseCoordinates.y);

		Entity entity = TilePosition.getEntityAt(x, y);

		if (entity == null || Family.all(Placable.class).get().matches(entity)) {
			InGame.currentGame.batch.draw((TextureRegion) GameState.state.assets.get("selector"), x, y, 1f, 1f);
			//InGame.currentGame.batch.draw(highlightTex, x, y, 1, 1);
		}
	}
}
