package zero1hd.polyjet.ui.builders;

import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.GlyphLayout;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Widget;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;

public class ScrollText extends Widget {
	Rectangle scissors = new Rectangle();
	Rectangle clipBounds = new Rectangle();
	
	GlyphLayout gLayout;
	String text;
	BitmapFont font;
	private float fontHeight;
	private float fontWidth;
	
	private boolean scrollOnHover;
	private boolean currentlyHovering;
	
	private float textOffset;
	
	private Vector2 coords;
	public ScrollText(String text, Skin skin, boolean scrollOnHover) {
		super();
		setName(text);
		this.scrollOnHover = scrollOnHover;
		
		this.text = text;
		font = skin.getFont("default-font");
		font.setColor(skin.getColor("default"));
		gLayout = new GlyphLayout(font, text);
		
		fontHeight = gLayout.height;
		fontWidth = gLayout.width;
		
		coords = new Vector2();
		
		addListener(new ClickListener() {
			@Override
			public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor) {
				currentlyHovering = true;
				super.enter(event, x, y, pointer, fromActor);
			}
			
			@Override
			public void exit(InputEvent event, float x, float y, int pointer, Actor toActor) {
				currentlyHovering = false;
				super.exit(event, x, y, pointer, toActor);
			}
			
			@Override
			public void clicked(InputEvent event, float x, float y) {
			}
		});
	}
	
	public float getFontHeight() {
		return fontHeight;
	}
	
	public float getFontWidth() {
		return fontWidth;
	}
	
	@Override
	public void layout() {
		setHeight(fontHeight+4);
		clipBounds.setSize(getWidth(), getHeight()*1.5f);
		super.layout();
	}
	
	@Override
	public void act(float delta) {
		if (fontWidth > clipBounds.getWidth()) {
			if (scrollOnHover) {
				if ((int) textOffset != 0 || currentlyHovering) {
					if (textOffset < -fontWidth) {
						textOffset = clipBounds.getWidth();
					}
					textOffset -= 60*delta;
				}
			} else {
				if (textOffset < -fontWidth) {
					textOffset = clipBounds.getWidth();
				}
				textOffset -= 60*delta;
			}
		}
		super.act(delta);
	}
	
	
	@Override
	public void draw(Batch batch, float parentAlpha) {
		validate();
		
		coords.x = getX();
		coords.y = getY();
		getParent().localToStageCoordinates(coords);
		
		clipBounds.setX(coords.x);
		clipBounds.setY(coords.y - 0.5f*getHeight());
		
		getStage().calculateScissors(clipBounds, scissors);

		batch.flush();
		if (ScissorStack.pushScissors(scissors)) {
			font.draw(batch, text, coords.x + textOffset, coords.y + getFontHeight());
			batch.flush();
			ScissorStack.popScissors();
		};
	}
	
	@Override
	public float getMinHeight() {
		return fontHeight;
	}
}
