function createModel(renderer, modelType, texture, textureLights) {
    var model = renderer.createResource("MODEL", modelType);

    if (typeof textureLights !== "undefined") {
        model.texture.set(texture, textureLights);
    }
    else {
        model.texture.set(texture);
    }

    return model;
}

function bindBeam(renderer, propertyName, beam, anchor, color, entries) {
    var prop = renderer.bindProperty(propertyName).setAnchor(anchor);
    var constln = renderer.createResource("BEAM_CONSTELLATION", null);

    for (var i = 0; i < entries.length; ++i) {
        constln.bindBeam(entries[i]);
    }

    if (typeof beam === "string") {
        beam = renderer.createResource("BEAM_RENDERER", beam);
    }

    prop.setConstellation(constln);
    prop.setRenderer(beam);
    prop.color.set(color);
    return prop;
}

function setOpacityWithMaskTimer(renderer) {
    renderer.bindProperty("fiskheroes:opacity").setOpacity((entity, renderLayer) => {
        return 1.0 + (0.99 - 1.0) * (entity.getData("fiskheroes:mask_open_timer2") == 0 || entity.isDisplayStand());
    });
}

function setOpacityWithMask(renderer) {
    renderer.bindProperty("fiskheroes:opacity").setOpacity((entity, renderLayer) => {
        return 1.0 + (0.99 - 1.0) * (!entity.getData("fiskheroes:mask_open") || entity.isDisplayStand());
    });
}

function setOpacityWithMaskAndTransformation(renderer, dataVar) {
    renderer.bindProperty("fiskheroes:opacity").setOpacity((entity, renderLayer) => {
        return 1.0 + (0.99 - 1.0) * ((entity.getData(dataVar) && !entity.getData('fiskheroes:mask_open')) || entity.isDisplayStand());
    });
}

function setOpacityWithoutMask(renderer) {
    renderer.bindProperty("fiskheroes:opacity").setOpacity((entity, renderLayer) => {
        return 1.0 + (0.99 - 1.0) * (entity.isAlive() || entity.isDisplayStand());
    });
}

function initLanternBlaster(renderer, color) {
    renderer.bindProperty("fiskheroes:energy_bolt").color.set(color);
    bindBeam(renderer, "fiskheroes:energy_manipulation", "fiskheroes:energy_discharge", "rightArm", color, [
        { "firstPerson": [-2.5, 0.0, -7.0], "offset": [-0.5, 19.0, -12.0], "size": [1.5, 1.5] }
    ]);
}

function bindGlidingTrail(renderer, trailType) {
    var prop = renderer.bindProperty("fiskheroes:trail");
    prop.setTrail(renderer.createResource("TRAIL", trailType));
    prop.setCondition(entity => entity.getData("fiskheroes:gliding"));
    return prop;
}

function bindSpeedingTrail(renderer, trailType) {
    var prop = renderer.bindProperty("fiskheroes:trail");
    prop.setTrail(renderer.createResource("TRAIL", trailType));
    prop.setCondition(entity => entity.getData("fiskheroes:speeding"));
    return prop;
}

function bindBoostingTrail(renderer, trailType) {
    var prop = renderer.bindProperty("fiskheroes:trail");
    prop.setTrail(renderer.createResource("TRAIL", trailType));
    prop.setCondition(entity => entity.getData("fiskheroes:flight_boost_timer") > 0);
    return prop;
}

function bindLanternTransformTrail(renderer, trailType) {
    var prop = renderer.bindProperty("fiskheroes:trail");
    prop.setTrail(renderer.createResource("TRAIL", trailType));
    prop.setCondition(entity => entity.getData("ahhp:dyn/lantern_timer") > 0 && entity.getData("ahhp:dyn/lantern_timer") < 1);
    return prop;
}

function bindGrowShrinkTrail(renderer, trailType) {
    var prop = renderer.bindProperty("fiskheroes:trail");
    prop.setTrail(renderer.createResource("TRAIL", trailType));
    prop.setCondition(entity => entity.getData("fiskheroes:size_state") > 0);
    return prop;
}

function chestplateOnly(renderer, id) {
    head = ("ahhp:" + id + "_head");
    chest = ("ahhp:" + id + "_chest");
    rightArm = ("ahhp:" + id + "_right_arm");
    leftArm = ("ahhp:" + id + "_left_arm");
    rightLeg = ("ahhp:" + id + "_right_leg");
    leftLeg = ("ahhp:" + id + "_left_leg");
    var head_model = renderer.createEffect("fiskheroes:model");
    head_model.setModel(createModel(renderer, head, "model", "model_lights"));
    head_model.anchor.set("head");
    var chest_model = renderer.createEffect("fiskheroes:model");
    chest_model.setModel(createModel(renderer, chest, "model", "model_lights"));
    chest_model.anchor.set("body");
    var rightArm_model = renderer.createEffect("fiskheroes:model");
    rightArm_model.setModel(createModel(renderer, rightArm, "model", "model_lights"));
    rightArm_model.anchor.set("rightArm");
    var leftArm_model = renderer.createEffect("fiskheroes:model");
    leftArm_model.setModel(createModel(renderer, leftArm, "model", "model_lights"));
    leftArm_model.anchor.set("leftArm");
    var rightLeg_model = renderer.createEffect("fiskheroes:model");
    rightLeg_model.setModel(createModel(renderer, rightLeg, "model", "model_lights"));
    rightLeg_model.anchor.set("rightLeg");
    var leftLeg_model = renderer.createEffect("fiskheroes:model");
    leftLeg_model.setModel(createModel(renderer, leftLeg, "model", "model_lights"));
    leftLeg_model.anchor.set("leftLeg");
    return {
        head_model: head_model,
        chest_model: chest_model,
        rightArm_model: rightArm_model,
        leftArm_model: leftArm_model,
        rightLeg_model: rightLeg_model,
        leftLeg_model: leftLeg_model,
        render: (entity, renderLayer, transform_variable) => {
            if ((renderLayer == "CHESTPLATE" && entity.getData(transform_variable)) || entity.isDisplayStand()) {
                chest_model.opacity = 1.0;
                chest_model.render();
                rightArm_model.opacity = 1.0;
                rightArm_model.render();
                leftArm_model.opacity = 1.0;
                leftArm_model.render();
                rightLeg_model.opacity = 1.0;
                rightLeg_model.render();
                leftLeg_model.opacity = 1.0;
                leftLeg_model.render();
                if (!entity.getData("fiskheroes:mask_open")) {
                    head_model.opacity = 1.0;
                    head_model.render();
                }
            }
        }
    };
}

function lanternModels(renderer, lantern, lantern_ring) {
    chest = ("ahhp:" + lantern + "_chest");
    ring = ("ahhp:" + lantern_ring);
    var head_model = renderer.createEffect("fiskheroes:model");
    head_model.setModel(createModel(renderer, "ahhp:lantern_head", "model", "model_lights"));
    head_model.anchor.set("head");
    var chest_model = renderer.createEffect("fiskheroes:model");
    chest_model.setModel(createModel(renderer, chest, "model", "model_lights"));
    chest_model.anchor.set("body");
    var rightArm_model = renderer.createEffect("fiskheroes:model");
    rightArm_model.setModel(createModel(renderer, "ahhp:lantern_right_arm", "model", "model_lights"));
    rightArm_model.anchor.set("rightArm");
    var leftArm_model = renderer.createEffect("fiskheroes:model");
    leftArm_model.setModel(createModel(renderer, "ahhp:lantern_left_arm", "model", "model_lights"));
    leftArm_model.anchor.set("leftArm");
    var rightLeg_model = renderer.createEffect("fiskheroes:model");
    rightLeg_model.setModel(createModel(renderer, "ahhp:lantern_right_leg", "model", "model_lights"));
    rightLeg_model.anchor.set("rightLeg");
    var leftLeg_model = renderer.createEffect("fiskheroes:model");
    leftLeg_model.setModel(createModel(renderer, "ahhp:lantern_left_leg", "model", "model_lights"));
    leftLeg_model.anchor.set("leftLeg");
    var ring_model = renderer.createEffect("fiskheroes:model");
    ring_model.setModel(createModel(renderer, ring, "ring", "ring_lights"));
    ring_model.anchor.set("rightArm");
    var hammer_model = renderer.createEffect("fiskheroes:model");
    hammer_model.setModel(createModel(renderer, "ahhp:lantern_hammer", "hammer", "hammer_lights"));
    hammer_model.anchor.set("rightArm");
    return {
        head_model: head_model,
        chest_model: chest_model,
        rightArm_model: rightArm_model,
        leftArm_model: leftArm_model,
        rightLeg_model: rightLeg_model,
        leftLeg_model: leftLeg_model,
        ring_model: ring_model,
        hammer_model: hammer_model,
        render: (entity, renderLayer) => {
            if (renderLayer == "CHESTPLATE") {
                ring_model.render();
                if ((!entity.getData('fiskheroes:mask_open') && entity.getData('ahhp:dyn/lantern_timer') == 1) || entity.isDisplayStand()) {
                    head_model.opacity = 1.0;
                    head_model.render();
                }
                if (entity.getData('ahhp:dyn/lantern_timer') == 1 || entity.isDisplayStand()) {
                    chest_model.opacity = 1.0;
                    chest_model.render();
                    rightArm_model.opacity = 1.0;
                    rightArm_model.render();
                    leftArm_model.opacity = 1.0;
                    leftArm_model.render();
                    rightLeg_model.opacity = 1.0;
                    rightLeg_model.render();
                    leftLeg_model.opacity = 1.0;
                    leftLeg_model.render();
                }
                if (entity.getData('ahhp:dyn/lanternhammer') && entity.getHeldItem().isEmpty()) {
                    hammer_model.opacity = 1.0;
                    hammer_model.render();
                }
            }
        }
    };
}

function maskToggleModels(renderer, id) {
    head = ("ahhp:" + id + "_head");
    chest = ("ahhp:" + id + "_chest");
    rightArm = ("ahhp:" + id + "_right_arm");
    leftArm = ("ahhp:" + id + "_left_arm");
    rightLeg = ("ahhp:" + id + "_right_leg");
    leftLeg = ("ahhp:" + id + "_left_leg");
    var head_model = renderer.createEffect("fiskheroes:model");
    head_model.setModel(createModel(renderer, head, "model", "model_lights"));
    head_model.anchor.set("head");
    var chest_model = renderer.createEffect("fiskheroes:model");
    chest_model.setModel(createModel(renderer, chest, "model", "model_lights"));
    chest_model.anchor.set("body");
    var rightArm_model = renderer.createEffect("fiskheroes:model");
    rightArm_model.setModel(createModel(renderer, rightArm, "model", "model_lights"));
    rightArm_model.anchor.set("rightArm");
    var leftArm_model = renderer.createEffect("fiskheroes:model");
    leftArm_model.setModel(createModel(renderer, leftArm, "model", "model_lights"));
    leftArm_model.anchor.set("leftArm");
    var rightLeg_model = renderer.createEffect("fiskheroes:model");
    rightLeg_model.setModel(createModel(renderer, rightLeg, "model", "model_lights"));
    rightLeg_model.anchor.set("rightLeg");
    var leftLeg_model = renderer.createEffect("fiskheroes:model");
    leftLeg_model.setModel(createModel(renderer, leftLeg, "model", "model_lights"));
    leftLeg_model.anchor.set("leftLeg");
    return {
        head_model: head_model,
        chest_model: chest_model,
        rightArm_model: rightArm_model,
        leftArm_model: leftArm_model,
        rightLeg_model: rightLeg_model,
        leftLeg_model: leftLeg_model,
        render: (entity, renderLayer) => {
            if (renderLayer == "HELMET" && !entity.getData("fiskheroes:mask_open")) {
                head_model.opacity = 1.0;
                head_model.render();
            }
            if (renderLayer == "CHESTPLATE") {
                chest_model.opacity = 1.0;
                chest_model.render();
                rightArm_model.opacity = 1.0;
                rightArm_model.render();
                leftArm_model.opacity = 1.0;
                leftArm_model.render();
            }
            if (renderLayer == "LEGGINGS") {
                rightLeg_model.opacity = 1.0;
                rightLeg_model.render();
                leftLeg_model.opacity = 1.0;
                leftLeg_model.render();
            }
        }
    };
}

function basicModels(renderer, id) {
    head = ("ahhp:" + id + "_head");
    chest = ("ahhp:" + id + "_chest");
    rightArm = ("ahhp:" + id + "_right_arm");
    leftArm = ("ahhp:" + id + "_left_arm");
    rightLeg = ("ahhp:" + id + "_right_leg");
    leftLeg = ("ahhp:" + id + "_left_leg");
    var head_model = renderer.createEffect("fiskheroes:model");
    head_model.setModel(createModel(renderer, head, "model", "model_lights"));
    head_model.anchor.set("head");
    var chest_model = renderer.createEffect("fiskheroes:model");
    chest_model.setModel(createModel(renderer, chest, "model", "model_lights"));
    chest_model.anchor.set("body");
    var rightArm_model = renderer.createEffect("fiskheroes:model");
    rightArm_model.setModel(createModel(renderer, rightArm, "model", "model_lights"));
    rightArm_model.anchor.set("rightArm");
    var leftArm_model = renderer.createEffect("fiskheroes:model");
    leftArm_model.setModel(createModel(renderer, leftArm, "model", "model_lights"));
    leftArm_model.anchor.set("leftArm");
    var rightLeg_model = renderer.createEffect("fiskheroes:model");
    rightLeg_model.setModel(createModel(renderer, rightLeg, "model", "model_lights"));
    rightLeg_model.anchor.set("rightLeg");
    var leftLeg_model = renderer.createEffect("fiskheroes:model");
    leftLeg_model.setModel(createModel(renderer, leftLeg, "model", "model_lights"));
    leftLeg_model.anchor.set("leftLeg");
    return {
        head_model: head_model,
        chest_model: chest_model,
        rightArm_model: rightArm_model,
        leftArm_model: leftArm_model,
        rightLeg_model: rightLeg_model,
        leftLeg_model: leftLeg_model,
        render: (entity, renderLayer) => {
            if (renderLayer == "HELMET") {
                head_model.opacity = 1.0;
                head_model.render();
            }
            if (renderLayer == "CHESTPLATE") {
                chest_model.opacity = 1.0;
                chest_model.render();
                rightArm_model.opacity = 1.0;
                rightArm_model.render();
                leftArm_model.opacity = 1.0;
                leftArm_model.render();
            }
            if (renderLayer == "LEGGINGS") {
                rightLeg_model.opacity = 1.0;
                rightLeg_model.render();
                leftLeg_model.opacity = 1.0;
                leftLeg_model.render();
            }
        }
    };
}

function noHelmetModels(renderer, id) {
    chest = ("ahhp:" + id + "_chest");
    rightArm = ("ahhp:" + id + "_right_arm");
    leftArm = ("ahhp:" + id + "_left_arm");
    rightLeg = ("ahhp:" + id + "_right_leg");
    leftLeg = ("ahhp:" + id + "_left_leg");
    var chest_model = renderer.createEffect("fiskheroes:model");
    chest_model.setModel(createModel(renderer, chest, "model", "model_lights"));
    chest_model.anchor.set("body");
    var rightArm_model = renderer.createEffect("fiskheroes:model");
    rightArm_model.setModel(createModel(renderer, rightArm, "model", "model_lights"));
    rightArm_model.anchor.set("rightArm");
    var leftArm_model = renderer.createEffect("fiskheroes:model");
    leftArm_model.setModel(createModel(renderer, leftArm, "model", "model_lights"));
    leftArm_model.anchor.set("leftArm");
    var rightLeg_model = renderer.createEffect("fiskheroes:model");
    rightLeg_model.setModel(createModel(renderer, rightLeg, "model", "model_lights"));
    rightLeg_model.anchor.set("rightLeg");
    var leftLeg_model = renderer.createEffect("fiskheroes:model");
    leftLeg_model.setModel(createModel(renderer, leftLeg, "model", "model_lights"));
    leftLeg_model.anchor.set("leftLeg");
    return {
        chest_model: chest_model,
        rightArm_model: rightArm_model,
        leftArm_model: leftArm_model,
        rightLeg_model: rightLeg_model,
        leftLeg_model: leftLeg_model,
        render: (entity, renderLayer) => {
            if (renderLayer == "CHESTPLATE") {
                chest_model.opacity = 1.0;
                chest_model.render();
                rightArm_model.opacity = 1.0;
                rightArm_model.render();
                leftArm_model.opacity = 1.0;
                leftArm_model.render();
            }
            if (renderLayer == "LEGGINGS") {
                rightLeg_model.opacity = 1.0;
                rightLeg_model.render();
                leftLeg_model.opacity = 1.0;
                leftLeg_model.render();
            }
        }
    };
}

function imposterModels(renderer, id) {
    chest = ("ahhpspecial:" + id);
    var chest_model = renderer.createEffect("fiskheroes:model");
    chest_model.setModel(createModel(renderer, chest, "model"));
    chest_model.anchor.set("body");
    return {
        chest_model: chest_model,
        render: (entity, renderLayer) => {
            if (renderLayer == "CHESTPLATE") {
                chest_model.opacity = 1.0;
                chest_model.render();
            }
        }
    };
}