﻿claim_the_imamate_scripted_effect = {
	# Create a HoF title & assign it appropriately.
	if = {
		limit = {
			faith.religious_head != root
		}
		hidden_effect = {
			# Are we Nizari?
			if = {
				limit = { faith = faith:nizari }
				# Transfer the title.
				create_title_and_vassal_change = {
					type = created
					save_scope_as = change
					add_claim_on_loss = no
				}
				title:d_nizari = {
					change_title_holder = {
						holder = scope:founder
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
				faith = { set_religious_head_title = title:d_nizari }

				# Set up the remaining bits'n'bobs.
				if = {
					limit = {
						NOT = { has_realm_law = same_faith_succession_law }
					}
					add_realm_law_skip_effects = same_faith_succession_law
				}
				title:d_nizari = { add_title_law = temporal_head_of_faith_succession_law }
				# Make it the HoF head.
			}
			# Are we Zayidi?
			else_if = {
				limit = { faith = faith:zayidi }
				# Transfer the title.
				create_title_and_vassal_change = {
					type = created
					save_scope_as = change
					add_claim_on_loss = no
				}
				title:d_zayidi = {
					change_title_holder = {
						holder = scope:founder
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
				faith = { set_religious_head_title = title:d_zayidi }

				# Set up the remaining bits'n'bobs.
				if = {
					limit = {
						NOT = { has_realm_law = same_faith_succession_law }
					}
					add_realm_law_skip_effects = same_faith_succession_law
				}
				title:d_zayidi = { add_title_law = temporal_head_of_faith_succession_law }
				# Make it the HoF head.
			}
			# Are we Twelvers?
			else_if = {
				limit = { faith = faith:imami }
				# Transfer the title.
				create_title_and_vassal_change = {
					type = created
					save_scope_as = change
					add_claim_on_loss = no
				}
				title:d_imami = {
					change_title_holder = {
						holder = scope:founder
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
				faith = { set_religious_head_title = title:d_imami }

				# Set up the remaining bits'n'bobs.
				if = {
					limit = {
						NOT = { has_realm_law = same_faith_succession_law }
					}
					add_realm_law_skip_effects = same_faith_succession_law
				}
				title:d_imami = { add_title_law = temporal_head_of_faith_succession_law }
				# Make it the HoF head.
			}
			# Are we Ismailis?
			else_if = {
				limit = { faith = faith:ismaili }
				# Transfer the title.
				create_title_and_vassal_change = {
					type = created
					save_scope_as = change
					add_claim_on_loss = no
				}
				title:d_ismaili = {
					change_title_holder = {
						holder = scope:founder
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
				faith = { set_religious_head_title = title:d_ismaili }

				# Set up the remaining bits'n'bobs.
				if = {
					limit = {
						NOT = { has_realm_law = same_faith_succession_law }
					}
					add_realm_law_skip_effects = same_faith_succession_law
				}
				title:d_ismaili = { add_title_law = temporal_head_of_faith_succession_law }
				# Make it the HoF head.
			}
			# If not, follow the usual flow.
			else = {
				set_up_dynamic_temporal_hof_title_effect = { NEW_HOLDER = scope:founder }
				faith = { set_religious_head_title = scope:new_title }
			}
		}
		custom_tooltip = appoint_a_righteous_caliph_decision.tt.you_become_caliph
	}
	dynasty = {
		add_dynasty_modifier = {
			modifier = fp3_light_of_the_walayah_modifier
			years = 100
		}
		# Also gain a large amount of dynasty prestige.
		add_dynasty_prestige = 5000
	}
}

avenge_the_battle_of_nahrawan_scripted_effect = {
		
	give_nickname = nick_the_avenger
	add_character_modifier = {
		modifier = fp3_azariqa_ascendancy_modifier 
	}

	add_prestige = massive_prestige_gain

	#Religious bonuses
	add_piety = major_piety_gain
	faith = {
		change_fervor = {
			value = medium_fervor_gain
			desc = fervor_gain_avenged_battle_of_nahrawan
		}
	}

	if = {
		limit = {
			any_vassal_or_below = {
				NOT = { 
					faith = { has_doctrine = muhammad_succession_muhakkima_doctrine }
				}
				is_ai = yes
				capital_province ?= { 
					OR = {
						geographical_region = world_middle_east
						geographical_region = world_persian_empire 
					}
				}
			}
		}
		every_vassal_or_below = {
			custom = every_non_muhakkima_vassal_middle_east
			limit = {
				NOT = { 
					faith = { has_doctrine = muhammad_succession_muhakkima_doctrine }
				}
				is_ai = yes
				capital_province ?= { 
					OR = {
						geographical_region = world_middle_east
						geographical_region = world_persian_empire 
					}
				}
			}
			random = {
				chance = 25
				set_character_faith = scope:avenger.faith
			}
		}
	}

	if = {
		limit = {
			any_realm_province = {
				NOT = { 
					faith = { has_doctrine = muhammad_succession_muhakkima_doctrine }
				}
				OR = {
					geographical_region = world_middle_east
					geographical_region = world_persian_empire 
				}
			}
		}
		every_realm_province = {
			limit = {
				NOT = { 
					faith = { has_doctrine = muhammad_succession_muhakkima_doctrine }
				}
				OR = {
					geographical_region = world_middle_east
					geographical_region = world_persian_empire 
				}
			}
			county = {
				add_to_list = every_realm_county
			}	
		}

		every_in_list = {
			list = every_realm_county
			custom = every_non_muhakkima_faith_county_middle_east	
			random = {
				chance = 25
				custom_tooltip = every_non_avenger_faith_convert
				hidden_effect = { set_county_faith = scope:avenger.faith }
			}
		}
	}

	if = {
		limit = { exists = scope:sunni_faith }
		scope:sunni_faith = {
			change_fervor = {
				value = medium_fervor_loss
				desc = fervor_loss_avenged_battle_of_nahrawan
			}
		}
	}
	if = {
		limit = { exists = scope:shia_faith }
		scope:shia_faith = {
			change_fervor = {
				value = medium_fervor_loss
				desc = fervor_loss_avenged_battle_of_nahrawan
			}
		}
	}
}

promote_persian_scripture_scripted_effect = {

	if = { 
		limit = { 
			exists = faith.religious_head 
			this != faith.religious_head
		}
		custom_tooltip = fp3_promoted_scriptures_caliph_angry.tt
	}

	add_character_modifier = {
		modifier = fp3_promoted_persian_scripture_modifier
	}
	
	if = {
		limit = { is_ai = no } #this is so that we don't get AI converting Persia too quickly
		custom_tooltip = fp3_promoted_scriptures_conversion_bonus.tt
	}

	if = {
		limit = {
			faith = { has_doctrine = doctrine_pluralism_fundamentalist }
			NOT = {
				has_trait = cynical 
				has_trait = excommunicated
			}
		}
		custom_tooltip = {
			text = promote_persian_scripture_fundamentalist_stress
			add_stress = promote_persian_scripture_fundamentalist_stress_value
		}
	}

	if = {
		limit = {
			any_realm_province = { 
				culture = { has_cultural_pillar = heritage_iranian }
				OR = {
					geographical_region = world_steppe_tarim
					geographical_region = world_persian_empire 
				}
			}
		}
		every_realm_province = {
			limit = {
					culture = { has_cultural_pillar = heritage_iranian }
				OR = {
					geographical_region = world_steppe_tarim
					geographical_region = world_persian_empire 
				}
			}
			county = {
				add_to_list = every_realm_county
			}	
		}

		every_in_list = {
			list = every_realm_county
			custom = every_iranian_county
			random = {
				chance = 33
				add_county_modifier = {
					modifier = fp3_persian_resurgence_county_modifier 
					years = 50
				}
			}
		}
	}

	if = {
		limit = {
			any_vassal_or_below = {
				is_ai = yes
				OR = {
					culture = { has_cultural_pillar = heritage_arabic }
					AND = {
						knows_language_of_culture = culture:levantine
						OR = {
							has_trait = zealous
							has_trait = stubborn
							has_trait = humble
						}
					}
				}
			}
		}
		every_vassal_or_below = {
			custom = every_contrarian_vassal
			limit = {
				is_ai = yes
				OR = {
					culture = { has_cultural_pillar = heritage_arabic }
					AND = {
						knows_language_of_culture = culture:levantine
						OR = {
							has_trait = zealous
							has_trait = stubborn
							has_trait = humble
						}
					}
				}
			}
			add_opinion = {
				target = scope:persian_fanboy
				modifier = fp3_disrespected_word_of_god_opinion
				opinion = -30
			}
		}
	}	
}

read_name_in_friday_prayer_guts_become_supporter_effect = {
	hidden_effect = { remove_trait = fp3_struggle_detractor }
	add_trait_force_tooltip = fp3_struggle_supporter
	add_character_modifier = {
		modifier = fp3_displayed_pious_submission_to_caliph_modifier
		years = 20
	}
}

read_name_in_friday_prayer_guts_become_detractor_effect = {
	hidden_effect = { remove_trait = fp3_struggle_supporter }
	add_trait_force_tooltip = fp3_struggle_detractor
	add_character_modifier = {
		modifier = fp3_name_read_in_friday_prayer_modifier
		years = 20
	}
}

read_name_in_friday_prayer_guts_apply_sundry_changes_effect = {
	# More like notoriety, but potato-potatoe.
	if = {
		limit = {
			NOT = { exists = scope:prestige_gained }
		}
		add_prestige = medium_prestige_gain
	}
	# Are we getting an extra surcharge of stress?
	if = {
		limit = { fp3_name_read_in_friday_prayer_decision_liege_requirements_trigger = no }
		add_stress = major_stress_gain
	}
	# Plus check who the current caliph is so we can see who you swore to.
	set_variable = {
		name = last_explicit_friday_prayer_caliph
		value = title:d_sunni.holder
	}
}

read_name_in_friday_prayer_guts_apply_house_unity_changes_effect = {
	# Twiddle with house unity a smidge.
	## We need to invert the triggers before the decision is taken, so we add a flag that'll only be applied when the effect runs.
	add_character_flag = fp3_friday_prayer_twiddling_house_unity_force_correct_ui
	## Ok, now, what's the UI look like before the decision is taken?
	### Moving out of sync.
	if = {
		limit = {
			NOT = { has_character_flag = fp3_friday_prayer_twiddling_house_unity_force_correct_ui }
			exists = house.house_head
			this != house.house_head
			OR = {
				AND = {
					has_trait = fp3_struggle_supporter
					house.house_head = { has_trait = fp3_struggle_supporter }
				}
				AND = {
					has_trait = fp3_struggle_detractor
					house.house_head = { has_trait = fp3_struggle_detractor }
				}
			}
		}
		add_clan_unity_interaction_effect = {
			CHARACTER = root
			TARGET = root.house.house_head
			VALUE = major_unity_loss
			DESC = clan_unity_name_read_in_friday_prayer_rebellious.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
	### Moving into sync.
	else_if = {
		limit = {
			NOT = { has_character_flag = fp3_friday_prayer_twiddling_house_unity_force_correct_ui }
			exists = house.house_head
			this != house.house_head
			OR = {
				AND = {
					has_trait = fp3_struggle_detractor
					house.house_head = { has_trait = fp3_struggle_supporter }
				}
				AND = {
					has_trait = fp3_struggle_supporter
					house.house_head = { has_trait = fp3_struggle_detractor }
				}
			}
		}
		add_clan_unity_interaction_effect = {
			CHARACTER = root
			TARGET = root.house.house_head
			VALUE = medium_unity_gain
			DESC = clan_unity_name_read_in_friday_prayer.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
	## And what's it look like after?
	### Moving into sync.
	if = {
		limit = {
			has_character_flag = fp3_friday_prayer_twiddling_house_unity_force_correct_ui
			exists = house.house_head
			this != house.house_head
			OR = {
				AND = {
					has_trait = fp3_struggle_supporter
					house.house_head = { has_trait = fp3_struggle_supporter }
				}
				AND = {
					has_trait = fp3_struggle_detractor
					house.house_head = { has_trait = fp3_struggle_detractor }
				}
			}
		}
		add_clan_unity_interaction_effect = {
			CHARACTER = root
			TARGET = root.house.house_head
			VALUE = medium_unity_gain
			DESC = clan_unity_name_read_in_friday_prayer.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
	### Moving out of sync.
	else_if = {
		limit = {
			has_character_flag = fp3_friday_prayer_twiddling_house_unity_force_correct_ui
			exists = house.house_head
			this != house.house_head
			OR = {
				AND = {
					has_trait = fp3_struggle_detractor
					house.house_head = { has_trait = fp3_struggle_supporter }
				}
				AND = {
					has_trait = fp3_struggle_supporter
					house.house_head = { has_trait = fp3_struggle_detractor }
				}
			}
		}
		add_clan_unity_interaction_effect = {
			CHARACTER = root
			TARGET = root.house.house_head
			VALUE = major_unity_loss
			DESC = clan_unity_name_read_in_friday_prayer_rebellious.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}
	## Aaaaand remove the very legitimate and not at all hacky flag we used to trick the tooltips into behaving.
	remove_character_flag = fp3_friday_prayer_twiddling_house_unity_force_correct_ui
}

read_name_in_friday_prayer_scripted_effect = {
	# Apply the appropriate modifier.
	## Neutrals pick in the event.
	if = {
		limit = {
			NOR = {
				has_trait = fp3_struggle_supporter
				has_trait = fp3_struggle_detractor
			}
		}
		show_as_tooltip = {
			random_list = {
				# Become a supporter.
				100 = {
					show_chance = no
					desc = fp3_name_read_in_friday_prayer_decision.tt.become_supporter
					read_name_in_friday_prayer_guts_become_supporter_effect = yes
				}
				# Become a detractor.
				100 = {
					show_chance = no
					desc = fp3_name_read_in_friday_prayer_decision.tt.become_detractor
					read_name_in_friday_prayer_guts_become_detractor_effect = yes
				}
			}
		}
	}
	## Supporters become Detractors.
	else_if = {
		limit = { has_trait = fp3_struggle_supporter }
		read_name_in_friday_prayer_guts_become_detractor_effect = yes
		struggle:persian_struggle = {
			every_involved_ruler = {
				custom = custom.every_opposed_involved_struggle_ruler.supporter
				limit = {
					this != root
					has_trait = fp3_struggle_supporter
				}
				add_opinion = {
					target = root
					modifier = turncoat_opinion
					opinion = -40
				}
			}
			every_interloper_ruler = {
				custom = custom.every_opposed_interloper_struggle_ruler.supporter
				limit = {
					this != root
					has_trait = fp3_struggle_supporter
				}
				add_opinion = {
					target = root
					modifier = turncoat_opinion
					opinion = -20
				}
			}
		}
	}
	## Detractors become Supporters.
	else_if = {
		limit = { has_trait = fp3_struggle_detractor }
		read_name_in_friday_prayer_guts_become_supporter_effect = yes
		struggle:persian_struggle = {
			every_involved_ruler = {
				custom = custom.every_opposed_involved_struggle_ruler.detractor
				limit = {
					this != root
					has_trait = fp3_struggle_detractor
				}
				add_opinion = {
					target = root
					modifier = turncoat_opinion
					opinion = -40
				}
			}
			every_interloper_ruler = {
				custom = custom.every_opposed_interloper_struggle_ruler.detractor
				limit = {
					this != root
					has_trait = fp3_struggle_detractor
				}
				add_opinion = {
					target = root
					modifier = turncoat_opinion
					opinion = -20
				}
			}
		}
	}
	# Finish things off.
	read_name_in_friday_prayer_guts_apply_sundry_changes_effect = yes
	read_name_in_friday_prayer_guts_apply_house_unity_changes_effect = yes
}

favour_skilled_outsiders_decision_apply_opinions_effect = {
	# Courtly vassals.
	every_vassal = {
		custom = favour_skilled_outsiders_decision.tt.upset_elites.courtly_non_heritage
		limit = { favour_skilled_outsiders_decision_grab_courtlies_trigger = yes }
		add_opinion = {
			target = root
			modifier = upset_elites_opinion
			opinion = -40
		}
	}
	# Landless councillors.
	every_councillor = {
		custom = favour_skilled_outsiders_decision.tt.upset_elites.landless_councillor_non_heritage
		limit = { favour_skilled_outsiders_decision_grab_landless_councillors_trigger = yes }
		add_opinion = {
			target = root
			modifier = upset_elites_opinion
			opinion = -30
		}
	}
	# Remaining vassals.
	every_vassal = {
		custom = favour_skilled_outsiders_decision.tt.upset_elites.non_courtly_non_heritage
		limit = { favour_skilled_outsiders_decision_non_courtlies_trigger = yes }
		add_opinion = {
			target = root
			modifier = upset_elites_opinion
			opinion = -20
		}
	}
}

fp3_story_cycle_seljuks_invasion_events_grant_seljuks_land_effect = {
	#to display proper reason in title history, as effect is used in both event and war cb
	if = {
		limit = {
			exists = scope:war
		}
		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
		}
	}
	else = {
		create_title_and_vassal_change = {
			type = granted
			save_scope_as = change
		}
	}

	show_as_tooltip = { # UI/UX
		scope:fp3_seljuk_settle_target = {
			change_title_holder = {
				holder = scope:lead_seljuk
				change = scope:change
			}
		}
	}
	hidden_effect = {
		scope:fp3_seljuk_settle_target = {
			change_title_holder = {
				holder = scope:lead_seljuk
				change = scope:change
			}
			every_de_jure_county = {
				change_title_holder = {
					holder = scope:lead_seljuk
					change = scope:change
				}
			}
		}
	}
	if = {
		limit = {
			highest_held_title_tier > tier_duchy

		}
		send_interface_toast = {
			type = event_toast_effect_good
			title = east_europe.0030.t
			left_icon = scope:lead_seljuk
			right_icon = scope:fp3_seljuk_settle_target
			
			scope:lead_seljuk = {
				change_liege = {
					liege = scope:seljuk_victim
					change = scope:change
				}
			}
		}
	}


	resolve_title_and_vassal_change = scope:change

	# Give them an Alliance
	scope:lead_seljuk = {
		save_scope_as = recipient # for loc purposes
		add_opinion = {
			target = scope:seljuk_victim
			modifier = event_negotiated_alliance_opinion
		}
	}
	custom_tooltip = negotiate_alliance_effect_tooltip
	create_alliance = {
		target = scope:lead_seljuk
		allied_through_owner = scope:seljuk_victim
		allied_through_target = scope:lead_seljuk
	}

	# Housekeeping for the lead seljuk fella
	scope:lead_seljuk = {
		add_character_flag = settled_seljuk
		change_government = clan_government
	}
}

set_house_head_effect = {
	$NEW_HEAD$ = { save_scope_as = new_head }
	$OLD_HEAD$ = {
		save_scope_as = old_head
		if = {
			limit = {
				any_held_title = { is_noble_family_title = yes }
			}
			random_held_title = {
				limit = { is_noble_family_title = yes }
				save_scope_as = nf_title
			}
		}
	}
	create_title_and_vassal_change = {
		type = granted
		save_scope_as = change
		add_claim_on_loss = no
	}
	scope:nf_title = {
		change_title_holder_include_vassals = {
			holder = scope:new_head
			change = scope:change
		}
	}
	resolve_title_and_vassal_change = scope:change
	scope:new_head.house = { set_house_head = scope:new_head }
}
