extend("fiskheroes:hero_basic");
loadTextures({
	"layer1": "fan:clayface",
    "blank": "fan:blank",
    "clayface": "fan:clayface_big",
    "tentacle": "fan:tentacle",
    "shadowdome": "fan:shadow",
    "ears": "fan:ears" 
});

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

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

    renderer.setTexture((entity, renderLayer) => {
		if (entity.getData("fan:dyn/joker_form")) {
            return "joker";
        }
		if (entity.getData("fan:dyn/batman_form")) {
            return "batman";
        }
		if (entity.getData("fan:dyn/flash_form")) {
            return "the_flash";
        }
		if (entity.getData("fan:dyn/face_form")) {
            return "two_face";
        }
        return entity.getInterpolatedData("fan:dyn/giant_timer") > 0.4 || entity.getData("fiskheroes:speeding") ? "blank" : "layer1";
    });

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

function initEffects(renderer) {
	var physics = renderer.createResource("CAPE_PHYSICS", null);
    physics.maxFlare = 0.4;
    cape = capes.createDefault(renderer, 24, "fiskheroes:cape_default.mesh.json", physics);
    cape.effect.texture.set("cape");
    cape.effect.width = 14;
	
	utils.setOpacityWithData(renderer, 0.999, 0.999, "fiskheroes:intangibility_timer"); 
	
	joker = renderer.createEffect("fiskheroes:overlay");
    joker.texture.set("joker", null);
	
	batman = renderer.createEffect("fiskheroes:overlay");
	batman.texture.set("batman", null);
	
	two_face = renderer.createEffect("fiskheroes:overlay");
    two_face.texture.set("two_face", null);
	
	the_flash = renderer.createEffect("fiskheroes:overlay");
    the_flash.texture.set("the_flash", null);
	
	
	var model_clay_ball = renderer.createResource("MODEL", "fan:clay_ball");
    model_clay_ball.texture.set("clayball");
	model_clay_ball.bindAnimation("fan:clay_ball_spin").setData((entity,data) => data.load(entity.getInterpolatedData("fan:dyn/move_timer")*2));
    clay_ball = renderer.createEffect("fiskheroes:model").setModel(model_clay_ball);
    clay_ball.anchor.set("body");
	clay_ball.setOffset(0, 72, 0);
	clay_ball.setScale(2.0); 
    
	var model_clay_head = renderer.createResource("MODEL", "fan:clay_head");
    model_clay_head.texture.set("clayface");
    clay_head = renderer.createEffect("fiskheroes:model").setModel(model_clay_head);
    clay_head.anchor.set("head");
	clay_head.setOffset(0, 8, 1);
	clay_head.setScale(0.75); 
	
    var model_clay_body = renderer.createResource("MODEL", "fan:clay_body_nh");
    model_clay_body.texture.set("clayface");
    clay_body = renderer.createEffect("fiskheroes:model").setModel(model_clay_body);
    clay_body.anchor.set("body");
	clay_body.setOffset(0, 6, 0);
	clay_body.setScale(0.75); 

    var model_clay_rightArm = renderer.createResource("MODEL", "fan:clay_r_arm");
    model_clay_rightArm.texture.set("clayface");
    clay_rightArm = renderer.createEffect("fiskheroes:model").setModel(model_clay_rightArm);
    clay_rightArm.anchor.set("leftArm");
	clay_rightArm.setOffset(5, 9, 0);
	clay_rightArm.setScale(0.75);  

    var model_clay_leftArm = renderer.createResource("MODEL", "fan:clay_larm");
    model_clay_leftArm.texture.set("clayface");
    clay_leftArm = renderer.createEffect("fiskheroes:model").setModel(model_clay_leftArm);
    clay_leftArm.anchor.set("rightArm");
	clay_leftArm.setOffset(-5, 9, 0);
	clay_leftArm.setScale(0.75); 
	
	var model_clay_rightLeg = renderer.createResource("MODEL", "fan:clay_r_leg");
    model_clay_rightLeg.texture.set("clayface");
    clay_rightLeg = renderer.createEffect("fiskheroes:model").setModel(model_clay_rightLeg);
    clay_rightLeg.anchor.set("rightLeg");
	clay_rightLeg.setOffset(-2, -6, 0);
	clay_rightLeg.setScale(0.75);  

    var model_clay_leftLeg = renderer.createResource("MODEL", "fan:clay_l_leg");
    model_clay_leftLeg.texture.set("clayface");
    clay_leftLeg = renderer.createEffect("fiskheroes:model").setModel(model_clay_leftLeg);
    clay_leftLeg.anchor.set("leftLeg");
	clay_leftLeg.setOffset(2, -6, 0);
	clay_leftLeg.setScale(0.75); 

    var model_clay_mace = renderer.createResource("MODEL", "fan:clay_lmace");
    model_clay_mace.texture.set("clayface");
    model_clay_mace.bindAnimation("fan:clay_mace").setData((entity,data) => data.load(entity.getInterpolatedData("fiskheroes:blade_timer")));
    clay_mace = renderer.createEffect("fiskheroes:model").setModel(model_clay_mace);
    clay_mace.anchor.set("rightArm");
	// clay_mace.setScale(0.75);
	clay_mace.setOffset(-5, 11, 0);


    var model_clay_hammer = renderer.createResource("MODEL", "fan:clay_lhammer");
    model_clay_hammer.texture.set("clayface");
    model_clay_hammer.bindAnimation("fan:clay_hammer").setData((entity,data) => data.load(entity.getInterpolatedData("fiskheroes:blade_timer")));
    clay_hammer = renderer.createEffect("fiskheroes:model").setModel(model_clay_hammer);
    clay_hammer.anchor.set("leftArm");
	// clay_hammer.setScale(0.75);
	clay_hammer.setOffset(-16, 11, 0);
    
	var model_clay_hand = renderer.createResource("MODEL", "fan:clay_lhand");
    model_clay_hand.texture.set("clayface");
    clay_hand = renderer.createEffect("fiskheroes:model").setModel(model_clay_hand);
    clay_hand.anchor.set("rightArm");
	clay_hand.setScale(0.75);
	clay_hand.setOffset(-5, 9, 0);
	
	utils.bindBeam(renderer, "fiskheroes:energy_projection", "fan:no_beam", "head", 0xffffff, [
        { "firstPerson": [-3.75, 3.0, -8.0], "offset": [0.5, 12.0, 0.0], "size": [3.0, 3.0],}
    ]);

        ears = renderer.createEffect("fiskheroes:model");
        ears.setModel(utils.createModel(renderer, "fan:ears", "ears"));
        ears.anchor.set("head");
        ears.setScale(1);
        ears.setOffset(4, -11, 4);
        ears.setRotation(90, 90, -90)

	var shadowdome = renderer.bindProperty("fiskheroes:shadowdome");
	shadowdome.texture.set("shadowdome", "shadowdome");
	shadowdome.setShape(36, 36);
 
    var forcefield = renderer.bindProperty("fiskheroes:forcefield");
    forcefield.color.set(0xf92424);
    forcefield.setShape(36, 18).setOffset(0.0, 16.0, 0.0).setScale(3);
    forcefield.setCondition(entity => {
        forcefield.opacity = entity.getInterpolatedData("fiskheroes:shield_blocking_timer") * 0.5;
        return true;
    });

        var ock_arm = utils.createModel(renderer, "fan:tentacle", "tentacle");
        var ock_claw = utils.createModel(renderer, "fan:tentacle_end", "tentacle");
        ock_claw.bindAnimation("fiskheroes:ock_claw").setData((entity, data) => {
            var t = entity.as("TENTACLE");
            data.load(0, 1 - Math.min(t.getCaster().getInterpolatedData("fiskheroes:tentacle_extend_timer") * 2, 1));
            data.load(1, t.getIndex());
            data.load(2, t.getGrabTimer());
            data.load(3, t.getStrikeTimer());
            
        });
        
        utils.setOpacityWithData(renderer, 0.0, 1.0, "fiskheroes:shadowform_timer");
        utils.bindCloud(renderer, "fiskheroes:particle_cloud", "fiskheroes:shadow_smoke").setCondition(entity => entity.getData("fiskheroes:shadowform"));
        
        var tentacles = renderer.bindProperty("fiskheroes:tentacles").setTentacles([
            { "offset": [2.0, -4.5, -2.0], "direction": [13.0, 10.0, -10.0] },
            { "offset": [-2.0, -4.5, -2.0], "direction": [-13.0, 10.0, -10.0] },
            { "offset": [2.0, -7.5, -2.0], "direction": [13.0, -10.0, -10.0] },
            { "offset": [-2.0, -7.5, -2.0], "direction": [-13.0, -10.0, -10.0] }
        ]);
        tentacles.anchor.set("body");
        tentacles.setSegmentModel(ock_arm);
        tentacles.setHeadModel(ock_claw);
        tentacles.segmentLength = 1;
        tentacles.segments = 25;
	
}

function initAnimations(renderer) {
    parent.initAnimations(renderer);		
	renderer.removeCustomAnimation("basic.ENERGY_PROJ");                        		
	addAnimationWithData(renderer, "two.PUNCH", "fan:due_punch").setData((entity,data) => data.load(entity.getData("fiskheroes:blade_timer") > 0.3 && entity.isPunching())).priority = 5;	
	addAnimationWithData(renderer, "bane.WALK", "fan:grundy_walk", "fan:dyn/giant_timer")
    .setCondition(entity => entity.getInterpolatedData("fan:dyn/giant_timer") > 0.4);
    
	addAnimationWithData(renderer, "bane.TRANSFORM", "fan:bane_trans", "fan:dyn/giant_timer");

    var anim = renderer.createResource("ANIMATION", "fan:bane_mode");
    anim.setData((entity, data) => {
        data.load(0, entity.getInterpolatedData("fan:dyn/giant_timer") * 1.25);
        data.load(1, (-0.8 + entity.getInterpolatedData("fan:dyn/giant_timer")) < 0 ? 0 : -0.8 + entity.getInterpolatedData("fan:dyn/giant_timer"));
    });	
}

function render(entity, renderLayer, isFirstPersonArm) {
	
	if (entity.getData("fan:dyn/batman_form")) {
               cape.render(entity);
           }
	
	if (entity.getData("fan:dyn/joker_form") && !entity.getData("fan:dyn/batman_form") && !entity.getData("fan:dyn/face_form") && !entity.getData("fan:dyn/flash_form")) {
            joker.opacity = entity.getInterpolatedData("fan:dyn/joker_form_timer");
            joker.render();
        } else {
            joker.opacity = entity.getInterpolatedData("fan:dyn/joker_form_timer");
            joker.render();
        }
		
	if (entity.getData("fan:dyn/batman_form") && !entity.getData("fan:dyn/joker_form") && !entity.getData("fan:dyn/face_form") && !entity.getData("fan:dyn/flash_form")) {
           batman.opacity = entity.getInterpolatedData("fan:dyn/batman_form_timer");
           batman.render();
        } else {
            batman.opacity = entity.getInterpolatedData("fan:dyn/batman_form_timer");
            batman.render();
        }
		
	if (entity.getData("fan:dyn/face_form") && !entity.getData("fan:dyn/batman_form") && !entity.getData("fan:dyn/joker_form") && !entity.getData("fan:dyn/flash_form")) {
            two_face.opacity = entity.getInterpolatedData("fan:dyn/face_form_timer");
            two_face.render();
        } else {
            two_face.opacity = entity.getInterpolatedData("fan:dyn/face_form_timer");
            two_face.render();
        }
		
	if (entity.getData("fan:dyn/flash_form") && !entity.getData("fan:dyn/batman_form") && !entity.getData("fan:dyn/joker_form") && !entity.getData("fan:dyn/face_form")) {
            the_flash.opacity = entity.getInterpolatedData("fan:dyn/flash_form_timer");
            the_flash.render();
        } else {
            the_flash.opacity = entity.getInterpolatedData("fan:dyn/flash_form_timer");
            the_flash.render();
        }
		
	
    if (!isFirstPersonArm && renderLayer == "HELMET", "CHESTPLATE", "LEGGINGS", "BOOTS") {
	"fiskheroes:blade_timer"

	if (entity.getData("fiskheroes:blade_timer") > 0.4) {
		clay_hammer.setScale(0.75, 0.75 * entity.getInterpolatedData("fiskheroes:blade_timer"), 0.75 * entity.getInterpolatedData("fiskheroes:blade_timer"));  
		clay_hammer.render();    
	}  
		
	if (entity.getData("fiskheroes:blade_timer") > 0.4) {
		clay_mace.setScale(0.75, 0.75 * entity.getInterpolatedData("fiskheroes:blade_timer"), 0.75 * entity.getInterpolatedData("fiskheroes:blade_timer"));   
		clay_mace.render();    
	}  
	
	if (!entity.getData("fiskheroes:speeding") && entity.getInterpolatedData("fan:dyn/giant_timer") > 0.4){
		clay_head.render(); 
		clay_body.render();
		clay_leftLeg.render();
		clay_rightLeg.render();
		clay_leftArm.render();
		clay_rightArm.render();
		clay_hand.render(); 
		
    }
	
	if (entity.getData("fiskheroes:speeding")){
		clay_ball.render();
    }
      ears.render()
	}
}