package io.github.phantamanta44.rphud.hud.component.impl;

import io.github.phantamanta44.rphud.hud.ExpressionEngine;
import io.github.phantamanta44.rphud.hud.component.AbstractComponent;
import io.github.phantamanta44.rphud.util.DeserializingMap;
import io.github.phantamanta44.rphud.util.Renders;
import io.github.phantamanta44.rphud.util.ScreenAlign;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.ScaledResolution;
import net.minecraft.util.ResourceLocation;

public class ImgComponent extends AbstractComponent {

    private final ScreenAlign align;
    private final String xExpr, yExpr, wExpr, hExpr, uExpr, vExpr, swExpr, shExpr, aExpr;
    private final ResourceLocation resource;
    private final int texWidth, texHeight;

    public ImgComponent(DeserializingMap cfg) {
        super(cfg);
        this.align = cfg.has("align") ? ScreenAlign.parse(cfg.getString("align")) : ScreenAlign.TOP_LEFT;
        this.xExpr = cfg.getString("x");
        this.yExpr = cfg.getString("y");
        this.wExpr = cfg.getString("width");
        this.hExpr = cfg.getString("height");
        this.uExpr = cfg.get("u");
        this.vExpr = cfg.get("v");
        this.swExpr = cfg.get("sectionwidth");
        this.shExpr = cfg.get("sectionheight");
        this.aExpr = cfg.get("opacity");
        this.resource = new ResourceLocation(cfg.getString("source"));
        this.texWidth = cfg.getInt("texturewidth");
        this.texHeight = cfg.getInt("textureheight");
    }

    @Override
    public void render(Minecraft mc, ScaledResolution res, ExpressionEngine eval) {
        int x = eval.evalInt(xExpr);
        int y = eval.evalInt(yExpr);
        int w = eval.evalInt(wExpr);
        int h = eval.evalInt(hExpr);
        int u = uExpr != null ? eval.evalInt(uExpr) : 0;
        int v = vExpr != null ? eval.evalInt(vExpr) : 0;
        int sw = swExpr != null ? eval.evalInt(swExpr) : w;
        int sh = shExpr != null ? eval.evalInt(shExpr) : h;
        float a = aExpr != null ? eval.evalFloat(aExpr) : 1;
        x = align.offsetX(x, w, res.getScaledWidth());
        y = align.offsetY(y, h, res.getScaledHeight());
        mc.renderEngine.bindTexture(resource);
        Renders.alpha(a);
        Renders.textureUnderHud(x, y, u, v, w, h, sw, sh, texWidth, texHeight);
        Renders.alpha(1F);
    }

}

@@ -1399,16 +1399,42 @@
 xWidth =
+ cfg.has(%22texturewidth%22) ?
  cfg.get
@@ -1448,24 +1448,29 @@
 xturewidth%22)
+ : -1
 ;%0A        th
@@ -1483,16 +1483,43 @@
 Height =
+ cfg.has(%22textureheight%22) ?
  cfg.get
@@ -1534,24 +1534,29 @@
 tureheight%22)
+ : -1
 ;%0A    %7D%0A%0A   
@@ -2032,16 +2032,114 @@
 r) : h;%0A
+        int tw = texWidth != -1 ? texWidth : w;%0A        int th = texHeight != -1 ? texHeight : h;%0A
         
@@ -2434,34 +2434,21 @@
 w, sh, t
-exWidth, texHeight
+w, th
 );%0A     

Make image tw and th parameters optional