###########################################
#
# Religious Decisions for Sons of Abraham
#
###########################################
# Written by Henrik Fhraeus
###########################################

decisions = {

	create_fraticelli_papacy = {
		ai_check_interval = 3

		is_high_prio = yes

		only_playable = yes

		potential = {
			religion = fraticelli
			has_dlc = "Sons of Abraham"

			NOR = {
				is_title_active = d_fraticelli

				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}
		}

		allow = {
			piety >= 1000
			completely_controls = c_roma
		}

		effect = {
			activate_title = {
				title = d_fraticelli
				status = yes
			}

			piety = 1000

			religion_authority = {
				name = papacy_instituted
				years = 20
			}

			hidden_effect = {
				if = {
					limit = {
						any_realm_lord = {
							has_landed_title = b_roma
							religion = ROOT
							is_theocracy = yes
						}
					}

					random_realm_lord = {
						limit = {
							has_landed_title = b_roma
							religion = ROOT
							is_theocracy = yes
						}

						grant_title = d_fraticelli
						set_defacto_liege = ROOT
						pf_liege_change_effect = yes
						wealth = 500
						add_friend = ROOT
					}
				}
				else = {
					create_random_priest = {
						random_traits = yes
						dynasty = random
						religion = ROOT
						culture = ROOT
						age = 40
						health = 7
						add_trait = shrewd
						add_trait = zealous
						add_trait = scholar
						add_trait = mastermind_theologian

						attributes = {
							learning = 5
							diplomacy = 5
							martial = 2
							stewardship = 5
							intrigue = 5
						}
					}

					new_character = {
						grant_title = d_fraticelli
						grant_title = b_roma
						wealth = 500
						add_friend = ROOT
					}
				}
			}

			character_event = { id = 39703 }
		}

		ai_will_do = {
			factor = 1
		}
	}

	create_iconoclast_patriarchy = {
		ai_check_interval = 36

		is_high_prio = yes

		only_independent = yes

		potential = {
			religion = iconoclast
			has_dlc = "Sons of Abraham"

			NOR = {
				is_title_active = d_iconoclast

				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}
		}

		allow = {
			real_tier = EMPEROR
			num_of_holy_sites >= 1
			piety >= 1000
			NOT = { trait = cynical }
			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			piety = 1000

			religion_authority = {
				name = patriarchy_instituted
				years = 20
			}

			activate_title = {
				title = d_iconoclast
				status = yes
			}

			hidden_effect = {
				any_realm_province = {
					limit = {
						religion = iconoclast
						held_under_PREV = yes
					}

					add_province_modifier = {
						name = appeased_religious
						years = 5
					}
				}

				if = {
					limit = { completely_controls = b_hagiasophia }

					b_hagiasophia = {
						holder_scope = {
							if = {
								limit = { has_landed_title = k_orthodox }

								# Workaround for automatic usurption of theocratic characters
								set_character_flag = needs_temporary_fake_theocracy_government
								set_government_type = temporary_fake_theocracy_government
								unsafe_religion = iconoclast
								set_government_type = theocracy_government
								clr_character_flag = needs_temporary_fake_theocracy_government

								grant_title = d_iconoclast
								set_defacto_liege = ROOT
								pf_liege_change_effect = yes
								add_friend = ROOT
								unsafe_destroy_landed_title = k_orthodox

								activate_title = {
									title = k_orthodox
									status = no
								}
							}

							else_if = {
								limit = {
									religion = ROOT
									is_theocracy = yes
								}

								grant_title = d_iconoclast
								wealth = 500
								add_friend = ROOT
								set_defacto_liege = ROOT
								pf_liege_change_effect = yes
							}
							else = {
								create_character = {
									random_traits = yes
									dynasty = random
									religion = iconoclast
									culture = ROOT
									age = 39
									add_trait = shrewd
									add_trait = zealous
									add_trait = iconoclast_trait
									add_trait = mastermind_theologian

									attributes = {
										learning = 5
										diplomacy = 5
										martial = 2
										stewardship = 5
										intrigue = 5
									}
								}

								new_character = {
									grant_title = d_iconoclast
									grant_title = b_hagiasophia
									wealth = 500
									add_friend = ROOT
									set_defacto_liege = ROOT
								}
							}
						}
					}
				}
				else = {
					create_character = {
						random_traits = yes
						dynasty = random
						religion = iconoclast
						culture = ROOT
						age = 39
						add_trait = shrewd
						add_trait = zealous
						add_trait = iconoclast_trait
						add_trait = mastermind_theologian

						attributes = {
							learning = 5
							diplomacy = 5
							martial = 2
							stewardship = 5
							intrigue = 5
						}
					}

					new_character = {
						grant_title = d_iconoclast

						ROOT = {
							random_realm_title = {
								limit = { is_holy_site = ROOT }
								gain_title = PREVPREV
							}
						}

						wealth = 500
						add_friend = ROOT
						set_defacto_liege = ROOT
					}
				}
			}

			long_character_event = { id = 39704 }
		}

		ai_will_do = {
			factor = 1
		}
	}

	restore_pope_in_rome = {
		ai_check_interval = 36

		is_high_prio = yes

		only_independent = yes

		potential = {
			independent = yes

			trigger_if = {
				limit = { religion = catholic }
				is_title_active = k_papacy
			}
			trigger_else_if = {
				limit = { religion = fraticelli }
				is_title_active = d_fraticelli
			}
			trigger_else_if = {
				limit = {
					religion = chalcedonian

					rightful_religious_head_scope = {
						trait = latin_rites
					}
				}

				OR = {
					is_title_active = k_papacy

					b_roma = {
						holder_scope = {
							is_theocracy = yes
						}
					}
				}
			}

			has_dlc = "Sons of Abraham"
			controls_religion = no

			NOR = {
				rightful_religious_head = THIS

				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}

			c_roma = {
				holder_scope = {
					OR = {
						character = ROOT
						is_vassal_or_below_of = ROOT
					}

					trigger_if = {
						limit = {
							ROOT = {
								OR = {
									religion = catholic
									religion = chalcedonian
								}
							}
						}

						k_papacy = {
							holder_scope = {
								NOR = {
									character = PREVPREV
									is_liege_or_above_of = PREVPREV
								}
							}
						}
					}
					trigger_else_if = {
						limit = { ROOT = { religion = fraticelli } }

						d_fraticelli = {
							holder_scope = {
								NOR = {
									character = PREVPREV
									is_liege_or_above_of = PREVPREV
								}
							}
						}
					}
				}
			}
		}

		allow = {
			# Don't give away your only provincw
			trigger_if = {
				limit = { ai = no }
				num_of_count_titles > 1
			}

			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			religion_head = {
				show_scope_change = no

				save_event_target_as = restored_pope

				c_roma = {
					show_scope_change = no

					grant_title = PREV

					any_de_jure_vassal_title = {
						show_scope_change = no

						limit = {
							holder_scope = {
								OR = {
									character = ROOT
									is_vassal_or_below_of = ROOT
								}
							}
						}

						grant_title = PREVPREV
					}
				}
			}

			if = {
				limit = {
					trigger_if = {
						limit = { religion = catholic }
						NOT = { has_character_flag = restored_catholic_pope_in_rome }
					}
					trigger_else_if = {
						limit = { religion = chalcedonian }
						NOT = { has_character_flag = restored_chalcedonian_pope_in_rome }
					}
					trigger_else = {
						NOT = { has_character_flag = restored_fraticelli_pope_in_rome }
					}
				}

				religion_head = {
					show_scope_change = no

					opinion = {
						name = opinion_restored_to_rome
						who = ROOT
					}
				}

				piety = 1000

				if = {
					limit = { religion = catholic }
					set_character_flag = restored_catholic_pope_in_rome
				}
				else_if = {
					limit = { religion = chalcedonian }
					set_character_flag = restored_chalcedonian_pope_in_rome
				}
				else = {
					set_character_flag = restored_fraticelli_pope_in_rome
				}
			}

			character_event = { id = 39705 }
		}

		ai_will_do = {
			factor = 1

			trigger = {
				# AI will never take Rome province away from a player
				c_roma = {
					holder_scope = {
						NOR = {
							ai = no
							any_liege = { ai = no }
						}
					}
				}

				NOT = { trait = cynical }

				trigger_if = {
					limit = { real_tier = KING }
					trait = zealous
				}
			}
		}
	}

	restore_ecumenical_patriarchate = {
		ai_check_interval = 36

		is_high_prio = yes

		only_independent = yes

		potential = {
			has_dlc = "Sons of Abraham"
			religion = orthodox # TODO: Adjust for Chalcedonian

			NOR = {
				is_title_active = k_orthodox

				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}
		}

		allow = {
			completely_controls = c_constantinopolis
			real_tier = EMPEROR
			war = no
			is_not_in_regency_trigger = yes
			is_inaccessible_trigger = no
		}

		effect = {
			piety = 250

			activate_title = {
				title = k_orthodox
				status = yes
			}

			hidden_effect = {
				if = {
					limit = {
						any_realm_lord = {
							has_landed_title = b_hagiasophia
							religion = ROOT
							is_theocracy = yes
						}
					}

					random_realm_lord = {
						limit = {
							has_landed_title = b_hagiasophia
							religion = ROOT
							is_theocracy = yes
						}

						grant_title = k_orthodox
						set_defacto_liege = ROOT
						pf_liege_change_effect = yes
						add_friend = ROOT
					}
				}
				else = {
					create_random_priest = {
						random_traits = yes
						dynasty = random
						religion = ROOT
						culture = ROOT
						age = 40
						health = 7
						add_trait = shrewd
						add_trait = zealous
						add_trait = scholar
						add_trait = mastermind_theologian

						attributes = {
							learning = 5
							diplomacy = 5
							martial = 2
							stewardship = 5
							intrigue = 5
						}
					}

					new_character = {
						grant_title = k_orthodox
						grant_title = b_hagiasophia
						wealth = 500
						add_friend = ROOT
					}
				}
			}

			long_character_event = { id = 39706 }
		}
	}

	african_tolerate_abrahamics = {
		ai_check_interval = 36

		only_playable = yes

		potential = {
			# Only unreformed. Reformers can pick the Syncretism Doctrine to sympathize with everyone they wish.
			religion = west_african_pagan

			NOR = {
				trait = sympathy_christendom
				trait = sympathy_islam
				trait = sympathy_judaism
				has_character_flag = flag_picking_sympathy

				has_alternate_start_parameter = {
					key = religion_names
					value = random
				}
			}
		}

		allow = {
			piety >= 50
			NOT = { trait = zealous }

			custom_tooltip = {
				text = african_sympathy_abrahamic

				OR = {
					any_neighbor_independent_ruler = {
						OR = {
							religion_group = christian
							religion_group = muslim
							religion_group = jewish_group
						}
					}

					any_liege = {
						OR = {
							religion_group = christian
							religion_group = muslim
							religion_group = jewish_group
						}
					}
				}
			}
		}

		effect = {
			custom_tooltip = {
				text = tooltip_african_picking_syncretism

				character_event = { id = HFP.23027 }
			}
			piety = -50
		}

		ai_will_do = {
			factor = 1
		}
	}

}