package electrosphere.renderer.ui.font.bitmapchar;

import electrosphere.engine.assetmanager.AssetDataStrings;
import electrosphere.main.Globals;
import electrosphere.renderer.Model;
import electrosphere.renderer.ui.Widget;
import electrosphere.renderer.ui.font.FontUtils;
import org.joml.Vector3f;

/**
 *
 * @author satellite
 */
public class BitmapCharacter extends Widget {
    
    int positionX;
    int positionY;
    int width;
    int height;
    String text;
    
    Vector3f color = new Vector3f(0,0,0);
    
    
    
    public BitmapCharacter(int posX, int posY, int width, int height, char toDraw){
        positionX = posX;
        positionY = posY;
        this.width = width;
        this.height = height;
        text = "" + toDraw;
    }
    
    
    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
    
    public void setColor(Vector3f color) {
        this.color = color;
    }

    
    
    @Override
    public void draw(int parentFramebufferPointer, int parentWidth, int parentHeight){
        Globals.renderingEngine.bindFramebuffer(parentFramebufferPointer);
        Globals.renderingEngine.setViewportSize(parentWidth, parentHeight);
        float ndcX =      (float)positionX/parentWidth;
        float ndcY =      (float)positionY/parentHeight;// + (float)Globals.WINDOW_TITLE_BAR_HEIGHT/parentHeight;
        float ndcWidth =  (float)width/parentWidth;
        float ndcHeight = (float)height/parentHeight;
//        float charWidth = ndcWidth/cols;
//        float charHeight = ndcHeight/rows;
        char toDraw = text.charAt(0);
        Vector3f characterPosition = new Vector3f(ndcX,ndcY,0);
        Vector3f characterDimensions = new Vector3f(ndcWidth,ndcHeight,0);
        Vector3f bitMapPosition = FontUtils.getPositionOfCharacter(toDraw);
        Vector3f bitMapDimension = FontUtils.getDimensionOfCharacter(toDraw);
//        bitMapDimension.y = 1;
//        System.out.println(bitMapPosition);
//        System.out.println(bitMapDimension);
//        System.out.println("\n\n");
        Model charModel = Globals.assetManager.fetchModel(AssetDataStrings.BITMAP_CHARACTER_MODEL);
        if(charModel != null && toDraw != ' '){
            charModel.pushUniformToMesh(AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME, "mPosition", characterPosition);
            charModel.pushUniformToMesh(AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME, "mDimension", characterDimensions);
            charModel.pushUniformToMesh(AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME, "tPosition", bitMapPosition);
            charModel.pushUniformToMesh(AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME, "tDimension", bitMapDimension);
            charModel.pushUniformToMesh(AssetDataStrings.ASSET_STRING_BITMAP_FONT_MESH_NAME, "color", color);
            charModel.drawUI();
        }
    }
    
}
