package zero1hd.rhythmbullet.graphics.ui;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Group;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.viewport.ScreenViewport;

public class Page extends Group implements Disposable {
	private Label pageTitle;
	private int baseXPos, baseYPos;
	
	public Page(int baseXPos, int baseYPos) {
		this.baseXPos = baseXPos;
		this.baseYPos = baseYPos;
		setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		setTouchable(Touchable.childrenOnly);
		setToBasePosition();
		setName(getClass().getSimpleName());
	}
	
	public Page(int baseXPos, int baseYPos, String titleText, Skin skin) {
		this(baseXPos, baseYPos);
		pageTitle = new Label(titleText, skin, "large-font", skin.getColor("default"));
		pageTitle.setPosition(18f, getHeight()-pageTitle.getHeight()-15f);
		addActor(pageTitle);
	}
	
	public float getHeightBelowTitle() {
		return pageTitle.getY();
	}
	
	@Override
	public void setStage(Stage stage) {
		if (stage == null) {
			if (!hasParent()) {
				dispose();
			}
		} else if (!(stage.getViewport() instanceof ScreenViewport)) {
			throw new IllegalArgumentException("Pages are explicitly for GUIs, and thus should have a 1:1 ratio between pixel and texture size for maximum clarity. This means that the stage should be using a ScreenViewport.");
		}
		super.setStage(stage);
	}
	
	public void setCameraPositionToPage(Vector3 cameraPosition) {
		cameraPosition.x = (baseXPos+0.5f) * getWidth();
		cameraPosition.y = (baseYPos+0.5f) * getHeight();
	}
	
	public void setToBasePosition() {
		setPosition(baseXPos*getWidth(), baseYPos*getHeight());
	}
	
	@Override
	public void setParent(Group parent) {
		if (parent == null && getStage() == null) {
			dispose();
		}
		super.setParent(parent);
	}
	
	@Override
	public void dispose() {
		Gdx.app.debug(getClass().getSimpleName(), "Disposing...");
	}
	
	public void simpleDebug(String message) {
		Gdx.app.debug(getClass().getSimpleName(), message);
	}
}
