package me.km.items;

import net.minecraft.client.renderer.entity.model.BipedModel;
import net.minecraft.client.renderer.model.ModelRenderer;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.item.ArmorStandEntity;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;

@OnlyIn(Dist.CLIENT)
public class ModelHat extends BipedModel {
    private final static float FACTOR = ((float) Math.PI / 180.0f);

    private final ModelRenderer firstHeadPart;
    private final ModelRenderer secHeadPart;

    public ModelHat(float scale) {
        super(scale, 0, 64, 64);

        firstHeadPart = new ModelRenderer(this, 0, 32);
        firstHeadPart.addBox(-5, -8.6f, -5, 10, 1, 10);
        firstHeadPart.setRotationPoint(0.0f, 0.0f, 0.0f);
        firstHeadPart.setTextureSize(64, 64);
        firstHeadPart.mirror = true;
        setRotation(firstHeadPart, 0.0f, 0.0f, 0.0f);

        secHeadPart = new ModelRenderer(this, 0, 43);
        secHeadPart.addBox(-3, -11.6f, -3, 6, 3, 6);
        secHeadPart.setRotationPoint(0.0f, 0.0f, 0.0f);
        secHeadPart.setTextureSize(64, 64);
        secHeadPart.mirror = true;
        setRotation(secHeadPart, 0.0f, 0.0f, 0.0f);

        bipedHead.addChild(firstHeadPart);
        bipedHead.addChild(secHeadPart);
    }

    @Override
    public void setRotationAngles(LivingEntity ent, float limbSwing, float limbSwingAmount, float ageInTicks, float netHeadYaw, float headPitch) {
        if(ent instanceof ArmorStandEntity) {
            ArmorStandEntity ase = (ArmorStandEntity) ent;
            this.bipedHead.rotateAngleX = FACTOR * ase.getHeadRotation().getX();
            this.bipedHead.rotateAngleY = FACTOR * ase.getHeadRotation().getY();
            this.bipedHead.rotateAngleZ = FACTOR * ase.getHeadRotation().getZ();
            this.bipedHead.setRotationPoint(0.0f, 1.0f, 0.0f);
            this.bipedBody.rotateAngleX = FACTOR * ase.getBodyRotation().getX();
            this.bipedBody.rotateAngleY = FACTOR * ase.getBodyRotation().getY();
            this.bipedBody.rotateAngleZ = FACTOR * ase.getBodyRotation().getZ();
            this.bipedLeftArm.rotateAngleX = FACTOR * ase.getLeftArmRotation().getX();
            this.bipedLeftArm.rotateAngleY = FACTOR * ase.getLeftArmRotation().getY();
            this.bipedLeftArm.rotateAngleZ = FACTOR * ase.getLeftArmRotation().getZ();
            this.bipedRightArm.rotateAngleX = FACTOR * ase.getRightArmRotation().getX();
            this.bipedRightArm.rotateAngleY = FACTOR * ase.getRightArmRotation().getY();
            this.bipedRightArm.rotateAngleZ = FACTOR * ase.getRightArmRotation().getZ();
            this.bipedLeftLeg.rotateAngleX = FACTOR * ase.getLeftLegRotation().getX();
            this.bipedLeftLeg.rotateAngleY = FACTOR * ase.getLeftLegRotation().getY();
            this.bipedLeftLeg.rotateAngleZ = FACTOR * ase.getLeftLegRotation().getZ();
            this.bipedLeftLeg.setRotationPoint(1.9f, 11.0f, 0.0f);
            this.bipedRightLeg.rotateAngleX = FACTOR * ase.getRightLegRotation().getX();
            this.bipedRightLeg.rotateAngleY = FACTOR * ase.getRightLegRotation().getY();
            this.bipedRightLeg.rotateAngleZ = FACTOR * ase.getRightLegRotation().getZ();
            this.bipedRightLeg.setRotationPoint(-1.9f, 11.0f, 0.0f);
            this.bipedHeadwear.copyModelAngles(this.bipedHead);
        }
        super.setRotationAngles(ent, limbSwing, limbSwingAmount, ageInTicks, netHeadYaw, headPitch);
    }

    private void setRotation(ModelRenderer model, float x, float y, float z) {
        model.rotateAngleX = x;
        model.rotateAngleY = y;
        model.rotateAngleZ = z;
    }
}
