﻿
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
				exists = capital_province
				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
				exists = capital_province
				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	
			E_kCAFG_convert_faith_in_all_county_provinces = {
				FACTOR = 0.25
				VARIANCE = 0.6
				FAITH = scope:avenger.faith
				MAX = 0.9
			}
		}
	}

	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
			}
		}
	}
}

#### MISC STRUGGLE ENDING(s) STUFF ####
fp3_ending_effects_assertion = {
	# Save Sunni caliph or previous one as scope:struggle_liege
	fp3_save_sunni_caliph_or_previous_effect = yes

	dynasty ?= { add_dynasty_prestige = 10000 }
	
	if = {
		limit = {
			NOT = { title:e_arabia.holder.dynasty ?= dynasty }
		}
		title:e_arabia.holder.dynasty ?= { add_dynasty_prestige = 10000 }
	}

	custom_tooltip = fp3_struggle_assertion_subjugation_expanded_tt
	custom_tooltip = fp3_struggle_assertion_caliphal_muslim_faction_protection_tt

	hidden_effect = {
		destroy_title = title:e_persia
		title:d_sunni = {
			set_variable = {
				name = dar_al_islam
				value = yes
			}
		}
		house = {
			set_variable = {
				name = subjugations_expanded
				value = yes
			}
		}
		title:e_arabia.holder.house = {
			set_variable = {
				name = subjugations_expanded
				value = yes
			}
		}
	}

	struggle:persian_struggle = {
		title:e_arabia.holder = {
			add_prestige = 5000
			custom_tooltip = fp3_struggle_assertion_caliph_detractor_tt
			# Destroy Persia, make each of its kingdoms a de jure part of the Caliph's central title (assuming he still has an empire)
			custom_tooltip = {
				text = fp3_struggle_assertion_arabia_absorb_persia_swing_rewards_tt
				title:e_persia = {
					every_in_de_jure_hierarchy = {
						limit = { tier = tier_kingdom }
						set_de_jure_liege_title = title:e_arabia
					}
				}
			}
			# transfer the supporter trait to the next generation while detractors fade away
			custom_tooltip = {
				text = fp3_struggle_assertion_transfer_supporter_to_next_gen_tt
				struggle:persian_struggle = {
					every_involved_ruler = {
						limit = {
							has_trait = fp3_struggle_supporter
							NOT = {
								primary_title = { is_mercenary_company = yes }
							}
						}
						every_child = {
							if = {
								limit = {
									NOR = {
										has_trait = fp3_struggle_supporter
										primary_title ?= { is_mercenary_company = yes }
									}
								}
								if = {
									limit = { has_trait = fp3_struggle_detractor }
									remove_trait = fp3_struggle_detractor
								}
								add_trait = fp3_struggle_supporter
							}
						}
					}

					every_interloper_ruler = {
						limit = {
							has_trait = fp3_struggle_supporter
							NOT = {
								primary_title = { is_mercenary_company = yes }
							}
						}
						every_child = {
							if = {
								limit = {
									NOR = {
										has_trait = fp3_struggle_supporter
										primary_title ?= { is_mercenary_company = yes }
									}
								}
								if = {
									limit = { has_trait = fp3_struggle_detractor }
									remove_trait = fp3_struggle_detractor
								}
								add_trait = fp3_struggle_supporter
							}
						}
					}
				}
			}
			custom_tooltip = fp3_struggle_persia_ending_assertion_faction_reluctance_tt
		}
		hidden_effect = {
			every_involved_ruler = {
				limit = { has_trait = fp3_struggle_detractor }
				custom = every_struggle_detractor
				title:e_arabia.holder = {
					add_opinion = {
						modifier = caliphal_authority
						target = prev
					}
				}
			}
		}
		# Vassalize supporters
		every_involved_ruler = {
			limit = {
				is_independent_ruler = yes
				OR = {
					has_trait = fp3_struggle_supporter
					scope:struggle_liege = {
						has_hook_of_type = {
							target = prev
							type = caliphal_submission_hook
						}
					}
				}
				NOR = {
					this = scope:struggle_liege
					primary_title = { is_mercenary_company = yes }
				}
			}
			custom = fp3_struggle_assertion_vassalization_rewards_tt
			create_title_and_vassal_change = {
				type = swear_fealty
				save_scope_as = change
				add_claim_on_loss = no
			}
			change_liege = {
				liege = scope:struggle_liege
				change = scope:change
			}
			resolve_title_and_vassal_change = scope:change
		}
		# Involved event
		every_involved_ruler = {
			limit = { NOT = { this = root } }
			trigger_event = fp3_struggle.0072
		}
		# Interloper event
		every_interloper_ruler = {
			limit = { NOT = { this = root } }
			trigger_event = fp3_struggle.0072
		}
	}

	faith = {
		change_fervor = {
			value = 50
			desc = fervor_gain_strengthen_caliphate
		}
		# Propagate the Caliph's Faith in the struggle region
		custom_tooltip = {
			text = fp3_struggle_assertion_spread_faith_through_struggle_region_tt
			struggle:persian_struggle = {
				every_involved_county = {
					limit = {
						NOT = { faith = root.faith }
					}
					E_kCAFG_convert_faith_in_all_county_provinces = {
						FACTOR = fp3_strengthen_caliphate_ending_conversion_chance_decimal_value
						VARIANCE = 0.25
						FAITH = root.faith
						MAX = 0.9
					}
				}

				every_involved_ruler = {
					limit = {
						NOR = {
							faith = root.faith
							primary_title = { is_mercenary_company = yes }
						}
					}
					random = {
						chance = fp3_strengthen_caliphate_ending_conversion_chance_percent_value
						set_character_faith = root.faith
					}
				}
			}
		}
		# Sunni faiths gets a bonus for the rest of the game converting other counties
		custom_tooltip = {
			text = struggle_persia_ending_assertion_boost_sunni_county_conversion_tt
			set_global_variable = {
				name = fp3_struggle_ending_assertion_school
				value = flag:sunni
			}
		}
	}

	if = {
		limit = { is_ai = no }
		add_character_flag = {
			flag = fp3_abassid_strong_achievement_unlocked
			years = 4
		}
	}
}

fp3_struggle_ending_shia_caliphate_effects = {
	### CHARACTER
	give_nickname = nick_the_righteous_caliph

	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_persia = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
		save_scope_as = title_e_persia
	}
	resolve_title_and_vassal_change = scope:title_change

	save_scope_as = founder
	claim_the_imamate_scripted_effect = yes
	add_piety = 5000

	### FAITH
	faith = {
		change_fervor = {
			value = 50
			desc = fervor_gain_shia_caliphate
		}
		if = {
			limit = { has_doctrine = muhammad_succession_muhakkima_doctrine }
			# Muhakkima faiths gets a bonus for the rest of the game converting other counties
			custom_tooltip = {
				text = struggle_persia_ending_assertion_boost_muhakkima_county_conversion_tt
				set_global_variable = {
					name = fp3_struggle_ending_assertion_school
					value = flag:muhakkima
				}
			}
		}
		else_if = {
			limit = { has_doctrine = muhammad_succession_zandaqa_doctrine }
			# Zandaqa faiths gets a bonus for the rest of the game converting other counties
			custom_tooltip = {
				text = struggle_persia_ending_assertion_boost_zandaqa_county_conversion_tt
				set_global_variable = {
					name = fp3_struggle_ending_assertion_school
					value = flag:zandaqa
				}
			}
		}
		else_if = {
			limit = { has_doctrine = muhammad_succession_sunni_doctrine }
			# Sunni faiths gets a bonus for the rest of the game converting other counties
			custom_tooltip = {
				text = struggle_persia_ending_assertion_boost_sunni_county_conversion_tt
				set_global_variable = {
					name = fp3_struggle_ending_assertion_school
					value = flag:sunni
				}
			}
		}
		else = {
			# Shia faiths gets a bonus for the rest of the game converting other counties
			custom_tooltip = {
				text = struggle_persia_ending_assertion_boost_shia_county_conversion_tt
				set_global_variable = {
					name = fp3_struggle_ending_assertion_school
					value = flag:shia
				}
			}
		}
		# Propagate the Caliph's Faith in the struggle region
		custom_tooltip = {
			text = fp3_struggle_ending_shia_caliphate_spread_faith_through_struggle_region_tt
			struggle:persian_struggle = {
				every_involved_county = {
					limit = {
						NOT = { faith = root.faith }
					}
					E_kCAFG_convert_faith_in_all_county_provinces = {
						FACTOR = fp3_shia_caliphate_ending_conversion_chance_decimal_value
						VARIANCE = 0.25
						FAITH = root.faith
						MAX = 0.9
					}
				}
				every_involved_ruler = {
					limit = {
						NOR = {
							faith = root.faith
							primary_title = { is_mercenary_company = yes }
						}
					}
					random = {
						chance = fp3_shia_caliphate_ending_conversion_chance_percent_value
						set_character_faith = root.faith
					}
				}
			}
		}
	}

	if = {
		limit = { is_ai = no }
		add_character_flag = {
			flag = fp3_challenger_caliphate_achievement_unlocked
			years = 4
		}
	}
}

fp3_struggle_ending_vassalize_caliph_effects = {

	# We vassalize the caliph, their vassals might use the confusion to declare independence
	create_title_and_vassal_change = { # Note: do not attempt to simulate faction logic, it is too volatile
		type = usurped
		save_scope_as = change
	}
	title:e_arabia.holder = {
		every_vassal = {
			add_to_list = vassals_to_transfer
		}
		add_to_list = vassals_to_transfer
	}
	title:e_arabia = {
	 	change_title_holder_include_vassals = {
	 		holder = root
			change = scope:change
		}
		save_scope_as = title_e_arabia
	}
	title:d_sunni.holder = {
		change_liege = {
			liege = root
			change = scope:change
		}
	}
	hidden_effect = {
		every_in_list = {
			list = vassals_to_transfer
			limit = {
				NOT = { root = liege }
			}
			change_liege = {
				liege = root
				change = scope:change
			}
		}
	}

	resolve_title_and_vassal_change = scope:change

	dynasty ?= {
		add_dynasty_prestige = 5000
	}

	give_nickname = nick_the_caliph_protector
	
	add_prestige = 5000

	# Unlocks permanent bonuses:
	house = {
		custom_tooltip = fp3_struggle_request_honorary_title_interactions_rewards_tt
		custom_tooltip = fp3_struggle_request_unity_interference_interactions_rewards_tt
	}

	if = {
		limit = { is_ai = no }
		add_character_flag = {
			flag = fp3_vassalize_the_caliph_achievement_unlocked
			years = 4
		}
	}
}

	# Strong independent iranian ruler becomes persian emperor, frees all the iranian subjects and has an easier time vassalizing and or converting them
fp3_struggle_rekindle_iran_effects = {
	give_nickname = nick_the_rekindler_of_iran
	create_title_and_vassal_change = {
		type = created
		save_scope_as = title_change
		add_claim_on_loss = no
	}
	title:e_persia = {
		change_title_holder = {
			holder = root
			change = scope:title_change
		}
		save_scope_as = title_e_persia
	}
	resolve_title_and_vassal_change = scope:title_change
	
	custom_tooltip = fp3_event_troops_reformer_effect_tt
	hidden_effect = {
		spawn_army = {
			name = persian_warriors
			men_at_arms = {
				type = asawira
				stacks = 5
			}
			men_at_arms = {
				type = ayyar
				stacks = 10
			}
			location = this.capital_province
			uses_supply = yes
			inheritable = yes
		}
		spawn_army = {
			name = persian_warriors
			men_at_arms = {
				type = asawira
				stacks = 5
			}
			men_at_arms = {
				type = ayyar
				stacks = 10
			}
			location = this.capital_province
			uses_supply = yes
			inheritable = yes
		}
	}
	add_prestige = 5000

	# increase the religious conversion demand and offer vassalage acceptance for this character
	dynasty = {
		add_dynasty_modifier = { modifier = fp3_rekindler_of_iran_modifier }
		custom_tooltip = fp3_rekindler_of_iran_modifier_vassalization_effect_tt
		custom_tooltip = fp3_rekindler_of_iran_modifier_conversion_effect_tt
		add_dynasty_prestige = 10000
	}

	if = {
		limit = {
			scope:struggle_persia_ending_rekindle_iran_decision_option_secret_faith = yes
		}
		set_character_faith_with_conversion = secret_faith
		hidden_effect = {
			remove_character_secret_faith = yes
		}
	}

	faith = {
		custom_tooltip = {
			text = fp3_iranian_faith_for_rekindle_persia_spread_to_fellow_iranians_tt
			struggle:persian_struggle = {
				every_involved_county = {
					limit = {
						culture = { has_cultural_pillar = heritage_iranian }
						NOT = { faith = scope:propagated_faith }
					}
					E_kCAFG_convert_faith_in_all_county_provinces = {
						FACTOR = fp3_iranian_faith_for_rekindle_persia_conversion_chance_decimal_value
						VARIANCE = 0.25
						FAITH = scope:propagated_faith
						MAX = 0.9
					}
				}
				every_involved_ruler = {
					limit = {
						culture = { has_cultural_pillar = heritage_iranian }
						NOT = { faith = scope:propagated_faith }
						NOT = {
							primary_title = { is_mercenary_company = yes }
						}
					}
					random = {
						chance = fp3_iranian_faith_for_rekindle_persia_conversion_chance_percent_value
						set_character_faith = scope:propagated_faith
					}
				}
			}
		}
	}

	struggle:persian_struggle = {
		every_involved_ruler = {
			limit = { culture = { has_cultural_pillar = heritage_iranian } }
			# Add a character modifiers for the Iranian to help them rise!
			custom = fp3_flame_of_iran_modifier_effect_tt
			add_character_modifier = { modifier = fp3_flame_of_iran_modifier }
			custom_tooltip = fp3_flame_of_iran_independence_effect_tt
			custom_tooltip = fp3_event_troops_effect_tt
			hidden_effect = {
				spawn_army = {
					name = persian_warriors
					men_at_arms = {
						type = asawira
						stacks = 2
					}
					location = this.capital_province
					uses_supply = yes
					inheritable = yes
				}
				spawn_army = {
					name = persian_warriors
					men_at_arms = {
						type = ayyar
						stacks = 4
					}
					location = this.capital_province
					uses_supply = yes
					inheritable = yes
				}
				spawn_army = {
					name = persian_warriors
					men_at_arms = {
						type = tawashi
						stacks = 2
					}
					location = this.capital_province
					uses_supply = yes
					inheritable = yes
				}
				spawn_army = {
					name = persian_warriors
					men_at_arms = {
						type = zupin_warrior
						stacks = 4
					}
					location = this.capital_province
					uses_supply = yes
					inheritable = yes
				}
				spawn_army = {
					name = persian_warriors
					men_at_arms = {
						type = tarkhan
						stacks = 2
					}
					location = this.capital_province
					uses_supply = yes
					inheritable = yes
				}
				if = {
					limit = {
						is_independent_ruler = no
						NOT = {
							liege.culture = { has_cultural_pillar = heritage_iranian }
						}
					}
					create_title_and_vassal_change = {
						type = independency
						save_scope_as = change
						add_claim_on_loss = yes
					}
					becomes_independent = {
						change = scope:change
					}
					resolve_title_and_vassal_change = scope:change
				}
			}
		}
	}

	culture = {
		# increase the cultural acceptance for every iranian culture with the rekindler of iran's culture
		custom_tooltip = {
			text = fp3_rekindle_iran_cultural_acceptance_tt
			every_culture_global = {
				limit = {
					has_cultural_pillar = heritage_iranian
					NOT = { this = root.culture }
				}
				change_cultural_acceptance = {
					target = root.culture
					value = 100
					desc = fp3_rekindle_iran_cultural_acceptance_tt
				}
			}
		}

		custom_tooltip = struggle_persia_ending_rekindle_iran_easier_to_convert_culture_tt
	}

	if = {
		limit = { is_ai = no }
		add_character_flag = {
			flag = fp3_rekindle_iran_achievement_unlocked
			years = 4
		}
	}
}