
mario_palettes = [
    [0x5F, 0x63, 0x1D, 0x58, 0x0A, 0x00, 0x1F, 0x39, 0xC4, 0x44, 0x08, 0x4E, 0x70, 0x67, 0xB6, 0x30, 0xDF, 0x35, 0xFF, 0x03], # Mario
    [0x3F, 0x4F, 0x1D, 0x58, 0x40, 0x11, 0xE0, 0x3F, 0x07, 0x3C, 0xAE, 0x7C, 0xB3, 0x7D, 0x00, 0x2F, 0x5F, 0x16, 0xFF, 0x03], # Luigi
    [0x5F, 0x63, 0x1D, 0x58, 0x0A, 0x00, 0x1F, 0x03, 0xC4, 0x44, 0x08, 0x4E, 0x70, 0x67, 0x16, 0x02, 0xDF, 0x35, 0xFF, 0x03], # Wario
    [0x5F, 0x63, 0x1D, 0x58, 0x0A, 0x00, 0x12, 0x7C, 0xC4, 0x44, 0x08, 0x4E, 0x70, 0x67, 0x0D, 0x58, 0xDF, 0x35, 0xFF, 0x03], # Waluigi
    [0x5F, 0x63, 0x1D, 0x58, 0x0A, 0x00, 0x00, 0x7C, 0xC4, 0x44, 0x08, 0x4E, 0x70, 0x67, 0x00, 0x58, 0xDF, 0x35, 0xFF, 0x03], # Geno
    [0x5F, 0x63, 0x1D, 0x58, 0x0A, 0x00, 0x1F, 0x7C, 0xC4, 0x44, 0x08, 0x4E, 0x70, 0x67, 0x16, 0x58, 0xDF, 0x35, 0xFF, 0x03], # Princess
    [0x5F, 0x63, 0x1D, 0x58, 0x0A, 0x00, 0xE0, 0x00, 0xC4, 0x44, 0x08, 0x4E, 0x70, 0x67, 0x80, 0x00, 0xDF, 0x35, 0xFF, 0x03], # Dark
    [0x5F, 0x63, 0x1D, 0x58, 0x0A, 0x00, 0xFF, 0x01, 0xC4, 0x44, 0x08, 0x4E, 0x70, 0x67, 0x5F, 0x01, 0xDF, 0x35, 0xFF, 0x03], # Sponge
]

fire_mario_palettes = [
    [0x5F, 0x63, 0x1D, 0x58, 0x29, 0x25, 0xFF, 0x7F, 0x08, 0x00, 0x17, 0x00, 0x1F, 0x00, 0x7B, 0x57, 0xDF, 0x0D, 0xFF, 0x03], # Mario
    [0x1F, 0x3B, 0x1D, 0x58, 0x29, 0x25, 0xFF, 0x7F, 0x40, 0x11, 0xE0, 0x01, 0xE0, 0x02, 0x7B, 0x57, 0xDF, 0x0D, 0xFF, 0x03], # Luigi
    [0x5F, 0x63, 0x1D, 0x58, 0x29, 0x25, 0xFF, 0x7F, 0x08, 0x00, 0x16, 0x02, 0x1F, 0x03, 0x7B, 0x57, 0xDF, 0x0D, 0xFF, 0x03], # Wario
    [0x5F, 0x63, 0x1D, 0x58, 0x29, 0x25, 0xFF, 0x7F, 0x08, 0x00, 0x0D, 0x58, 0x12, 0x7C, 0x7B, 0x57, 0xDF, 0x0D, 0xFF, 0x03], # Waluigi
    [0x5F, 0x63, 0x1D, 0x58, 0x29, 0x25, 0xFF, 0x7F, 0x08, 0x00, 0x00, 0x58, 0x00, 0x7C, 0x7B, 0x57, 0xDF, 0x0D, 0xFF, 0x03], # Geno
    [0x5F, 0x63, 0x1D, 0x58, 0x29, 0x25, 0xFF, 0x7F, 0x08, 0x00, 0x16, 0x58, 0x1F, 0x7C, 0x7B, 0x57, 0xDF, 0x0D, 0xFF, 0x03], # Princess
    [0x5F, 0x63, 0x1D, 0x58, 0x29, 0x25, 0xFF, 0x7F, 0x08, 0x00, 0x80, 0x00, 0xE0, 0x00, 0x7B, 0x57, 0xDF, 0x0D, 0xFF, 0x03], # Dark
    [0x5F, 0x63, 0x1D, 0x58, 0x29, 0x25, 0xFF, 0x7F, 0x08, 0x00, 0x5F, 0x01, 0xFF, 0x01, 0x7B, 0x57, 0xDF, 0x0D, 0xFF, 0x03], # Sponge
]

ow_mario_palettes = [
    [0x16, 0x00, 0x1F, 0x00], # Mario
    [0x80, 0x02, 0xE0, 0x03], # Luigi
    [0x16, 0x02, 0x1F, 0x03], # Wario
    [0x0D, 0x58, 0x12, 0x7C], # Waluigi
    [0x00, 0x58, 0x00, 0x7C], # Geno
    [0x16, 0x58, 0x1F, 0x7C], # Princess
    [0x80, 0x00, 0xE0, 0x00], # Dark
    [0x5F, 0x01, 0xFF, 0x01], # Sponge
]

level_music_address_data = [
    0x284DB,
    0x284DC,
    0x284DD,
    0x284DE,
    0x284DF,
    0x284E0,
    0x284E1,
    0x284E2,
]

level_music_value_data = [
    0x02,
    0x06,
    0x01,
    0x08,
    0x07,
    0x03,
    0x05,
    0x12,
]

ow_music_address_data = [
    [0x25BC8, 0x20D8A],
    [0x25BC9, 0x20D8B],
    [0x25BCA, 0x20D8C],
    [0x25BCB, 0x20D8D],
    [0x25BCC, 0x20D8E],
    [0x25BCD, 0x20D8F],
    [0x25BCE, 0x20D90],
    [0x16C7]
]

ow_music_value_data = [
    0x02,
    0x03,
    0x04,
    0x06,
    0x07,
    0x09,
    0x05,
    0x01,
]

valid_foreground_palettes = {
    0x00: [0x00, 0x01, 0x03, 0x04, 0x05, 0x07], # Normal 1
    0x01: [0x03, 0x04, 0x05, 0x07],             # Castle 1
    0x02: [0x01, 0x02, 0x03, 0x04, 0x05, 0x07], # Rope 1
    0x03: [0x02, 0x03, 0x04, 0x05, 0x07],       # Underground 1
    0x04: [0x01, 0x02, 0x03, 0x04, 0x05, 0x07], # Switch Palace 1
    0x05: [0x04, 0x05],                         # Ghost House 1
    0x06: [0x01, 0x02, 0x03, 0x04, 0x05, 0x07], # Rope 2
    0x07: [0x00, 0x01, 0x03, 0x04, 0x05, 0x07], # Normal 2
    0x08: [0x01, 0x02, 0x03, 0x04, 0x05, 0x07], # Rope 3
    0x09: [0x01, 0x02, 0x03, 0x04, 0x05, 0x07], # Underground 2
    0x0A: [0x01, 0x02, 0x03, 0x04, 0x05, 0x07], # Switch Palace 2
    0x0B: [0x03, 0x04, 0x05, 0x07],             # Castle 2
    #0x0C: [],                                  # Cloud/Forest
    0x0D: [0x04, 0x05],                         # Ghost House 2
    0x0E: [0x02, 0x03, 0x04, 0x05, 0x07],       # Underground 3
}

valid_background_palettes = {
    0x06861B: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Ghost House Exit
    0xFFD900: [0x01],                                           # P. Hills
    0xFFDAB9: [0x04],                                           # Water
    0xFFDC71: [0x00, 0x01, 0x02, 0x03, 0x05, 0x06, 0x07],       # Hills & Clouds
    0xFFDD44: [0x00, 0x01, 0x02, 0x03, 0x05, 0x06, 0x07],       # Clouds
    0xFFDE54: [0x00, 0x01, 0x02, 0x03, 0x05, 0x06, 0x07],       # Small Hills
    0xFFDF59: [0x00, 0x01, 0x02, 0x03, 0x05, 0x06, 0x07],       # Mountains & Clouds
    0xFFE103: [0x00, 0x01, 0x02, 0x03, 0x05, 0x06, 0x07],       # Castle Pillars
    0xFFE472: [0x00, 0x01, 0x02, 0x03, 0x05, 0x06, 0x07],       # Big Hills
    0xFFE674: [0x00, 0x01, 0x02, 0x03, 0x05, 0x06, 0x07],       # Bonus
    0xFFE684: [0x01, 0x03, 0x05, 0x06],                         # Stars
    0xFFE7C0: [0x00, 0x01, 0x02, 0x03, 0x05, 0x06, 0x07],       # Mountains
    0xFFE8EE: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Empty/Layer 2
    0xFFE8FE: [0x01, 0x06],                                     # Cave
    0xFFEC82: [0x00, 0x02, 0x03, 0x05, 0x06, 0x07],             # Bushes
    0xFFEF80: [0x01, 0x03, 0x05, 0x06],                         # Ghost House
    0xFFF175: [0x00, 0x01, 0x02, 0x03, 0x05, 0x06],             # Ghost Ship
    0xFFF45A: [0x01, 0x03, 0x06],                               # Castle
}

valid_background_colors = {
    0x06861B: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Ghost House Exit
    0xFFD900: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # P. Hills
    0xFFDAB9: [0x02, 0x03, 0x05],                               # Water
    0xFFDC71: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Hills & Clouds
    0xFFDD44: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Clouds
    0xFFDE54: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Small Hills
    0xFFDF59: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Mountains & Clouds
    0xFFE103: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Castle Pillars
    0xFFE472: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Big Hills
    0xFFE674: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Bonus
    0xFFE684: [0x02, 0x03, 0x04, 0x05],                         # Stars
    0xFFE7C0: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Mountains
    0xFFE8EE: [0x03, 0x05],                                     # Empty/Layer 2
    0xFFE8FE: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Cave
    0xFFEC82: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Bushes
    0xFFEF80: [0x03, 0x04],                                     # Ghost House
    0xFFF175: [0x02, 0x03, 0x04, 0x05],                         # Ghost Ship
    0xFFF45A: [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07], # Castle
}

valid_ow_palettes = {
    0x2D1E: [0x00, 0x01, 0x03],       # Main OW
    0x2D1F: [0x00, 0x03, 0x04],       # Yoshi's Island
    0x2D20: [0x00, 0x01, 0x03, 0x04], # Vanilla Dome
    0x2D21: [0x00, 0x02, 0x03, 0x04], # Forest of Illusion
    0x2D22: [0x00, 0x01, 0x03, 0x04], # Valley of Bowser
    0x2D24: [0x00, 0x02, 0x03],       # Star Road
}

def generate_shuffled_level_music(world, player):
    shuffled_level_music = level_music_value_data.copy()

    if world.music_shuffle[player] == "consistent":
        world.per_slot_randoms[player].shuffle(shuffled_level_music)
    elif world.music_shuffle[player] == "singularity":
        single_song = world.per_slot_randoms[player].choice(shuffled_level_music)
        shuffled_level_music = [single_song for i in range(len(shuffled_level_music))]

    return shuffled_level_music

def generate_shuffled_ow_music(world, player):
    shuffled_ow_music = ow_music_value_data.copy()

    if world.music_shuffle[player] == "consistent" or world.music_shuffle[player] == "full":
        world.per_slot_randoms[player].shuffle(shuffled_ow_music)
    elif world.music_shuffle[player] == "singularity":
        single_song = world.per_slot_randoms[player].choice(shuffled_ow_music)
        shuffled_ow_music = [single_song for i in range(len(shuffled_ow_music))]

    return shuffled_ow_music

def generate_shuffled_ow_palettes(rom, world, player):
    if world.overworld_palette_shuffle[player]:
        for address, valid_palettes in valid_ow_palettes.items():
            chosen_palette = world.per_slot_randoms[player].choice(valid_palettes)
            rom.write_byte(address, chosen_palette)

def generate_shuffled_header_data(rom, world, player):
    if world.music_shuffle[player] != "full" and not world.foreground_palette_shuffle[player] and not world.background_palette_shuffle[player]:
        return

    for level_id in range(0, 0x200):
        layer1_ptr_list = list(rom.read_bytes(0x2E000 + level_id * 3, 3))
        layer1_ptr = (layer1_ptr_list[2] << 16 | layer1_ptr_list[1] << 8 | layer1_ptr_list[0])

        if layer1_ptr == 0x68000:
            # Unused Levels
            continue

        if layer1_ptr >= 0x70000:
            layer1_ptr -= 0x8000

        layer1_ptr -= 0x38000

        level_header = list(rom.read_bytes(layer1_ptr, 5))

        tileset = level_header[4] & 0x0F

        if world.music_shuffle[player] == "full":
            level_header[2] &= 0x8F
            level_header[2] |= (world.per_slot_randoms[player].randint(0, 7) << 5)

        if (world.foreground_palette_shuffle[player] and tileset in valid_foreground_palettes):
            level_header[3] &= 0xF8
            level_header[3] |= world.per_slot_randoms[player].choice(valid_foreground_palettes[tileset])

        if world.background_palette_shuffle[player]:
            layer2_ptr_list = list(rom.read_bytes(0x2E600 + level_id * 3, 3))
            layer2_ptr = (layer2_ptr_list[2] << 16 | layer2_ptr_list[1] << 8 | layer2_ptr_list[0])

            if layer2_ptr in valid_background_palettes:
                level_header[0] &= 0x1F
                level_header[0] |= (world.per_slot_randoms[player].choice(valid_background_palettes[layer2_ptr]) << 5)

            if layer2_ptr in valid_background_colors:
                level_header[1] &= 0x1F
                level_header[1] |= (world.per_slot_randoms[player].choice(valid_background_colors[layer2_ptr]) << 5)

        rom.write_bytes(layer1_ptr, bytes(level_header))
