/**
 * 
 */
package studio.junk.jam.coloritum.ui.systems;

import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.gdx.Gdx;
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.Sprite;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;

import studio.junk.jam.coloritum.screen.InGame;
import studio.junk.jam.coloritum.ui.components.Menu;
import studio.junk.jam.coloritum.ui.components.MenuEntries;
import studio.junk.jam.coloritum.ui.components.MenuEntries.Entry;

/**
 * @author marvin
 */
public class MenuRender extends IteratingSystem {

	@SuppressWarnings("unchecked")
	public static final Family family = Family.all(MenuEntries.class).get();

	public static final int	MENU_ITEM_WIDTH		= 60;
	public static final int	MENU_ITEM_HEIGHT	= 60;

	protected Sprite background;

	public MenuRender(int priority) {
		super(family, priority);
		// TODO Auto-generated constructor stub
	}

	public MenuRender() {
		super(family);
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * com.badlogic.ashley.systems.IteratingSystem#processEntity(com.badlogic.
	 * ashley.core.Entity, float)
	 */
	@Override
	protected void processEntity(Entity entity, float deltaTime) {
		Menu menu = Menu.mapper.get(entity);
		MenuEntries entries = MenuEntries.mapper.get(entity);

		if (!menu.visible) {
			return;
		}

		Vector2 pos = menu.position;


		float height = entries.entries.size * (menu.horizontal ? 1 : MENU_ITEM_HEIGHT);
		float width = entries.entries.size * (menu.horizontal ? MENU_ITEM_WIDTH : 1);
		// DEBUG:
		height = 120;

		background.setAlpha((float) menu.opacity);
		background.setPosition(pos.x, pos.y - height);
		background.setSize(120, height);


		System.out.println(menu.opacity);

		//InGame.currentGame.batch.draw((TextureRegion) GameState.state.assets.get("obstacle90"), 100, 100);

		background.draw(InGame.currentGame.batch);

		int i = 0;
		for (Entry e : entries.entries) {
			InGame.currentGame.batch.draw(e.icon, (menu.horizontal ? i : 0) * MENU_ITEM_WIDTH + pos.x,
					(menu.horizontal ? i : 0) * MENU_ITEM_WIDTH + pos.y, MENU_ITEM_WIDTH, MENU_ITEM_HEIGHT);
			i++;
		}
	}

	public static BoundingBox getMenuBoundingBox(String menuId) {
		Entity entity = Menu.getMenu(menuId);

		Menu menu = Menu.mapper.get(entity);
		MenuEntries entries = MenuEntries.mapper.get(entity);

		Vector3 minimum = new Vector3(menu.position, 0);

		Vector3 maximum = new Vector3((menu.horizontal ? entries.entries.size : 1) * MENU_ITEM_WIDTH + menu.position.x,
				(menu.horizontal ? 1 : entries.entries.size) * MENU_ITEM_HEIGHT + menu.position.y, 0);

		return new BoundingBox(minimum, maximum);
	}

	@Override
	public void addedToEngine(Engine engine) {
		Gdx.app.postRunnable(new Runnable() {

			@Override
			public void run() {
				Pixmap pixmap = new Pixmap(MENU_ITEM_WIDTH, MENU_ITEM_HEIGHT, Format.RGBA8888);
				pixmap.setColor(Color.WHITE);
				pixmap.fill();
				background = new Sprite(new Texture(pixmap));
			}
		});
		super.addedToEngine(engine);
	}

	@Override
	public void removedFromEngine(Engine engine) {
		background.getTexture().dispose();
		super.removedFromEngine(engine);
	}


}
