﻿# Shared collection helpers for the independent Title and Province Manager.
# These deliberately use only script scopes and global lists; no vanilla window
# controller (TitleViewWindow, HoldingView, GUIBuildingItem, etc.) is required.

advcm_tm_clear_workspace_effect = {
	clear_global_variable_list = advcm_tm_title_list
	clear_global_variable_list = advcm_tm_title_sort_buffer
	clear_global_variable_list = advcm_tm_title_rank_header_titles
	clear_global_variable_list = advcm_tm_selected_titles
	clear_global_variable_list = advcm_tm_action_counties
	clear_global_variable_list = advcm_tm_province_list
	clear_global_variable_list = advcm_tm_county_header_provinces
	clear_global_variable_list = advcm_tm_selected_provinces
	set_global_variable = { name = advcm_tm_title_count value = 0 }
	set_global_variable = { name = advcm_tm_selected_title_count value = 0 }
	set_global_variable = { name = advcm_tm_province_count value = 0 }
	set_global_variable = { name = advcm_tm_selected_province_count value = 0 }
	remove_global_variable = advcm_tm_de_jure_liege
}

advcm_tm_add_current_title_once_effect = {
	if = {
		limit = {
			NOT = {
				is_target_in_global_variable_list = {
					name = advcm_tm_title_list
					target = this
				}
			}
		}
		add_to_global_variable_list = { name = advcm_tm_title_list target = this }
		change_global_variable = { name = advcm_tm_title_count add = 1 }
	}
}

advcm_tm_sort_title_list_effect = {
	# Never mutate the displayed list while iterating over it. Rebuild it in a
	# temporary buffer so rank sections remain stable across reloads.
	clear_global_variable_list = advcm_tm_title_sort_buffer
	clear_global_variable_list = advcm_tm_title_rank_header_titles

	remove_global_variable = advcm_tm_rank_header_added
	every_in_global_list = {
		variable = advcm_tm_title_list
		limit = { tier = tier_empire }
		if = {
			limit = { NOT = { exists = global_var:advcm_tm_rank_header_added } }
			add_to_global_variable_list = { name = advcm_tm_title_rank_header_titles target = this }
			set_global_variable = { name = advcm_tm_rank_header_added value = 1 }
		}
		add_to_global_variable_list = { name = advcm_tm_title_sort_buffer target = this }
	}

	remove_global_variable = advcm_tm_rank_header_added
	every_in_global_list = {
		variable = advcm_tm_title_list
		limit = { tier = tier_kingdom }
		if = {
			limit = { NOT = { exists = global_var:advcm_tm_rank_header_added } }
			add_to_global_variable_list = { name = advcm_tm_title_rank_header_titles target = this }
			set_global_variable = { name = advcm_tm_rank_header_added value = 1 }
		}
		add_to_global_variable_list = { name = advcm_tm_title_sort_buffer target = this }
	}

	remove_global_variable = advcm_tm_rank_header_added
	every_in_global_list = {
		variable = advcm_tm_title_list
		limit = { tier = tier_duchy }
		if = {
			limit = { NOT = { exists = global_var:advcm_tm_rank_header_added } }
			add_to_global_variable_list = { name = advcm_tm_title_rank_header_titles target = this }
			set_global_variable = { name = advcm_tm_rank_header_added value = 1 }
		}
		add_to_global_variable_list = { name = advcm_tm_title_sort_buffer target = this }
	}

	remove_global_variable = advcm_tm_rank_header_added
	every_in_global_list = {
		variable = advcm_tm_title_list
		limit = { tier = tier_county }
		if = {
			limit = { NOT = { exists = global_var:advcm_tm_rank_header_added } }
			add_to_global_variable_list = { name = advcm_tm_title_rank_header_titles target = this }
			set_global_variable = { name = advcm_tm_rank_header_added value = 1 }
		}
		add_to_global_variable_list = { name = advcm_tm_title_sort_buffer target = this }
	}

	clear_global_variable_list = advcm_tm_title_list
	every_in_global_list = {
		variable = advcm_tm_title_sort_buffer
		add_to_global_variable_list = { name = advcm_tm_title_list target = this }
	}
	clear_global_variable_list = advcm_tm_title_sort_buffer
	remove_global_variable = advcm_tm_rank_header_added
}

advcm_tm_add_current_county_once_effect = {
	if = {
		limit = {
			NOT = {
				is_target_in_global_variable_list = {
					name = advcm_tm_action_counties
					target = this
				}
			}
		}
		add_to_global_variable_list = { name = advcm_tm_action_counties target = this }
	}
}

advcm_tm_add_current_province_once_effect = {
	if = {
		limit = {
			NOT = {
				is_target_in_global_variable_list = {
					name = advcm_tm_province_list
					target = this
				}
			}
		}
		add_to_global_variable_list = { name = advcm_tm_province_list target = this }
		change_global_variable = { name = advcm_tm_province_count add = 1 }
	}
}

# Builds a de-duplicated county work list from the selected titles. Selecting a
# duchy and one of its counties therefore never applies a county action twice.
advcm_tm_collect_selected_counties_effect = {
	clear_global_variable_list = advcm_tm_action_counties

	every_in_global_list = {
		variable = advcm_tm_selected_titles

		if = {
			limit = { tier = tier_county }
			advcm_tm_add_current_county_once_effect = yes
		}
		else = {
			every_de_jure_county = {
				advcm_tm_add_current_county_once_effect = yes
			}
		}
	}
}
