extend("fiskheroes:hero_basic");
loadTextures({
    "base": "skarlet:blue_beetle/blue_beetle",
    "suit": "skarlet:blue_beetle/blue_beetle_suit.tx.json",
    "lights": "skarlet:blue_beetle/blue_beetle_lights",
    "mask": "skarlet:blue_beetle/blue_beetle_mask.tx.json",
    "mask_lights": "skarlet:blue_beetle/blue_beetle_lights_mask",
	"null": "skarlet:null",
	"scarab": "skarlet:blue_beetle/blue_beetle_scarab",
    "wings": "skarlet:blue_beetle/blue_beetle_wings",
    "wings_lights": "skarlet:blue_beetle/blue_beetle_wings_lights",
    "blades_anim": "skarlet:blue_beetle/blue_beetle_blades.tx.json",
    "blades": "skarlet:blue_beetle/blue_beetle_blades",
    "blades_lights": "skarlet:blue_beetle/blue_beetle_blades_lights",
    "blaster_anim": "skarlet:blue_beetle/blue_beetle_blaster.tx.json",
    "blaster": "skarlet:blue_beetle/blue_beetle_blaster",
    "blaster_lights": "skarlet:blue_beetle/blue_beetle_blaster_lights",
	"scarab_anim": "skarlet:blue_beetle/blue_beetle_scarab.tx.json",
	"scarab_lights": "skarlet:blue_beetle/blue_beetle_scarab_lights",
	"buster_sword_anim": "skarlet:blue_beetle/blue_beetle_buster_sword.tx.json",
	"wing_shield": "skarlet:blue_beetle/blue_beetle_wing_shield",
	"wing_shield_lights": "skarlet:blue_beetle/blue_beetle_wing_shield_lights",
    "buster_sword": "skarlet:blue_beetle/blue_beetle_buster_sword",
    "buster_sword_lights": "skarlet:blue_beetle/blue_beetle_buster_sword_lights"
});

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

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

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

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

function initEffects(renderer) {
    var night_vision = renderer.bindProperty("fiskheroes:night_vision").setCondition(entity => {
        night_vision.factor = 1 - entity.getInterpolatedData("fiskheroes:mask_open_timer2");
        night_vision.firstPersonOnly = false;
        if (entity.getData("skarlet:dyn/scarab")) {
            return true;
        } else {
            return false;
        }
    });

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

    model_wing_shield_2 = renderer.createResource("MODEL", "skarlet:wings2");
    model_wing_shield_2.texture.set("wing_shield", "wing_shield_lights");
    model_wing_shield_2.bindAnimation("skarlet: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");
	
    model_wing_shield = renderer.createResource("MODEL", "skarlet:wings");
    model_wing_shield.texture.set("wing_shield", "wing_shield_lights");
    model_wing_shield.bindAnimation("skarlet: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", "skarlet:scarab");
    model_scarab.texture.set("scarab", "scarab_lights");
    model_scarab.bindAnimation("skarlet: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", "skarlet:blue_beetle_blades");
	model_blades.texture.set("blades", "blades_lights");
        
    blades = renderer.createEffect("fiskheroes:model").setModel(model_blades);
    blades.setOffset(0, 0.0, 0);
    blades.anchor.set("rightArm");
    blades.mirror = true;

    model_blaster = renderer.createResource("MODEL", "skarlet:blue_beetle_blaster");
	model_blaster.texture.set("blaster", "blaster_lights");
        
    blaster = renderer.createEffect("fiskheroes:model").setModel(model_blaster);
    blaster.setOffset(0, 0.0, 0);
    blaster.anchor.set("rightArm");
	
    model_buster_sword = renderer.createResource("MODEL", "skarlet: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");
	
    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", "skarlet: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") {
		
		if (entity.getData("skarlet: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("fiskheroes:aimed_timer") < 1 && entity.getInterpolatedData("fiskheroes:aimed_timer") > 0) {
			model_blaster.texture.set("blaster_anim");
		} else if (entity.getInterpolatedData("fiskheroes:aimed_timer") == 0) {
			model_blaster.texture.set("null");
		} else {
			model_blaster.texture.set("blaster", "blaster_lights");
		}
		
		if (entity.getInterpolatedData("skarlet:dyn/scarab_timer") < 1 && entity.getInterpolatedData("skarlet: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("skarlet:dyn/scarab_timer") == 0 && !entity.isDisplayStand() && !entity.isBookPlayer()) {
			model_scarab.texture.set("null");
		} else {
			model_scarab.texture.set("scarab", "scarab_lights");
        }
		if (entity.getInterpolatedData("skarlet:dyn/buster_sword_timer") < 1 && entity.getInterpolatedData("skarlet:dyn/buster_sword_timer") > 0) {
			model_buster_sword.texture.set("buster_sword_anim");
		} else if (entity.getInterpolatedData("skarlet: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("skarlet:dyn/buster_sword_timer") > 0) {
		    buster_sword.render();
		}
		
		if (entity.getData("fiskheroes:blade_timer") > 0) {
		    blades.render();
		}

		if (entity.getData("fiskheroes:aimed_timer") > 0) {
            blaster.render();
        }
		scarab.render();  
	}
}
    
