package com.pesets.idle.city;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
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.PlayerStatistics;
import com.pesets.idle.ISceneSwitcher;
import com.pesets.idle.scenes.Scene;
import com.pesets.idle.scenes.ui.Inventory;
import com.pesets.idle.scenes.ui.SkillBar;
import com.pesets.idle.scenes.ui.SkillPriority;
import com.pesets.idle.scenes.ui.SkillTree;
import com.pesets.idle.scenes.ui.StatWindow;
import com.pesets.resources.ResourceManager;

public class CityScene extends Scene implements ClickListener {
	private static final int ButtonsWidth = 100;
	
	private Player player;
	private ISceneSwitcher app;
	private SpriteBatch batch = new SpriteBatch();
	private Sprite bgSprite;
	
	private Button btnStatus, btnInventory, btnSkills, btnMap;
	private Button btnResetStatus;
	private Window menuWnd, statusWnd, mapWnd;
	private Label statusLabel;
	private Button[] locations;
	private Inventory inventory;
	private SkillBar skillBar;
	private SkillTree skillTree;
	private StatWindow stats;
	private SkillPriority skillPrior;
	private TextButton btnSound;
	
	public CityScene(ISceneSwitcher application)
	{
		super();
		app = application;
		this.player = app.getPlayer();
		
		bgSprite = new Sprite(ResourceManager.getAssetManager().get(ResourceManager.textureFolder + "cityBG.png", Texture.class), 1024, 600);
		
		Skin skin = ResourceManager.getSkin();
		menuWnd = new Window(skin);
		
		btnStatus = new TextButton("Statistics", skin);
		btnStatus.width(ButtonsWidth);
		btnStatus.setClickListener(this);
		btnInventory = new TextButton("Inventory", skin);
		btnInventory.width(ButtonsWidth);
		btnInventory.setClickListener(this);
		btnSkills = new TextButton("Skills", skin);
		btnSkills.width(ButtonsWidth);
		btnSkills.setClickListener(this);
		btnMap = new TextButton("Map", skin);
		btnMap.width(ButtonsWidth);
		btnMap.setClickListener(this);
		
		menuWnd.add(btnStatus);
		menuWnd.add(btnInventory);
		menuWnd.add(btnSkills);
		menuWnd.add(btnMap);
		
		menuWnd.pack();
		menuWnd.y = ui.height() - menuWnd.height;
		ui.addActor(menuWnd);

		stats = new StatWindow(skin, player);
		statusWnd = new Window("Player info", skin);
		statusLabel = new Label(skin);
		statusLabel.setAlignment(Align.CENTER);
		statusWnd.add(statusLabel);
		updateStatus();
		statusWnd.pack();
		statusWnd.x = 0;
		statusWnd.y = ui.height() - menuWnd.height - statusWnd.height;
		
		ui.addActor(statusWnd);
		
		mapWnd = new Window("Map", skin);
		mapWnd.width = 600;
		mapWnd.height = 400;
		mapWnd.x = ui.centerX() - mapWnd.width / 2;
		mapWnd.y = ui.centerY() - mapWnd.height / 2;
		mapWnd.visible = false;
		
		locations = new Button[9];
		for (int i = 0; i < locations.length; i++)
		{
			locations[i] = new TextButton(String.format("Location %d [%d-%d]", i, 1 + i * 5, (i + 1) * 5), skin);
			mapWnd.add(locations[i]).expand();
			locations[i].setClickListener(this);
			if ((i % 3) == 2)
				mapWnd.row();
		}
		
		ui.addActor(mapWnd);
		
		inventory = new Inventory(skin, player);
		inventory.x = ui.centerX() - inventory.width / 2;
		inventory.y = ui.centerY() - inventory.height / 2;
		inventory.visible = false;
		ui.addActor(inventory);
		
		skillTree = new SkillTree(skin, player);
		skillTree.x = ui.centerX() - skillTree.width / 2;
		skillTree.y = ui.centerY() - skillTree.height / 2;
		skillTree.visible = false;
		ui.addActor(skillTree);
		
		skillBar = new SkillBar(skin, player);
		skillBar.x = ui.centerX() - skillBar.width / 2;
		skillBar.y = skillTree.y - skillBar.height * 2;
		skillBar.visible = false;
		ui.addActor(skillBar);
		
		skillPrior = new SkillPriority(skin, player);
		skillPrior.x = ui.width() - skillPrior.width - 50;
		skillPrior.y = skillTree.y + skillTree.height;
		skillPrior.visible = false;
		ui.addActor(skillPrior);
		
		stats.x = ui.centerX() - stats.width / 2;
		stats.y = ui.centerY() - stats.height / 2;
		ui.addActor(stats);
		
		btnSound = new TextButton("Sound: ON ", skin);
		btnSound.setClickListener(this);
		btnSound.y = ui.height() - btnSound.height;
		btnSound.x = ui.width() - btnSound.width;
		ui.addActor(btnSound);
	}
	
	public void updateStatus()
	{
		String statusText = 
				String.format("Level %d\nHP: %.0f\nExp: %d/%d\nStamina: %d\nAgility: %d\nStrength: %d\nIntelligence: %d\nWeight: %d/%d\n\nGold: %d", player.getLevel(),
				player.getMaxHP(), player.getExperience(), player.getMaxExperience(), 
				player.getStamina(), player.getAgility(), player.getStrength(), player.getIntelligence(),
				player.getWeight(), player.getMaxWeight(), player.getGold());
		statusLabel.setText(statusText);
		statusWnd.pack();
		stats.update();
	}
	
	@Override
	public void render(float delta) {
		batch.begin();
		bgSprite.draw(batch);
		batch.end();
		super.render(delta);
		updateStatus();
		PlayerStatistics.getInstance().totalTimePlayed += delta;
	}

	@Override
	public void click(Actor actor, float x, float y) {
		if (actor.parent == menuWnd)
			clickMenu(actor);
		else if (actor.parent == mapWnd)
			clickMap(actor);
		
		if (actor == btnSound)
		{
			boolean snd =! ResourceManager.getInstance().isSound();
			ResourceManager.getInstance().setSound(snd);
			if (snd)
			{
				btnSound.setText("Sound: ON ");
				app.getMidi().play();
			}
			else
			{
				btnSound.setText("Sound: OFF");
				app.getMidi().stop();
			}
		}
	}
	
	private void clickMap(Actor actor) {
		int locationIndex = 0;
		for (int i = 0; i < locations.length; i++)
		{
			if (actor == locations[i])
			{
				locationIndex = i;
				break;
			}
		}
		
		app.setActiveScene(ISceneSwitcher.SceneGame);
		app.getGameScene().setActiveMap(locationIndex);
	}

	private void clickMenu(Actor actor) {
		mapWnd.visible = false;
		inventory.visible = false;
		skillTree.visible = false;
		skillBar.visible = false;
		skillPrior.visible = false;
		stats.visible = false;
		if (actor == btnMap)
		{
			mapWnd.visible = true;
		}
		else if (actor == btnInventory)
		{
			inventory.visible = true;
		}
		else if (actor == btnSkills)
		{
			skillTree.visible = true;
			skillBar.visible = true;
			skillPrior.visible = true;
		}
		else
		{
			stats.visible = true;
		}
	}
}
