package com.tiedup.remake.client.renderer.models;

import net.minecraft.client.model.geom.ModelLayerLocation;
import net.minecraft.client.model.geom.PartPose;
import net.minecraft.client.model.geom.builders.CubeDeformation;
import net.minecraft.client.model.geom.builders.CubeListBuilder;
import net.minecraft.client.model.geom.builders.LayerDefinition;
import net.minecraft.client.model.geom.builders.MeshDefinition;
import net.minecraft.client.model.geom.builders.PartDefinition;
import net.minecraft.resources.ResourceLocation;

public class BondageLayerDefinitions {

    public static final ModelLayerLocation BONDAGE_LAYER =
        new ModelLayerLocation(
            ResourceLocation.fromNamespaceAndPath("tiedup", "bondage_layer"),
            "main"
        );

    public static final ModelLayerLocation BONDAGE_LAYER_SLIM =
        new ModelLayerLocation(
            ResourceLocation.fromNamespaceAndPath(
                "tiedup",
                "bondage_layer_slim"
            ),
            "main"
        );

    /**
     * Create bondage layer for normal arms (4px wide - Steve model).
     */
    public static LayerDefinition createBodyLayer() {
        return createBondageLayer(4);
    }

    /**
     * Create bondage layer for slim arms (3px wide - Alex model).
     */
    public static LayerDefinition createSlimBodyLayer() {
        return createBondageLayer(3);
    }

    /**
     * Create bondage layer with specified arm width.
     *
     * @param armWidth Arm width in pixels (4 for Steve, 3 for Alex)
     */
    private static LayerDefinition createBondageLayer(int armWidth) {
        MeshDefinition meshdefinition = new MeshDefinition();
        PartDefinition partdefinition = meshdefinition.getRoot();

        // Inflation for tight fit on body
        CubeDeformation deformation = new CubeDeformation(0.35F);

        // Head at standard position
        partdefinition.addOrReplaceChild(
            "head",
            CubeListBuilder.create()
                .texOffs(0, 0)
                .addBox(-4.0F, -8.0F, -4.0F, 8.0F, 8.0F, 8.0F, deformation),
            PartPose.offset(0.0F, 0.0F, 0.0F)
        );

        partdefinition.addOrReplaceChild(
            "hat",
            CubeListBuilder.create()
                .texOffs(32, 0)
                .addBox(
                    -4.0F,
                    -8.0F,
                    -4.0F,
                    8.0F,
                    8.0F,
                    8.0F,
                    deformation.extend(0.1F)
                ),
            PartPose.offset(0.0F, 0.0F, 0.0F)
        );

        partdefinition.addOrReplaceChild(
            "body",
            CubeListBuilder.create()
                .texOffs(16, 16)
                .addBox(-4.0F, 0.0F, -2.0F, 8.0F, 12.0F, 4.0F, deformation),
            PartPose.offset(0.0F, 0.0F, 0.0F)
        );

        // Arms - width varies based on model type
        float armWidthF = (float) armWidth;
        float rightArmX = -(armWidthF - 1.0F); // -3 for 4px, -2 for 3px

        partdefinition.addOrReplaceChild(
            "right_arm",
            CubeListBuilder.create()
                .texOffs(40, 16)
                .addBox(
                    rightArmX,
                    -2.0F,
                    -2.0F,
                    armWidthF,
                    12.0F,
                    4.0F,
                    deformation
                ),
            PartPose.offset(-5.0F, 2.0F, 0.0F)
        );

        partdefinition.addOrReplaceChild(
            "left_arm",
            CubeListBuilder.create()
                .texOffs(40, 16)
                .mirror()
                .addBox(
                    -1.0F,
                    -2.0F,
                    -2.0F,
                    armWidthF,
                    12.0F,
                    4.0F,
                    deformation
                ),
            PartPose.offset(5.0F, 2.0F, 0.0F)
        );

        partdefinition.addOrReplaceChild(
            "right_leg",
            CubeListBuilder.create()
                .texOffs(0, 16)
                .addBox(-2.0F, 0.0F, -2.0F, 4.0F, 12.0F, 4.0F, deformation),
            PartPose.offset(-1.9F, 12.0F, 0.0F)
        );

        partdefinition.addOrReplaceChild(
            "left_leg",
            CubeListBuilder.create()
                .texOffs(0, 16)
                .mirror()
                .addBox(-2.0F, 0.0F, -2.0F, 4.0F, 12.0F, 4.0F, deformation),
            PartPose.offset(1.9F, 12.0F, 0.0F)
        );

        return LayerDefinition.create(meshdefinition, 64, 32);
    }
}
