package electrosphere.renderer.ui.label;

import electrosphere.main.Globals;
import electrosphere.renderer.Model;
import electrosphere.renderer.ui.Widget;
import electrosphere.renderer.ui.font.FontUtils;
import electrosphere.renderer.ui.font.bitmapchar.BitmapCharacter;
import java.util.LinkedList;
import java.util.List;
import org.joml.Vector3f;

/**
 *
 * @author amaterasu
 */
public class Label extends Widget{
    
    String text = "";
    int textPixelWidth = 0;
    
    float fontSize = 1.0f;
    
    List<Widget> childrenElements = new LinkedList();
    
    public Label(int x, int y, float fontSize){
        this.positionX = x;
        this.positionY = y;
        this.width = 0;
        this.height = (int)(FontUtils.getFontHeight() * fontSize);
        this.fontSize = fontSize;
    }
    
    void generateLetters(){
        childrenElements.clear();
        int rollingOffset = 0;
        for(int i = 0; i < text.length(); i++){
            char toDraw = text.charAt(i);
            Vector3f bitMapDimension = FontUtils.getDimensionOfCharacterDiscrete(toDraw);
            Widget newLetter = new BitmapCharacter((int)(rollingOffset * fontSize) + positionX, positionY, (int)(bitMapDimension.x * fontSize), this.height, toDraw);
            rollingOffset += (int)bitMapDimension.x;
            childrenElements.add(newLetter);
        }
    }
    
    public void setText(String text){
        this.text = text;
        textPixelWidth = 0;
        for(int i = 0; i < text.length(); i++){
            Vector3f bitMapDimension = FontUtils.getDimensionOfCharacterDiscrete(text.charAt(i));
            textPixelWidth = textPixelWidth + (int)bitMapDimension.x;
        }
        generateLetters();
    }
    
    public String getText(){
        return text;
    }

    @Override
    public void draw(int parentFramebufferPointer, int parentWidth, int parentHeight) {
        for(Widget child : childrenElements){
            child.draw(parentFramebufferPointer, parentWidth, parentHeight);
        }
    }
    
}
