#####################################################################
# EFFECT LIST
#####################################################################
# !!! Remember to add all new effects with a short description here !!!

### Reward effects

# everpreg_pilgrimage_additional_reward_effect -- Gives additional rewards for going on everpreg_pilgrimage with certain tenets
# short_everpreg_pilgrimage_reward_effect -- Short everpreg_pilgrimage reward
# medium_everpreg_pilgrimage_reward_effect -- Medium everpreg_pilgrimage reward
# long_everpreg_pilgrimage_reward_effect -- Long everpreg_pilgrimage reward
# very_long_everpreg_pilgrimage_reward_effect -- Very Long everpreg_pilgrimage reward
# give_everpreg_pilgrimage_reward_effect -- Picks the correct everpreg_pilgrimage reward effect to grant


### Setup effects

# select_everpreg_pilgrimage_effect -- Run when choosing Holy Site destination
# calculate_everpreg_pilgrimage_distance_effect -- Categorise the distance to a given Holy Site as short, medium, long or very long (See everpreg_pilgrimage_values.txt)
# set_everpreg_pilgrimage_distance_variable_effect -- Set variables tracking the length of the everpreg_pilgrimage
# set_up_holy_sites_for_selection_effect -- Save every potential Holy Site as a saved scope for use in selection events
# holy_site_selection_event_option_effect -- Run when actually selecting a Holy Site


### Event effects

# set_up_next_everpreg_pilgrimage_event_effect -- Makes sure the next everpreg_pilgrimage event fires correctly. Use in the `after` of all Journey events (unless making a custom chain, in which case only the last event must have it)
# save_holy_site_for_loc_effect -- Used in immediate blocks to get the destination holy site saved as loc
# abandon_everpreg_pilgrimage_early_effect -- Effect used to end a everpreg_pilgrimage before arriving at the destination

#####################################################################
# EFFECTS
#####################################################################

# Extra reward effects for specific tenets
everpreg_pilgrimage_additional_reward_effect = {
	# Everpreg_Esotericism
	if = {
		limit = {
			faith = { has_doctrine = tenet_everpreg_holywomb }
#			NOT = { has_trait = mystic_3 }
			is_female = yes
		}
		custom_description_no_bullet = {
			text = everpreg_pilgrimage_because_i_have_tenet_esotericism
			subject = root
		}
		random_list = {
			desc = everpreg_pilgrimage_learn_from_the_bishops
			100 = {
				desc = everpreg_pilgrimage_learn_from_the_bishops_success

					#Chance of gaining hpreg if your religion or educator's religion is hpreg esoteric equiv
					if = {
						limit = {
							faith = {
								has_doctrine_parameter = tenet_everpreg_holywomb
							}
							NOR = {
								has_trait = everpreg_1
								has_trait = everpreg_2
								has_trait = everpreg_3
								is_female = no
							}

						}
						random = {
							chance = 100
							modifier = {
								add = 25
								has_trait_rank = {
									trait = education_diplomacy
									rank > 1
								}
							}
							modifier = {
								add = 50
								has_council_position = councillor_court_chaplain
							}
							modifier = {
								add = 35
								has_trait_rank = {
									trait = education_diplomacy
									rank > 2
								}
							}
							modifier = {
								add = 50
								has_trait_rank = {
									trait = education_diplomacy
									rank > 3
								}
							}
							modifier = {
								add = 50
								has_trait = everpreg_saoshyant_descendant
							}

							add_trait = everpreg_1
						}
					}

					else_if = {
						limit = {
							faith = {
								has_doctrine_parameter = tenet_everpreg_holywomb
							}
								has_trait = everpreg_1
						}
						random = {
							chance = 40
							modifier = {
								add = 10
								has_trait_rank = {
									trait = education_diplomacy
									rank > 1
								}
							}
							modifier = {
								add = 20
								has_trait_rank = {
									trait = education_diplomacy
									rank > 2
								}
							}
							modifier = {
								add = 30
								has_trait_rank = {
									trait = education_diplomacy
									rank > 3
								}
							}
							modifier = {
								add = 40
								has_trait = everpreg_saoshyant_descendant
							}

							remove_trait = everpreg_1
							add_trait = everpreg_2
						}
					}

					else_if = {
							limit = {
								faith = {
									has_doctrine_parameter = tenet_everpreg_holywomb
								}
									has_trait = everpreg_2
							}
							random = {
								chance = 30
								modifier = {
									add = 5
									has_trait_rank = {
										trait = education_diplomacy
										rank > 1
									}
								}
								modifier = {
									add = 10
									has_trait_rank = {
										trait = education_diplomacy
										rank > 2
									}
								}
								modifier = {
									add = 15
									has_trait_rank = {
										trait = education_diplomacy
										rank > 3
									}
								}
								modifier = {
									add = 30
									has_trait = everpreg_saoshyant_descendant
								}

								remove_trait = everpreg_2
								add_trait = everpreg_3
							}
					}

					set_immortal_age = 18

#				compare_modifier = {
#					value = learning
#					multiplier = 5
#				}
#				change_trait_rank = {
#					trait = lifestyle_mystic
#					rank = 1
#					max = 3
#				}
			}
#			50 = { #Note: this can be used to set a 50/50 (or other % chance between diff. outcomes. If I wanted an immortality thing or something, this would be how I'd do it.
#				desc = everpreg_pilgrimage_learn_from_the_bishops_failure
#				send_interface_toast = {
#					title = everpreg_pilgrimage_learn_from_the_bishops_failure
#					left_icon = root
#				}
#			}
		}
	}
}

everpreg_immortal_age__effect = {
	add_piety = short_everpreg_pilgrimage_piety_gain
	# add_piety = short_pilgrimage_piety_gain
	set_immortal_age = 18
}



###############

short_everpreg_pilgrimage_reward_effect = {
	add_piety = short_everpreg_pilgrimage_piety_gain
	# add_piety = short_pilgrimage_piety_gain
	if = {
		limit = {
			can_perform_the_hajj_trigger = yes
			exists = scope:activity.var:is_hajj
		}
#		add_trait = pilgrim (create sex based pilgrim thinger? Probably not, but an option)

	}
	everpreg_pilgrimage_additional_reward_effect = yes
}

medium_everpreg_pilgrimage_reward_effect = {
	add_piety = long_everpreg_pilgrimage_piety_gain
	# add_piety = long_pilgrimage_piety_gain
	if = {
		limit = {
			trigger_if = {
				limit = { can_perform_the_hajj_trigger = yes }
				exists = scope:activity.var:is_hajj
			}
			trigger_else = { always = yes }
		}
#		add_trait = pilgrim		(create sex based pilgrim thinger? Probably not, but an option)
	}
	everpreg_pilgrimage_additional_reward_effect = yes
}

long_everpreg_pilgrimage_reward_effect = {
	add_piety = very_long_everpreg_pilgrimage_piety_gain
	# add_piety = very_long_pilgrimage_piety_gain
	if = {
		limit = {
			trigger_if = {
				limit = { can_perform_the_hajj_trigger = yes }
				exists = scope:activity.var:is_hajj
			}
			trigger_else = { always = yes }
		}
		# add_trait = pilgrim	(create sex based pilgrim thinger? Probably not, but an option)
	}
	everpreg_pilgrimage_additional_reward_effect = yes
}

very_long_everpreg_pilgrimage_reward_effect = {
	add_piety = exceptionally_long_everpreg_pilgrimage_piety_gain
	# add_piety = exceptionally_long_pilgrimage_piety_gain
	if = {
		limit = {
			trigger_if = {
				limit = { can_perform_the_hajj_trigger = yes }
				exists = scope:activity.var:is_hajj
			}
			trigger_else = { always = yes }
		}
#		add_trait = pilgrim (create sex based pilgrim thinger? Probably not, but an option)
	}
	everpreg_pilgrimage_additional_reward_effect = yes
}

give_everpreg_pilgrimage_reward_effect = {
	if = {
		limit = { scope:activity.var:everpreg_pilgrimage_distance = short_everpreg_pilgrimage_var }
		short_everpreg_pilgrimage_reward_effect = yes
	}
	else_if = {
		limit = { scope:activity.var:everpreg_pilgrimage_distance = medium_everpreg_pilgrimage_var }
		medium_everpreg_pilgrimage_reward_effect = yes
	}
	else_if = {
		limit = { scope:activity.var:everpreg_pilgrimage_distance = long_everpreg_pilgrimage_var }
		long_everpreg_pilgrimage_reward_effect = yes
	}
	else_if = {
		limit = { scope:activity.var:everpreg_pilgrimage_distance = very_long_everpreg_pilgrimage_var }
		very_long_everpreg_pilgrimage_reward_effect = yes
	}
}

###select_everpreg_pilgrimage_effect = {
###	# EVERPREG_PILGRIMAGE_DISTANCE
###	save_temporary_scope_value_as = {
###		name = distance
###		value = flag:$EVERPREG_PILGRIMAGE_DISTANCE$
###	}
###	if = {
###		limit = {
###			NOT = { has_character_flag = is_on_hajj }
###		}
###		switch = {
###			trigger = scope:distance
###			flag:short = {
###				remove_short_term_gold = everpreg_pilgrimage_minimum_cost
###			}
###			flag:medium = {
###				remove_short_term_gold = everpreg_pilgrimage_medium_extra_cost
###			}
###			flag:long = {
###				remove_short_term_gold = everpreg_pilgrimage_long_extra_cost
###			}
###			flag:very_long = {
###				remove_short_term_gold = everpreg_pilgrimage_very_long_extra_cost
###			}
###		}
###	}
###}

calculate_everpreg_pilgrimage_distance_effect = {
	# Requires PILGRIM = character
	# Requires HOLY_SITE = barony_title
	$PILGRIM$.capital_barony = { # Only landed characters can go on regular everpreg_pilgrimages
		save_temporary_scope_as = start_barony
	}
	# Calculate distance to the target holy site
	# The value set indicates how many Journey Events a character will get
	if = {
		limit = {
			scope:start_barony = {
				squared_distance = {
					target = $HOLY_SITE$
					value > long_everpreg_pilgrimage_max_length
				}
			}
		}
		custom_tooltip = very_long_everpreg_pilgrimage_tt
		select_everpreg_pilgrimage_effect = {
			EVERPREG_PILGRIMAGE_DISTANCE = very_long
		}
	}
	else_if = {
		limit = {
			scope:start_barony = {
				squared_distance = {
					target = $HOLY_SITE$
					value > medium_everpreg_pilgrimage_max_length
				}
			}
		}
		custom_tooltip = long_everpreg_pilgrimage_tt
		select_everpreg_pilgrimage_effect = {
			EVERPREG_PILGRIMAGE_DISTANCE = long
		}
	}
	else_if = {
		limit = {
			scope:start_barony = {
				squared_distance = {
					target = $HOLY_SITE$
					value > short_everpreg_pilgrimage_max_length
				}
			}
		}
		custom_tooltip = medium_everpreg_pilgrimage_tt
		select_everpreg_pilgrimage_effect = {
			EVERPREG_PILGRIMAGE_DISTANCE = medium
		}
	}
	else = {
		custom_tooltip = short_everpreg_pilgrimage_tt
		select_everpreg_pilgrimage_effect = {
			EVERPREG_PILGRIMAGE_DISTANCE = short
		}
	}
}

set_everpreg_pilgrimage_distance_variable_effect = {
	# Requires PILGRIM = character
	# Requires HOLY_SITE = barony_title
	$PILGRIM$.capital_barony = { # Only landed characters can go on regular everpreg_pilgrimages
		save_temporary_scope_as = start_barony
	}
	# Calculate distance to the target holy site
	# The value set indicates how many Journey Events a character will get
	if = {
		limit = {
			scope:start_barony = {
				squared_distance = {
					target = $HOLY_SITE$
					value > long_everpreg_pilgrimage_max_length
				}
			}
		}
		set_variable = {
			name = everpreg_pilgrimage_distance
			value = very_long_everpreg_pilgrimage_var
		}
	}
	else_if = {
		limit = {
			scope:start_barony = {
				squared_distance = {
					target = $HOLY_SITE$
					value > medium_everpreg_pilgrimage_max_length
				}
			}
		}
		set_variable = {
			name = everpreg_pilgrimage_distance
			value = long_everpreg_pilgrimage_var
		}
	}
	else_if = {
		limit = {
			scope:start_barony = {
				squared_distance = {
					target = $HOLY_SITE$
					value > short_everpreg_pilgrimage_max_length
				}
			}
		}
		set_variable = {
			name = everpreg_pilgrimage_distance
			value = medium_everpreg_pilgrimage_var
		}
	}
	else = {
		set_variable = {
			name = everpreg_pilgrimage_distance
			value = short_everpreg_pilgrimage_var
		}
	}
}


set_up_next_everpreg_pilgrimage_event_effect = {
	scope:activity = {
		change_variable = {
			name = journey_event_counter
			add = 1
		}
	}

	save_scope_value_as = {
		name = event_counter
		value = scope:activity.var:journey_event_counter
	}
	save_scope_value_as = {
		name = halfway_distance
		value = scope:activity.everpreg_pilgrimage_halfway_distance
		# value = scope:activity.pilgrimage_halfway_distance
	}

	if = {
		limit = { # TODO: Is any fallback handling needed in an else here?
			exists = scope:activity
		}
		if = {
			limit = {
				scope:activity.var:journey_event_counter = {
					compare_value < scope:activity.var:everpreg_pilgrimage_distance
				}
			}
			if = {
				limit = { exists = scope:activity.var:is_hajj }
				trigger_event = {
					on_action = hajj_journey_events
					days = $DAYS$
				}
			}
			else = {
				trigger_event = {
					on_action = everpreg_pilgrimage_journey_events
					days = $DAYS$
				}
			}
		}
		else = {
			if = { #For event background
				limit = { scope:activity.var:everpreg_pilgrimage_arrived = no }
				scope:activity = {
					set_variable = {
						name = everpreg_pilgrimage_arrived
						value = yes
					}
				}
			}
			trigger_event = {
				on_action = everpreg_pilgrimage_destination_events
				days = $DAYS$
			}
		}
		scope:activity = {
			change_variable = {
				name = journey_event_counter
				add = 1
			}
		}
	}
}


set_up_everpreg_holy_sites_for_selection_effect = {
	if = {
		limit = { NOT = { exists = scope:has_set_up_everpreg_pilgrimage_sites } }
		save_temporary_scope_as = pilgrim
		faith = {
			every_holy_site = {
				# Exclude Mecca for Muslims
				limit = {
					trigger_if = {
						limit = {
							scope:pilgrim.faith.religion = religion:islam_religion
						}
						NOT = { county = title:c_mecca }
					}
				}
				# Assign the Holy Site to the correct "scope slot"
				if = {
					limit = {
						NOT = { exists = scope:holy_site_1 }
					}
					save_scope_as = holy_site_1
				}

				else_if = {
					limit = {
						NOT = { exists = scope:holy_site_2 }
					}
					save_scope_as = holy_site_2
				}

				else_if = {
					limit = {
						NOT = { exists = scope:holy_site_3 }
					}
					save_scope_as = holy_site_3
				}

				else_if = {
					limit = {
						NOT = { exists = scope:holy_site_4 }
					}
					save_scope_as = holy_site_4
				}

				else_if = {
					limit = {
						NOT = { exists = scope:holy_site_5 }
					}
					save_scope_as = holy_site_5
				}

				else_if = {
					limit = {
						NOT = { exists = scope:holy_site_6 }
					}
					save_scope_as = holy_site_6
				}

				else_if = {
					limit = {
						NOT = { exists = scope:holy_site_7 }
					}
					save_scope_as = holy_site_7
				}

				else_if = {
					limit = {
						NOT = { exists = scope:holy_site_8 }
					}
					save_scope_as = holy_site_8
				}

				else_if = {
					limit = {
						NOT = { exists = scope:holy_site_9 }
					}
					save_scope_as = holy_site_9
				} # Stopping here, on the assumption that no Faith can ever get more than 9 Holy Sites
				else = {
					debug_log = "Holy Site failed to get an assigned scope slot!"
					debug_log_scopes = yes
				}
			}
		}
		save_temporary_scope_value_as = {
			name = has_set_up_everpreg_pilgrimage_sites
			value = yes
		}
	}
	# Debug checks:
	if = {
		limit = { NOT = { exists = scope:holy_site_1 } }
		debug_log = "No holy sites found in faith! (Except possibly mecca)"
		root.faith = {
			save_scope_as = current_faith
		}
		debug_log_scopes = yes
	}
}



# Used in the option to pick a specific Holy Site
everpreg_holy_site_selection_event_option_effect = {
	# HOLY_SITE = holy_site
	hidden_effect = {
		set_variable = {
			name = everpreg_pilgrimage_cooldown_timer
			years = everpreg_pilgrimage_cooldown_year_amount
		}
	}
	$HOLY_SITE$.title_province = {
		spawn_activity = {
			owner = root
			type = activity_everpreg_pilgrimage
		}
	}
	if = {
		limit = { is_ai = no }
		calculate_pilgrimage_distance_effect = {
			PILGRIM = root
			HOLY_SITE = $HOLY_SITE$
		}
	}
}


#save_holy_site_for_loc_effect = {
#	scope:activity.activity_province.barony = {
#		save_scope_as = destination_title
#	}
#}

abandon_everpreg_pilgrimage_early_effect = {
	custom_tooltip = everpreg_pilgrimage_return_home
	hidden_effect = {
		remove_variable = everpreg_pilgrimage_cooldown_timer
		scope:activity = {
			complete_activity = no
		}
		if = {
			limit = {
				has_character_flag = is_on_hajj
			}
			remove_character_flag = is_on_hajj
		}
	}
}