import { NoveltyManager, AccessoriesSlot } from "./NoveltyManager.js"; 
import { system, world } from "@minecraft/server";

NoveltyManager.registerAccessoriesItem

const hatItems = [
                 "just:special",
                //common
                "just:acorn_hat",
                "just:alien_antennae_hat",
                "just:astronaut_helmet",
                "just:bandana",
                "just:backwards_cap",
                "just:balaclava",
                "just:baseball_hat",
                "just:beanie",
                "just:bicorne",
                "just:big_brain",
                "just:binky",
                "just:bowler",
                "just:brown_brick",
                "just:bucket_cap",
                "just:burger_cap",
                "just:burger_hat",
                "just:caddy_cap",
                "just:cardboard_box",
                "just:cartoon_egg",
                "just:chalk_stick",
                "just:cheese_slice",
                "just:chefs_hat",
                "just:chicken_head",
                "just:cowboy",
                "just:crown",
                "just:cyclops_eye",
                "just:dairy_queen_lips",
                "just:elf_hat",
                "just:explorers_hat",
                "just:fez",
                "just:fro",
                "just:front_thorns",
                "just:gnome",
                "just:grandma_disguise",
                "just:halo",
                "just:headshot",
                "just:horse_mask",
                "just:large_horns",
                "just:lil_bow",
                "just:mohawk",
                "just:neck_gaiter",
                "just:neko_ears",
                "just:party_hat",
                "just:paper_bag",
                "just:pig_head",
                "just:pog",
                "just:police_bucket",
                "just:pom_moog",
                "just:poofball_hat",
                "just:pope_hat",
                "just:ranahat",
                "just:right_hand_hat",
                "just:right_hand_hat_cyborg",
                "just:round_wig",
                "just:round_wig_glasses",

                "just:rubber_nipple",
                "just:sausage",
                "just:shiesty",
                "just:sheep_hat",
                "just:sombrero",
                "just:sprout",
                "just:stuck_lollipop",
                "just:summer_hat",
                "just:swimmer",
                "just:tick_on_head",
                "just:toast_hat",
                "just:top_hat",
                "just:tricorne",
                "just:udder_hat",
                "just:ushanka",
                "just:villager_nose",
                "just:worm_hat",
                //uncommon
                "just:angry_mask",
                "just:antlers",
                "just:apple_hat",
                "just:beetle_on_head",
                "just:beret_ribbon",
                "just:big_crown",
                "just:big_ribbon",
                "just:big_steve_head",
                "just:bread_hat",
                "just:camera_beard",
                "just:carrot_on_stick",
                "just:cat_hat",
                "just:chicken_on_head",
                "just:chi_ears",
                "just:cuphead",
                "just:dark_eye_of_the_holder",
                "just:dimma",
                "just:dipper",
                "just:doctor_hat",
                "just:dragon_hat",
                "just:dragon_skull",
                "just:drinking_hat",
                "just:druid_antlers",
                "just:dum_hat",
                "just:dwarf_miner_beard",
                "just:dwarf_beard",
                "just:easter_egg",
                "just:eevee_ears",
                "just:evil_eye_of_the_holder",
                "just:finn_hat",
                "just:flies",
                "just:frozen_head",
                "just:full_iron_helm",
                "just:full_gold_helm",
                "just:full_diamond_helm",
                "just:full_steel_helm",
                "just:gnome_clover_wig",
                "just:gnome_clover_wig_glasses",
                "just:greaser",
                "just:green_stache",
                "just:ice_dragon_skull",
                "just:jester_hat",
                "just:ladybug_on_head",
                "just:long_fox_ears",
                "just:miner_hat",
                "just:mango_hat",
                "just:mango_on_head",
                "just:megaman",
                "just:mimic_head",
                "just:monkey_king",
                "just:more_eyes",
                "just:orange_hat",
                "just:pika_ears",
                "just:penguin_hat",
                "just:questbook",
                "just:red_shroom_cap",
                "just:red_stache",
                "just:shake_hat",
                "just:shrek_ears",
                "just:shroom_cap",
                "just:side_bow",
                "just:springer",
                "just:spy_zombie",
                "just:strawberry_hat",
                "just:tanuki_leaf",
                "just:tinker_hat",
                "just:top_top_hat_hat",
                "just:toy_story_alien",
                "just:unicorn_horn",
                "just:worms_mine",
                //rare
                "just:aegis_hat",
                "just:alien_phil_hat",
                "just:azumanga_hat",
                "just:baby_crewmate",
                "just:baby_penguin",
                "just:bee_on_head",
                "just:cake_hat",
                "just:camera",
                "just:candle_on_head",
                "just:cat_on_head",
                "just:caterpillar_on_head",
                "just:chocolate_sauced",
                "just:clockface",
                "just:dejiko",
                "just:demon_eyes",
                "just:demon_horns",
                "just:digger",
                "just:dragon_skull_ender",
                "just:duck_hat",
                "just:fake_fire",
                "just:fish_on_head",
                "just:floating_hearts",
                "just:floating_star",
                "just:flower_crown",
                "just:flower_on_head",
                "just:fox_on_head",
                "just:grey_fox_hat",
                "just:hat_of_discipline",
                "just:holy_head",
                "just:host_hat",
                "just:lil_bows",
                "just:magikarp",
                "just:medusa",
                "just:milady_doll",
                "just:mimic_head_dark",
                "just:mindflayer",
                "just:paypay",
                "just:peppino",
                "just:police_siren",
                "just:propel_hat",
                "just:puchiko",
                "just:rabbit_on_head",
                "just:rabi_en_rose",
                "just:rabi_en_rose_wig",
                "just:sims_gem",
                "just:slime_head",
                "just:smoking_pipe",
                "just:snowman_baby",
                "just:sonic_hood",
                "just:spade_soldier",
                "just:stress",
                "just:super_sand_hat",
                "just:the_noise",
                "just:toilet_hat",
                "just:top_cat_hat",
                "just:traffic_cone",
                "just:viking_hat_beard",
                "just:viking_hat",
                "just:warm_eye_of_the_holder",
                "just:wing_hat",
                "just:wizard_hat",
                 //epic
                "just:angel_and_devil_hat",
                "just:amalgalich_hat",
                "just:artsy_mask",
                "just:axolotl_on_head",
                "just:baby_turtle",
                "just:baby_dolphin",
                "just:baseball_hat_rgb",
                "just:beanie_rgb",
                "just:bee_hat",
                "just:bee_hat_pride",
                "just:bee_hat_trans",
                "just:big_ribbon_rgb",
                "just:bowler_rgb",
                "just:brick_head",
                "just:burning_bison",
                "just:cowboy_rgb",
                "just:crab_on_head",
                "just:crystal_horns",
                "just:disco_ball",
                "just:dragon_skull_rgb",
                "just:drinking_hat_rgb",
                "just:druid_antlers_rare",
                "just:easter_head_rgb",
                "just:eye_of_the_bee_holder",
                "just:fake_blight",
                "just:fishing_hat",
                "just:full_helm_rgb",
                "just:green_birb",
                "just:headphones_blue",
                "just:headphones_red",
                "just:headphones_reggae",
                "just:headphones_pink",
                "just:just_ducke",
                "just:kirby_mouthful",
                "just:lil_termagant",
                "just:mimic_head_gold",
                "just:mindflayer_alhoon",
                "just:octodad",
                "just:nyan_doll",
                "just:party_hat_rgb",
                "just:peepo",
                "just:player_jockey",
                "just:pohatoe",
                "just:poofball_hat_rgb",
                "just:potions_head",
                "just:rainbow_orbiters",
                "just:rain_cloud",
                "just:shiny_magikarp",
                "just:small_bowler_rgb",
                "just:slime_cube_dnd",
                "just:stinky_cheese_man",
                "just:the_noisier",
                "just:thumb_nail",
                "just:tomato_splats",
                "just:top_top_hat_hat_rgb",
                "just:tv_head",
                "just:tv_head_rat",
                "just:tv_head_static",
                "just:ushanka_rgb",
                "just:xanath_eye_of_the_holder",
                //easter
                "just:baseball_easter",
                "just:beanie_easter",
                "just:bunny_hat",
                "just:easter_bunny_head",
                "just:egg_on_head",
                "just:egg_cracked",
                "just:easter_eggs",
                "just:egghead_spots",
                "just:egg_head",
                "just:farmers_brim",
                "just:rabbit_ears",
                "just:stack_of_eggs",
                //summer
                "just:balaclava1",
                "just:baseball_hat_july",
                "just:beanie_july",
                "just:dangerous_july",
                "just:flag_july",
                "just:firework",
                "just:july_double",
                "just:neck_gaiter1",
                "just:sand_castle",
                "just:seaweed_hat",
                "just:shiesty_duck",
                "just:palm_tree",
                //halloween
                "just:balaclava2",
                "just:batwing_hat",
                "just:beanie_spooky",
                "just:ghostmask",
                "just:head_bolts",
                "just:hockey_mask",
                "just:jack_o_hat",
                "just:murdered",
                "just:neck_gaiter2",
                "just:pinhead",
                "just:plague_doctor",
                "just:spider_web_hat",
                "just:zigzag_witch_hat",
                //festive
                "just:baseball_hat_festive",
                "just:beanie_festive",
                "just:candy_cane",
                "just:christmas_cake",
                "just:christmas_tree",
                "just:festive_antlers",
                "just:festive_ribbon",
                "just:grinch_hat",
                "just:mistletoe",
                "just:red_nose",
                "just:santa_claus",
                "just:santa_hat",
                "just:present_stack",
]
const faceItems = [
                 //Face
                 "just:big_eyes",
                 "just:circular_glasses",
                 "just:cucumber_eyes",
                 "just:dangeresque",
                 "just:double_take",
                 "just:eye_patch",
                 "just:goggles",
                 "just:scouter",
                 "just:sleepy_eye_mask",
                 "just:triange_shades",
                 "just:disguise",
                 "just:dork",
                 "just:monocle",
                 "just:rock_hat",
                 "just:sports_sunglasses",
                 "just:big_sunglasses",
                 "just:blue_fire_eye",
                 "just:mad_scientist",
                 "just:lightning_eyes",
                 "just:rim_glasses",
                 "just:red_eyes",
                 "just:sunglasses",
                 "just:sunglasses_rgb",
                 "just:bandana",
                 "just:bandana_rgb",
                 "just:july_sunglasses"
                 
]
const shirtItems = [
                 //shirts
                "just:addon_merch",
                "just:addon_merch_billey",
                "just:addon_merch_dorios",
                "just:addon_merch_forsaken",
                "just:addon_merch_pokes",
                "just:bomber_jacket",
                "just:bulletproof_vest",
                "just:cactus_spines",
                "just:camo_shirt",
                "just:chef_shirt",
                "just:crop_top",
                "just:donut_tshirt",
                "just:duck_tshirt",
                "just:easter_bunny_shirt",
                "just:flannel_jacket",
                "just:frog_tshirt",
                "just:hawaiian_shirt",
                "just:hawaiian_shirt1",
                "just:hawaiian_shirt2",
                "just:hawaiian_shirt3",
                "just:hawaiian_shirt4",
                "just:hawaiian_shirt5",
                "just:knitted_sweater",
                "just:knitted_sweater_dress",
                "just:letter_tee",
                "just:letter_tee_b",
                "just:letter_tee_c",
                "just:letter_tee_d",
                "just:letter_tee_e",
                "just:letter_tee_f",
                "just:letter_tee_g",
                "just:letter_tee_h",
                "just:letter_tee_i",
                "just:letter_tee_j",
                "just:letter_tee_k",
                "just:letter_tee_l",
                "just:letter_tee_m",
                "just:letter_tee_n",
                "just:letter_tee_o",
                "just:letter_tee_p",
                "just:letter_tee_q",
                "just:letter_tee_r",
                "just:letter_tee_s",
                "just:letter_tee_t",
                "just:letter_tee_u",
                "just:letter_tee_v",
                "just:letter_tee_w",
                "just:letter_tee_x",
                "just:letter_tee_y",
                "just:letter_tee_z",
                "just:nether_sweatshirt",
                "just:pfe_listener_hoodie",
                "just:rose_t_shirt",
                "just:say_that_again_tshirt",
                "just:ship_tshirt",
                "just:slimed_trenchcoat",
                "just:sports_jacket",
                "just:super_sand_shirt",
                "just:sweatshirt",
                "just:t_shirt",
                "just:the_sun_tshirt",
                "just:tie_dye_crop_top",
                "just:tree_tshirt",
                "just:trench_coat"

]
const pantItems = [
                //pants
                "just:astronaut_suit",
                "just:ballroom_gown",
                "just:bunny_tail",
                "just:camo_pants",
                "just:camo_shorts",
                "just:camo_skirt",
                "just:cat_tail",
                "just:corduroy_pants",
                "just:corduroy_shorts",
                "just:duck_floatie",
                "just:easter_bunny_pants",
                "just:fish_tail",
                "just:jeans",
                "just:jean_shorts",
                "just:leopard_skirt",
                "just:monkey_tail",
                "just:ombre_skirt",
                "just:overalls",
                "just:plaid_overalls",
                "just:plaid_pants",
                "just:plaid_shorts",
                "just:reptile_tail",
                "just:scorpion_tail",
                "just:slacks",
                "just:sports_pants",
                "just:sports_shorts",
                "just:squirrel_tail",
                "just:super_sand_pants",
                "just:sweatpants",
                "just:track_suit",
                "just:wolf_tail"

]
const footItems = [
                //feet
                "just:airwalks",
                "just:bunny_slippers",
                "just:duck_boots",
                "just:easter_bunny_feet",
                "just:flipflop",
                "just:old_boots",
                "just:sandals",
                "just:sneakers",
                "just:super_sand_boots"
]
const backItems = [
                //accessory
                "just:easter_bunny_bag",
                "just:seraphim",
                "just:butterfly_wings",
                "just:duck_wings",
                "just:rosy_maple_moth_wings"
]
const suitItems = [
                //costume
                "just:castle_crasher",
                "just:easter_bunny_costume",
                "just:easter_bunny",
                "just:general_chungus_costume",
                "just:hotdog_costume",
                "just:inflatable_trex",
                "just:shake_zula",
                "just:sus",
                "just:sus_imposter",
                "just:tenna_costume"
]

function registerAccessories(slot, items) {
    items.forEach(id => NoveltyManager.registerAccessoriesItem(id, AccessoriesSlot[slot]));
}

registerAccessories("Hat", hatItems);
registerAccessories("Face", faceItems)
registerAccessories("Foot", footItems);
registerAccessories("Back", backItems);
registerAccessories("Belt", shirtItems);
registerAccessories("Foot", pantItems);
registerAccessories("Trinket", suitItems);


const accessoryCache = new Map();

system.runInterval(() => {
    for (const player of world.getPlayers()) {
        const hatItem = NoveltyManager.getAccessoriesSlot(player, AccessoriesSlot.Hat);
        const newId = (hatItem?.hasItem?.() && hatItems.includes(hatItem.typeId)) ? hatItem.typeId : undefined;
        const oldId = accessoryCache.get(player.id);

        if (oldId !== newId) {
            try {
                if (oldId) {
                    player.runCommand(`event entity @s detach ${oldId}`);
                }
                if (newId) {
                    player.runCommand(`event entity @s attach ${newId}`);
                }
                accessoryCache.set(player.id, newId);
            } catch (err) {
                console.warn(`Failed to attach/detach ${newId} for ${player.name}: ${err}`);
            }
        }
    }
}, 10);