package com.pesets.idle.scenes.ui;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.ui.Align;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.ClickListener;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.pesets.game.Player;
import com.pesets.game.items.EquipItem;
import com.pesets.game.items.Item;
import com.pesets.resources.TextureGenerator;

public class Inventory extends Window implements ClickListener {
	private static final int CellSize = 40;
	private static final int IconSize = 32;
	private static final int EquipSize = 5;
	private static final int InventorySize = 50;
	private static final int InvWidth = 410;
	private Player player;
	private ShapeRenderer shaper = new ShapeRenderer();
	private final float EqStartX, EqStartY;
	private final float InvStartX, InvStartY;
	private Label nameLabel;
	private Label infoLabel;
	private Button sortButton;
	private Item hoverItem = null;
	private int mouseX, mouseY;

	public Inventory(Skin skin, Player player) {
		super("Inventory", skin);

		this.player = player;
		width = 600;
		height = 300;
		EqStartX = CellSize * EquipSize / 2f;
		EqStartY = height - 64;
		InvStartX = - IconSize + CellSize;
		InvStartY = EqStartY - CellSize * 1.5f;
		
		nameLabel = new Label(skin);
		nameLabel.setWrap(true);
		nameLabel.width = width - InvWidth;
		nameLabel.setAlignment(Align.TOP, Align.CENTER);
		nameLabel.x = InvWidth;
		nameLabel.y = EqStartY;
		addActor(nameLabel);
		
		infoLabel = new Label(skin);
		infoLabel.setWrap(true);
		infoLabel.width = width - InvWidth;
		infoLabel.setAlignment(Align.TOP, Align.CENTER);
		infoLabel.x = InvWidth;
		infoLabel.y = EqStartY - 40;
		addActor(infoLabel);
		
		sortButton = new TextButton("Sort", skin);
		sortButton.setClickListener(this);
		add(sortButton).expand().bottom().right();
	}

	public void drawItem(float x, float y, Item it, SpriteBatch batch)
	{
		boolean colorChanged = false;
		if (it instanceof EquipItem && ((EquipItem) it).getLevel() > player.getLevel())
		{
			batch.setColor(1, 0.3f, 0.3f, 1);
			colorChanged = true;
		}
		batch.draw(TextureGenerator.getInstance().getIcon(it.getItemIcon()), x, y);
		if (colorChanged)
		{
			batch.setColor(1, 1, 1, 1);
		}
		if (hoverItem == null && mouseX > x && mouseY < y + IconSize && mouseX < x + IconSize && mouseY > y)
		{
			hoverItem = it;
		}
	}
	
	

	@Override
	protected void drawBackground(SpriteBatch batch, float parentAlpha) {
		super.drawBackground(batch, parentAlpha);
		batch.end();
		shaper.begin(ShapeType.FilledRectangle);
			shaper.setColor(0.2f, 0.2f, 0.2f, 1);
			float x = EqStartX + this.x;
			float y = EqStartY + this.y;
			for (int i = 0; i < EquipSize; i++)
			{
				shaper.filledRect(x, y, IconSize, IconSize);
				x += CellSize;
			}
			x = InvStartX + this.x;
			y = InvStartY + this.y;
			for (int i = 0; i < InventorySize; i++)
			{
				shaper.filledRect(x, y, IconSize, IconSize);
				x += CellSize;
				if (x + CellSize >= InvWidth + this.x)
				{
					x = InvStartX + this.x;
					y -= CellSize;
				}
			}
		shaper.end();
		batch.begin();
	}

	@Override
	public void draw(SpriteBatch batch, float parentAlpha) {
		super.draw(batch, parentAlpha);
		hoverItem = null;
		mouseX = (int) (Gdx.input.getX() * stage.width() / Gdx.graphics.getWidth());
		mouseY = (int) (stage.height() - Gdx.input.getY() * stage.height() / Gdx.graphics.getHeight());
		
		EquipItem[] equip = player.getEquip();
		float x = EqStartX + this.x;
		float y = EqStartY + this.y;
		
		for (EquipItem it : equip)
		{
			if (it != null)
				drawItem(x, y, it, batch);
			x += CellSize;
		}
		
		
		ArrayList<Item> items = player.getInventory();
		x = InvStartX + this.x;
		y = InvStartY + this.y;
		for (Item it : items)
		{
			if (it != null)
				drawItem(x, y, it, batch);
			x += CellSize;
			if (x + CellSize >= InvWidth + this.x)
			{
				x = InvStartX + this.x;
				y -= CellSize;
			}
		}
		
		if (hoverItem != null)
		{
			nameLabel.setText(String.format("%s", hoverItem.getItemName()));
			nameLabel.setColor(hoverItem.getNameColor());
			infoLabel.setText(String.format("%s\n\nSells for %d", hoverItem.getItemDesc(), hoverItem.getCost()));
		}
		else
		{
			infoLabel.setText("");
			nameLabel.setText("");
		}
	}

	@Override
	public boolean touchDown(float x, float y, int pointer) {
		if (hoverItem != null)
		{
			if (Gdx.input.isKeyPressed(Keys.CONTROL_LEFT))
			{
				player.dropItem(hoverItem);
			}
			else
				player.useItem(hoverItem);
		}
		return super.touchDown(x, y, pointer);
	}

	@Override
	public void click(Actor actor, float x, float y) {
		player.sortInventory();
	}
	
	
	
	
}
