﻿namespace = CTFA_events

# Find an available vanity title and bind it to this character's primary title.
CTFA_events.1001 = {
	type = character_event

	hidden = yes

	immediate = {
		debug_log = "CTFA_events.1001 (Find an available vanity title and bind it to this character's primary title.)"

		assert_if = {
			limit = { NOT = { exists = primary_title } }

			text = "character must have a primary title"
		}

		primary_title = {
			if = {
				limit = {
					NOT = { CTFA_has_current_bound_vt = yes }
				}

				# This sets scope:selected_vanity_title
				# ...and local_var:selected_vanity_title_cost, at least for now
				CTFA_select_best_available_vanity_title = yes

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

					text = "VT selection must always succeed"
				}

				CTFA_bind_vanity_title = {
					VANITY_TITLE = scope:selected_vanity_title
				}


				# Replace VT name with a FoA suggestion, unless we've managed to select our own VT again
				if = {
					limit = {
						local_var:selected_vanity_title_cost >= CTFA_vt_cost_unbound
					}

					scope:selected_vanity_title = {
						# FIXME: For some reason this only works once per VT.
						#        And reset_title doesn't help either. :(
						set_title_name = CTFA_vanity_title_name_suggest_default
					}
				}

				remove_local_variable = selected_vanity_title_cost
			}
		}
	}
}

# Unbind and destroy vanity title, if one is currently bound to this character's primary title.
CTFA_events.1002 = {
	type = character_event

	hidden = yes

	immediate = {
		debug_log = "CTFA_events.1002 (Unbind and destroy vanity title, if one is currently bound to this character's primary title.)"

		assert_if = {
			limit = { NOT = { exists = primary_title } }

			text = "character must have a primary title"
		}

		primary_title = {
			if = {
				limit = {
					CTFA_has_current_bound_vt = yes
				}

				CTFA_unbind_and_destroy_vanity_title = yes
			}
		}
	}
}
