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.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.ui.Align;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Window;
import com.pesets.game.Player;
import com.pesets.game.skills.Skill;
import com.pesets.resources.TextureGenerator;

public class SkillTree extends Window {
	private static final int RowSize = 40;
	private static final int IconSize = 32;
	private static final int ColSize = 120;
	
	Player player;
	ShapeRenderer shaper = new ShapeRenderer();
	Skill hoverSkill = null;
	BitmapFont font;
	private int mouseX, mouseY;
	private Label infoLabel;
	private int points = 0;

	public SkillTree(Skin skin, Player player) {
		super(skin);
		font = skin.getFont("default-font");
		this.player = player;
		
		height = RowSize * 7;
		width = ColSize * 3 + 150;
				
		infoLabel = new Label(skin);
		infoLabel.setWrap(true);
		infoLabel.x = ColSize * 3 - ColSize / 4;
		infoLabel.width = width - infoLabel.x - 10;
		infoLabel.setAlignment(Align.TOP, Align.CENTER);
		
		infoLabel.y = y + height - RowSize;
		addActor(infoLabel);
	}
	
	private boolean isSkillCanBeLearned(ArrayList<Skill> skills, int i)
	{
		if (i < 3) return true;
		return skills.get(i - 3).getLevel() >= 5;
	}
	
	private void drawSkill(float x, float y, Skill skill, SpriteBatch batch, boolean canBeLearned)
	{
		boolean colorChanged = false;
		if (!canBeLearned)
		{
			colorChanged = true;
			batch.setColor(0.4f, 0.4f, 0.4f, 1);
		}
		batch.draw(TextureGenerator.getInstance().getSkillIcon(skill.getIcon()), x, y);
		if (skill.getLevel() > 0)
			font.draw(batch, String.valueOf(skill.getLevel()), x + IconSize - font.getSpaceWidth(), y + font.getCapHeight());
		
		if (hoverSkill == null && mouseX > x && mouseY < y + IconSize && mouseX < x + IconSize && mouseY > y)
		{
			hoverSkill = skill;
		}
		
		if (colorChanged)
		{
			batch.setColor(1, 1, 1, 1);
		}
	}
	
	private float getSkillPosX(int i)
	{
		return x + (i % 3) * ColSize + ColSize / 2 - IconSize / 2;
	}
	
	private float getSkillPosY(int i)
	{
		return y + height - RowSize - 20 - (i / 3) * RowSize;
	}
	
	@Override
	public void draw(SpriteBatch batch, float parentAlpha) {
		super.draw(batch, parentAlpha);
		hoverSkill = null;
		points = player.getLevel();
		mouseX = (int) (Gdx.input.getX() * stage.width() / Gdx.graphics.getWidth());
		mouseY = (int) (stage.height() - Gdx.input.getY() * stage.height() / Gdx.graphics.getHeight());
		ArrayList<Skill> skills = player.getSkills();
		
		for (int i = 0; i < skills.size(); i++)
		{
			drawSkill(getSkillPosX(i), getSkillPosY(i), skills.get(i), batch, isSkillCanBeLearned(skills, i));
			points -= skills.get(i).getLevel();
		}
		
		if (hoverSkill == null)
		{
			infoLabel.visible = false;
		}
		else
		{
			infoLabel.setText(String.format("%s\n\n%s", hoverSkill.getName(), hoverSkill.getDescription()));
			infoLabel.visible = true;
		}
	}
	
	@Override
	public boolean touchDown(float x, float y, int pointer) {
		if (hoverSkill != null)
		{
			int index = 0;
			ArrayList<Skill> skills = player.getSkills();
			for (int i = 0; i < skills.size(); i++)
				if (skills.get(i) == hoverSkill)
				{
					index = i;
					break;
				}
			
			if (Gdx.input.isKeyPressed(Keys.CONTROL_LEFT))
			{
				if (hoverSkill.getLevel() > 0)
				{
					for (int i = index + 3; i < skills.size(); i += 3)
					{
						if (skills.get(i).getLevel() > 0)
							return super.touchDown(x, y, pointer);
					}
					hoverSkill.setLevel(hoverSkill.getLevel() - 1);
				}
			}
			else
			{
				if (points > 0 && hoverSkill.getLevel() < 5 && isSkillCanBeLearned(skills, index))
					hoverSkill.setLevel(hoverSkill.getLevel() + 1);
			}
		}
		return super.touchDown(x, y, pointer);
	}
}
