extend("fiskheroes:hero_basic");
loadTextures({
    "layer1":"diminpack:idp364_layer1",
    "layer2":"diminpack:idp364_layer2",
    "cape":"diminpack:spawn_cape"
});
var utils = implement("fiskheroes:external/utils");
var mandalas = implement("fiskheroes:external/tao_mandalas");
var eldritch_sword = implement("fiskheroes:external/eldritch_sword");
var capes = implement("fiskheroes:external/capes");
var cape;

var shield;
var sword;
var spell;

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


    renderer.showModel("CHESTPLATE", "head", "body", "rightArm", "leftArm");

}

function initEffects(renderer) {
    var color = 0xFF4800;
    var chain = utils.bindCloud(renderer, "fiskheroes:telekinesis_chain", "fiskheroes:shadow_smoke");
    chain.anchor.set("rightArm");
    chain.setOffset(-0.5, 10.0, 0.0);
    chain.setFirstPerson(-4.75, 4.0, -8.5);
    var tao_mandala = renderer.createResource("SHAPE", "fiskheroes:tao_mandala");
    var beam = renderer.createResource("BEAM_RENDERER", "fiskheroes:line");
    spell = renderer.createEffect("fiskheroes:lines").setShape(tao_mandala).setRenderer(beam);
    spell.color.set(color);
    spell.setOffset(1.0, 8.0, 0.0).setScale(3.2);
    spell.anchor.set("rightArm");
    spell.mirror = true;

    shield = mandalas.create(renderer, color, tao_mandala, beam);
    sword = eldritch_sword.create(renderer, color, beam);
    sword.setOffset(1.0, 9.4, -4.0).setRotation(10.0, 0.0, 0.0).setScale(16.0);

    var magic = renderer.bindProperty("fiskheroes:spellcasting");
    magic.colorGeneric.set(0xFF8000);
    magic.colorEarthCrack.set(0xB366FF);
    magic.colorAtmosphere.set(0xFF8000);
    magic.colorWhip.set(color);
    /*utils.bindBeam(renderer, "fiskheroes:charged_beam", "fiskheroes:charged_beam", "rightArm", 0xFFC462, [
        { "firstPerson": [0.0, 0.0, 0.0], "offset": [0.0, 20.0, 0.0], "size": [3.0, 3.0] }
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_charged_beam"));*/
    
    utils.bindBeam(renderer, "fiskheroes:charged_beam", "fiskheroes:energy_projection", "head", 0xFFC462, [
        { "firstPerson": [0.0, 10.0, -16.0], "offset": [0.0, 2.0, -14.0], "size": [3.0, 3.0] }
    ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_charged_beam"));
    
    utils.bindBeam(renderer, "fiskheroes:energy_projection", "fiskheroes:energy_projection", "rightArm", 0xFFC462, [
     { "firstPerson": [-4.0, 0.0, -20.0], "offset": [0.0, 0.0, 0.0], "size": [1.5, 1.5] },
     { "firstPerson": [4.0, 0.0, -20.0], "offset": [0.0, 0.0, 0.0], "size": [1.5, 1.5], "anchor": "leftArm" }
 ]).setParticles(renderer.createResource("PARTICLE_EMITTER", "fiskheroes:impact_energy_projection"));
    
    var physics = renderer.createResource("CAPE_PHYSICS", null);
    /*physics.weight = 0.7;
    physics.maxFlare = 1.6;
    physics.flareDegree = 2.5;
    physics.flareFactor = 1.5;
    physics.flareElasticity = 150;*/
    physics.maxFlare = 0.3;
    physics.flareDegree = 1.5;
    physics.flareFactor = 1.2;
    physics.flareElasticity = 5;
    physics.setTickHandler(entity => {
        var f = 1 - entity.getData("fiskheroes:flight_timer");
        f = 1 - f * f * f;
        physics.headingAngle = 90 - f * 20;
        physics.restAngle = f * 45;
        physics.restFlare = f * 0.8;
        physics.idleFlutter = 0.30 + 0.25 * f;
        physics.flutterSpeed = f * 0.4;
    });

    cape = capes.createDefault(renderer, 25, "fiskheroes:cape_default.mesh.json", physics);
    cape.effect.width = 16;
    cape.effect.texture.set("cape");

    

}
function initAnimations(renderer) {
    parent.initAnimations(renderer);
    renderer.removeCustomAnimation("basic.ENERGY_PROJ");
    renderer.removeCustomAnimation("basic.CHARGED_BEAM");
    renderer.removeCustomAnimation("basic.AIMING");
    
    addAnimationWithData(renderer, "basic.AIMING", "fiskheroes:dual_aiming", "fiskheroes:aiming_timer")
        .priority = 10;
    addAnimationWithData(renderer, "basic.ENERGY_PROJ", "fiskheroes:dual_aiming", "fiskheroes:energy_projection_timer")
        .priority = 10;
    /*addAnimation(renderer, "basic.AIMING", "fiskheroes:dual_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)));
    });*/

    renderer.removeCustomAnimation("basic.BLOCKING");
    addAnimationWithData(renderer, "basic.BLOCKING", "fiskheroes:dual_aiming", "fiskheroes:shield_blocking_timer")
        .priority = -5;

    addAnimationWithData(renderer, "strange.SWORD_POSE", "fiskheroes:sword_pose", "fiskheroes:blade_timer");

    utils.addHoverAnimation(renderer, "strange.HOVER", "fiskheroes:flight/idle/neutral");
    utils.addFlightAnimation(renderer, "strange.FLIGHT", "fiskheroes:flight/levitate.anim.json", (entity, data) => {
        data.load(entity.getInterpolatedData("fiskheroes:flight_timer"));
    });
}


function render(entity, renderLayer, isFirstPersonArm) {
    if (renderLayer == "CHESTPLATE") {
        if (!isFirstPersonArm) {
            cape.render(entity);
        }
        spell.progress = entity.getInterpolatedData("fiskheroes:spellcast_timer");
        spell.render();

        shield.render(entity, isFirstPersonArm);
        sword.render(entity.getInterpolatedData("fiskheroes:blade_timer"));

    }
}
