namespace = fp3_story_cycle_zanj_rebellion_events

### Ali Ibn Muhammad and Zanj Rebellion Setup
fp3_story_cycle_zanj_rebellion_events.0001 = {
	scope = none
	hidden = yes

	immediate = {
		spawn_zanj_leader_character_effect = yes
		scope:zanj_leader = {
			save_scope_as = story_owner
			create_story = story_zanj_rebellion
			give_nickname = nick_albaghid
		}

		title:c_basra.holder = {
			add_visiting_courtier = scope:zanj_leader
		}
		title:c_basra.holder.top_liege = {
			trigger_event = {
				id = fp3_story_cycle_zanj_rebellion_events.0002
			}
			trigger_event = {
				id = fp3_story_cycle_zanj_rebellion_events.0005
				days = { 60 120 }
			}
		}
	}
}

### Ali Ibn Muhammad riling up locals in Basra
fp3_story_cycle_zanj_rebellion_events.0002 = {
	type = character_event
	theme = intrigue
	override_background = { reference = wilderness_wetlands }
	title = fp3_story_cycle_zanj_rebellion_events.0002.t
	desc = fp3_story_cycle_zanj_rebellion_events.0002.desc

	left_portrait = {
		character = root
		animation = marshal
	}
	right_portrait = {
		character = scope:zanj_leader
		animation = scheme
	}

	immediate = {
		scope:zanj_leader = {
			save_scope_as = outspoken_rebel
		}
	}

	#Try to arrest Ali
	option = {
		name = fp3_story_cycle_zanj_rebellion_events.0002.a
		duel = {
			skill = intrigue
			target = scope:outspoken_rebel
			50 = {
				# You arrest Ali Ibn Muhammad
				desc = fp3_story_cycle_zanj_rebellion_events.0002.a.success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				send_interface_toast = {
					title = fp3_story_cycle_zanj_rebellion_events.0002.a.success
					left_icon = root
					right_icon = scope:outspoken_rebel
					rightfully_imprison_character_effect = {
						TARGET = scope:outspoken_rebel
						IMPRISONER = root
					}
				}
				inherit_zanj_rebellion_effect = yes
			}
			50 = {
				# Ali Ibn Muhammad escapes
				desc = fp3_story_cycle_zanj_rebellion_events.0002.a.failure
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				send_interface_toast = {
					title = fp3_story_cycle_zanj_rebellion_events.0002.a.failure
					custom_tooltip = fp3_story_cycle_zanj_rebellion_events_cause_greatly_strengthened_tt
					custom_tooltip = fp3_story_cycle_zanj_rebellion_leaves_for_baghdad
					left_icon = root
					right_icon = scope:outspoken_rebel
				}
				hidden_effect = {
					scope:zanj_leader = {
						change_variable = {
							name =  zanj_rebellion_strength_score
							add = 4
						}
					}
					title:c_baghdad.holder = {
						hidden_effect = {
							title:c_baghdad.holder = {
								add_visiting_courtier = scope:outspoken_rebel
							}
						}
					}
					title:c_basra.holder.top_liege = {
						trigger_event = {
							id = fp3_story_cycle_zanj_rebellion_events.0003
							days = { 30 60 }
						}
					}
				}
			}
		}
		root = {
			progress_towards_rival_effect = {
				REASON = fp3_zanj_rebellion_opressed_me
				CHARACTER = scope:outspoken_rebel
				OPINION = default_rival_opinion
			}
		}
		stress_impact = {
			paranoid = medium_stress_impact_loss
			just = minor_stress_impact_loss
			callous = minor_stress_impact_loss
			sadistic = minor_stress_impact_loss
			trusting = medium_stress_impact_gain
			forgiving = medium_stress_impact_gain
			compassionate = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0.5
				OR = {
					has_trait = trusting
					has_trait = forgiving
					has_trait = compassionate
				}
			}
		}
	}

	#Ignore Ali
	option = {
		name = fp3_story_cycle_zanj_rebellion_events.0002.c
		custom_tooltip = fp3_story_cycle_zanj_rebellion_events_cause_strengthened_tt
		scope:zanj_leader = {
			change_variable = {
				name =  zanj_rebellion_strength_score
				add = 2
			}
		}
		custom_tooltip = fp3_story_cycle_zanj_rebellion_leaves_for_baghdad
		hidden_effect = {
			title:c_baghdad.holder = {
				add_visiting_courtier = scope:outspoken_rebel
			}
		}
		title:c_basra.holder.top_liege = {
			trigger_event = {
				id = fp3_story_cycle_zanj_rebellion_events.0003
				days = { 30 60 }
			}
		}
		stress_impact = {
			paranoid = medium_stress_impact_gain
			just = minor_stress_impact_gain
			callous = minor_stress_impact_gain
			sadistic = medium_stress_impact_gain
			trusting = medium_stress_impact_loss
			forgiving = medium_stress_impact_loss
			compassionate = medium_stress_impact_loss
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0.5
				OR = {
					has_trait = paranoid
					has_trait = just
					has_trait = callous
					has_trait = sadistic
				}
			}
		}
	}
}

### Setup to find correct scope for event 0004 and check if Ali is dead or imprisoned
fp3_story_cycle_zanj_rebellion_events.0003 = {
	scope = none
	hidden = yes

	immediate = {
		if ={
			#check if Ali is still alive and not in prison and is not an inheritor of the rebellion
			limit = {
				scope:zanj_leader = {
					AND = {
						is_alive = yes
						is_imprisoned = no
						NOT = { has_character_flag = new_zanj_leader }
					}
				}
			}
			title:c_baghdad.holder.top_liege = {
				trigger_event = {
					id = fp3_story_cycle_zanj_rebellion_events.0004
				}
			}
		}
	}
}

### Ali Ibn Muhammad claims divinity
fp3_story_cycle_zanj_rebellion_events.0004 = {
	type = character_event
	theme = faith
	title = fp3_story_cycle_zanj_rebellion_events.0004.t
	desc = fp3_story_cycle_zanj_rebellion_events.0004.desc

	left_portrait = {
		character = root
		animation = dismissal
	}
	right_portrait = {
		character = scope:zanj_leader
		animation = personality_zealous
	}

	#Contest Ali Ibn Muhammad's claims
	option = {
		name = fp3_story_cycle_zanj_rebellion_events.0004.a
		duel = {
			skill = learning
			target = scope:zanj_leader
			50 = {
				# You disprove Ali's claims
				desc = fp3_story_cycle_zanj_rebellion_events.0004.a.success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3.5
					min = -49
				}
				send_interface_toast = {
					title = fp3_story_cycle_zanj_rebellion_events.0004.a.success
					add_piety = medium_piety_gain
					add_prestige = minor_prestige_gain
					left_icon = root
					right_icon = scope:zanj_leader
				}
			}
			50 = {
				# Ali Ibn Muhammad validates his claims
				desc = fp3_story_cycle_zanj_rebellion_events.0004.a.failure
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3.5
					min = -49
				}
				custom_tooltip = fp3_story_cycle_zanj_rebellion_events_cause_greatly_strengthened_tt
				send_interface_toast = {
					title = fp3_story_cycle_zanj_rebellion_events.0004.a.failure
					left_icon = root
					right_icon = scope:zanj_leader

					scope:zanj_leader = {
						add_trait = sayyid
						add_pressed_claim = title:e_arabia
						change_variable = {
							name =  zanj_rebellion_strength_score
							add = 4
						}
					}
				}
			}
		}
		root = {
			progress_towards_rival_effect = {
				REASON = fp3_zanj_rebellion_opressed_me
				CHARACTER = scope:zanj_leader
				OPINION = default_rival_opinion
			}
		}
		stress_impact = {
			paranoid = minor_stress_impact_loss
			just = minor_stress_impact_loss
			zealous = medium_stress_impact_loss
			trusting = medium_stress_impact_gain
			forgiving = medium_stress_impact_gain
			cynical = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0.5
				OR = {
					has_trait = cynical
					has_trait = trusting
					has_trait = forgiving
				}
			}
		}
	}

	#Ignore Ali
	option = {
		name = fp3_story_cycle_zanj_rebellion_events.0004.b
		custom_tooltip = fp3_story_cycle_zanj_rebellion_events_cause_strengthened_tt

		scope:zanj_leader = {
			add_trait = sayyid
			add_pressed_claim = title:e_arabia
			change_variable = {
				name =  zanj_rebellion_strength_score
				add = 2
			}
		}
		stress_impact = {
			paranoid = minor_stress_impact_gain
			just = minor_stress_impact_gain
			zealous = medium_stress_impact_gain
			trusting = minor_stress_impact_loss
			forgiving = medium_stress_impact_loss
			cynical = medium_stress_impact_loss
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0.5
				OR = {
					has_trait = paranoid
					has_trait = just
					has_trait = zealous
				}
			}
		}
	}
}

### Setup event to find the correct scope and check if Ali or the current Zanj Leader is dead or imprisoned
fp3_story_cycle_zanj_rebellion_events.0005 = {
	scope = none
	hidden = yes
	immediate = {
		if = {
			limit = {
				scope:zanj_leader = {
					OR = {
						is_imprisoned = yes
						is_alive = no
						is_landed = yes
					}
				}
			}
			inherit_zanj_rebellion_effect = yes
		}
		title:c_basra.holder.top_liege = {
			trigger_event = {
				id = fp3_story_cycle_zanj_rebellion_events.0006
			}
		}
	}
}

### A Zanj Steps Forth
fp3_story_cycle_zanj_rebellion_events.0006 = {
	type = character_event
	theme = corruption
	title = fp3_story_cycle_zanj_rebellion_events.0006.t
	desc = {
		desc = fp3_story_cycle_zanj_rebellion_events.0006.desc.a

		triggered_desc = {
			trigger = { scope:zanj_leader = { NOT =  { has_character_flag = new_zanj_leader } } }
			desc = fp3_story_cycle_zanj_rebellion_events.0006.desc.b
		}

		desc = fp3_story_cycle_zanj_rebellion_events.0006.desc.c
	}

	left_portrait = {
		character = root
		animation = eyeroll
	}

	right_portrait = {
		character = scope:outspoken_rebel
		animation = worry
	}

	lower_left_portrait = {
		trigger = { scope:zanj_leader = { NOT =  { has_character_flag = new_zanj_leader } } }
		character = scope:zanj_leader
	}

	immediate = {
		if = {
			limit = { scope:zanj_leader = { NOT =  { has_character_flag = new_zanj_leader } } }
			spawn_zanj_representative_character_effect = yes
			scope:zanj_representative = { save_scope_as = outspoken_rebel }
		}
		else = {
			scope:zanj_leader = { save_scope_as = outspoken_rebel }
		}
	}
	#Give Basra to the Zanj
	option = {
		name = fp3_story_cycle_zanj_rebellion_events.0006.a

		create_title_and_vassal_change = {
			type = independency
			save_scope_as = change
			add_claim_on_loss = no
		}

		title:c_basra = {
			set_county_culture = culture:east_bantu
			set_county_faith = faith:azariqa
		}

		show_as_tooltip = { # UI/UX
			title:c_basra = {
				change_title_holder = {
					holder = scope:outspoken_rebel
					change = scope:change
				}
			}
		}

		scope:outspoken_rebel = {
			becomes_independent = {
				change = scope:change
			}
			add_truce_both_ways = {
				character = root
				years = 10
				name = fp3_landed_zanj_reason
			}
		}

		if = {
			limit = {
				NOT = { title:c_basra.holder = root  }
			}
			add_tyranny = 30
			set_relation_rival = {
				reason = fp3_rival_gave_title_away
				target = title:c_basra.holder
			}
		}

		hidden_effect = {
			title:c_basra = {
				change_title_holder = {
					holder = scope:outspoken_rebel
					change = scope:change
				}
				every_de_jure_county = {
					change_title_holder = {
						holder = scope:outspoken_rebel
						change = scope:change
					}
				}
			}
			scope:zanj_leader = {
				random_owned_story = {
					type = story_zanj_rebellion
					end_story = yes
				}
			}
		}
		resolve_title_and_vassal_change = scope:change

		stress_impact = {
			compassionate = medium_stress_impact_loss
			just = minor_stress_impact_loss
			forgiving = minor_stress_impact_loss
			generous = medium_stress_impact_loss
			content = medium_stress_impact_loss
			callous = medium_stress_impact_gain
			sadistic = major_stress_impact_gain
			ambitious = medium_stress_impact_gain
			greedy = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0.5
				OR = {
					has_trait = callous
					has_trait = sadistic
					has_trait = ambitious
					has_trait = greedy
				}
			}
		}
	}

	#Make minor concessions to the Zanj
	option = {
		name = fp3_story_cycle_zanj_rebellion_events.0006.b

		scope:zanj_leader = {
			change_variable = {
				name =  zanj_rebellion_strength_score
				add = 3
			}
		}
		custom_tooltip = fp3_story_cycle_zanj_rebellion_events_cause_strengthened_tt
		hidden_effect = {
			title:c_basra.holder = {
				if = {
					limit = { scope:zanj_leader != scope:outspoken_rebel}
					add_visiting_courtier = scope:zanj_leader
				}
				add_visiting_courtier = scope:outspoken_rebel
			}
		}

		if = {
			limit = {
				NOT = { title:c_basra.holder = root  }
			}
			progress_towards_rival_effect = {
				REASON = fp3_zanj_rebellion_interfered_with_my_workers
				CHARACTER = title:c_basra.holder
				OPINION = default_rival_opinion
			}
		}

		title:c_basra = {
			add_county_modifier = {
				modifier = fp3_zanj_rebellion_work_concessions_modifier
				years = 5
			}
		}

		trigger_event = {
			id = fp3_story_cycle_zanj_rebellion_events.0007
			days = { 30 60 }
		}
		stress_impact = {
			compassionate = minor_stress_impact_loss
			just = minor_stress_impact_loss
			forgiving = minor_stress_impact_loss
			generous = minor_stress_impact_loss
			content = minor_stress_impact_loss
			callous = minor_stress_impact_gain
			sadistic = medium_stress_impact_gain
			ambitious = minor_stress_impact_gain
			greedy = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0.5
				OR = {
					has_trait = callous
					has_trait = sadistic
					has_trait = ambitious
					has_trait = greedy
				}
			}
		}
	}

	#Ignore the Zanj
	option = {
		name = fp3_story_cycle_zanj_rebellion_events.0006.c

		scope:zanj_leader = {
			change_variable = {
				name =  zanj_rebellion_strength_score
				add = 5
			}
		}
		custom_tooltip = fp3_story_cycle_zanj_rebellion_events_cause_greatly_strengthened_tt
		hidden_effect = {
			title:c_basra.holder = {
				add_visiting_courtier = scope:zanj_leader
				add_visiting_courtier = scope:zanj_representative
			}
		}

		if = {
			limit = {
				NOT = { title:c_basra.holder = root  }
			}

			progress_towards_friend_effect = {
				REASON = fp3_rival_stayed_out_of_my_business
				CHARACTER = title:c_basra.holder
				OPINION = default_friend_opinion
			}
		}

		trigger_event = {
			id = fp3_story_cycle_zanj_rebellion_events.0007
			days = { 30 60 }
		}

		stress_impact = {
			compassionate = minor_stress_impact_gain
			just = minor_stress_impact_gain
			forgiving = minor_stress_impact_gain
			generous = minor_stress_impact_gain
			content = minor_stress_impact_gain
			callous = minor_stress_impact_loss
			sadistic = medium_stress_impact_loss
			ambitious = minor_stress_impact_loss
			greedy = minor_stress_impact_loss
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0.5
				OR = {
					has_trait = compassionate
					has_trait = just
					has_trait = forgiving
					has_trait = generous
				}
			}
		}
	}
}

fp3_story_cycle_zanj_rebellion_events.0007 = {
	scope = none
	hidden = yes
	immediate = {
		if = {
			limit = {
				scope:zanj_leader = {
					OR = {
						is_imprisoned = yes
						is_alive = no
						is_landed = yes
					}
				}
			}
			inherit_zanj_rebellion_effect = yes
		}
		title:c_basra.holder.top_liege = {
			trigger_event = {
				id = fp3_story_cycle_zanj_rebellion_events.0008

			}
		}
	}
}

### The Zanj Rebellion
fp3_story_cycle_zanj_rebellion_events.0008 = {
	type = character_event
	theme = battle
	override_background = { reference = wilderness_wetlands }
	title = fp3_story_cycle_zanj_rebellion_events.0008.t
	desc = {
		desc = fp3_story_cycle_zanj_rebellion_events.0008.desc.a
		triggered_desc = {
			trigger = {
				scope:zanj_leader = { NOT = { has_character_flag = new_zanj_leader}}
			}
			desc = fp3_story_cycle_zanj_rebellion_events.0008.desc.b
		}
		triggered_desc = {
			trigger = {
				scope:zanj_leader = { has_character_flag = new_zanj_leader }
			}
			desc = fp3_story_cycle_zanj_rebellion_events.0008.desc.c
		}
		desc = fp3_story_cycle_zanj_rebellion_events.0008.desc.d
	}

	left_portrait = {
		character = root
		animation = shock
	}
	right_portrait = {
		character = scope:zanj_leader
		animation = celebrate_sword
	}

	immediate = {
		scope:zanj_leader = {
			give_nickname = nick_chief_of_the_zanj
			hidden_effect = {
				random_owned_story = {
					type = story_zanj_rebellion
					end_story = yes
				}
			}
		}
		hidden_effect = {
			fp3_start_zanj_rebellion_effect = {
				TARGET_COUNTY = title:c_basra
				REBEL_LEADER = scope:zanj_leader
			}
			create_zanj_courtier_effect = {
				EMPLOYER = scope:zanj_leader
				LOCATION = scope:zanj_leader.location
			}
			create_zanj_courtier_effect = {
				EMPLOYER = scope:zanj_leader
				LOCATION = scope:zanj_leader.location
			}
			create_zanj_courtier_effect = {
				EMPLOYER = scope:zanj_leader
				LOCATION = scope:zanj_leader.location
			}
			create_zanj_courtier_effect = {
				EMPLOYER = scope:zanj_leader
				LOCATION = scope:zanj_leader.location
			}
		}
	}

	option = {
		name = fp3_story_cycle_zanj_rebellion_events.0008.a
	}
}