// priority: -21
console.info("[SOCIETY-S-COBBLEMON] cobblemonGymUtils.js loaded");

const trainerBuckets = new Map([
  [
    10,
    [
      "bug_catcher_anthony_0213",
      "gambler_0107",
      "friendly_maniac_0006",
      "expert_lancere_0024",
      "bug_catcher_rick_0066",
      "bug_catcher_greg_006a",
      "bug_catcher_doug_0067",
      "biker_0212",
      "bug_catcher_charlie_0214",
      "super_nerd_jovan_00a9",
      "lass_janice_0074",
      "gentleman_komodo_0005",
      "lass_iris_0079",
      "camper_liam_008e",
      "bug_catcher_james_006b",
      "bug_catcher_kent_006c",
      "bug_catcher_robby_006d",
      "bug_catcher_sammy_0068",
      "youngster_ben_0059",
    ],
  ],
  [
    15,
    [
      "bug_catcher_anthony_0213",
      "gambler_0107",
      "friendly_maniac_0006",
      "expert_lancere_0024",
      "bug_catcher_rick_0066",
      "bug_catcher_greg_006a",
      "bug_catcher_doug_0067",
      "biker_0212",
      "bug_catcher_charlie_0214",
      "bug_catcher_colton_0069",
      "super_nerd_jovan_00a9",
      "gentleman_komodo_0005",
      "lass_iris_0079",
      "bug_catcher_greg_006a",
      "bug_catcher_james_006b",
      "bug_catcher_kent_006c",
      "bug_catcher_robby_006d",
      "bug_catcher_sammy_0068",
    ],
  ],
  [
    20,
    [
      "bug_catcher_cale_006e",
      "swimmerm_luis_00ea",
      "youngster_josh_005b",
      "youngster_josh_005b",
      "bug_catcher_conner_0073",
      "rocker_randall_011c",
      "picnicker_kelsey_0099",
      "picnicker_diana_0096",
      "lass_reli_007a",
      "hiker_wayne_00b8",
      "hiker_franklin_01fe",
      "gentleman_walter_01a9",
      "hiker_marcos_00b5",
      "hiker_nob_00b7",
      "lass_ali_007b",
      "camper_flint_01d7",
      "camper_ethan_0090",
      "camper_jeff_0092",
      "channeler_01c6",
      "channeler_01c8",
      "channeler_01c8",
      "channeler_01cc",
    ],
  ],
  [
    25,
    [
      "lass_megan_0288",
      "lass_reli_01f5",
      "picnicker_isabelle_0098",
      "picnicker_nancy_0097",
      "engineer_braxton_00dd",
      "youngster_joey_005d",
      "bug_catcher_colton_02d9",
      "camper_chris_027d",
      "picnicker_kelsey_026d",
      "picnicker_isabelle_0276",
      "picnicker_hannah_00a1",
      "picnicker_alicia_0280",
      "lass_janice_01fbl",
      "hiker_franklin_01fe",
      "gentleman_thomas_01a5",
      "camper_jeff_0273",
      "camper_jeff_0274",
      "channeler_01c7",
      "channeler_01ca",
      "rocket_admin_archer_002e",
    ],
  ],
  [
    30,
    [
      "camper_drew_0095",
      "lass_reli_01f6",
      "picnicker_alicia_009a",
      "bird_keeper_robert_0290",
      "engineer_bernie_027b",
      "engineer_bernie_00de",
      "engineer_baily_00dc",
      "fisherman_elliot_028d",
      "fisherman_dale_00df",
      "fisherman_barny_00e0",
      "bug_catcher_colton_02da",
      "camper_chris_0094",
      "camper_chris_027e",
      "super_nerd_glenn_028a",
      "pokemaniac_herman_0285",
      "picnicker_susie_0292",
      "picnicker_kelsey_026e",
      "picnicker_isabelle_0277",
      "picnicker_heidi_009c",
      "picnicker_alicia_0281",
      "lass_janice_01fc",
      "hiker_jeremy_01d1",
      "hiker_jeremy_0283",
      "gentleman_arthur_01a6",
      "gentleman_lamar_01e3",
      "gentleman_tucker_01a7",
      "burglar_00d4",
      "channeler_hope_01bb",
      "channeler_jody_01be",
      "channeler_karina_01c1",
      "sailor_duncan_0089",
      "sailor_dwayne_008d",
    ],
  ],
  [
    35,
    [
      "pokemaniac_hector_0249",
      "bird_keeper_jacob_02a8",
      "bird_keeper_benny_0296",
      "fisherman_ned_00e1",
      "fisherman_hank_00e3",
      "beauty_lauren_0113",
      "beauty_lori_010b",
      "beauty_tamia_010a",
      "super_nerd_leslie_00ad",
      "super_nerd_glenn_00ac",
      "super_nerd_aidan_00ab",
      "rocker_luca_028e",
      "burglar_00d3",
      "burglar_00d9",
      "channeler_emilia_01c4",
      "channeler_carly_01ba",
      "channeler_janae_01c2",
      "channeler_jennifer_01c5",
      "channeler_patricia_01b9",
      "channeler_tammy_01bf",
      "pokemaniac_mark_0252",
    ],
  ],
  [
    40,
    [
      "fisherman_wade_02ae",
      "fisherman_elliot_00e4",
      "fisherman_chip_00e2",
      "punk_girl_providence1",
      "ace_trainer_yuji_0190",
      "ace_trainer_mary_0192",
      "ace_trainer_samuel_0188",
      "ace_trainer_warren_0191",
      "beauty_bridget_0109",
      "beauty_grace_0111",
      "bird_keeper_reed_0137",
      "tamer_jason_0128",
      "lass_megan_0082",
      "lass_lisa_0085",
      "channeler_laurel_01bd",
      "channeler_paula_01bc",
      "channeler_angelica_01c3",
    ],
  ],
  [
    45,
    [
      "tamer_cole_0129",
      "tamer_john_012b",
      "black_belt_atsushi_0142",
      "juggler_mason_02cf",
      "ace_trainer_shannon_0195",
      "ace_trainer_julie_0199",
      "ace_trainer_gilbert_018d",
      "ace_trainer_austina_0198",
      "ace_trainer_berke_018f",
      "tuber_amira_02bd",
      "ace_trainer_brooke_0197",
      "ace_trainer_owen_018e",
      "ace_trainer_paul_018b",
      "swimmerf_melissa_02de",
      "lass_joana_0269",
      "gentleman_norton_01a8",
      "ruin_mamoac_brandon_025a",
      "ruin_mamoac_foster_0246",
      "painter_daisy_020e",
      "painter_rayna_0233",
    ],
  ],
  [
    50,
    [
      "lass_megan_0289",
      "black_belt_hitoshi_0141",
      "black_belt_hideki_013f",
      "bird_keeper_benny_0297",
      "fisherman_tylor_023d",
      "beauty_sheila_028f",
      "fisherman_tylor_023d",
      "bird_keeper_jacob_02a9",
      "bird_keeper_robert_0291",
      "tamer_evan_0251",
      "swimmerm_tony_02dd",
      "swimmerm_toby_0236",
      "swimmerm_samir_0242",
      "swimmerm_jack_02af",
      "swimmerf_denise_0243",
      "swimmerf_nicole_023f",
      "swimmerf_alice_02aa",
      "psychic_preston_011b",
      "picnicker_susie_0293",
      "picnicker_kelsey_026f",
      "picnicker_isabelle_0278",
      "gentleman_clifford_025d",
      "lady_selphy_025e",
      "lady_jacki_020d",
      "lady_gillian_0234",
      "channeler_stacy_01cf",
      "ruin_mamoac_benjamin_025b",
      "ruin_mamoac_larry_0247",
      "ruin_mamoac_lawson_025f",
      "ruin_mamoac_layton_026c",
      "ruin_mamoac_stanly_020c",
      "hiker_daryl_0248",
      "painter_celina_0232",
      "painter_edna_025c",
      "painter_rayna_02bf",
      "psychic_rodette_024b",
      "psychic_laura_0260",
      "psychic_jaclyn_0205",
      "ranger_jackson_0254",
      "camper_riley_026a",
      "camper_jeff_0275",
      "black_belt_hugh_02b9",
    ],
  ],
  [
    55,
    [
      "black_belt_hugh_02ba",
      "ranger_jackson_02d4",
      "psychic_jaclyn_02c8",
      "psychic_rodette_02ce",
      "picnicker_susie_0294",
      "tamer_evan_02d3",
      "black_belt_aaron_0140",
      "juggler_mason_02cf",
      "aroma_lady_rose_0241",
      "aroma_lady_miah_024c",
      "ace_trainer_michelle_0258",
      "ace_trainer_leroy_0257",
      "beauty_grace_029a",
      "biker_billy_00cb",
      "camper_chris_027f",
      "swimmerm_samir_02c9",
      "swimmerm_mymo_023e",
      "swimmerm_matthew_02dc",
      "swimmerf_nicole_02c7",
      "pokemaniac_herman_0286",
      "picnicker_alicia_0282",
      "channeler_amanda_01ce",
      "ruin_mamoac_larry_02cb",
      "swimmerm_darrin_02ab",
      "picnicker_missy_02ac",
      "picnicker_missy_02ad",
      "pokemaniac_mark_0284",
      "crush_girl_tanya_02b5",
      "psychic_tyron_0119",
      "psychic_johan_0118",
    ],
  ],
  [
    60,
    [
      "crush_girl_tanya_02b6",
      "cue_ball_raul_00fc",
      "bird_keeper_benny_0130",
      "bird_keeper_beck_013b",
      "bug_catcher_colton_02db",
      "biker_ernest_00c5",
      "biker_alex_00c6",
      "ace_trainer_leroy_02d6",
      "ace_trainer_michelle_02d7",
      "biker_gerald_00d1",
      "swimmerm_tony_00f0",
      "swimmerm_richard_00eb",
      "swimmerm_reece_00ec",
      "swimmerm_jack_00f6",
      "channeler_tasha_01d0",
      "cue_ball_zeek_00fe",
      "rocket_admin_ariana_0036",
      "rocket_admin_archer_0035",
    ],
  ],
  [
    65,
    [
      "pokemaniac_mark_00a2",
      "swimmerf_alice_0115",
      "swimmerf_melissa_0110",
      "swimmerf_connie_0116",
      "swimmerf_anya_0114",
      "swimmerm_jerome_00f7",
      "swimmerm_matthew_00ed",
      "swimmerm_roland_00f8",
      "swimmerm_spencer_00f5",
      "tamer_edgar_0127",
      "tamer_phil_0126",
      "juggler_nate_0125",
      "juggler_shawn_0121",
      "fisherman_nolan_00e8",
      "fisherman_ronald_00e5",
      "beauty_grace_029a",
      "beauty_olivia_0112",
      "bird_keeper_roger_0136",
      "swimmerf_shirley_0117",
      "swimmerf_tiffany_010e",
      "swimmerm_axle_00f1",
      "swimmerm_barry_00f2",
      "swimmerm_david_00ef",
      "swimmerm_jerome_00f7",
      "picnicker_irene_01d9",
      "juggler_kayden_0124",
      "juggler_kirk_0120",
      "picnicker_irene_01d9",
    ],
  ],
  [
    70,
    [
      "punk_girl_providence2",
      "swimmerm_dean_00f3",
      "swimmerm_douglas_00ee",
      "fisherman_wade_00e7",
      "burglar_arnie_00d8",
      "burglar_lewis_00db",
      "burglar_simon_00da",
      "super_nerd_zac_00b4",
      "bird_keeper_porto",
      "picnicker_missy_01d8",
    ],
  ],
  [
    75,
    [
      "scientist_ted_014f",
      "swimmerf_nora_010f",
      "swimmerm_darrin_00f4",
      "black_belt_blanche",
      "fisherman_claude_00e6",
      "ace_trainer_jexy",
      "fisher_big_bite",
      "ace_trainer_stalwart",
      "black_belt_blanche"
    ],
  ],
  [
    80,
    [
      "trainer_may_003d",
      "super_nerd_tolstoy",
      "ace_trainer_stream",
      "fisher_big_bite",
      "youngster_adam_nant",
      "beauty_darling",
      "team_rocket_grunt_0046",
      "team_rocket_grunt_0047",
      "team_rocket_grunt_0048",
      "team_rocket_grunt_0049",
      "tamer_vincent_012a",
      "rocket_admin_ariana_0044",
      "rocket_admin_archer_0043",
    ],
  ],
  [
    85,
    [
      "ranger_grimes",
      "black_belt_daisuke_0145",
      "friendly_thug_0015",
      "friendly_thug_0016",
      "ace_trainer_caroline_0193",
      "ace_trainer_colby_018a",
      "ace_trainer_george_0189",
      "ace_trainer_alexa_0194",
      "pokemaniac_dawson_00a7",
      "juggler_gregory_0122",
      "fisherman_drizzy",
      "bug_catcher_beabadoobee"
    ],
  ],
  [
    90,
    [
      "bird_keeper_robert_012e",
      "bird_keeper_perry_012d",
      "biker_hideo_00c9",
      "ace_trainer_naomi_0196",
      "ace_trainer_rolando_018c",
      "swimmerf_amara_0222",
      "picnicker_susie_01d3",
      "gentleman_brooks_01e2",
      "rocket_admin_ariana_0029",
      "rocket_admin_archer_002a",
      "sailor_trevor_0087",
      "cue_ball_koji_00f9",
      "cue_ball_luke_00fa",
      "lass_anne_0004",
    ],
  ],
  [
    95,
    [
      "rocker_luca_011d",
      "swimmerf_maria_0223",
      "bird_keeper_jacob_0135",
      "biker_william_00ce",
      "biker_jared_00c3",
      "fisherman_andrew_00e9",
      "beauty_lola_010c",
      "aroma_lady_violet_022e",
      "ace_trainer_samantha_0052",
      "ace_trainer_sharon_00ae",
      "beauty_sheila_010d",
      "lass_ann_007e",
      "crush_girl_tanya_0228",
      "team_rocket_grunt_0164",
      "team_rocket_grunt_0163",
      "ranger_beth_0255",
      "black_belt_hugh_022a",
      "swimmerf_amara_0222",
    ],
  ],
  [
    100,
    [
      "ace_trainer_bowie",
      "swimmerf_maria_0223",
      "swimmerm_garrett_0226",
      "ruin_mamoac_halotosis",
      "bird_keeper_sebastian_012c",
      "fisherman_tommy_0227",
      "tuber_alexis_022f",
      "tuber_amira_0207",
      "aroma_lady_nikki_020b",
      "dragon_tamer_ramiro_0134",
    ],
  ],
  [
    105,
    [
      "ace_trainer_maclachlan",
      "ace_trainer_zephira",
      "beauty_nadine",
      "picnicker_donna",
      "ace_trainer_zephira",
      "fisher_packard",
      "picnicker_claire_022c",
      "swimmerf_tisha_0231",
      "swimmerm_finn_0225",
      "ace_trainer_symes_0071",
      "ruin_mamoac_reslo",
      "bug_catcher_tainy",
      "psychic_margaret",
      "pokefan_unove",
      "super_nerd_leroy"
    ],
  ],
  [
    110,
    [
      "camper_justin_01dd",
      "super_nerd_miguel_00aa",
      "picnicker_caitlin_009b",
      "ace_trainer_andrew_0002",
      "ace_trainer_wilton_0133",
      "punk_girl_providence3",
      "gym_crusher_alexander",
      "gym_crusher_howitzer",
      "channeler_rachel_0093",
      "gym_crusher_foxxy",
      "ace_trainer_janny_00af",
      "ace_trainer_barry_0058"
    ],
  ],
  [
    115,
    [
      "ace_swimmer_torrent",
      "cue_ball_camron_00fb",
      "picnicker_alma_01d2",
      "ace_trainer_runan_007c",
      "ace_trainer_nick_0055",
      "professor_oak_00d2",
      "gym_crusher_salad",
      "gym_rat_king",
      "bird_keeper_glizzy"
    ],
  ],
  [
    120,
    [
      "gym_crusher_cruelty",
      "reggie",
      "professor_oak_00c8",
      "gym_crusher_dennis",
      "raging_red",
      "raging_blue",
      "punk_girl_providence4",
      "gym_crusher_twine",
    ],
  ],
  [
    125,
    [
      "gym_obliterator_blaze",
      "bird_legend_arctic",
      "bird_legend_zap",
      "bird_legend_melt",
      "gym_obliterator_warp",
      "gym_obliterator_cofh",
      "gym_obliterator_artemis",
    ],
  ],
  [
    130,
    [
      "gym_obliterator_warlord",
      "gym_obliterator_origin",
      "gym_obliterator_tao",
      "gym_obliterator_primal"
    ],
  ],
  [
    135,
    [
      "dimensional_entity_0",
      "dimensional_entity_1", 
      "dimensional_entity_2",
      "dimensional_entity_3", 
      "dimensional_entity_4", 
      "dimensional_entity_5",
      "dimensional_entity_6", 
      "dimensional_entity_7", 
      "dimensional_entity_8", 
      "dimensional_entity_9", 
      "dimensional_entity_10"
    ],
  ],
]);

global.getRandomTrainer = (levelBucket) => {
  const trainerBucket = trainerBuckets.get(levelBucket);
  // const trainer = trainerBucket[rnd(0, trainerBucket.length)];
  // console.log("Spawning trainer " + trainer + " in bucket " + levelBucket);
  return trainerBucket[rnd(0, trainerBucket.length - 1)];
};

global.getPlayerPodiumLevelTier = (player, partyLevel) => Math.max(10, (Math.round(partyLevel / 5) * 5 + (Math.floor(player.persistentData.winStreak / 10) * 5)) - 5);

const leagueBosses = ["leon", "aiden", "ace", "caroline", "haruna", "maria", "karma", "king", "kingkarma"];
const tier9Bosses = ["leon", "aiden", "ace", "caroline", "haruna", "maria"];

global.getLeagueBoss = (levelBucket) => {
  let bossNumber = Math.max(1, Math.floor(levelBucket / 10) - 2);
  if (levelBucket == 105) bossNumber = 8;
  if (bossNumber >= 9) return `league_${tier9Bosses[rnd(0, tier9Bosses.length - 1)]}${bossNumber}`;
  return `league_${leagueBosses[rnd(0, leagueBosses.length - 1)]}${bossNumber}`;
};

const getWinStreakBucket = (winStreak) => {
  let buckets = [500, 400, 300, 160, 80, 40, 20];
  for (let i = 0; i < buckets.length; i++) {
    if (winStreak >= buckets[i]) {
      return buckets[i];
    }
  }
  return -1;
}
global.getStreakRewards = (player, pos, winStreak) => {
  let rewardTier = getWinStreakBucket(winStreak)
  if (rewardTier == -1) return;
  let reward;
  let rolledLoot = Utils.rollChestLoot(`sunlit_cobblemon:trainer_podium_streak/${rewardTier}_plus_common`).toArray();
  rolledLoot.forEach((item) => {
    reward = player.level.createEntity("minecraft:item");
    reward.x = pos.x + 0.5;
    reward.y = pos.y + 0.4;
    reward.z = pos.z + 0.5;
    reward.item = item;
    reward.spawn();
  });
  if (Math.random() <= 0.15) {
    player.tell("rolled_rare")
    rolledLoot = Utils.rollChestLoot(`sunlit_cobblemon:trainer_podium_streak/${rewardTier}_plus_rare`).toArray();
    rolledLoot.forEach((item) => {
      reward = player.level.createEntity("minecraft:item");
      reward.x = pos.x + 0.5;
      reward.y = pos.y + 0.4;
      reward.z = pos.z + 0.5;
      reward.item = item;
      reward.spawn();
    });
    if (winStreak >= 640 && player.stages.has("husbandry_mastery") && Math.random() < 0.05) {
      reward = player.level.createEntity("minecraft:item");
      reward.x = pos.x + 0.5;
      reward.y = pos.y + 0.4;
      reward.z = pos.z + 0.5;
      reward.item = "society:animal_cracker";
      reward.spawn();
    }
  }
};
