package org.liquidengine.legui.system.renderer.nvg.icon;

import org.joml.Vector2f;
import org.joml.Vector4f;
import org.liquidengine.legui.color.ColorConstants;
import org.liquidengine.legui.component.Component;
import org.liquidengine.legui.component.optional.align.HorizontalAlign;
import org.liquidengine.legui.component.optional.align.VerticalAlign;
import org.liquidengine.legui.font.FontRegister;
import org.liquidengine.legui.icon.CharIcon;
import org.liquidengine.legui.system.context.Context;
import org.liquidengine.legui.system.renderer.nvg.NvgIconRenderer;
import org.liquidengine.legui.util.TextUtil;
import org.lwjgl.nanovg.NVGColor;

import static org.liquidengine.legui.system.renderer.nvg.util.NvgRenderUtils.*;

/**
 * Created by ShchAlexander on 13.03.2017.
 */
public class NvgCharIconRenderer<I extends CharIcon> extends NvgIconRenderer<I> {
    private NVGColor colorA;

    @Override
    public void initialize() {
        colorA = NVGColor.calloc();
    }

    @Override
    public void destroy() {
        colorA.free();
    }

    @Override
    protected void renderIcon(I icon, Component component, Context context, long nanovg) {
        if (!component.isVisible() || icon == null || icon.getFont() == null) return;
        // render simple rectangle border
        Vector2f position = component.getScreenPosition();
        Vector2f size     = component.getSize();
        Vector2f iconSize = icon.getSize();

        float x = position.x + icon.getHorizontalAlign().index * (size.x - iconSize.x) / 2f;
        float y = position.y + icon.getVerticalAlign().index * (size.y - iconSize.y) / 2f;
        float w = iconSize.x;
        float h = iconSize.y;

        drawIcon(nanovg, x, y, w, h, icon, component);
    }


    private void drawIcon(long context, float x, float y, float w, float h, CharIcon icon, Component component) {
        if (component.isFocused()) {
            renderTextLineToBounds(context, x - 1, y + 1, w, h, icon.getSize().x, icon.getFont(),
                    component.getFocusedStrokeColor(), colorA, TextUtil.cpToStr(icon.getCharCode()), icon.getHorizontalAlign(), icon.getVerticalAlign(), false);
        }

//        renderTextStateLineToBounds(nanovg, pos, size, textState);
        renderTextLineToBounds(context,
            /* X position             */ x + 0.5f,
            /* Y position             */ y,
            /* Width                  */ w,
            /* Height                 */ h,
            /* font size              */ icon.getSize().y,
            /* font name              */ icon.getFont(),
            /* text color             */ icon.getColor(),
            /* text to render         */ TextUtil.cpToStr(icon.getCharCode()),
            /* horizontal alignment   */ HorizontalAlign.CENTER,
            /* vertical alignment     */ VerticalAlign.MIDDLE,
            /* hide out of bound text */ false);
        drawRectStroke(context, x, y, w, h, ColorConstants.red(), 0,1);
//        renderTextLineToBounds(context, x, y, w, h, icon.getSize().x, icon.getFont(),
//                icon.getColor(), colorA, TextUtil.cpToStr(icon.getCharCode()), icon.getHorizontalAlign(), icon.getVerticalAlign(), false);
    }
}

@@ -107,60 +107,8 @@
 4f;%0A
-import org.liquidengine.legui.color.ColorConstants;%0A
 impo
@@ -668,17 +668,144 @@
 erUtils.
-*
+renderTextLineToBounds;%0Aimport static org.liquidengine.legui.system.renderer.nvg.util.NvgRenderUtils.renderTextStateLineToBounds
 ;%0A%0A/**%0A 
@@ -2015,17 +2015,20 @@
 xt, x - 
-1
+0.5f
 , y + 1,
@@ -2041,33 +2041,33 @@
  icon.getSize().
-x
+y
 , icon.getFont()
@@ -2118,32 +2118,24 @@
 rokeColor(),
- colorA,
  TextUtil.cp
@@ -2205,36 +2205,35 @@
 erticalAlign(), 
-fals
+tru
 e);%0A        %7D%0A%0A/
@@ -2235,931 +2235,132 @@
  %7D%0A%0A
-//        renderTextStateLineToBounds(nanovg, pos, size, textState);%0A        renderTextLineToBounds(context,%0A            /* X position             */ x + 0.5f,%0A            /* Y position             */ y,%0A            /* Width                  */ w,%0A            /* Height                 */ h,%0A            /* font size              */ icon.getSize().y,%0A            /* font name              */ icon.getFont(),%0A            /* text color             */ icon.getColor(),%0A            /* text to render         */ TextUtil.cpToStr(icon.getCharCode()),%0A            /* horizontal alignment   */ HorizontalAlign.CENTER,%0A            /* vertical alignment     */ VerticalAlign.MIDDLE,%0A            /* hide out of bound text */ false);%0A        drawRectStroke(context, x, y, w, h, ColorConstants.red(), 0,1);%0A//        renderTextLineToBounds(context, x, y, w, h, icon.getSize().x, icon.getFont(),%0A//                icon.getColor(), colorA
+        renderTextLineToBounds(context, x + 0.5f, y, w, h, icon.getSize().y, icon.getFont(),%0A                icon.getColor()
 , Te
@@ -2451,12 +2451,11 @@
 (), 
-fals
+tru
 e);%0A

fix renderer