package com.pesets.game;

import java.util.ArrayList;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.scenes.scene2d.Actor;
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.idle.ISceneSwitcher;
import com.pesets.idle.scenes.Scene;
import com.pesets.idle.scenes.ui.SkillBar;
import com.pesets.resources.ResourceManager;

public class GameScene extends Scene implements ClickListener {
	ArrayList<Map> maps = new ArrayList<Map>();
	Map currentMap = null;
	Player player;
	ISceneSwitcher app;
	
	Label playerStateLabel;
	Window playerWnd;
	Label targetStateLabel;
	Window targetWnd;
	Button returnBtn;
	
	public GameScene(ISceneSwitcher application)
	{
		super();
		player = application.getPlayer();
		app = application;
		
		for (int i = 0; i < 12; i++)
		{
			maps.add(new Map(i, 1 + i * 5, (i + 1) * 5));
		}
		setActiveMap(0);
		
		setupUI();
	}
	
	private void setupUI()
	{
		Skin skin = ResourceManager.getSkin();
		playerWnd = new Window(skin);
		playerStateLabel = new Label(skin);
		playerStateLabel.setText("Player\n\nHP:100/100\nExp:1000/5000\nSta: 10\nAgi: 10\nStr: 10\nInt: 10");
		playerWnd.add(playerStateLabel);
		playerWnd.pack();
		playerWnd.x = ui.width() - playerWnd.width - 50;
		
		targetWnd = new Window(skin);
		targetStateLabel = new Label(skin);
		targetWnd.add(targetStateLabel);
		targetWnd.visible = false;
		targetWnd.x = ui.width() - targetWnd.width;
		targetWnd.y = ui.height() - targetWnd.height - 32;
		
		ui.addActor(targetWnd);
		ui.addActor(playerWnd);
		
		returnBtn = new TextButton("Return to city", skin);
		returnBtn.y = ui.height() - returnBtn.height;
		returnBtn.x = ui.centerX() - returnBtn.width / 2;
		returnBtn.setClickListener(this);
		ui.addActor(returnBtn);
		
		SkillBar skillBar = new SkillBar(skin, player);
		ui.addActor(skillBar);
	}
	
	public void setActiveMap(int id)
	{
		Map map = maps.get(id);
		if (currentMap != null)
		{
			currentMap.setPlayer(null);
		}
		map.setPlayer(player);
		currentMap = map;
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0, 0, 0, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		synchronized (maps) {
			currentMap.render();
		}
		super.render(delta);
	}

	@Override
	public void update(float dt) {
		PlayerStatistics.getInstance().totalTimePlayed += dt;
		synchronized (maps) {
			//for (int i = 0; i < 10; i++)
			currentMap.update(dt);
		}
		super.update(dt);
		
		String text = String.format("Player level %d\n\nHealth: %.0f/%.0f\nExperience: %d/%d" +
				"\nStamina: %d\nAgility: " +
				"%d\nStrength: %d\nIntelligence: %d\nWeight: %d/%d", player.getLevel(),
				player.getHP(), player.getMaxHP(), player.getExperience(), player.getMaxExperience(), 
				player.getStamina(),
				player.getAgility(), player.getStrength(), player.getIntelligence(),
				player.getWeight(), player.getMaxWeight());
		text = text + String.format("\n\nAttack power: %.0f\nDamage reduction: %.0f%%\nAttack speed: %.1f sec/hit\nColdown reduction: %.0f%%", 
				player.getAttackPower(), (1 - player.getDamageReduction()) * 100, 
				player.getAttackColdown(), player.getColdownReduction() * 100);
		playerStateLabel.setText(text);
		playerWnd.pack();
		
		if (player.getTarget() != null)
		{
			Enemy target = player.getTarget();
			text = String.format("Enemy level %d\n\nHP:%.0f/%.0f\nDamage: %.0f\nSpeed: %.1f\nArmor %.0f%%", target.getLevel(),
					target.getHP(), target.getMaxHP(),
					target.getAttackPower(), target.getAttackColdown(), (1 - target.getDamageReduction()) * 100);
			targetStateLabel.setText(text);
			targetWnd.pack();
			targetWnd.visible = true;
		}
		else
			targetWnd.visible = false;
	}

	@Override
	public void click(Actor actor, float x, float y) {
		if (actor == returnBtn)
		{
			player.spawn();
			app.setActiveScene(ISceneSwitcher.SceneCity);
		}
	}

	
}
