﻿utc_new_caliph_decision = {
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_destiny_goal.dds"
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { faith.religious_head_title.holder.dynasty = dynasty }
				desc = utc_new_caliph_decision_desc.usurpation
			}
			triggered_desc = {
				trigger = { dynasty = { has_variable = utc_caliphate_restoration_variable } }
				desc = utc_new_caliph_decision_desc.restoration
			}
			desc = utc_new_caliph_decision_desc
		}
	}
	selection_tooltip = utc_new_caliph_decision_tooltip
	decision_group_type = major

	ai_check_interval_by_tier = {
		barony = 0
		county = 0
		duchy = 36
		kingdom = 12
		empire = 12
		hegemony = 12
	}

	is_shown = {
		NOT = { has_game_rule = utc_turned_off_usurpation_barriers }
		exists = faith.religious_head_title
		exists = faith.religious_head_title.holder
		utc_faith_check_trigger = yes
		is_ruler = yes
		is_landed = yes
		is_playable_character = yes
		NOT = { has_title = faith.religious_head_title }
		# Gender doctrine
		OR = {
			AND = {
				faith = { has_doctrine = doctrine_gender_male_dominated }
				is_male = yes
			}
			AND = {
				faith = { has_doctrine = doctrine_gender_female_dominated }
				is_female = yes
			}
			faith = { has_doctrine = doctrine_gender_equal }
		}
	}

	is_valid = {
		# Two or more holy sites
		custom_description = {
			text = utc_two_holy_sites_control_custom_description_trigger
			faith = {
				any_holy_site = {
					count > 1
					OR = {
						county.holder = root
						county.holder = { 	
							any_liege_or_above = { this = root }
						}
					}
				}
			}
		}
		# Either the ruler must be from the same dynasty and have caliph as his vassal or variable for usurpation doesnt exist
		OR = {
			AND = {
				custom_description = {
					text = utc_muslim_rulers_army_strength_check_custom_description_trigger
					subject = root					
					NOT = {
						any_independent_ruler = {
							exists = faith.religious_head_title
							utc_faith_check_trigger = yes
							faith.religious_head_title = root.faith.religious_head_title
							# We have to check that it's not the player's ruler cause otherwise it would not pass the military strength value
							this != root
							utc_max_military_strength_plus_percent_value > root.max_military_strength
						}
					}
				}
				# First trigger is for the old caliphate while they have a grace period and second for everyone else
				trigger_if = {
					limit = { NOT = { has_game_rule = utc_0_years_usurpation_cooldown } }
					trigger_if = {
						limit = { dynasty = { has_variable = utc_caliphate_restoration_variable } }
						custom_description = {
							text = utc_caliphate_restoration_custom_description_trigger
							subject = root
							dynasty = { has_variable = utc_caliphate_restoration_variable }
						}
					}
					trigger_else = {
						custom_description = {
							text = utc_recent_usurpation_custom_description_trigger
							subject = root
							NOT = { faith.religious_head_title = { has_variable = utc_title_was_recently_usurped_variable } }
						}
					}
				}
				piety_level >= 3
			}
			AND = {
				dynasty = faith.religious_head_title.holder.dynasty
				custom_description = {
					text = utc_caliph_vassal_custom_description_trigger
					subject = faith.religious_head_title.holder
					any_vassal_or_below = { this = faith.religious_head_title.holder }
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		is_capable_adult = yes
		is_imprisoned = no
		is_independent_ruler = yes
		is_tributary = no
		is_at_war = no
		custom_description = {
			text = utc_caliph_piety_level_custom_description_trigger
			subject = root
			NOT = { faith.religious_head_title.holder.piety_level > piety_level }
		}
		NOT = { is_allied_to = faith.religious_head_title.holder }
	}

	effect = {
		# Setting up scopes
		save_scope_as = caliph
		if = {
			limit = {
				is_ai = no
			}
			add_to_list = utc_usurpation_observers_list
		}
		faith.religious_head_title = {
			save_scope_as = caliphate
			holder = {
				save_scope_as = old_caliph
				add_to_list = utc_usurpation_observers_list
			}
		}
		if = {
			limit = {
				dynasty != scope:old_caliph.dynasty
			}
			save_scope_as = new_caliphate
		}
		# Changing the head of faith title holder
		create_title_and_vassal_change = {
			type = usurped
			save_scope_as = change
			add_claim_on_loss = no
		}
		scope:caliphate = {
			change_title_holder = {
				holder = root
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
		# Changing the house unity and head if it's appropriate
		if = {
			limit = {
				scope:old_caliph.house = house
			}
			if = {
				limit = {
					house = { house_head = scope:old_caliph }
					NOT = {
						exists = scope:new_caliphate
						any_ancestor = {
							is_alive = yes
							house = scope:caliph.house
						}
					}
				}
				house = { set_house_head = scope:caliph }
			}
			if = {
				limit = {
					government_has_flag = government_is_clan
				}
				house = {
					add_unity_value = {
						value = massive_unity_loss
						character = root
						desc = utc_clan_unity_caliph_got_usurped.desc
					}
				}
			}
		}
		hidden_effect = {
			# Setting up the usurpation variable
			if = {
				limit = {
					NOT = {
						has_game_rule = utc_0_years_usurpation_cooldown
					}
					exists = scope:new_caliphate
				}
				scope:caliphate = {
					set_variable = {
						name = utc_title_was_recently_usurped_variable
						value = yes
						years = utc_usurpation_cooldown_value
					}
					previous_holder.dynasty = {
						set_variable = {
							name = utc_caliphate_restoration_variable
							value = yes
							years = utc_usurpation_cooldown_grace_value
						}
					}
				}
			}
			# If the new caliph is a restorer of an old caliphate we should remove variable.
			if = {
				limit = {
					dynasty = {
						has_variable = utc_caliphate_restoration_variable
					}
				}
				dynasty = {
					remove_variable = utc_caliphate_restoration_variable
				}
				# Also, we are saving the scope to be later used for flavor text in the event.
				save_scope_as = restored_caliphate
			}
			# Changing both caliphs stand for the Persian Struggle
			if = {
				limit = {
					has_trait = fp3_struggle_detractor
				}
				remove_trait = fp3_struggle_detractor
				add_trait = fp3_struggle_supporter
			}
			if = {
				limit = {
					scope:old_caliph = {
						has_trait = fp3_struggle_supporter
					}
				}
				scope:old_caliph = {
					remove_trait = fp3_struggle_supporter
					add_trait = fp3_struggle_detractor
				}
			}
			# Setting rulers for the event
			every_player = {
				limit = {
					NOT = { is_in_list = utc_usurpation_observers_list }
					OR = {
						# New Caliphate
						AND = {
							exists = scope:new_caliphate
							OR = {
								in_diplomatic_range = scope:caliph
								in_diplomatic_range = scope:old_caliph
								faith.religion = scope:caliph.faith.religion
							}
						}
						# Old Caliphate
						AND = {
							NOT = { exists = scope:new_caliphate }
							is_vassal_or_below_of = scope:caliph
							faith.religion = scope:caliph.faith.religion
						}
					}
				}
				add_to_list = utc_usurpation_observers_list
			}
			# Triggering the event
			every_in_list = {
				list = utc_usurpation_observers_list
				trigger_event = {
					id = utc_decision_events.0001
					# days = 1
				}
			}
		}
	}
	
	cost = { piety = 500 }
	
	ai_potential = {
		# primary_title.tier = tier_empire
	}

	ai_will_do = {
		base = 0
		modifier = {
			add = 40
			OR = {
				ai_boldness >= medium_positive_ai_value
				ai_greed >= medium_positive_ai_value
			}
		}
		modifier = {
			add = -20
			OR = {
				ai_boldness <= low_negative_ai_value
				ai_greed <= low_negative_ai_value
			}
		}
		modifier = {
			add = 40
			ai_zeal >= medium_positive_ai_value
			faith.religious_head_title.holder = {
				any_character_trait = {
					count > 1
					prev.faith = {
						trait_is_sin = prev
					}
				}
			}
		}
		modifier = {
			add = -40
			ai_zeal >= medium_positive_ai_value
			faith.religious_head_title.holder = {
				any_character_trait = {
					count > 1
					prev.faith = {
						trait_is_virtue = prev
					}
				}
			}
		}
		modifier = {
			factor = 1.9
			ai_honor <= very_low_negative_ai_value
			OR = {
				has_relation_rival = faith.religious_head_title.holder
				has_relation_potential_rival = faith.religious_head_title.holder
				has_relation_nemesis = faith.religious_head_title.holder
				opinion = {
					target = faith.religious_head_title.holder
					value < -20
				}
			}
		}
		modifier = {
			factor = 0.1
			ai_honor >= very_low_positive_ai_value
			OR = {
				has_relation_friend = faith.religious_head_title.holder
				has_relation_best_friend = faith.religious_head_title.holder
				has_relation_lover = faith.religious_head_title.holder
				has_relation_soulmate = faith.religious_head_title.holder
				opinion = {
					target = faith.religious_head_title.holder
					value > 40
				}
			}
		}
		# Doing this for a consistency of the caliph's getting deposed by one of his relatives
		modifier = {
			add = 10000
			dynasty = faith.religious_head_title.holder.dynasty
		}
	}
}
