﻿DI_artifact_select_char = {
    scope = character

    saved_scopes = {
        target # player character
    }

    effect = {
        scope:target = {
            set_variable = {
                name = DI_artifact_selected_char
                value = prev
            }
        }
    }
}

### Select Character Init ###
DI_artifact_select_char_init = {
    scope = character

    effect = {
        if = {
            limit = {
                NOT = { has_variable = DI_artifact_selected_char }
            }
            set_variable = {
                name = DI_artifact_selected_char
                value = this
            }
        }
    }
}

DI_take_artifact = {
    scope = character

    saved_scopes = {
        selected_artifact # artifact
    }

    effect = {
        save_scope_as = new_owner

        scope:selected_artifact = {
            set_owner = scope:new_owner
        }
    }
}

DI_destroy_artifact = {
    scope = character

    saved_scopes = {
        selected_artifact # artifact
    }

    effect = {
        scope:selected_artifact = { save_scope_as = selected_artifact }
        destroy_artifact = scope:selected_artifact
    }
}

DI_get_artifact_list = {
    scope = character

    effect = {
        save_scope_as = target

        every_in_list = {
            variable = pinned_characters

            every_character_artifact  = {
                scope:target = {
                    add_to_variable_list = { name = DI_artifact_list target = prev }
                }
            }
        }
    }
}

DI_clear_artifact_list = {
    scope = character

    effect = {
        clear_variable_list = DI_artifact_list
    }
}

### Artifact Creation Scripts ###
DI_create_artifact = {
    scope = character

    effect = {
        save_scope_as = player

        var:DI_artifact_selected_char = {
            # Set rarity bonus
            DI_set_rarity_bonus = yes

            # Get artifact parameters based on type
            DI_get_artifact_params = yes

            # Route to appropriate creation effect based on category
            if = {
                limit = { var:artifact_category = flag:weapon }
                DI_create_weapon_internal = yes
            }
            else_if = {
                limit = { var:artifact_category = flag:armor }
                DI_create_armor_internal = yes
            }
            else_if = {
                limit = { var:artifact_category = flag:regalia }
                DI_create_regalia_internal = yes
            }
            else_if = {
                limit = { var:artifact_category = flag:trinket }
                DI_create_trinket_internal = yes
            }
            else_if = {
                limit = { var:artifact_category = flag:throne }
                DI_create_throne_internal = yes
            }
            else_if = {
                limit = {
                    OR = {
                        var:artifact_category = flag:pedestal
                        var:artifact_category = flag:furniture
                        var:artifact_category = flag:ornament
                    }
                }
                DI_create_court_artifact_internal = yes
            }
        }
    }
}

### Load Artifact Creator Dropdowns ###
DI_load_artifact_quality_dropdown = {
    scope = character # Player Character

    is_valid = {
        NOT = { has_global_variable_list = DI_artifact_quality_filter_choices }
    }

    effect = {
        save_scope_as = player

        add_to_global_variable_list = { name = DI_artifact_quality_filter_choices target = flag:DI_quality_common }
        add_to_global_variable_list = { name = DI_artifact_quality_filter_choices target = flag:DI_quality_masterwork }
        add_to_global_variable_list = { name = DI_artifact_quality_filter_choices target = flag:DI_quality_famed }
        add_to_global_variable_list = { name = DI_artifact_quality_filter_choices target = flag:DI_quality_illustrious }
    }
}

DI_load_artifact_type_dropdown = {
    scope = character # Player Character

    is_valid = {
        NOT = { has_global_variable_list = DI_artifact_type_filter_choices }
    }

    effect = {
        save_scope_as = player

        add_to_global_variable_list = { name = DI_artifact_type_filter_choices target = flag:DI_category_weapon }
        add_to_global_variable_list = { name = DI_artifact_type_filter_choices target = flag:DI_category_armor }
        add_to_global_variable_list = { name = DI_artifact_type_filter_choices target = flag:DI_category_regalia }
        add_to_global_variable_list = { name = DI_artifact_type_filter_choices target = flag:DI_category_trinket }
        add_to_global_variable_list = { name = DI_artifact_type_filter_choices target = flag:DI_category_throne }
        add_to_global_variable_list = { name = DI_artifact_type_filter_choices target = flag:DI_category_pedestal }
        add_to_global_variable_list = { name = DI_artifact_type_filter_choices target = flag:DI_category_furniture }
        add_to_global_variable_list = { name = DI_artifact_type_filter_choices target = flag:DI_category_ornament }
    }
}

DI_load_artifact_subtype_dropdown = {
    scope = character # Player Character

    saved_scopes = {
        category # artifact category (weapon, armor, etc.)
    }

    effect = {
        save_scope_as = player

        clear_global_variable_list = DI_artifact_subtype_filter_choices

        switch = {
            trigger = scope:category
            flag:DI_category_weapon = {
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_axe }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_dagger }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_hammer }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_mace }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_spear }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_sword }

				set_variable = { name = DI_artifact_category_display value = flag:DI_category_weapon }
				set_variable = { name = DI_artifact_subtype_display value = flag:DI_subtype_axe }
				set_variable = { name = DI_artifact_type value = flag:axe }
            }
            flag:DI_category_armor = {
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_armor_mail }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_armor_brigandine }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_armor_laminar }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_armor_lamellar }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_armor_scale }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_armor_plate }

				set_variable = { name = DI_artifact_category_display value = flag:DI_category_armor }
				set_variable = { name = DI_artifact_subtype_display value = flag:DI_subtype_armor_mail }
				set_variable = { name = DI_artifact_type value = flag:armor_mail }
            }
            flag:DI_category_regalia = {
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_crown }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_regalia }

				set_variable = { name = DI_artifact_category_display value = flag:DI_category_regalia }
				set_variable = { name = DI_artifact_subtype_display value = flag:DI_subtype_crown }
				set_variable = { name = DI_artifact_type value = flag:crown }
            }
            flag:DI_category_trinket = {
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_necklace }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_brooch }

				set_variable = { name = DI_artifact_category_display value = flag:DI_category_trinket }
				set_variable = { name = DI_artifact_subtype_display value = flag:DI_subtype_necklace }
				set_variable = { name = DI_artifact_type value = flag:necklace }
            }
            flag:DI_category_throne = {
				add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_throne }

				
				set_variable = { name = DI_artifact_category_display value = flag:DI_category_throne }
				set_variable = { name = DI_artifact_subtype_display value = flag:DI_subtype_throne }
				set_variable = { name = DI_artifact_type value = flag:throne }
            }
            flag:DI_category_pedestal = {
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_small_box }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_medium_box }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_goblet }
				
				set_variable = { name = DI_artifact_category_display value = flag:DI_category_pedestal }
				set_variable = { name = DI_artifact_subtype_display value = flag:DI_subtype_small_box }
				set_variable = { name = DI_artifact_type value = flag:small_box }
            }
            flag:DI_category_furniture = {
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_cabinet }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_chest }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_divan }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_table }

				set_variable = { name = DI_artifact_category_display value = flag:DI_category_furniture }
				set_variable = { name = DI_artifact_subtype_display value = flag:DI_subtype_cabinet }
				set_variable = { name = DI_artifact_type value = flag:cabinet }
            }
            flag:DI_category_ornament = {
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_small_shield }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_large_shield }
                add_to_global_variable_list = { name = DI_artifact_subtype_filter_choices target = flag:DI_subtype_tapestry }

				set_variable = { name = DI_artifact_category_display value = flag:DI_category_ornament }
				set_variable = { name = DI_artifact_subtype_display value = flag:DI_subtype_small_shield }
				set_variable = { name = DI_artifact_type value = flag:small_shield }
            }
        }
    }
}
### Dropdown Selection Handlers ###
# Handle subtype dropdown selection
DI_artifact_subtype_select = {
    scope = character # Player Character

    saved_scopes = {
        subtype # Selected subtype flag
    }

    effect = {
        save_scope_as = player

        # Update display variable to match the selected subtype flag
        set_variable = {
            name = DI_artifact_subtype_display
            value = scope:subtype
        }

        # Set the actual artifact type variable (map display flag to internal type flag)
        if = {
            limit = { scope:subtype = flag:DI_subtype_axe }
            set_variable = { name = DI_artifact_type value = flag:axe }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_dagger }
            set_variable = { name = DI_artifact_type value = flag:dagger }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_hammer }
            set_variable = { name = DI_artifact_type value = flag:hammer }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_mace }
            set_variable = { name = DI_artifact_type value = flag:mace }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_spear }
            set_variable = { name = DI_artifact_type value = flag:spear }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_sword }
            set_variable = { name = DI_artifact_type value = flag:sword }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_armor_mail }
            set_variable = { name = DI_artifact_type value = flag:armor_mail }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_armor_brigandine }
            set_variable = { name = DI_artifact_type value = flag:armor_brigandine }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_armor_laminar }
            set_variable = { name = DI_artifact_type value = flag:armor_laminar }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_armor_lamellar }
            set_variable = { name = DI_artifact_type value = flag:armor_lamellar }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_armor_scale }
            set_variable = { name = DI_artifact_type value = flag:armor_scale }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_armor_plate }
            set_variable = { name = DI_artifact_type value = flag:armor_plate }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_crown }
            set_variable = { name = DI_artifact_type value = flag:crown }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_regalia }
            set_variable = { name = DI_artifact_type value = flag:regalia }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_necklace }
            set_variable = { name = DI_artifact_type value = flag:necklace }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_brooch }
            set_variable = { name = DI_artifact_type value = flag:brooch }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_small_box }
            set_variable = { name = DI_artifact_type value = flag:small_box }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_medium_box }
            set_variable = { name = DI_artifact_type value = flag:medium_box }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_goblet }
            set_variable = { name = DI_artifact_type value = flag:goblet }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_cabinet }
            set_variable = { name = DI_artifact_type value = flag:cabinet }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_chest }
            set_variable = { name = DI_artifact_type value = flag:chest }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_divan }
            set_variable = { name = DI_artifact_type value = flag:divan }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_table }
            set_variable = { name = DI_artifact_type value = flag:table }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_small_shield }
            set_variable = { name = DI_artifact_type value = flag:small_shield }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_large_shield }
            set_variable = { name = DI_artifact_type value = flag:large_shield }
        }
        else_if = {
            limit = { scope:subtype = flag:DI_subtype_tapestry }
            set_variable = { name = DI_artifact_type value = flag:tapestry }
        }
    }
}

# Handle quality dropdown selection
DI_artifact_quality_select = {
    scope = character # Player Character

    saved_scopes = {
        quality # Selected quality flag
    }

    effect = {
        save_scope_as = player

        # Update display variable to match the selected quality flag
        set_variable = {
            name = DI_artifact_quality_display
            value = scope:quality
        }

        # Set the actual quality variable (map display flag to internal quality flag)
        if = {
            limit = { scope:quality = flag:DI_quality_common }
            set_variable = { name = DI_artifact_quality value = flag:common }
        }
        else_if = {
            limit = { scope:quality = flag:DI_quality_masterwork }
            set_variable = { name = DI_artifact_quality value = flag:masterwork }
        }
        else_if = {
            limit = { scope:quality = flag:DI_quality_famed }
            set_variable = { name = DI_artifact_quality value = flag:famed }
        }
        else_if = {
            limit = { scope:quality = flag:DI_quality_illustrious }
            set_variable = { name = DI_artifact_quality value = flag:illustrious }
        }
    }
}

DI_test_animal_hide = {
    scope = character

    effect = {
        save_scope_as = player

        DI_set_rarity_bonus = yes
        scope:player = { save_scope_as = owner }
        scope:player = { save_scope_as = hunter }
        scope:player = { save_scope_as = creator }
        save_scope_value_as = { name = hide_size value = flag:big }
        save_scope_value_as = { name = animal value = flag:bear }
        save_scope_value_as = { name = legendary value = yes }

        DI_create_animal_hide_test = { RARITY = illustrious }
    }
}
