﻿# Saves the best available vanity title for the primary title
# of the scoped character into scope:selected_vanity_title.
# Scope: Character
#
# See CTFA_select_best_available_vanity_title
# for VT selection logic.
CTFA_select_best_available_vanity_title_for_character = {
	assert_if = {
		limit = { NOT = { exists = primary_title }}

		text = "character must have a primary title"
	}

	primary_title = {
		CTFA_select_best_available_vanity_title = yes
	}
}

# Saves the best available vanity title for the scoped title
# (assumed to be some character's primary)
# into scope:selected_vanity_title.
# Scope: Title
#
# Vanity title is selected according to the following order of preference:
#   0. The VT already bound to this title and currently held by the same character as this title
#      (there should never be more than 1 of these).
#   1. Any VT already bound to this title and not currently held by any character.
#   2. Any unbound VT (expected to have no holder).
#   3. Any VT already bound to a different title and not currently held by any character.
#   4. Any VT. (By elimination this will be an already bound VT already held by some character,
#      so we're going to have to steal their VT unfortunatelly, since we ran out of better options.)
CTFA_select_best_available_vanity_title = {
	assert_if = {
		limit = { NOT = { exists = holder } }

		text = "primary title must have a holder"
	}

	if = {
		limit = { exists = scope:selected_vanity_title }	
		clear_saved_scope = selected_vanity_title
	}

	set_local_variable = {
		name = selected_vanity_title_cost
		value = CTFA_vt_cost_infinite
	}

	every_in_global_list = {
		variable = CTFA_vanity_titles

		set_local_variable = {
			name = current_vanity_title_cost
			value = CTFA_vt_cost_infinite
		}

		#
		# Assign cost to current title
		#

		if = {
			limit = {
				CTFA_is_vt_bound_to_title = {
					TARGET_TITLE = PREV
				}
			}

			if = {
				limit = {
					NOT = { exists = holder }
				}

				# 1. This VT is already bound to this title and not currently held by any character.
				set_local_variable = {
					name = current_vanity_title_cost
					value = CTFA_vt_cost_own_bound_former
				}
			}
			else = {
				assert_if = {
					limit = {
						NOT = { holder = PREV.holder }
					}

					text = "if a bound VT has a holder, it must match the holder of the title it's bound to"
				}

				# 0. This VT is already bound to this title and currently held by the same character as this title.
				set_local_variable = {
					name = current_vanity_title_cost
					value = CTFA_vt_cost_own_bound_current
				}
			}
		}
		else_if = {
			limit = {
				CTFA_is_vt_unbound = yes
			}

			assert_if = {
				limit = { exists = holder }

				text = "unbound VTs must not have holders"
			}

			# 2. This is an as-of-yet unbound VT.
			set_local_variable = {
				name = current_vanity_title_cost
				value = CTFA_vt_cost_unbound
			}
		}
		else_if = {
			limit = {
				NOT = { exists = holder }
			}

			# 3. This VT is already bound to a different title but not currently held by any character.
			set_local_variable = {
				name = current_vanity_title_cost
				value = CTFA_vt_cost_other_bound_former
			}
		}
		else = {
			# 4. Worst case scenario. This VT is already bound to a different title and is currently held by some character.
			set_local_variable = {
				name = current_vanity_title_cost
				value = CTFA_vt_cost_any
			}
		}

		# Update selected VT and cost, if current title is better
		if = {
			limit = {
				local_var:current_vanity_title_cost < local_var:selected_vanity_title_cost
			}

			save_scope_as = selected_vanity_title
			set_local_variable = {
				name = selected_vanity_title_cost
				value = local_var:current_vanity_title_cost
			}
		}
	}

	# DEBUG
	set_global_variable = {
		name = CTFA_temp_selected_vanity_title_cost_for_logging
		value = local_var:selected_vanity_title_cost
	}

	debug_log = "CTFA_select_best_available_vanity_title selected VT cost: [GetGlobalVariable('CTFA_temp_selected_vanity_title_cost_for_logging').GetValue]"

	remove_global_variable = CTFA_temp_selected_vanity_title_cost_for_logging
	# END DEBUG

	remove_local_variable = current_vanity_title_cost
	# TODO: After smart FoA suggestions are implemented, also clear selected_vanity_title_cost.
	#       For now we have to keep this variable, since current implementation of
	#       this scripted_effect's client, CTFA_events.1001, needs to examine it.
	#remove_local_variable = selected_vanity_title_cost

	assert_if = {
		limit = { NOT = { exists = scope:selected_vanity_title } }

		text = "CTFA_select_best_available_vanity_title failed to select any vanity title for primary title"
	}
}
