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

function init(hero) {
    hero.setName("Thor");
    hero.setAliases("God_of_Thunder", "Butcher", "Drunk_Warrior");
    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:godofthunder", "gowhp:mjolnir");
    hero.addAttribute("PUNCH_DAMAGE", 11.5, 0);
    hero.addAttribute("KNOCKBACK", 1.1, 0);
    hero.addAttribute("JUMP_HEIGHT", 1.15, 0);
    hero.addAttribute("SPRINT_SPEED", 0.3, 1);
    hero.addAttribute("FALL_RESISTANCE", 140.0, 0);
    
    hero.addKeyBind("MJOLNIR", "Mjolnir", 2);
    hero.addKeyBind("SHIELD", "Block", 3);
    hero.addKeyBindFunc("THOR_ABILITY_CYCLE", thorabilityCycle, "Cycle Abilities", 4);
    hero.addKeyBindFunc("MJOLNIR_RUNIC_CYCLE", mjolnirrunicCycle, "Cycle Ranged/Runic Attacks", 4);
    hero.addKeyBind("MJOLNIRRUNICCYCLEUNAVAILABLE", "\u00A7cUnavailable", 4);
    hero.addKeyBind("CHARGED_BEAM", "Throw", 5);
    hero.addKeyBind("MJOLNIRTHROWANIMATION", "Throw Mjolnir", 5);
    hero.addKeyBind("THETHUNDERBLASTANIMATION", "Thunder Blast", 5);
    hero.addKeyBind("THEMJOLNIRSLAMANIMATION", "Mjolnir Slam", 5);
    hero.addKeyBind("THEMJOLNIRSLAMUNAVAILABLE", "\u00A7cUnavailable", 5);
    hero.addKeyBind("THUNDERCLAPANIMATION", "Thunder Clap", 5);
    hero.addKeyBind("GROUND_SMASH", "Ground Smash", 5);
    
    hero.setDefaultScale(1.35);
    hero.setModifierEnabled(isModifierEnabled);
    hero.setKeyBindEnabled(isKeyBindEnabled);
    hero.addAttributeProfile("MJOLNIR", mjolnirProfile);
    hero.addAttributeProfile("BLOCK", blockProfile);
    hero.addAttributeProfile("RUNICATTACK", runicattackProfile);
    hero.setAttributeProfile(getAttributeProfile);
    hero.setHasProperty(hasProperty);

    hero.setDamageProfile(entity => entity.getData("gowhp:dyn/mjolnir") ? "MJOLNIR_LIGHTNING" : null);
    hero.addDamageProfile("MJOLNIR_LIGHTNING", {
        "types": {
            "BLUNT": 1.0,
            "LIGHTNING": 0.6,
            "ASGARD_MAGIC": 0.25
        }
    });

    hero.addSoundEvent("MASK_OPEN", "gowhp:thor_voice_lines");
    hero.addSoundEvent("MASK_CLOSE", "gowhp:thor_voice_lines");
    hero.addSoundEvent("PUNCH", ["gowhp:thor_punch", "gowhp:mjolnir_hit"]);
    hero.addSoundEvent("LAND", "gowhp:mjolnirslam");

    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 thorabilityCycle(player, manager) {
  manager.setData(player, "gowhp:dyn/thor_ability_cycle", player.getData("gowhp:dyn/thor_ability_cycle") + 1);
  if (player.getData("gowhp:dyn/thor_ability_cycle") > 1) {
    manager.setData(player, "gowhp:dyn/thor_ability_cycle", 0);
  }
  return true;
}

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

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

function blockProfile(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("gowhp:dyn/mjolnir") && entity.getData("gowhp:dyn/thor_ability_cycle") == 0) || (entity.getData("gowhp:dyn/mjolnir") && (entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 1 || entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 2))) && entity.getData("fiskheroes:beam_charging")) {
        return "RUNICATTACK";
    }
    if (entity.getData("fiskheroes:shield_blocking")) {
        return "BLOCK";
    }
    if (entity.getData("gowhp:dyn/mjolnir")) {
        return "MJOLNIR";
    }
}

function isModifierEnabled(entity, modifier) {
    switch (modifier.name()) {
    case "fiskheroes:controlled_flight":
        return (entity.getData("gowhp:dyn/mjolnir")) && !(entity.getData("fiskheroes:beam_charging") && entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 2);
    case "fiskheroes:shield":
        return entity.getData("gowhp:dyn/mjolnir") && !entity.getData("fiskheroes:beam_charging");
    case "fiskheroes:arrow_catching":
        return !entity.getData("gowhp:dyn/mjolnir") && !entity.getData("fiskheroes:shield");
    case "fiskheroes:leaping":
        return !entity.getData("gowhp:dyn/mjolnir");
    case "fiskheroes:lightning_cast":
        return entity.getData("gowhp:dyn/mjolnir") && !entity.getData("fiskheroes:beam_charging");
    case "fiskheroes:charged_beam":
      switch (modifier.id()) {
        case "mjolnirthrow":
          return entity.getData("gowhp:dyn/mjolnir") && entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 0;
        case "thunderblast":
          return entity.getData("gowhp:dyn/mjolnir") && entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 1;
        case "mjolnirslam":
          return entity.getData("gowhp:dyn/mjolnir") && !entity.getData("fiskheroes:flying") && entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 2;
        case "thunderclap":
          return !entity.getData("gowhp:dyn/mjolnir") && entity.getData("gowhp:dyn/thor_ability_cycle") == 0;
      };
    case "fiskheroes:ground_smash":
        return !entity.getData("gowhp:dyn/mjolnir") && entity.getData("gowhp:dyn/thor_ability_cycle") == 1;
    default:
        return true;
    }
}

function isKeyBindEnabled(entity, keyBind) {
    switch (keyBind) {
        case "MJOLNIR":
            return entity.getHeldItem().isEmpty() && !entity.getData("fiskheroes:shield_blocking");
        case "SHIELD":
            return entity.getData("gowhp:dyn/mjolnir") && !entity.getData("fiskheroes:beam_charging");
        case "THOR_ABILITY_CYCLE":
            return !entity.getData("gowhp:dyn/mjolnir");
        case "MJOLNIR_RUNIC_CYCLE":
            return entity.getData("gowhp:dyn/mjolnir") && !entity.getData("fiskheroes:shield") && !((entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 0 || entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 1 || entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 2) && entity.getData("fiskheroes:beam_charging"));
        case "MJOLNIRRUNICCYCLEUNAVAILABLE":
            return ((entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 0 || entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 1 || entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 2) && entity.getData("fiskheroes:beam_charging"));
        case "CHARGED_BEAM":
            return !entity.getData("fiskheroes:shield");
        case "MJOLNIRTHROWANIMATION":
            return entity.getData("gowhp:dyn/mjolnir") && !entity.getData("fiskheroes:shield") && entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 0 && entity.getData("fiskheroes:beam_charging");
        case "THETHUNDERBLASTANIMATION":
            return entity.getData("gowhp:dyn/mjolnir") && !entity.getData("fiskheroes:shield") && entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 1;
        case "THEMJOLNIRSLAMANIMATION":
            return entity.getData("gowhp:dyn/mjolnir") && !entity.getData("fiskheroes:shield") && !entity.getData("fiskheroes:flying") && entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 2;
        case "THEMJOLNIRSLAMUNAVAILABLE":
            return entity.getData("gowhp:dyn/mjolnir") && !entity.getData("fiskheroes:shield") && entity.getData("fiskheroes:flying") && entity.getData("gowhp:dyn/mjolnir_runic_cycle") == 2;
        case "THUNDERCLAPANIMATION":
            return !entity.getData("gowhp:dyn/mjolnir") && !entity.getData("fiskheroes:shield") && entity.getData("gowhp:dyn/thor_ability_cycle") == 0;
        case "GROUND_SMASH":
            return !entity.getData("gowhp:dyn/mjolnir") && !entity.getData("fiskheroes:shield") && entity.getData("gowhp:dyn/thor_ability_cycle") == 1;
        default:
            return true;
    }
}