﻿aureliani_prepare_compact_cast_effect = {
	clear_saved_scope = aureliani_compact_adviser
	if = {
		limit = { var:aureliani_compact_route = 1 }
		cp:councillor_steward ?= { if = { limit = { is_alive = yes is_imprisoned = no } save_scope_as = aureliani_compact_adviser } }
	}
	else_if = {
		limit = { var:aureliani_compact_route = 2 }
		cp:councillor_court_chaplain ?= { if = { limit = { is_alive = yes is_imprisoned = no } save_scope_as = aureliani_compact_adviser } }
	}
	else_if = {
		limit = { var:aureliani_compact_route = 3 }
		cp:councillor_marshal ?= { if = { limit = { is_alive = yes is_imprisoned = no } save_scope_as = aureliani_compact_adviser } }
	}
}

aureliani_end_compact_effect = {
	if = {
		limit = { house:house_aureliani = { var:aureliani_compact_owner = prev } }
		house:house_aureliani = { remove_variable = aureliani_compact_owner }
	}
	remove_character_flag = aureliani_compact_pending
	remove_variable = aureliani_compact_route
	remove_variable = aureliani_compact_stage
	remove_variable = aureliani_compact_method
	remove_variable = aureliani_compact_king
	remove_variable = aureliani_compact_approved
	clear_saved_scope = aureliani_compact_adviser
}

aureliani_cancel_current_compact_effect = {
	if = {
		limit = { has_character_flag = aureliani_compact_pending var:aureliani_compact_serial = scope:aureliani_compact_attempt }
		aureliani_end_compact_effect = yes
	}
}

aureliani_record_compact_effect = {
	custom_tooltip = aureliani_compact_recorded_tt
	house = {
		set_variable = { name = aureliani_italian_compact value = 1 }
		set_variable = { name = aureliani_italian_compact_route value = prev.var:aureliani_compact_route }
		set_variable = { name = aureliani_legacy value = 6 }
	}
	dynasty = { add_dynasty_prestige = 50 }
	aureliani_end_compact_effect = yes
	trigger_event = aureliani.0071
}

aureliani_fund_compact_effect = {
	if = {
		limit = { aureliani_compact_signing_ready_trigger = yes gold >= 100 }
		remove_short_term_gold = 100
		add_character_modifier = { modifier = aureliani_compact_funded_obligations years = 10 }
		dynasty = { add_dynasty_prestige = 25 }
		scope:aureliani_compact_adviser ?= {
			if = {
				limit = { is_alive = yes }
				add_opinion = { target = root modifier = aureliani_compact_heard_opinion years = 5 }
			}
		}
		aureliani_record_compact_effect = yes
	}
	else = { aureliani_cancel_current_compact_effect = yes }
}

aureliani_concede_compact_effect = {
	if = {
		limit = {
			aureliani_compact_signing_ready_trigger = yes
			trigger_if = { limit = { var:aureliani_compact_route = 2 } piety >= 100 }
			trigger_else = { prestige >= 100 }
		}
		if = {
			limit = { var:aureliani_compact_route = 1 }
			add_prestige = -100
			title:c_firenze = { add_county_modifier = { modifier = aureliani_compact_civic_concessions years = 5 } }
		}
		else_if = {
			limit = { var:aureliani_compact_route = 2 }
			add_piety = -100
			add_character_modifier = { modifier = aureliani_compact_sacred_concessions years = 5 }
		}
		else = {
			add_prestige = -100
			add_character_modifier = { modifier = aureliani_compact_imperial_concessions years = 5 }
		}
		aureliani_record_compact_effect = yes
	}
	else = { aureliani_cancel_current_compact_effect = yes }
}

aureliani_choose_compact_terms_effect = {
	if = {
		limit = { aureliani_compact_session_valid_trigger = yes var:aureliani_compact_stage = 0 }
		set_variable = { name = aureliani_compact_method value = $METHOD$ }
		if = {
			limit = { has_title = title:k_italy }
			aureliani_commit_compact_terms_effect = yes
		}
		else = {
			custom_tooltip = aureliani_compact_royal_tt
			set_variable = { name = aureliani_compact_stage value = 1 }
			house = { set_variable = { name = aureliani_compact_petition_cooldown value = 1 days = 365 } }
			save_scope_as = aureliani_compact_petitioner
			var:aureliani_compact_king = {
				save_scope_as = aureliani_compact_king
				trigger_event = { id = aureliani.0072 days = 7 }
			}
		}
	}
	else = { aureliani_cancel_current_compact_effect = yes }
}

aureliani_commit_compact_terms_effect = {
	if = { limit = { var:aureliani_compact_method = 1 } aureliani_fund_compact_effect = yes }
	else_if = { limit = { var:aureliani_compact_method = 2 } aureliani_concede_compact_effect = yes }
}

aureliani_answer_compact_petition_effect = {
	scope:aureliani_compact_petitioner ?= {
		if = {
			limit = { aureliani_compact_session_valid_trigger = yes var:aureliani_compact_stage = 1 var:aureliani_compact_king = root }
			set_variable = { name = aureliani_compact_approved value = $APPROVAL$ }
			set_variable = { name = aureliani_compact_stage value = 2 }
			trigger_event = { id = aureliani.0073 days = 7 }
		}
		else = { aureliani_cancel_current_compact_effect = yes }
	}
}
