﻿succession_crisis_execute_usurpation_effect = {
	# 0. Save current locations
	root = {
		location = {
			save_scope_as = usurper_original_location
		}
	}
	
	scope:crisis_heir = {
		location = {
			save_scope_as = heir_original_location
		}
		# Grab all vassals of crisis_heir and put them in the list to use
		every_vassal_or_below = {
			add_to_list = crisis_vassals
		}
	}

	# 1. Initialize the Title Change
	create_title_and_vassal_change = {
		type = conquest
		save_scope_as = change
		add_claim_on_loss = yes
	}
	
	# 2. Seize the Primary Top-Level Title
	scope:crisis_title = {
		change_title_holder = {
			holder = root
			change = scope:change
		}

		# 3. Seize the Capital County and the Kingdom title it belongs to
		scope:crisis_destination = {
			county = {
				save_scope_as = capital_county_title
				# We seize the Kingdom title to ensure the usurper has the 
				# actual land and power base underneath the Empire title.
				kingdom = {
					change_title_holder = {
						holder = root
						change = scope:change
					}
				}
				scope:capital_county_title = {
					change_title_holder = {
						holder = root
						change = scope:change
					}
				}
			}
		}
	}

	# 4. Execute all title transfers
	resolve_title_and_vassal_change = scope:change

	# 5. Establish the Kingdom Hierarchy (Fealty)
	if = {
		limit = {
			scope:crisis_heir = {
				is_alive = yes
				NOT = { this = root }
				any_held_title = { count >= 1 }
			}
		}
		
		create_title_and_vassal_change = {
			type = swear_fealty
			save_scope_as = fealty_change
			add_claim_on_loss = no
		}
		
		scope:crisis_heir = {
			change_liege = {
				liege = root
				change = scope:fealty_change
			}
		}
		
		resolve_title_and_vassal_change = scope:fealty_change
	}

	# 6. Restore Locations
	root = {
		set_location = scope:usurper_original_location
	}

	# Trigger succession crisis event for the heir
	if = {
		limit = {
			scope:crisis_heir = { is_alive = yes }
		}
		scope:crisis_heir = {
			trigger_event = {
				id = succession_crisis_misc.0002
				days = 1
			}
		}
	}
}