﻿# Any theocratic vassal of $LIEGE$'s faith, no matter the rite
pam_valid_antipope_clergy_vassal_trigger = {
	save_temporary_scope_as = antipope_temp
	is_clergy = yes
	custom_tooltip = {
		text = pam_antipope_clerical_gender_tt
		$LIEGE$.rite = { rite_has_allowed_gender_for_clergy = scope:antipope_temp }
	}
	faith = $LIEGE$.faith
	NOT = { any_held_title = { is_head_of_faith = yes } }
	pam_is_antipope_trigger = no
}

# Holds one of their faith's antipope titles
pam_is_antipope_trigger = {
	save_temporary_scope_as = pam_antipope_holder_check
	faith ?= {
		any_religious_head_challenger = {
			holder ?= scope:pam_antipope_holder_check
		}
	}
}

pam_title_is_antipope_office_trigger = {
	has_variable = pam_antipope_office
}

# Holds the sponsor title behind one of their faith's antipopes. A self-declared antipope sponsors themselves
pam_is_antipope_sponsor_trigger = {
	save_temporary_scope_as = pam_antipope_sponsor_check
	faith = {
		any_religious_head_challenger = {
			challenger_sponsor ?= {
				holder ?= scope:pam_antipope_sponsor_check
			}
		}
	}
}

pam_sponsors_other_antipope_trigger = {
	save_temporary_scope_as = pam_other_antipope_sponsor_check
	faith = {
		any_religious_head_challenger = {
			NOT = { holder ?= scope:pam_other_antipope_sponsor_check }
			challenger_sponsor ?= {
				holder ?= scope:pam_other_antipope_sponsor_check
			}
		}
	}
}

# Scoped to an antipope: $SPONSOR$ is the ruler whose title backs them
pam_antipope_is_sponsored_by_trigger = {
	save_temporary_scope_as = pam_checked_antipope
	$SPONSOR$ = { save_temporary_scope_as = pam_expected_sponsor }
	faith = {
		any_religious_head_challenger = {
			holder ?= scope:pam_checked_antipope
			challenger_sponsor ?= {
				holder ?= scope:pam_expected_sponsor
			}
		}
	}
}

pam_is_sponsored_antipope_trigger = {
	pam_is_antipope_trigger = yes
	pam_is_antipope_sponsor_trigger = no
}

pam_antipope_declaration_discounted_trigger = {
	situation:the_christian_church ?= {
		has_situation_top_phase_parameter = the_christian_church_cheaper_antipope_declaration
	}
}

# Covers the antipope, their sponsor, and anyone under either of them
pam_antipope_protected_from_excommunication_trigger = {
	is_following_religious_head_challenger = yes
	faith.religious_head ?= $EXCOMMUNICATOR$
	NOT = { has_personal_tenet_flag = tenet_apostolic_succession_antipope_opposition }
}

# Clerical requests that the mainline hierarchy would answer
# Anyone under an antipope has to take them to that antipope instead
pam_antipope_clerical_request_trigger = {
	trigger_if = {
		limit = { pam_is_antipope_trigger = yes }
		custom_tooltip = {
			text = pam_clerical_request_actor_is_antipope_tt
			religious_head_or_challenger ?= $RECIPIENT$
		}
	}
	trigger_else_if = {
		limit = { pam_is_antipope_sponsor_trigger = yes }
		custom_tooltip = {
			text = pam_clerical_request_actor_is_antipope_sponsor_tt
			religious_head_or_challenger ?= $RECIPIENT$
		}
	}
	trigger_else_if = {
		limit = { is_following_religious_head_challenger = yes }
		custom_tooltip = {
			text = pam_clerical_request_in_antipope_realm_tt
			religious_head_or_challenger ?= $RECIPIENT$
		}
	}
	trigger_else = {
		always = yes
	}
}

pam_antipope_may_grant_clerical_region_trigger = {
	trigger_if = {
		limit = { scope:actor = { pam_is_antipope_trigger = yes } }
		trigger_if = {
			limit = { exists = $SEE$.holder }
			$SEE$.holder.religious_head_or_challenger ?= scope:actor
		}
		trigger_else = {
			$SEE$.title_capital_county.holder.religious_head_or_challenger ?= scope:actor
		}
	}
	trigger_else = {
		always = yes
	}
}

# Antipope + Sponsor
pam_can_use_challenge_hof_cb_trigger = {
	OR = {
		pam_is_antipope_trigger = yes
		pam_is_antipope_sponsor_trigger = yes
	}
	faith = {
		exists = religious_head
		NOT = { religious_head = scope:attacker }
	}
}

pam_challenge_hof_join_war_trigger = {
	is_ai = yes
	highest_held_title_tier >= tier_county
	NOR = {
		is_participant_in_war = scope:war
		# Don't add vassals of targets
		is_vassal_or_below_of = scope:defender
		is_vassal_or_below_of = scope:attacker
		# Not part of an attacker's realm
		any_liege_or_above = { is_participant_in_war = scope:war }
	}
	OR = {
		# Military Holy Orders always join
		government_has_flag = government_is_military_holy_order
		AND = {
			# Landed rulers join if zealous independents or theocrats, prevent every cardinal and monastic abbot joining
			is_landed = yes
			OR = {
				AND = {
					is_independent_ruler = yes
					# Attackers always join if rite is different, to reward spreading rite
					trigger_if = {
						limit = {
							scope:defender.rite != scope:attacker.rite
							rite = scope:attacker.rite
						}
						always = yes
					}
					# Otherwise attackers/defenders join only if zealous
					trigger_else = {
						ai_zeal >= very_high_positive_ai_value
					}
				}
				government_has_flag = government_is_theocracy
			}
		}
	}
}

# Against the Antipope sponsor (which can be the antipope themselves if self-declared)
pam_can_target_antiking_cb_trigger = {
	scope:defender = {
		pam_is_antipope_sponsor_trigger = yes
		faith = scope:attacker.faith
		top_liege = this
	}
	scope:attacker = {
		is_landed = yes
		faith = scope:defender.faith
		NOT = { this = scope:defender }
		NOR = {
			pam_is_antipope_sponsor_trigger = yes
			pam_is_antipope_trigger = yes
		}
	}
}

antipope_requirements_trigger = {
	calc_true_if = {
		amount >= $AMOUNT$
		has_trait = herald
		trigger_if = {
			limit = { religion = religion:islam_religion }
			# Sayyids get a free pass.
			has_trait = sayyid
		}
		trigger_else_if = {
			limit = { religion = religion:zoroastrianism_religion }
			OR = {
				has_trait = saoshyant
				has_trait = saoshyant_descendant
			}
		}
		# to prevent "false" visible, but you should not get this as a potential antipope since this can only be granted to a HoF
		trigger_else = {
			has_trait = savior
		}
		has_trait_xp = {
			trait = lifestyle_scholar
			value >= 100
		}
		custom_description = {
			text = is_head_of_rite_tt
			subject = root
			rite.head_of_rite ?= this
		}
		# has_relation_rival has no first-person trigger localization, so describe it explicitly
		custom_description = {
			text = declare_yourself_antipope_decision_hof_rival_tt
			has_relation_rival = faith.religious_head
		}
		custom_description = {
			text = appoint_a_righteous_caliph_decision.caliph_not_in_diplo_range
			NOT = { in_diplomatic_range = faith.religious_head }
		}
		# Having a holy site.
		custom_description = {
			text = appoint_a_righteous_caliph_decision.control_one_holy_site
			faith = {
				any_holy_site_barony = {
					count >= 1
					holder ?= { target_is_same_character_or_above = root }
				}
			}
		}
		# Having *2* holy sites.
		custom_description = {
			text = appoint_a_righteous_caliph_decision.control_two_holy_sites
			faith = {
				any_holy_site_barony = {
					count >= 2
					holder ?= { target_is_same_character_or_above = root }
				}
			}
		}
		# Having a higher-than-minimum piety level.
		piety_level >= very_high_piety_level
		custom_description = {
			text = declare_yourself_antipope_decision_hof_piety_level_tt
			piety_level > faith.religious_head.piety_level
		}
		# And just having a caliph who doesn't live up to the root's faith's ideals.
		custom_description = {
			text = appoint_a_righteous_caliph_decision.caliph_has_x_or_more_traits_sinful_to_your_faith
			faith.religious_head = {
				num_sinful_traits = {
					value >= 2
					target = root.rite
				}
			}
		}
		# Has planned to schism off for a *while*.
		custom_description = {
			text = appoint_a_righteous_caliph_decision.has_wishes_to_schism_caliphate_flag
			has_character_flag = wishes_to_schism_caliphate_flag
		}
	}
}
