extend("fiskheroes:hero_basic");
loadTextures({
    "wand": "pkg:wand",
    "layer1": "pkg:null"
});

var utils = implement("fiskheroes:external/utils");

var wand;
var wand_back;


function init(renderer) {
    parent.init(renderer);
    renderer.showModel("CHESTPLATE", "head", "headwear", "body", "rightArm", "leftArm");
}

function initEffects(renderer) {
    parent.initEffects(renderer);

    wand = renderer.createEffect("fiskheroes:model");
    wand.setModel(utils.createModel(renderer, "pkg:wand", "wand", null));
    wand.anchor.set("rightArm");
    wand.mirror = false;

    utils.bindParticles(renderer, "pkg:bifrost_smoke").setCondition(entity => entity.getInterpolatedData('fiskheroes:teleport_timer') > 0 && entity.getInterpolatedData('fiskheroes:teleport_timer') < 1);
    utils.setOpacityWithData(renderer, 2.0, 21.0, "fiskheroes:shadowform_timer");
    utils.bindCloud(renderer, "fiskheroes:particle_cloud", "pkg:death_eater").setCondition(entity => entity.getData("fiskheroes:shadowform"));

    // charged beam
    utils.bindBeam(renderer, "fiskheroes:charged_beam", "pkg:bifrost", "rightArm", 0x8EFE66, [
        { "firstPerson": [0.80, 0.80, -1.0], "offset": [0.0, 4.0, -8.0], "size": [1.0, 1.0] }
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_antimatter"));

    // energy manipulation
    utils.bindBeam(renderer, "fiskheroes:energy_manipulation", "pkg:bifrost", "rightArm", 0xE6240E, [
        { "firstPerson": [-2.5, 0.0, -7.0], "offset": [-0.5, 19.0, -12.0], "size": [0.5, 0.5] }
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_antimatter"));
}


function initAnimations(renderer) {
    parent.initAnimations(renderer);
    utils.addFlightAnimation(renderer, "firestorm.FLIGHT", "fiskheroes:flight/default_arms_forward.anim.json");
    utils.addHoverAnimation(renderer, "firestorm.HOVER", "fiskheroes:flight/idle/propelled_hands");
    addAnimationWithData(renderer, "iron_man.LAND", "fiskheroes:superhero_landing", "fiskheroes:dyn/superhero_landing_timer")
        .priority = -8;

    renderer.removeCustomAnimation("basic.AIMING");
    renderer.removeCustomAnimation("basic.ENERGY_PROJ");
    renderer.removeCustomAnimation("basic.CHARGED_BEAM");
    addAnimation(renderer, "basic.AIMING", "fiskheroes:aiming").setData((entity, data) => {
        var charge = entity.getInterpolatedData("fiskheroes:beam_charge");
        data.load(Math.max(entity.getInterpolatedData("fiskheroes:aiming_timer"), entity.getData("fiskheroes:beam_charging") ? Math.min(charge * 3, 1) : Math.max(charge * 5 - 4, 0)));
    });
}

function render(entity, renderLayer, isFirstPersonArm) {
    if (entity.getData("pkg:dyn/wand_timer") > 0.6) {
        var wand_anchor = wand.anchor.set("rightArm");
        var wand_offSet = wand.setOffset(0.0, 8, 0);
        var wand_rot = wand.setRotation(0, 180, 0);

        wand.anchor = wand_anchor, wand_offSet, wand_rot;
        wand.render();
    }
    else {
        var back_anchor = wand.anchor.set("body");
        //pocket        var back_offSet = wand.setOffset(-4.0, 8, 0);
        var back_offSet = wand.setOffset(-4.0, 7, -2);
        //back        var back_offSet = wand.setOffset(-2.0, 8, 3);
        //pocket&back        var back_rot = wand.setRotation(-90.0, 0.0, 0.0);
        var back_rot = wand.setRotation(-75.0, 0.0, 0.0);

        wand.anchor = back_anchor, back_offSet, back_rot;
        wand.render();
    }
}