var landing = implement("fiskheroes:external/superhero_landing");

function init(hero) {
    hero.setName("Odin");
    hero.setAliases("Allfather", "Madman", "Raven _God");
    hero.setVersion("\u00A7b\u00A7lGOW \u00A7f\u00A7lRagnarok\u00A7r");
    hero.setTier(10);
    
    hero.setHelmet("Head");
    hero.setChestplate("Body");
    hero.setLeggings("Legs");
    hero.setBoots("Boots");
    
    hero.addPowers("gowhp:allfather", "gowhp:gungnir");
    hero.addAttribute("PUNCH_DAMAGE", 10.0, 0);
    hero.addAttribute("KNOCKBACK", 1.1, 0);
    hero.addAttribute("JUMP_HEIGHT", 1.05, 0);
    hero.addAttribute("SPRINT_SPEED", 0.35, 1);
    hero.addAttribute("FALL_RESISTANCE", 0.7, 1);
    
    hero.addKeyBind("GUNGNIR", "Gungnir", 1);
    hero.addKeyBind("GUNGNIRBLADE", "Gungnir Blade", 1);
    hero.addKeyBind("SPELL_MENU", "Dark Magic", 2);
    hero.addKeyBind("SHIELD", "Elemental ForceField", 3);
    hero.addKeyBindFunc("GUNGNIR_RUNIC_CYCLE", gungnirrunicCycle, "Cycle Magical/Runic Attacks", 4);
    hero.addKeyBind("GUNGNIRRUNICCYCLEUNAVAILABLE", "\u00A7cUnavailable", 4);
    hero.addKeyBind("CHARGED_BEAM", "Glettings Bara", 5);
    hero.addKeyBind("THELEIPTRAANIMATION", "Leiptra", 5);
    hero.addKeyBind("THEBLASTOFFIREANIMATION", "Book of Eldr", 5);
    hero.addKeyBind("THEGLETTINGSBARAANIMATION", "Glettings Bara", 5);
    hero.addKeyBind("THEGLETTINGSBARAUNAVAILABLE", "\u00A7cUnavailable", 5);
    hero.addKeyBind("GROUND_SMASH", "Ground Smash",5);
    
    hero.setDefaultScale(1.0);
    hero.setModifierEnabled(isModifierEnabled);
    hero.setKeyBindEnabled(isKeyBindEnabled);
    hero.addAttributeProfile("GUNGNIR", gungnirProfile);
    hero.addAttributeProfile("GUNGNIRBLADF", gungnirbladeProfile);
    hero.addAttributeProfile("RUNICATTACK", runicattackProfile);
    hero.setAttributeProfile(getAttributeProfile);
    hero.setHasProperty(hasProperty);

    hero.setDamageProfile(entity => entity.getData("gowhp:dyn/gungnir") ? "GUNGNIR_BIFROST" : null);
    hero.setDamageProfile(entity => entity.getData("gowhp:dyn/gungnirblade") ? "GUNGNIR_BLADE" : null);
    hero.addDamageProfile("GUNGNIR_BIFROST", {
        "types": {
            "BLUNT": 1.0,
            "BIFROST": 0.6,
            "ENERGY": 0.25,
            "MAGIC": 0.25,
            "COSMIC": 0.1
        }
    });
    hero.addDamageProfile("GUNGNIR_BLADE", {
        "types": {
            "SHARP": 1.0,
            "BIFROST": 0.6,
            "ENERGY": 0.25,
            "MAGIC": 0.25,
            "COSMIC": 0.1
        }
    });

    hero.addSoundEvent("MASK_OPEN", "gowhp:odin_voice_lines");
    hero.addSoundEvent("MASK_CLOSE", "gowhp:odin_voice_lines");

    hero.setTickHandler((entity, manager) => {
        var flying = entity.getData("fiskheroes:flying");
        manager.incrementData(entity, "fiskheroes:dyn/booster_timer", 2, flying);

        var item = entity.getHeldItem();
        flying &= !entity.as("PLAYER").isUsingItem();
        manager.incrementData(entity, "fiskheroes:dyn/booster_r_timer", 2, flying && item.isEmpty() && !entity.isPunching() && entity.getData("fiskheroes:aiming_timer") == 0);
        manager.incrementData(entity, "fiskheroes:dyn/booster_l_timer", 2, flying && !item.doesNeedTwoHands());

        landing.tick(entity, manager);
    });
}

function hasProperty(entity, property) {
    return property == "BREATHE_SPACE" || property == "MASK_TOGGLE";
}

function gungnirrunicCycle(player, manager) {
  manager.setData(player, "gowhp:dyn/gungnir_runic_cycle", player.getData("gowhp:dyn/gungnir_runic_cycle") + 1);
  if (player.getData("gowhp:dyn/gungnir_runic_cycle") > 2) {
    manager.setData(player, "gowhp:dyn/gungnir_runic_cycle", 0);
  }
  return true;
}

function gungnirProfile(profile) {
    profile.inheritDefaults();
    profile.addAttribute("PUNCH_DAMAGE", 12.5, 0);
    profile.addAttribute("SPRINT_SPEED", 0.35, 1);
    profile.addAttribute("JUMP_HEIGHT", 1.05, 0);
}

function gungnirbladeProfile(profile) {
    profile.inheritDefaults();
    profile.addAttribute("PUNCH_DAMAGE", 15.0, 0);
    profile.addAttribute("SPRINT_SPEED", 0.35, 1);
    profile.addAttribute("JUMP_HEIGHT", 1.05, 0);
}

function shieldProfile(profile) {
    profile.inheritDefaults();
    profile.addAttribute("BASE_SPEED", -0.5, 1);
    profile.addAttribute("JUMP_HEIGHT", -1.0, 0);
}

function runicattackProfile(profile) {
    profile.revokeAugments();
    profile.addAttribute("BASE_SPEED", -1, 0);
    profile.addAttribute("JUMP_HEIGHT", -10, 0);
}

function getAttributeProfile(entity) {
    if (entity.getData("fiskheroes:beam_charging") && (entity.getData("gowhp:dyn/gungnir_runic_cycle") == 1 || entity.getData("gowhp:dyn/gungnir_runic_cycle") == 2)) {
        return "RUNICATTACK";
    }
    if (entity.getData("fiskheroes:shield_blocking")) {
        return "SHIELD";
    }
    if (entity.getData("gowhp:dyn/gungnir")) {
        return "GUNGNIR";
    }
    if (entity.getData("gowhp:dyn/gungnirblade")) {
        return "GUNGNIRBLADE";
    }
}

function isModifierEnabled(entity, modifier) {
    switch (modifier.name()) {
    case "fiskheroes:controlled_flight":
        return entity.getData("gowhp:dyn/gungnir") && !(entity.getData("fiskheroes:beam_charging") && entity.getData("gowhp:dyn/gungnir_runic_cycle") == 2);
    case "fiskheroes:shield":
        return entity.getData("gowhp:dyn/gungnir") && !entity.getData("fiskheroes:beam_charging");
    case "fiskheroes:arrow_catching":
        return !entity.getData("gowhp:dyn/gungnirblade");
    case "fiskheroes:charged_beam":
      switch (modifier.id()) {
        case "leiptra":
          return entity.getData("gowhp:dyn/gungnir") && entity.getData("gowhp:dyn/gungnir_runic_cycle") == 0;
        case "eldr":
          return entity.getData("gowhp:dyn/gungnir") && entity.getData("gowhp:dyn/gungnir_runic_cycle") == 1;
        case "glettingsbara":
          return entity.getData("gowhp:dyn/gungnir") && entity.getData("gowhp:dyn/gungnirblade") && !entity.getData("fiskheroes:flying") && entity.getData("gowhp:dyn/gungnir_runic_cycle") == 2;
      };
    case "fiskheroes:ground_smash":
        return entity.getData("gowhp:dyn/gungnir") && !entity.getData("gowhp:dyn/gungnirblade") && entity.getData("gowhp:dyn/gungnir_runic_cycle") == 2;
    default:
        return true;
    }
}

function isKeyBindEnabled(entity, keyBind) {
    switch (keyBind) {
        case "GUNGNIR":
            return (!entity.getData("gowhp:dyn/gungnir") && entity.getHeldItem().isEmpty()) || (!entity.getData("gowhp:dyn/gungnirblade") && entity.getData("gowhp:dyn/gungnir") && entity.isSneaking());
        case "GUNGNIRBLADE":
            return (entity.getInterpolatedData("gowhp:dyn/gungnir_timer") == 1&& !entity.isSneaking()) || (entity.getInterpolatedData("gowhp:dyn/gungnirblade_timer") == 1 && !entity.isSneaking());
        case "SHIELD":
            return entity.getData("gowhp:dyn/gungnir") && !entity.getData("fiskheroes:beam_charging");
        case "GUNGNIR_RUNIC_CYCLE":
            return entity.getData("gowhp:dyn/gungnir") && !entity.getData("fiskheroes:shield") && !((entity.getData("gowhp:dyn/gungnir_runic_cycle") == 0 || entity.getData("gowhp:dyn/gungnir_runic_cycle") == 1 || entity.getData("gowhp:dyn/gungnir_runic_cycle") == 2) && entity.getData("fiskheroes:beam_charging"));
        case "GUNGNIRRUNICCYCLEUNAVAILABLE":
            return ((entity.getData("gowhp:dyn/gungnir_runic_cycle") == 0 || entity.getData("gowhp:dyn/gungnir_runic_cycle") == 1 || entity.getData("gowhp:dyn/gungnir_runic_cycle") == 2) && entity.getData("fiskheroes:beam_charging"));
        case "CHARGED_BEAM":
            return entity.getData("gowhp:dyn/gungnir");
        case "THELEIPTRAANIMATION":
            return entity.getData("gowhp:dyn/gungnir") && !entity.getData("fiskheroes:shield") && entity.getData("gowhp:dyn/gungnir_runic_cycle") == 0;
        case "THEBLASTOFFIREANIMATION":
            return entity.getData("gowhp:dyn/gungnir") && !entity.getData("fiskheroes:shield") && entity.getData("gowhp:dyn/gungnir_runic_cycle") == 1;
        case "THEGLETTINGSBARAANIMATION":
            return entity.getData("gowhp:dyn/gungnir") && entity.getData("gowhp:dyn/gungnirblade") && !entity.getData("fiskheroes:shield") && !entity.getData("fiskheroes:flying") && entity.getData("gowhp:dyn/gungnir_runic_cycle") == 2;
        case "THEGLETTINGSBARAUNAVAILABLE":
            return entity.getData("gowhp:dyn/gungnir") && entity.getData("gowhp:dyn/gungnirblade") && !entity.getData("fiskheroes:shield") && entity.getData("fiskheroes:flying") && entity.getData("gowhp:dyn/gungnir_runic_cycle") == 2;
        case "GROUND_SMASH":
            return entity.getData("gowhp:dyn/gungnir") && !entity.getData("gowhp:dyn/gungnirblade") && !entity.getData("fiskheroes:shield") && entity.getData("gowhp:dyn/gungnir_runic_cycle") == 2;
        default:
            return true;
    }
}