﻿namespace = pam_saint_events

###########################################
## Canonizing Saints
###########################################
## Canonization
## 0001 - Petition sent: tells the canonizer the HoF's answer is awaited (fired at commit)
## 0002 - Head of Faith canonization request (HoF decides)
## 0003 - Canonization approved / finalized (shown to the canonizer)
## 0004 - Canonization denied (notify canonizer)
## Relic extraction
## 0005 - Relic extraction begun: tells the extractor the relic will arrive soon
## 0006 - The relic is drawn a delay after the saint is picked in the extract-a-relic decision
## Buried-saint tomb visit
## 0007 - Visiting a saint's tomb (player): pay respects, or draw a relic on the spot
###########################################

# Only the canonizing ruler, root, hears about this when they petition the Head of Faith
# scope:saint_candidate, consecration_province and petition_recipient stay set
pam_saint_events.0001 = {
	type = character_event
	title = pam_saint_events.0001.t
	desc = pam_saint_events.0001.desc
	theme = christian

	left_portrait = {
		character = root
		# Muslims pray in their own posture; trigger runs in the portrait character's scope.
		triggered_animation = {
			trigger = { faith.religion = religion:islam_religion }
			animation = prayer_muslim
		}
		animation = prayer_standing_eyes
	}
	lower_right_portrait = {
		character = scope:saint_candidate
		animation = dead
	}
	right_portrait = {
		trigger = { exists = scope:petition_recipient }
		character = scope:petition_recipient
		animation = thinking
	}

	option = {
		name = pam_saint_events.0001.a
		# Wording picked here so the saint_candidate / consecration_province / petition_recipient scopes stay reachable in the loc key.
		if = {
			limit = {
				OR = {
					faith.religion = religion:catholic_religion
					faith.religion = religion:protestant_religion
					faith.religion = religion:eastern_orthodox_religion
					faith.religion = religion:oriental_orthodox_religion
				}
			}
			custom_tooltip = pam_saint_events.0001.a.tt_saint
		}
		else = {
			custom_tooltip = pam_saint_events.0001.a.tt_va
		}

		# Acknowledging the petition starts the HoF's clock (-> 0003/0004).
		hidden_effect = {
			scope:petition_recipient = {
				trigger_event = {
					id = pam_saint_events.0002
					days = 30
				}
			}
		}
	}
}

# Sent to the Head of Faith. They approve or reject the canonization.
# scope:canonizer            - the ruler putting the saint forward
# scope:saint_candidate      - the dead character to be canonized
# scope:consecration_province - where they would be consecrated/buried
pam_saint_events.0002 = {
	type = character_event
	title = pam_saint_events.0002.t
	desc = {
		# Saint vs Venerated Ancestor wording picked here so canonizer / saint_candidate / consecration_province stay reachable in the loc key.
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						faith.religion = religion:catholic_religion
						faith.religion = religion:protestant_religion
						faith.religion = religion:eastern_orthodox_religion
						faith.religion = religion:oriental_orthodox_religion
					}
				}
				desc = pam_saint_events.0002.desc_saint
			}
			desc = pam_saint_events.0002.desc_va
		}
		# Name the candidate's personality traits (handles 1, 2, or 3).
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:pam_saint_ptrait_3 }
				desc = pam_saint_events.0002.desc.traits_3
			}
			triggered_desc = {
				trigger = { exists = scope:pam_saint_ptrait_2 }
				desc = pam_saint_events.0002.desc.traits_2
			}
			triggered_desc = {
				trigger = { exists = scope:pam_saint_ptrait_1 }
				desc = pam_saint_events.0002.desc.traits_1
			}
		}
		# Close that paragraph with a line scaled to how virtuous they were.
		first_valid = {
			triggered_desc = {
				trigger = { scope:saint_candidate = { num_virtuous_traits >= 3 } }
				desc = pam_saint_events.0002.desc.virtue_3
			}
			triggered_desc = {
				trigger = { scope:saint_candidate = { num_virtuous_traits >= 2 } }
				desc = pam_saint_events.0002.desc.virtue_2
			}
			triggered_desc = {
				trigger = { scope:saint_candidate = { num_virtuous_traits >= 1 } }
				desc = pam_saint_events.0002.desc.virtue_1
			}
		}
	}
	theme = christian

	left_portrait = {
		character = scope:canonizer
		animation = personality_zealous
	}
	lower_center_portrait = {
		character = scope:saint_candidate
		animation = dead
	}
	right_portrait = {
		character = root
		animation = thinking
	}

	# Pull the candidate's personality traits into scopes so the description can name them.
	immediate = {
		scope:saint_candidate = {
			every_character_trait = {
				limit = { has_trait_category = personality }
				add_to_list = pam_saint_personality_traits
			}
		}
		if = {
			limit = { list_size:pam_saint_personality_traits > 0 }
			ordered_in_list = {
				list = pam_saint_personality_traits
				save_scope_as = pam_saint_ptrait_1
				remove_from_list = pam_saint_personality_traits
			}
		}
		if = {
			limit = { list_size:pam_saint_personality_traits > 0 }
			ordered_in_list = {
				list = pam_saint_personality_traits
				save_scope_as = pam_saint_ptrait_2
				remove_from_list = pam_saint_personality_traits
			}
		}
		if = {
			limit = { list_size:pam_saint_personality_traits > 0 }
			ordered_in_list = {
				list = pam_saint_personality_traits
				save_scope_as = pam_saint_ptrait_3
				remove_from_list = pam_saint_personality_traits
			}
		}
	}

	option = { # Approve the canonization
		name = pam_saint_events.0002.a

		pam_canonize_saint_effect = {
			CANONIZER = scope:canonizer
			CANDIDATE = scope:saint_candidate
			CONSECRATION_PROVINCE = scope:consecration_province
			PRESET = no
		}

		# Gratitude toward the HoF (root) who granted the petition.
		scope:canonizer = {
			add_opinion = {
				target = root
				modifier = pam_canonization_approved_petitioner_opinion
			}
		}
		# Same-faith dynasty members warm to the HoF too; hidden so the option shows one summary line.
		custom_tooltip = {
			text = pam_saint_events.0002.a.dynasty_opinion_tt
			scope:canonizer.dynasty ?= {
				every_dynasty_member = {
					limit = {
						this != scope:canonizer
						faith = scope:canonizer.faith
					}
					add_opinion = {
						target = root
						modifier = pam_canonization_approved_dynasty_opinion
					}
				}
			}
		}

		hidden_effect = {
			scope:canonizer = {
				trigger_event = pam_saint_events.0003
			}
		}

		ai_chance = {
			base = 0
			modifier = {
				add = {
					value = scope:saint_candidate.pam_hof_canonization_approve_weight
				}
			}
		}
	}

	option = { # Reject the canonization
		name = pam_saint_events.0002.b
		trigger = {
			NOT = { puppeteer ?= scope:canonizer }
		}

		# Resentment toward the HoF (root) who refused the petition.
		scope:canonizer = {
			add_opinion = {
				target = root
				modifier = pam_canonization_denied_petitioner_opinion
			}
		}
		# Same-faith dynasty members sour on the HoF too; hidden so the option shows one summary line.
		custom_tooltip = {
			text = pam_saint_events.0002.b.dynasty_opinion_tt
			scope:canonizer.dynasty ?= {
				every_dynasty_member = {
					limit = {
						this != scope:canonizer
						faith = scope:canonizer.faith
					}
					add_opinion = {
						target = root
						modifier = pam_canonization_denied_dynasty_opinion
					}
				}
			}
		}

		hidden_effect = {
			scope:canonizer = {
				trigger_event = pam_saint_events.0004
			}
		}

		ai_chance = {
			base = 0
			modifier = {
				add = {
					value = scope:saint_candidate.pam_hof_canonization_reject_weight
				}
			}
			modifier = {
				add = pam_hof_canonization_cynical_weight
				has_trait = cynical # the deciding Head of Faith's own outlook
			}
		}
	}
}

# The Head of Faith approved (or no approval was needed).
pam_saint_events.0003 = {
	type = character_event
	title = pam_saint_events.0003.t
	# Wording picked here so saint_candidate / consecration_province stay reachable in the loc key.
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						faith.religion = religion:catholic_religion
						faith.religion = religion:protestant_religion
						faith.religion = religion:eastern_orthodox_religion
						faith.religion = religion:oriental_orthodox_religion
					}
				}
				desc = pam_saint_events.0003.desc_saint
			}
			desc = pam_saint_events.0003.desc_va
		}
	}
	theme = christian

	left_portrait = {
		character = scope:saint_candidate
		animation = dead
	}

	option = {
		name = pam_saint_events.0003.a
		# The canonizer's same-faith dynasty takes pride in their new saint; hidden so the option shows one summary line.
		custom_tooltip = {
			text = pam_saint_events.0003.a.dynasty_opinion_tt
			root.dynasty ?= {
				every_dynasty_member = {
					limit = {
						this != root
						faith = root.faith
					}
					add_opinion = {
						target = root
						modifier = pam_canonization_celebrated_dynasty_opinion
					}
				}
			}
		}
	}
}

# The Head of Faith rejected the canonization.
pam_saint_events.0004 = {
	type = character_event
	title = pam_saint_events.0004.t
	# Saint/Venerated-Ancestor vocabulary swapped inside the key by custom loc; saint_candidate / petition_recipient stay live via the direct desc reference.
	desc = pam_saint_events.0004.desc
	theme = christian

	left_portrait = {
		character = root
		animation = disappointed
	}
	lower_center_portrait = {
		character = scope:saint_candidate
		animation = dead
	}
	right_portrait = {
		character = root.faith.religious_head
		animation = disapproval
	}

	option = {
		name = pam_saint_events.0004.a
		# Refund the petition cost the canonize decision charged up front, since the HoF declined.
		add_piety_no_experience = pam_canonize_saint_piety_cost
		add_gold = pam_canonize_saint_gold_cost
	}
}

# Fired to the extracting ruler (root) the instant they begin drawing a relic, so they know the relic (0006) is on its way. Informational; the decision only fires it for players. scope:relic_saint / extract_site carry over.
pam_saint_events.0005 = {
	type = character_event
	title = pam_saint_events.0005.t
	desc = pam_saint_events.0005.desc
	theme = christian

	left_portrait = {
		character = root
		animation = personality_zealous
	}
	right_portrait = {
		trigger = { exists = scope:relic_saint }
		character = scope:relic_saint
	}

	option = {
		name = pam_saint_events.0005.a
		custom_tooltip = pam_saint_events.0005.a.tt

		# Acknowledging the start of the work begins the timer that delivers the relic.
		hidden_effect = {
			trigger_event = {
				id = pam_saint_events.0006
				days = 30
			}
		}
	}
}

# The relic is drawn here (a delay after the extract-a-relic pick) and shown to the extractor (root). scope:extract_site carries over; creation saves scope:relic_artifact for naming.
pam_saint_events.0006 = {
	type = character_event
	title = pam_saint_events.0006.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { exists = scope:relic_artifact }
				desc = pam_saint_events.0006.desc_relic
			}
			desc = pam_saint_events.0006.desc
		}
	}
	theme = christian

	left_portrait = {
		character = root
		animation = prayer_standing_eyes
	}
	right_portrait = {
		trigger = { exists = scope:relic_saint }
		character = scope:relic_saint
	}

	immediate = {
		if = {
			limit = { exists = scope:extract_site.saint_in_province }
			scope:extract_site.saint_in_province = { save_scope_as = relic_saint }
		}
		else = {
			root = { save_scope_as = relic_saint }
		}
		pam_extract_relic_from_saint_effect = {
			SITE = scope:extract_site
		}
	}

	option = {
		name = pam_saint_events.0006.a
		# Relic was built in immediate (hidden_effect_new_object), so it can't surface itself here; confirm the draw and that it can now be placed.
		custom_tooltip = pam_saint_events.0006.a.tt
		# Echo the blessing rolled in immediate (tooltip only), crediting the tenet.
		if = {
			limit = {
				rite = { rite_has_parameter = tenet_dulia_extra_relic_modifier }
				exists = scope:relic_artifact
			}
			pam_show_dulia_relic_blessing_effect = yes
		}
		# Echo the prestige granted in immediate (tooltip only), crediting the tenet.
		if = {
			limit = {
				rite = { rite_has_parameter = tenet_dulia_dynasty_prestige_from_relics }
				exists = dynasty
				exists = scope:relic_creator_saint.dynasty
				scope:relic_creator_saint.dynasty = dynasty
			}
			show_as_tooltip = { pam_grant_dulia_relic_dynasty_prestige_effect = yes }
		}
	}
}

# Runs from poi_buried_saint on_visit when a player reaches the tomb
# Historical tombs only reach it when a relic can be drawn
pam_saint_events.0007 = {
	type = character_event
	title = pam_saint_events.0007.t
	desc = pam_saint_events.0007.desc
	theme = christian

	left_portrait = {
		character = root
		animation = personality_zealous
	}
	right_portrait = {
		trigger = { exists = scope:visited_saint }
		character = scope:visited_saint
		animation = dead
	}

	option = {
		name = pam_saint_events.0007.a
		if = {
			limit = {
				scope:saint_tomb_province = { has_variable = pam_preset_buried_saint }
			}
			add_piety = point_of_interest_miniscule_piety_value
		}
		else = {
			add_piety = point_of_interest_piety_value
			add_stress = minor_stress_loss
		}
	}

	# Draw a relic now - only while the tomb is in your realm, you can afford it, and the saint can still yield one
	option = {
		name = pam_saint_events.0007.b
		trigger = {
			piety >= pam_extract_relic_piety_cost
			gold >= pam_extract_relic_gold_cost
			exists = scope:visited_saint
			faith = { has_saint_character = scope:visited_saint }
			scope:saint_tomb_province = {
				pam_can_extract_relic_from_saint_trigger = { EXTRACTOR = root SAINT = scope:visited_saint }
			}
		}
		custom_tooltip = pam_saint_events.0007.b.tt
		add_piety_no_experience = {
			value = pam_extract_relic_piety_cost
			multiply = -0.75
		}
		remove_short_term_gold = {
			value = pam_extract_relic_gold_cost
			multiply = 0.75
		}

		pam_extract_relic_from_saint_effect = {
			SITE = scope:saint_tomb_province
		}
		
		hidden_effect = {
			if = {
				limit = { exists = scope:relic_artifact }
				send_interface_toast = {
					title = pam_saint_events.0007.b.toast
					left_icon = root
					right_icon = scope:relic_artifact
					custom_tooltip = pam_saint_events.0007.b.toast_desc
				}
			}
		}
	}
}
