extend("skarlet:blue_beetle_base");
loadTextures({
    "base": "skarlet:blue_beetle/creetle/blue_beetle_creetle",
    "suit": "skarlet:blue_beetle/creetle/blue_beetle_creetle_suit.tx.json",
    "lights": "skarlet:null",
    "eyes": "skarlet:blue_beetle/creetle/blue_beetle_creetle_eyes",
    "mask": "skarlet:blue_beetle/creetle/blue_beetle_creetle_mask.tx.json",
    "mask_lights": "skarlet:null",
	"null": "skarlet:null",
	"scarab": "skarlet:blue_beetle/creetle/blue_beetle_creetle_scarab",
    "wings": "skarlet:blue_beetle/creetle/blue_beetle_creetle_wings",
    "wings_lights": "skarlet:null",
    "blades_anim": "skarlet:blue_beetle/creetle/blue_beetle_creetle_blades.tx.json",
    "blades": "skarlet:blue_beetle/creetle/blue_beetle_creetle_blades",
    "blades_lights": "skarlet:blue_beetle/creetle/blue_beetle_creetle_blades_lights",
    "blaster_anim": "skarlet:blue_beetle/creetle/blue_beetle_creetle_blaster.tx.json",
    "blaster": "skarlet:blue_beetle/creetle/blue_beetle_creetle_blaster",
    "blaster_lights": "skarlet:blue_beetle/creetle/blue_beetle_creetle_blaster_lights",
	"scarab_anim": "skarlet:blue_beetle/creetle/blue_beetle_creetle_scarab.tx.json",
	"scarab_lights": "skarlet:blue_beetle/creetle/blue_beetle_creetle_scarab_lights",
	"buster_sword_anim": "skarlet:blue_beetle/creetle/blue_beetle_creetle_buster_sword.tx.json",
	"wing_shield": "skarlet:blue_beetle/creetle/blue_beetle_creetle_wing_shield",
	"wing_shield_lights": "skarlet:null",
    "buster_sword": "skarlet:blue_beetle/creetle/blue_beetle_creetle_buster_sword",
    "buster_sword_lights": "skarlet:blue_beetle/creetle/blue_beetle_creetle_buster_sword_lights"
});

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

var overlay;

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

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

    overlay = renderer.createEffect("fiskheroes:overlay");
    overlay.texture.set(null, "eyes");
	
    utils.bindTrail(renderer, "skarlet:creetle").setCondition(entity => 
	(entity.getInterpolatedData("fiskheroes:blade_timer") < 1 && entity.getInterpolatedData("fiskheroes:blade_timer") > 0) || 
	(entity.getInterpolatedData("skarlet:dyn/buster_sword_timer") < 1 && entity.getInterpolatedData("skarlet: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) || 
    (entity.getInterpolatedData("fiskheroes:aimed_timer") < 1 && entity.getInterpolatedData("fiskheroes:aimed_timer") > 0));
	
    var beam = renderer.createResource("BEAM_RENDERER", "skarlet: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.bindBeam(renderer, "fiskheroes:repulsor_blast", "skarlet:blue_beetle_blaster", "rightArm", 0xFF0000, [
        { "firstPerson": [-4.5, 3.75, -7.0], "offset": [-0.5, 9.0, 0.0], "size": [3.5, 3.5] }
    ]);
}

function render(entity, renderLayer, isFirstPersonArm) {
    parent.render(entity, renderLayer, isFirstPersonArm);

    if (renderLayer == "CHESTPLATE") {
		if (entity.getData("fiskheroes:mask_open_timer2") == 0) {
			overlay.texture.set(null, "eyes");
		} else {
			overlay.texture.set(null, null);
		}
		overlay.opacity = entity.getInterpolatedData("fiskheroes:flight_timer") || entity.getInterpolatedData("fiskheroes:blade_timer") || entity.getInterpolatedData("skarlet:dyn/buster_sword_timer") || entity.getInterpolatedData("fiskheroes:aimed_timer");
        overlay.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"));
	}
}
    
