namespace = head_of_faith

##################################################
# #Islamic Caliphate Configuration
# 0001 - 0010	Heirs of the Prophet - Sunnis decide which caliph they consider most appropriate.
# 0011 - 0020	Heirs of the Prophet - Shias decide which caliph they consider most appropriate.
# 0021 - 0030	Heirs of the Prophet - Muhakkimas decide which caliph they consider most appropriate.
# 0031 - 0040	Heirs of the Prophet - Zandaqas decide which caliph they consider most appropriate.
#
##################################################










##################################################
# ISLAMIC CALIPHATE CONFIGURATION

scripted_trigger valid_prior_caliph_available_trigger = {
	faith = { has_doctrine = muhammad_succession_$MS_TYPE$_doctrine }
	scope:old_faith = {
		# Uses a temporal faith...
		has_doctrine = doctrine_temporal_head
		# ... with a current holder...
		exists = religious_head_title.holder
		# ... that follows the correct succession of the Prophet.
		has_doctrine = muhammad_succession_$MS_TYPE$_doctrine
		# Plus make sure this isn't either of the major orthodoxies.
		religious_head_title = {
			NOR = {
				this = title:d_sunni
				this = title:d_shiite
			}
		}
	}
}

scripted_effect grab_existing_caliph_effect = {
	if = {
		limit = { exists = $TITLE_PATH$.holder }
		$TITLE_PATH$ = { save_scope_as = $DENOMINATION$_title }
		$TITLE_PATH$.holder = { save_scope_as = $DENOMINATION$_caliph }
	}
}

scripted_effect set_default_caliph_effect = {
	faith = {
		# Default to scope:old_title, if it exists.
		if = {
			limit = { exists = scope:old_title }
			set_religious_head_title = scope:old_title
		}
		# Otherwise, try to pick the correct orthodoxy.
		else_if = {
			limit = {
				has_doctrine = muhammad_succession_sunni_doctrine
				exists = scope:sunni_title
			}
			set_religious_head_title = scope:sunni_title
		}
		else_if = {
			limit = {
				has_doctrine = muhammad_succession_shia_doctrine
				exists = scope:shia_title
			}
			set_religious_head_title = scope:shia_title
		}
	}
}

scripted_effect set_religious_head_title_to_and_gain_hook_effect = {
	# First, we set the new religious head.
	faith = { set_religious_head_title = $NEW_RELIGIOUS_HEAD_TITLE$ }
	# Next, if possible, we give a hook on the new religious head.
	if = {
		limit = {
			NOT = { has_hook = $NEW_RELIGIOUS_HEAD_TITLE$.holder }
		}
		$NEW_RELIGIOUS_HEAD_TITLE$.holder = {
			send_interface_toast = {	
				title = head_of_faith.0000.new_faith_submitted_for_hook
				left_icon = root
				root = {
					add_hook = {
						type = indebted_hook
						target = $NEW_RELIGIOUS_HEAD_TITLE$.holder
					}
				}
			}
		}
	}
	# And always give a little opinion of them.
	reverse_add_opinion = {
		target = $NEW_RELIGIOUS_HEAD_TITLE$.holder
		modifier = repentant_opinion
		opinion = 25
	}
}

scripted_effect set_religious_head_title_to_and_give_hook_effect = {
	# First, we set the new religious head.
	faith = { set_religious_head_title = $NEW_RELIGIOUS_HEAD_TITLE$ }
	# Next, if possible, we give a hook on the new religious head.
	if = {
		limit = {
			NOT = { has_hook = $NEW_RELIGIOUS_HEAD_TITLE$.holder }
		}
		$NEW_RELIGIOUS_HEAD_TITLE$.holder = {
			send_interface_toast = {	
				title = head_of_faith.0000.new_faith_submitted_for_hook
				left_icon = prev
				add_hook = {
					type = indebted_hook
					target = prev
				}
			}
		}
	}
	# And always give a little opinion of them.
	reverse_add_opinion = {
		target = $NEW_RELIGIOUS_HEAD_TITLE$.holder
		modifier = repentant_opinion
		opinion = 25
	}
}


scripted_effect set_religious_head_title_to_&_gain_flag_effect = {
	# First, we set the new religious head.
	faith = { set_religious_head_title = $NEW_RELIGIOUS_HEAD_TITLE$ }
	# Next, some piety.
	add_piety = major_piety_gain
	# Plus, we flag it that this character will likely try for independence later.
	add_character_flag = wishes_to_schism_caliphate_flag
	custom_tooltip = head_of_faith.0000.wishes_to_schism_caliphate_flag_added
	# And always give a little opinion of them.
	reverse_add_opinion = {
		target = $NEW_RELIGIOUS_HEAD_TITLE$.holder
		modifier = repentant_opinion
		opinion = 25
	}
}

##################################################
# Heirs of the Prophet - Sunni
# by Ewan Cowhig Croft
# 0001 - 0010
##################################################

#	A temporal Sunni reformer decides their HoF.
head_of_faith.0001 = {
	type = character_event
	title = head_of_faith.0001.t
	desc = head_of_faith.0001.desc
	theme = faith
	left_portrait = {
		character = root
		animation = personality_rational
	}
	lower_left_portrait = scope:sunni_caliph
	lower_center_portrait = {
		character = scope:old_caliph
		trigger = {
			valid_prior_caliph_available_trigger = { MS_TYPE = sunni }
		}
	}

	cooldown = { years = 10 }

	trigger = {
		# Requires either a prior caliphate or a Shia one to exist.
		OR = {
			exists = title:d_sunni.holder
			AND = {
				exists = scope:old_faith.religious_head_title.holder
				valid_prior_caliph_available_trigger = { MS_TYPE = sunni }
			}
		}
	}

	immediate = {
		play_music_cue = "mx_cue_sacredrite"
		# Grab caliphs.
		grab_existing_caliph_effect = {
			TITLE_PATH = title:d_sunni
			DENOMINATION = sunni
		}
		grab_existing_caliph_effect = {
			TITLE_PATH = scope:old_faith.religious_head_title
			DENOMINATION = old
		}
		# And sort defaults.
		set_default_caliph_effect = yes
	}

	# Realign with Sunni orthodoxy.
	option = {
		name = head_of_faith.0001.a
		trigger = { exists = scope:sunni_caliph }

		# Set up the new HoF.
		set_religious_head_title_to_and_gain_hook_effect = { NEW_RELIGIOUS_HEAD_TITLE = scope:sunni_title }

		# No stress for faith-guiding.
		ai_chance = {
			base = 0
			opinion_modifier = {
				opinion_target = scope:sunni_caliph
				multiplier = 5
			}
		}
	}
	
	# Follow scope:old_faith's caliph.
	option = {
		name = head_of_faith.0001.b
		trigger = {
			exists = scope:old_caliph
			valid_prior_caliph_available_trigger = { MS_TYPE = sunni }
		}

		# Set up the new HoF.
		set_religious_head_title_to_and_gain_hook_effect = { NEW_RELIGIOUS_HEAD_TITLE = scope:old_title }

		# No stress for faith-guiding.
		ai_chance = {
			base = 0
			opinion_modifier = {
				opinion_target = scope:old_caliph
				multiplier = 5
			}
		}
	}
	
	# Scope:sunni_caliph has our _present_ loyalties.
	option = {
		name = head_of_faith.0001.c
		trigger = { exists = scope:sunni_caliph }

		# Set up the new HoF.
		set_religious_head_title_to_&_gain_flag_effect = { NEW_RELIGIOUS_HEAD_TITLE = scope:sunni_title }

		# No stress for faith-guiding.
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_vengefulness = 1
				ai_zeal = 1
			}
			opinion_modifier = {
				opinion_target = scope:sunni_caliph
				multiplier = -2
			}
		}

	}

	# We shall follow scope:old_faith... for now...
	option = {
		name = head_of_faith.0001.d
		trigger = {
			exists = scope:old_caliph
			valid_prior_caliph_available_trigger = { MS_TYPE = sunni }
		}

		# Set up the new HoF.
		set_religious_head_title_to_&_gain_flag_effect = { NEW_RELIGIOUS_HEAD_TITLE = scope:old_title }

		# No stress for faith-guiding.
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_vengefulness = 1
				ai_zeal = 1
			}
			opinion_modifier = {
				opinion_target = scope:old_caliph
				multiplier = -2
			}
		}
	}
	
	# No option for striking out on your own immediately; you have a decision for that, or could've taken a different succession doctrine.
}

##################################################
# Heirs of the Prophet - Shia
# by Ewan Cowhig Croft
# 0001 - 0010
##################################################

#	A temporal Shia reformer decides their HoF.
head_of_faith.0011 = {
	type = character_event
	title = head_of_faith.0011.t
	desc = head_of_faith.0011.desc
	theme = faith
	left_portrait = {
		character = root
		animation = personality_rational
	}
	lower_center_portrait = {
		character = scope:old_caliph
		trigger = {
			valid_prior_caliph_available_trigger = { MS_TYPE = shia }
		}
	}
	lower_right_portrait = scope:shia_caliph

	cooldown = { years = 10 }

	trigger = {
		# Requires either a prior caliphate or a Shia one to exist.
		OR = {
			exists = title:d_shiite.holder
			AND = {
				exists = scope:old_faith.religious_head_title.holder
				valid_prior_caliph_available_trigger = { MS_TYPE = shia }
			}
		}
	}

	immediate = {
		play_music_cue = "mx_cue_sacredrite"
		# Grab caliphs.
		grab_existing_caliph_effect = {
			TITLE_PATH = title:d_shiite
			DENOMINATION = shia
		}
		grab_existing_caliph_effect = {
			TITLE_PATH = scope:old_faith.religious_head_title
			DENOMINATION = old
		}
		# And sort defaults.
		set_default_caliph_effect = yes
	}

	# Realign with Shia orthodoxy.
	option = {
		name = head_of_faith.0011.a
		trigger = { exists = scope:shia_caliph }

		# Set up the new HoF.
		set_religious_head_title_to_and_gain_hook_effect = { NEW_RELIGIOUS_HEAD_TITLE = scope:shia_title }

		# No stress for faith-guiding.
		ai_chance = {
			base = 0
			opinion_modifier = {
				opinion_target = scope:shia_caliph
				multiplier = 5
			}
		}
	}
	
	# Follow scope:old_faith's caliph.
	option = {
		name = head_of_faith.0011.c
		trigger = {
			exists = scope:old_caliph
			valid_prior_caliph_available_trigger = { MS_TYPE = shia }
		}

		# Set up the new HoF.
		set_religious_head_title_to_and_gain_hook_effect = { NEW_RELIGIOUS_HEAD_TITLE = scope:old_title }

		# No stress for faith-guiding.
		ai_chance = {
			base = 0
			opinion_modifier = {
				opinion_target = scope:old_caliph
				multiplier = 5
			}
		}
	}
	
	# Scope:shia_caliph has our _present_ loyalties.
	option = {
		name = head_of_faith.0011.b
		trigger = { exists = scope:shia_caliph }

		# Set up the new HoF.
		set_religious_head_title_to_&_gain_flag_effect = { NEW_RELIGIOUS_HEAD_TITLE = scope:shia_title }

		# No stress for faith-guiding.
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_vengefulness = 1
				ai_zeal = 1
			}
			opinion_modifier = {
				opinion_target = scope:shia_caliph
				multiplier = -2
			}
		}
	}
	
	# We shall follow scope:old_faith... for now...
	option = {
		name = head_of_faith.0011.d
		trigger = {
			exists = scope:old_caliph
			valid_prior_caliph_available_trigger = { MS_TYPE = shia }
		}

		# Set up the new HoF.
		set_religious_head_title_to_&_gain_flag_effect = { NEW_RELIGIOUS_HEAD_TITLE = scope:old_title }

		# No stress for faith-guiding.
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_vengefulness = 1
				ai_zeal = 1
			}
			opinion_modifier = {
				opinion_target = scope:old_caliph
				multiplier = -2
			}
		}
	}
	
	# No option for striking out on your own immediately; you have a decision for that, or could've taken a different succession doctrine.
}

##################################################
# Heirs of the Prophet - Muhakkima
# by Ewan Cowhig Croft
# 0001 - 0010
##################################################

#	A temporal Muhakkima reformer decides their HoF.
head_of_faith.0021 = {
	type = character_event
	title = head_of_faith.0021.t
	desc = head_of_faith.0021.desc
	theme = faith
	left_portrait = {
		character = root
		animation = personality_rational
	}
	lower_center_portrait = {
		character = scope:old_caliph
		trigger = {
			valid_prior_caliph_available_trigger = { MS_TYPE = muhakkima }
		}
	}

	cooldown = { years = 10 }

	trigger = {
		# Requires an old religious head to exist in some form.
		exists = scope:old_faith.religious_head_title.holder
		valid_prior_caliph_available_trigger = { MS_TYPE = muhakkima }
	}

	immediate = {
		play_music_cue = "mx_cue_sacredrite"
		# Grab the relevant caliph.
		grab_existing_caliph_effect = {
			TITLE_PATH = scope:old_faith.religious_head_title
			DENOMINATION = old
		}
		# And sort defaults.
		set_default_caliph_effect = yes
	}

	# Go our own way.
	option = {
		name = head_of_faith.0021.a

		# Another splinter faith enters the ring.
		faith = { remove_religious_head_title = yes }
		# Nice chunk of piety for asserting your independence.
		add_piety = major_piety_gain

		# No stress for faith-guiding.
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_zeal = 1
				ai_boldness = 1
				ai_vengefulness = 0.5
			}
			opinion_modifier = {
				opinion_target = scope:old_caliph
				multiplier = -2
			}
		}
	}

	# Follow scope:old_faith's caliph.
	option = {
		name = head_of_faith.0021.b

		# Set up the new HoF.
		set_religious_head_title_to_and_gain_hook_effect = { NEW_RELIGIOUS_HEAD_TITLE = scope:old_title }

		# No stress for faith-guiding.
		ai_chance = {
			base = 0
			opinion_modifier = {
				opinion_target = scope:old_caliph
				multiplier = 5
			}
		}
	}
}

##################################################
# Heirs of the Prophet - Zandaqa
# by Ewan Cowhig Croft
# 0001 - 0010
##################################################
scripted_effect decide_if_worthy_of_consideration_for_new_hof_effect = {
	if = {
		limit = { faith = { has_doctrine = muhammad_succession_zandaqa_doctrine } } # Check if Zandaqa (in case this gets expanded in the future for outside the Zandaqa)

		if = {
			limit = { has_strong_hook = scope:pledge_target }
			use_hook = scope:pledge_target
			set_religious_head_title_to_and_give_hook_effect = { NEW_RELIGIOUS_HEAD_TITLE = scope:pledge_target.faith.religious_head_title }
		}
		else = {
			show_as_tooltip = {
				random_list = {
					100 = { # You get Rejected
						show_chance = no
						desc = head_of_faith.0031.b.rejection
						custom_tooltip = head_of_faith.0031.rejection.outcome
						faith = { remove_religious_head_title = yes }
					}
					100 = {	# Set up the new HoF.
						show_chance = no
						desc = head_of_faith.0031.b.acceptance
						set_religious_head_title_to_and_give_hook_effect = { NEW_RELIGIOUS_HEAD_TITLE = scope:pledge_target.faith.religious_head_title }
					}
				}
			}
		}
	
		if = {
			limit = { is_developer_testing_trigger = yes }
			custom_tooltip = debug_generic_option_shortened_trigger_can_disable # Actually the opposite
			scope:pledge_target = {
				trigger_event = {
					id = head_of_faith.0032
					days = 2
				}
			}
		}
		else = { scope:pledge_target = { trigger_event = head_of_faith.0032 } }
	}
	else = { set_religious_head_title_to_and_give_hook_effect = { NEW_RELIGIOUS_HEAD_TITLE = scope:pledge_target.faith.religious_head_title } }
}

# A temporal Zandaqa reformer decides their HoF.
head_of_faith.0031 = {
	type = character_event
	title = head_of_faith.0031.t
	desc = head_of_faith.0031.desc
	theme = faith
	left_portrait = {
		character = root
		animation = personality_rational
	}
	lower_left_portrait = scope:sunni_caliph
	lower_center_portrait = {
		character = scope:old_caliph
		trigger = {
			valid_prior_caliph_available_trigger = { MS_TYPE = zandaqa }
		}
	}
	lower_right_portrait = scope:shia_caliph

	cooldown = { years = 10 }

	trigger = {
		# Requires any one caliphate to be around.
		OR = {
			exists = title:d_sunni.holder
			exists = title:d_shiite.holder
			AND = {
				exists = scope:old_faith.religious_head_title.holder
				valid_prior_caliph_available_trigger = { MS_TYPE = zandaqa }
			}
		}
	}

	immediate = {
		play_music_cue = "mx_cue_sacredrite"
		# Grab caliphs.
		grab_existing_caliph_effect = {
			TITLE_PATH = title:d_sunni
			DENOMINATION = sunni
		}
		grab_existing_caliph_effect = {
			TITLE_PATH = title:d_shiite
			DENOMINATION = shia
		}
		grab_existing_caliph_effect = {
			TITLE_PATH = scope:old_faith.religious_head_title
			DENOMINATION = old
		}
		# And sort defaults.
		set_default_caliph_effect = yes

		save_scope_as = pledgeing_individual
	}

	# Go our own way.
	option = {
		name = head_of_faith.0031.a

		# Another splinter faith enters the ring.
		faith = { remove_religious_head_title = yes }
		# Nice chunk of piety for asserting your independence.
		add_piety = major_piety_gain

		# No stress for faith-guiding.
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_zeal = 1
				ai_boldness = 1
				ai_vengefulness = 0.5
			}
			opinion_modifier = {
				opinion_target = scope:old_caliph
				multiplier = -2
			}
		}
	}
	
	# Realign with Sunni orthodoxy.
	option = {
		name = head_of_faith.0031.b
		trigger = { exists = scope:sunni_caliph }

		scope:sunni_caliph = { save_scope_as = pledge_target }
		decide_if_worthy_of_consideration_for_new_hof_effect = yes

		# No stress for faith-guiding.
		ai_chance = {
			base = 0
			opinion_modifier = {
				opinion_target = scope:sunni_caliph
				multiplier = 5
			}
		}
	}
	
	# Realign with Shia orthodoxy.
	option = {
		name = head_of_faith.0031.c
		trigger = { exists = scope:shia_caliph }

		scope:shia_caliph = { save_scope_as = pledge_target }
		decide_if_worthy_of_consideration_for_new_hof_effect = yes

		# No stress for faith-guiding.
		ai_chance = {
			base = 0
			opinion_modifier = {
				opinion_target = scope:shia_caliph
				multiplier = 5
			}
		}
	}

	# Follow scope:old_faith's caliph.
	option = {
		name = head_of_faith.0031.d
		trigger = {
			exists = scope:old_caliph
			valid_prior_caliph_available_trigger = { MS_TYPE = zandaqa }
		}

		scope:old_caliph = { save_scope_as = pledge_target }
		decide_if_worthy_of_consideration_for_new_hof_effect = yes

		# No stress for faith-guiding.
		ai_chance = {
			base = 0
			opinion_modifier = {
				opinion_target = scope:old_caliph
				multiplier = 5
			}
		}
	}
}

head_of_faith.0032 = {
	type = letter_event
	sender = scope:pledgeing_individual
	opening = head_of_faith.0032.t
	desc = head_of_faith.0032.desc

	option = { # Yes, I will be your head
		name = head_of_faith.0032.a
		add_piety = medium_piety_gain

		scope:pledgeing_individual = {
			send_interface_message = {
				type = event_religious_good
				title = head_of_faith.0032.notif.a
				right_icon = scope:pledge_target
			
				set_religious_head_title_to_and_give_hook_effect = { NEW_RELIGIOUS_HEAD_TITLE = scope:pledge_target.faith.religious_head_title }
			}
		}

		ai_chance = { # This should be a rare outcome (HOFs are usually more zealous than rational, so it should help balance it)
			base = 100
			ai_value_modifier = {
				ai_compassion = 0.05
				ai_greed = 0.1
				ai_rationality = 0.5	# Rational HOFs wish to expand their influence
				ai_sociability = 0.05
				ai_zeal = -0.5			# Faithful HOFs dislike the Zandaqa
			}
			opinion_modifier = {		# Opinion plays into it (no need to check with friendship, since you *should* have good op of your friends. Otherwise they are not really your friends!)
				who = root
				opinion_target = scope:pledgeing_individual
				multiplier = 0.2
			}
		}
	}

	option = { # No, go away I don't want to be your hof
		name = head_of_faith.0032.b
		add_piety = medium_piety_loss

		trigger = { NOT = {  scope:pledgeing_individual = { has_strong_hook = scope:pledge_target } } }
		show_as_unavailable = { always = yes }


		scope:pledgeing_individual = {
			send_interface_message = {
				type = event_religious_bad
				title = head_of_faith.0032.notif.b
				right_icon = scope:pledge_target
			
				faith = { remove_religious_head_title = yes }
			}
		}

		ai_chance = { base = 100 }
	}
}
