extend("ahhp:hero_with_models");

var utils = implement("fiskheroes:external/utils");
var models_stuff = implement("ahhp:external/model_stuff");
var lantern_aura = implement("ahhp:external/lantern_aura");

loadTextures({
    "layer1": "ahhp:layer1",
    "layer2": "ahhp:layer2"
});

function init(renderer) {
    parent.init(renderer);
    renderer.setItemIcon("CHESTPLATE", "%s_1");
    renderer.showModel("CHESTPLATE", "head", "headwear", "body", "rightArm", "leftArm", "leftLeg", "rightLeg");
    renderer.fixHatLayer("CHESTPLATE");
    initAnimations(renderer);
    initEffects(renderer);
}

function addAnimation(renderer, key, anim) {
    if (typeof anim === "string") {
        anim = renderer.createResource("ANIMATION", anim);
    }

    renderer.addCustomAnimation(key, anim);
    return anim;
}
function addAnimationWithData(renderer, key, anim, dataVar) {
    return addAnimation(renderer, key, anim).setData((entity, data) => data.load(entity.getInterpolatedData(dataVar)));
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    addAnimationWithData(renderer, "lantern.transform", "ahhp:lantern_transform", "ahhp:dyn/lantern_timer")
        .setCondition(entity => entity.getData("ahhp:dyn/lantern_timer") > 0 && entity.getData("ahhp:dyn/lantern_timer") < 1)
        .priority = 10;
    addAnimationWithData(renderer, "lantern.telekinesis", "ahhp:lantern_telekinesis", "fiskheroes:telekinesis")
        .setCondition(entity => entity.getData("fiskheroes:telekinesis"))
        .priority = 10;
    utils.addFlightAnimation(renderer, "lantern.FLIGHT", "fiskheroes:flight/default_arms_forward.anim.json");
    utils.addHoverAnimation(renderer, "lantern.HOVER", "fiskheroes:flight/idle/neutral");
    addAnimationWithData(renderer, "lantern.ROLL", "fiskheroes:flight/barrel_roll", "fiskheroes:barrel_roll_timer")
        .priority = 10;
    addAnimationWithData(renderer, "lantern.BLOCKING", "ahhp:force_field_holding", "fiskheroes:shield_blocking_timer")
        .priority = -5;
}

function initEffects(renderer) {
    var forcefield = renderer.bindProperty("fiskheroes:forcefield");
    forcefield.color.set(getColor());
    forcefield.setShape(10, 5).setOffset(0.0, 6.0, 0.0).setScale(1.5);
    forcefield.setCondition(entity => {
        forcefield.opacity = entity.getInterpolatedData("fiskheroes:shield_blocking_timer") * 0.15;
        return true;
    });
    models = models_stuff.lanternModels(renderer, getLantern(), getRing());
    blaster = models_stuff.initLanternBlaster(renderer, getColor());
    aura = lantern_aura.init(renderer, getColor());
    glow = renderer.createEffect("fiskheroes:glowerlay");
    glow.color.set(getColor());
    particles = "ahhp:" + getLantern() + "_telekinesis";
    utils.bindCloud(renderer, "fiskheroes:telekinesis", particles);
    trail = "ahhp:" + getLantern() + "_transform";
    models_stuff.bindLanternTransformTrail(renderer, trail)
}

function render(entity, renderLayer, isFirstPersonArm) {
    models.render(entity, renderLayer);
    aura.render(entity, renderLayer);
    glow.opacity = (entity.getData("ahhp:dyn/lantern_timer") > 0 && entity.getData("ahhp:dyn/lantern_timer") < 1) ? 1.0 : 0.0;
    glow.render();
}

function getColor() {
    return 0x00FF00;
}

function getLantern() {
    return "";
}

function getRing() {
    return "lantern_ring";
}