extend("fiskheroes:hero_basic");
loadTextures({
    "base": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle",
    "suit": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle_suit.tx.json",
    "lights": "skarredheroes:null",
    "eyes": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle_eyes",
	"null": "skarredheroes:null",
	"scarab": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle_scarab",
    "wings": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle_wings",
    "wings_lights": "skarredheroes:null",
    "blades_anim": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle_blades.tx.json",
    "blades": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle_blades",
    "blades_lights": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle_blades_lights",
	"scarab_anim": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle_scarab.tx.json",
	"scarab_lights": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle_scarab_lights",
	"buster_sword_anim": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle_buster_sword.tx.json",
	"wing_shield": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle_wing_shield",
	"wing_shield_lights": "skarredheroes:null",
    "buster_sword": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle_buster_sword",
    "buster_sword_lights": "skarredheroes:blue_beetle/creetle/blue_beetle_creetle_buster_sword_lights"
});

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

var wings;
var model_blades;
var model_buster_sword;
var model_wing_shield;
var model_wing_shield_2;
var model_scarab;
var overlay;

var sword_beam;
var booster_back;
var booster_back2;
var booster_back3;

function init(renderer) {
    parent.init(renderer);
    renderer.setTexture((entity, renderLayer) => {
        if (!entity.is("DISPLAY")) {
        if (!entity.isDisplayStand()) {
            var timer = entity.getInterpolatedData("skarredheroes:dyn/scarab_timer");
            return timer == 0 ? "null" : timer < 1 ? "suit" : "base";
        }
		}
        return "base";
    });
    renderer.setLights((entity, renderLayer) => {
        return !entity.isDisplayStand() && entity.getInterpolatedData("skarredheroes:dyn/scarab_timer") < 1 ? "null" : "lights";
    });

    renderer.showModel("CHESTPLATE", "head", "headwear", "body", "rightArm", "leftArm", "rightLeg", "leftLeg");
    renderer.fixHatLayer("CHESTPLATE");
}

function initEffects(renderer) {
    wings = wing_utils.create(renderer, "wings", "wings_lights", wing_utils.PRESET_CONTROLLED_FLIGHT);

    model_wing_shield_2 = renderer.createResource("MODEL", "skarredheroes:wings2");
    model_wing_shield_2.texture.set("wing_shield", "wing_shield_lights");
    model_wing_shield_2.bindAnimation("skarredheroes:wing_shield_2").setData((entity, data) => data.load(entity.getInterpolatedData("fiskheroes:shield_timer")));
        
    wing_shield_2 = renderer.createEffect("fiskheroes:model").setModel(model_wing_shield_2);
    wing_shield_2.setOffset(38, 4, 1);
    wing_shield_2.setRotation(0, 0, -90);
    wing_shield_2.setScale(1.5);
    wing_shield_2.anchor.set("body");
	
    overlay = renderer.createEffect("fiskheroes:overlay");
    overlay.texture.set(null, "eyes");
	
    model_wing_shield = renderer.createResource("MODEL", "skarredheroes:wings");
    model_wing_shield.texture.set("wing_shield", "wing_shield_lights");
    model_wing_shield.bindAnimation("skarredheroes:wing_shield").setData((entity, data) => data.load(entity.getInterpolatedData("fiskheroes:shield_timer")));
        
    wing_shield = renderer.createEffect("fiskheroes:model").setModel(model_wing_shield);
    wing_shield.setOffset(-38, 4, 1);
    wing_shield.setRotation(0, 0, 90);
    wing_shield.setScale(1.5);
    wing_shield.anchor.set("body");
	
    model_scarab = renderer.createResource("MODEL", "skarredheroes:scarab");
    model_scarab.texture.set("scarab", "scarab_lights");
    model_scarab.bindAnimation("skarredheroes:scarab_flight").setData((entity, data) => data.load(entity.getInterpolatedData("fiskheroes:flight_timer") || entity.getInterpolatedData("fiskheroes:shield_timer")));
        
    scarab = renderer.createEffect("fiskheroes:model").setModel(model_scarab);
    scarab.setOffset(0, 0.0, 0);
    scarab.anchor.set("body");
	
    model_blades = renderer.createResource("MODEL", "skarredheroes:blue_beetle_blades");
	model_blades.texture.set("blades", "blades_lights");
        
    utils.bindTrail(renderer, "skarredheroes:creetle").setCondition(entity => 
	(entity.getInterpolatedData("fiskheroes:blade_timer") < 1 && entity.getInterpolatedData("fiskheroes:blade_timer") > 0) || 
	(entity.getInterpolatedData("skarredheroes:dyn/buster_sword_timer") < 1 && entity.getInterpolatedData("skarredheroes:dyn/buster_sword_timer") > 0) || 
	(entity.getInterpolatedData("fiskheroes:shield_timer") < 1 && entity.getInterpolatedData("fiskheroes:shield_timer") > 0) || 
	(entity.getInterpolatedData("fiskheroes:flight_timer") < 0.5 && entity.getInterpolatedData("fiskheroes:flight_timer") > 0) || 
	(entity.getInterpolatedData("fiskheroes:flight_boost_timer") < 0.5 && entity.getInterpolatedData("fiskheroes:flight_boost_timer") > 0));
	
    blades = renderer.createEffect("fiskheroes:model").setModel(model_blades);
    blades.setOffset(0, 0.0, 0);
    blades.anchor.set("rightArm");
    blades.mirror = true;
	
    model_buster_sword = renderer.createResource("MODEL", "skarredheroes:blue_beetle_buster_sword");
	model_buster_sword.texture.set("buster_sword", "buster_sword_lights");
	
    buster_sword = renderer.createEffect("fiskheroes:model").setModel(model_buster_sword);
    buster_sword.setOffset(1, -22.0, -6);
	buster_sword.setScale(1.3);
    buster_sword.anchor.set("rightArm");
	
    var beam = renderer.createResource("BEAM_RENDERER", "skarredheroes:booster");
    var fire = renderer.createResource("ICON", "fiskheroes:red_fire_layer_%s");
    var color = 0xFF0000;
    booster_back = boosters.create(renderer, "body", true, color, fire, beam).setOffset(1.5, 7.6, 2.35).setRotation(20.0, 0.0, 15.0);
    booster_back2 = boosters.create(renderer, "body", true, color, fire, beam).setOffset(1.5, 5.6, 2.35).setRotation(20.0, 0.0, 15.0);
    booster_back3 = boosters.create(renderer, "body", true, color, fire, beam).setOffset(1.5, 3.6, 2.35).setRotation(20.0, 0.0, 15.0);
	
    utils.addCameraShake(renderer, 0.015, 1.5, "fiskheroes:flight_boost_timer");
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);
    renderer.removeCustomAnimation("basic.BLOCKING");
	
    utils.addHoverAnimation(renderer, "falcon.HOVER", "fiskheroes:flight/idle/falcon");
    utils.addFlightAnimation(renderer, "falcon.FLIGHT", getFlightAnimation(), (entity, data) => {
        data.load(0, entity.getInterpolatedData("fiskheroes:flight_timer"));
        data.load(1, entity.getInterpolatedData("fiskheroes:flight_boost_timer"));
        data.load(3, entity.getInterpolatedData("fiskheroes:dyn/flight_super_boost_timer"));
    });
	
    renderer.reprioritizeDefaultAnimation("RELOAD_GUN", -9);
    
    utils.addAnimationEvent(renderer, "FLIGHT_DIVE", "fiskheroes:iron_man_dive");
    utils.addAnimationEvent(renderer, "FLIGHT_DIVE_ROLL", "fiskheroes:falcon_dive_roll");
    
    addAnimationWithData(renderer, "beetle.SWORD", "fiskheroes:sword_pose", "skarredheroes:dyn/buster_sword_timer")
        .priority = 10;
		
    addAnimationWithData(renderer, "basic.BLOCK", "fiskheroes:ocular_beam", "fiskheroes:shield_blocking_timer");
	
    addAnimationWithData(renderer, "beetle.ROLL", "fiskheroes:flight/barrel_roll", "fiskheroes:barrel_roll_timer")
        .priority = 10;
}

function getFlightAnimation() {
    return "fiskheroes:flight/falcon.anim.json";
}

function render(entity, renderLayer, isFirstPersonArm) {
    if (renderLayer == "CHESTPLATE") {
        overlay.opacity = entity.getInterpolatedData("fiskheroes:flight_timer") || entity.getInterpolatedData("fiskheroes:blade_timer") || entity.getInterpolatedData("skarredheroes:dyn/buster_sword_timer");
        overlay.render();
		if (entity.getData("skarredheroes:dyn/scarab")) {
        wings.render(entity, 0);
		}
		
		if (entity.getData("fiskheroes:shield_timer") > 0.2) {
		wing_shield.render();
		wing_shield_2.render();
	
            if (isFirstPersonArm) {
                wing_shield.anchor.ignoreAnchor(true);
                wing_shield_2.anchor.ignoreAnchor(true);
            }
            else {
                wing_shield.anchor.ignoreAnchor(false);
                wing_shield_2.anchor.ignoreAnchor(false);
            }
		}
		
		if (entity.getInterpolatedData("fiskheroes:blade_timer") < 1 && entity.getInterpolatedData("fiskheroes:blade_timer") > 0) {
			model_blades.texture.set("blades_anim");
		} else if (entity.getInterpolatedData("fiskheroes:blade_timer") == 0) {
			model_blades.texture.set("null");
		} else {
			model_blades.texture.set("blades", "blades_lights");
		}
		
		if (entity.getInterpolatedData("skarredheroes:dyn/scarab_timer") < 1 && entity.getInterpolatedData("skarredheroes:dyn/scarab_timer") > 0) {
			model_scarab.texture.set("scarab_anim");
        } else if  (entity.is("DISPLAY")) { 
			model_scarab.texture.set("scarab", "scarab_lights");
		} else if (entity.getInterpolatedData("skarredheroes:dyn/scarab_timer") == 0 && !entity.isDisplayStand() && !entity.isBookPlayer()) {
			model_scarab.texture.set("null");
		} else {
			model_scarab.texture.set("scarab", "scarab_lights");
		}
		
		if (entity.getInterpolatedData("skarredheroes:dyn/buster_sword_timer") < 1 && entity.getInterpolatedData("skarredheroes:dyn/buster_sword_timer") > 0) {
			model_buster_sword.texture.set("buster_sword_anim");
		} else if (entity.getInterpolatedData("skarredheroes:dyn/buster_sword_timer") == 0) {
			model_buster_sword.texture.set("null");
		} else {
			model_buster_sword.texture.set("buster_sword", "buster_sword_lights");
		}
		if (entity.getData("skarredheroes:dyn/buster_sword_timer") > 0) {
		buster_sword.render();
		}
		
		if (entity.getData("fiskheroes:blade_timer") > 0) {
		blades.render();
		}
	}
	if (!isFirstPersonArm && renderLayer == "CHESTPLATE") { 

		var boost = entity.getInterpolatedData("fiskheroes:flight_boost_timer");
		booster_back.speedScale = 0.5 * boost;
		booster_back.flutter = 1 + boost;

		var f = Math.min(Math.max(boost * 3 - 1.25, 0), 1);
		f = entity.isSprinting() ? 0.5 - Math.cos(2 * f * Math.PI) / 2 : 0;
		booster_back.setSize(1.5 + f * 2, 3.5 - f * 2);
		booster_back.render(entity, entity.getInterpolatedData("fiskheroes:flight_timer"));
	
		booster_back2.speedScale = 0.5 * boost;
		booster_back2.flutter = 1 + boost;

		var f = Math.min(Math.max(boost * 3 - 1.25, 0), 1);
		f = entity.isSprinting() ? 0.5 - Math.cos(2 * f * Math.PI) / 2 : 0;
		booster_back2.setSize(1.5 + f * 2, 3.5 - f * 2);
		booster_back2.render(entity, entity.getInterpolatedData("fiskheroes:flight_timer"));
	
		booster_back3.speedScale = 0.5 * boost;
		booster_back3.flutter = 1 + boost;

		var f = Math.min(Math.max(boost * 3 - 1.25, 0), 1);
		f = entity.isSprinting() ? 0.5 - Math.cos(2 * f * Math.PI) / 2 : 0;
		booster_back3.setSize(1.5 + f * 2, 3.5 - f * 2);
		booster_back3.render(entity, entity.getInterpolatedData("fiskheroes:flight_timer"));
		if (!entity.isBookPlayer()) {
			scarab.render();    
		}
	}
}
    
