package zero1hd.rhythmbullet.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 com.badlogic.gdx.utils.Array;

import zero1hd.rhythmbullet.entity.Entity;
import zero1hd.rhythmbullet.entity.EntityFrame;
import zero1hd.rhythmbullet.entity.EntityManager;
import zero1hd.rhythmbullet.entity.ally.Laser;
import zero1hd.rhythmbullet.entity.coordinator.Coordinator;
import zero1hd.rhythmbullet.entity.coordinator.CoordinatorFrame;
import zero1hd.rhythmbullet.entity.coordinator.CoordinatorManager;
import zero1hd.rhythmbullet.entity.enemies.Flake;
import zero1hd.rhythmbullet.entity.enemies.Pellet;
import zero1hd.rhythmbullet.entity.enemies.Shard;
import zero1hd.rhythmbullet.entity.enemies.VoidCircle;

public class SpawnerWindow extends Window {
	private Stage stage;
	
	private Array<EntityFrame<? extends Entity>> entityFrames = new Array<>();
	private List<EntityFrame<? extends Entity>> listOfEntities;
	private ScrollPane entityListScroller;
	
	private Array<CoordinatorFrame<? extends Coordinator>> coordinatorFrames = new Array<>();
	private List<CoordinatorFrame<? extends Coordinator>> listOfCoordinators;
	private ScrollPane coordinatorListScroller;

	private Slider mod1;
	private Slider mod2;
	private Slider mod3;
	private Slider mod4;
	
	public SpawnerWindow(String title, Skin skin, EntityManager em, CoordinatorManager cm, Stage stageForEntities) {
		super(title, skin, "tinted");
		stage = stageForEntities;
		
		defaults().pad(15f);
		
		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);
		
		add(mod1, mod2, mod3, mod4);
		
		listOfEntities = new List<>(skin);
		entityFrames.add(em.flake);
		entityFrames.add(em.laser);
		entityFrames.add(em.pellet);
		entityFrames.add(em.shard);
		entityFrames.add(em.voidCircle);
		
		listOfEntities.setItems(entityFrames);
		entityListScroller = new ScrollPane(listOfEntities, skin);
		add(entityListScroller).width(100f).fillY();
		
		
		listOfCoordinators = new List<>(skin);
		coordinatorFrames.add(cm.slowLeft);
		coordinatorFrames.add(cm.slowRight);
		
		listOfCoordinators.setItems(coordinatorFrames);
		coordinatorListScroller = new ScrollPane(listOfCoordinators, skin);
		add(coordinatorListScroller).width(100f).fillY();

		
		pack();
	}
	
	
	@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 = listOfEntities.getSelected().buildEntity();
			
			if (Gdx.input.isKeyPressed(Keys.ALT_LEFT)) {
				entity.setCoordinator(listOfCoordinators.getSelected().buildCoordinator());
			}
			
			if (entity.getClass() == Laser.class) {
				Laser laser = (Laser) entity;
				laser.init(coords.x, coords.y, mod1.getValue());
			} else if (entity.getClass() == Pellet.class) {
				Pellet pellet = (Pellet) entity;
				pellet.init(coords.x, coords.y, mod2.getValue()/mod2.getMaxValue()*360f, mod1.getValue());
			} else if (entity.getClass() == VoidCircle.class) {
				VoidCircle voidCircle = (VoidCircle) entity;
				voidCircle.init(mod2.getValue(), coords.x, coords.y, mod1.getValue(), mod3.getValue());
			} else if (entity.getClass() == Shard.class) {
				Shard shard = (Shard) entity;
				shard.init(coords.x, coords.y, mod2.getValue()/mod2.getMaxValue()*360f, mod1.getValue(), (int) mod3.getValue());
			} else if (entity.getClass() == Flake.class) {
				Flake flake = (Flake) entity;
				flake.init(coords.x, coords.y, mod1.getValue(), mod3.getValue(), mod2.getValue()/mod2.getMaxValue()*360f, (int) mod4.getValue());
			}
			
			stage.addActor(entity);
		}
		super.act(delta);
	}
}
