package zero1hd.polyjet.ui.windows;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.List;
import com.badlogic.gdx.scenes.scene2d.ui.ScrollPane;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Slider;
import com.badlogic.gdx.scenes.scene2d.ui.Window;

import zero1hd.polyjet.entity.Entities;
import zero1hd.polyjet.entity.Entity;
import zero1hd.polyjet.entity.EntityController;
import zero1hd.polyjet.entity.ally.Laser;
import zero1hd.polyjet.entity.enemies.Bar;
import zero1hd.polyjet.entity.enemies.Flake;
import zero1hd.polyjet.entity.enemies.Pellet;
import zero1hd.polyjet.entity.enemies.Shard;
import zero1hd.polyjet.entity.enemies.VoidCircle;

public class SpawnerWindow extends Window {
	private EntityController ec;
	private Stage stage;
	
	private List<Entities> listOfEntities;
	private ScrollPane scroller;
	
	private Slider mod1;
	private Slider mod2;
	private Slider mod3;
	private Slider mod4;
	
	public SpawnerWindow(String title, Skin skin, EntityController ec, Stage stageForEntities) {
		super(title, skin, "tinted");
		this.ec = ec;
		stage = stageForEntities;
		
		mod1 = new Slider(0.1f, 50f, 0.01f, true, skin);
		mod2 = new Slider(0f, 15f, 0.01f, true, skin);
		mod3 = new Slider(0.1f, 15f, 0.01f, true, skin);
		mod4 = new Slider(1f, 12f, 0.01f, true, skin);
		
		listOfEntities = new List<>(skin);
		listOfEntities.setItems(Entities.values());
		
		scroller = new ScrollPane(listOfEntities);
		add(scroller).expandY().fillY().spaceRight(15f);
		
		add(mod1, mod2, mod3, mod4);
		
		setSize(300f, 375f);
	}
	
	
	@Override
	public boolean remove() {
		return super.remove();
	}
	
	private Vector2 coords = new Vector2();
	@Override
	public void act(float delta) {
		if (Gdx.input.justTouched() && Gdx.input.isKeyPressed(Keys.CONTROL_LEFT)) {
			coords.set(Gdx.input.getX(), Gdx.input.getY());
			stage.screenToStageCoordinates(coords);
			
			Entity entity;
			if ((entity = ec.retrieveEntity(listOfEntities.getSelected())) != null) {
				switch(entity.getEntityType()) {
				case LASER:
					Laser laser = (Laser) entity;
					laser.init(coords.x, coords.y, mod1.getValue());
					stage.addActor(laser);
					break;
				case PELLET:
					Pellet pellet = (Pellet) entity;
					pellet.init(coords.x, coords.y, mod2.getValue()/mod2.getMaxValue()*360f, mod1.getValue());
					stage.addActor(pellet);
					break;
				case VOID_CIRCLE:
					VoidCircle voidCircle = (VoidCircle) entity;
					voidCircle.init(mod2.getValue(), coords.x, coords.y, mod1.getValue(), mod3.getValue());
					stage.addActor(voidCircle);
					break;
				case SHARD:
					Shard shard = (Shard) entity;
					shard.init(coords.x, coords.y, mod2.getValue()/mod2.getMaxValue()*360f, mod1.getValue(), (int) mod3.getValue());
					stage.addActor(shard);
					break;
				case BAR:
					Bar bar = (Bar) entity;
					bar.init(coords.x, mod1.getValue());
					stage.addActor(bar);
					break;
				case FLAKE:
					Flake flake = (Flake) entity;
					Shard[] shards = new Shard[(int) mod4.getValue()];
					for (int i = 0; i < shards.length; i++) {
						shards[i] = (Shard) ec.retrieveEntity(Entities.SHARD);
						shards[i].init(coords.x, coords.y, 360f/shards.length*i, 0f, (int) 2);
						stage.addActor(shards[i]);
					}
					stage.addActor(flake);
					flake.init(coords.x, coords.y, mod1.getValue(), mod3.getValue(), mod2.getValue()/mod2.getMaxValue()*360f, shards);
				default:
					break;
				}
			}
		}
		super.act(delta);
	}
}
