namespace = faith_creation

# Fired when you make a faith. Temporal rel head setup
faith_creation.0001 = {
	hidden = yes
	
	trigger = {
		faith = {
			has_doctrine = doctrine_temporal_head
			can_create_temporal_head_of_faith_title_trigger = yes
		}
	}
	
	immediate = {
		set_up_dynamic_temporal_hof_title_effect = { NEW_HOLDER = root }
	}
}

# Fired when you make a faith. Spiritual rel head setup
faith_creation.0002 = {
	hidden = yes
	
	trigger = {
		faith = {
			has_doctrine = doctrine_spiritual_head
			can_create_spiritual_head_of_faith_title_trigger = yes
		}

	}
	
	immediate = {
		set_up_dynamic_spiritual_hof_title_effect = { CREATOR = root }
	}
}

# Fired when you make a faith. Flags faith as being player-created for future heresies.
faith_creation.0003 = {
	hidden = yes
	
	trigger = {
		is_ai = no
	}

	immediate = {
		if = {
			limit = { is_ai = no }
			faith = {
				set_variable = {
					name = player_created_faith
					value = yes
				}
			}
		}
		# Set original faith this one was based on
		faith = {
			if = {
				limit = { exists = scope:old_faith.var:foundational_faith }
				set_variable = {
					name = foundational_faith
					value = scope:old_faith.var:foundational_faith
				}
			}
			else = {
				set_variable = {
					name = foundational_faith
					value = scope:old_faith
				}
			}
		}
	}
}

# Temporal Head of Faith re-creation event
faith_creation.0011 = {
	hidden = yes

	trigger = {
		faith = {
			has_doctrine = doctrine_temporal_head
			can_create_temporal_head_of_faith_title_trigger = yes

			# Title must exist but be uncreated.
			exists = religious_head_title
			NOT = { exists = religious_head_title.holder }
		}
	}

	immediate = {
		faith = {	
			religious_head_title = {
				save_scope_as = my_hof_title
			}
		}

		create_title_and_vassal_change = {
			type = created
			save_scope_as = change
			add_claim_on_loss = no
		}
		scope:my_hof_title = {
			change_title_holder = {
				holder = root
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change
		scope:my_hof_title = {
			add_title_law = temporal_head_of_faith_succession_law
		}
	}
}

# Spiritual Head of Faith re-creation event
faith_creation.0012 = {
	hidden = yes

	trigger = {
		faith = {
			has_doctrine = doctrine_spiritual_head
			can_create_spiritual_head_of_faith_title_trigger = yes

			# Title must exist but be uncreated.
			exists = religious_head_title
			NOT = { exists = religious_head_title.holder }			
		}
	}

	immediate = {
		faith = {
			religious_head_title = {
				save_scope_as = my_hof_title
			}
		}

		if = {
			limit = {
				any_theocratic_vassal = {
					faith = root.faith
				}
			}
			ordered_theocratic_vassal = {
				limit = {
					faith = root.faith
				}
				order_by = head_of_faith_selection_weight
				save_scope_as = new_religious_head
			}
		}
		else = {	
			create_character = {
				location = root.capital_province
				age = { 30 50 }
				gender_female_chance = root_faith_clergy_gender_female_chance
				trait = education_learning_4
				faith = root.faith
				culture = root.culture
				learning = { 14 22 }
				save_scope_as = new_religious_head
			}
		}

		create_title_and_vassal_change = {
			type = created
			save_scope_as = change
			add_claim_on_loss = no
		}
		scope:my_hof_title = {
			change_title_holder = {
				holder = scope:new_religious_head
				change = scope:change
			}
		}
		resolve_title_and_vassal_change = scope:change		
	}
}

# Fired when a new Faith is created by a player, or when a Pagan faith is reformed. Fired off an informative event for all players.
faith_creation.1000 = {
	hidden = yes

	immediate = {
		save_scope_as = reformer
		faith = {
			save_scope_as = reformed_faith
		}
		if = {
			limit = {
				faith = {
					exists = religious_head
				}
			}
			faith = {
				religious_head = {
					save_scope_as = reformed_hof
				}
			}
		}

		if = {
			limit = {
				exists = scope:old_faith.religious_head
			}
			scope:old_faith = {
				religious_head = {
					save_scope_as = old_hof
				}
			}
		}

		every_player = {
			trigger_event = {
				id = faith_creation.1001
				days = 1
			}
		}
	}
}

faith_creation.1001 = {
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:old_faith = {
						has_doctrine_parameter = unreformed
					}
				}
				desc = faith_creation.1001.t.reformation
			}
			desc = faith_creation.1001.t.schism
		}
	}
	desc = {
		#Introduction to the faith's reformation/schism:
		first_valid = {
			# I am reforming a pagan faith for the first time.
			triggered_desc = {
				trigger = {
					scope:old_faith = {
						has_doctrine_parameter = unreformed
						root = scope:reformer
					}
				}
				desc = faith_creation.1001.desc.reformation.personal
			}
			# I am splitting off a new faith from an existing one.
			triggered_desc = {
				trigger = {
					scope:old_faith = {
						root = scope:reformer
					}
				}
				desc = faith_creation.1001.desc.schism.personal
			}
			# Someone else is reforming a pagan faith for the first time.
			triggered_desc = {
				trigger = {
					scope:old_faith = {
						has_doctrine_parameter = unreformed
					}
				}
				desc = faith_creation.1001.desc.reformation
			}
			# Someone else is splitting off a new faith from an existing one.
			desc = faith_creation.1001.desc.schism
		}

		#State the new faith's external outlook:
		first_valid = {
			#Faith is aggressive (Great Holy Wars are allowed):
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						OR = {
							has_doctrine_parameter = great_holy_wars_active
							has_doctrine_parameter = great_holy_wars_active_if_reformed
						}
					}
				}
				desc = faith_creation.1001.desc.outlook.ghw
			}
			#Faith is pacifist:
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						has_doctrine_parameter = pacifist_opinion_active
					}
				}
				desc = faith_creation.1001.desc.outlook.pacifism
			}
			#Faith is 'dogmatic' (fundamentalist, or at least not pluralist but has certain strict doctrinal tenets)
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						OR = {
							has_doctrine = doctrine_pluralism_fundamentalist
							AND = {
								has_doctrine = doctrine_pluralism_righteous
								OR = {
									has_doctrine = tenet_religious_legal_pronouncements
									has_doctrine = tenet_literalism
									has_doctrine = tenet_legalism
								}
							}
						}
					}
				}
				desc = faith_creation.1001.desc.outlook.dogmatic
			}
			#Faith is 'cosmopolitan' (pluralistic, or at least not fundamentalist but has certain syncretic tenets)
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						OR = {
							has_doctrine = doctrine_pluralism_pluralistic
							AND = {
								has_doctrine = doctrine_pluralism_righteous
								OR = {
									has_doctrine = tenet_eastern_syncretism
									has_doctrine = tenet_christian_syncretism
									has_doctrine = tenet_islamic_syncretism
									has_doctrine = tenet_jewish_syncretism
									has_doctrine = tenet_unreformed_syncretism
									has_doctrine = tenet_tax_nonbelievers
									has_doctrine = special_doctrine_jizya
									has_doctrine = tenet_adaptive
								}
							}
						}
					}
				}
				desc = faith_creation.1001.desc.outlook.accepting
			}
			desc = faith_creation.1001.desc.outlook.fallback
		}

		#State the new faith's most important and/or distinctive belief:
		first_valid = {
			#Human Sacrifice
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						OR = {
							has_doctrine = tenet_human_sacrifice
							has_doctrine = tenet_ritual_cannibalism
						}
					}
				}
				desc = faith_creation.1001.desc.belief.sacrifice
			}
			#Indulgent
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						OR = {
							has_doctrine = tenet_carnal_exaltation
							has_doctrine = tenet_hedonistic
						}
					}
				}
				desc = faith_creation.1001.desc.belief.indulgent
			}
			#Incest
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						OR = {
							has_doctrine = tenet_divine_marriage
							has_doctrine = doctrine_consanguinity_unrestricted
						}
					}
				}
				desc = faith_creation.1001.desc.belief.incest
			}
			#Aniconism
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						has_doctrine = tenet_aniconism
					}
				}
				desc = faith_creation.1001.desc.belief.aniconism
			}
			#Ascetic
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						OR = {
							has_doctrine = tenet_monasticism
							has_doctrine = tenet_vows_of_poverty
							has_doctrine = tenet_mendicant_preachers
							has_doctrine = tenet_gnosticism
							has_doctrine = tenet_inner_journey
							has_doctrine = tenet_asceticism
						}
					}
				}
				desc = faith_creation.1001.desc.belief.ascetic
			}
			#Spirits
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						OR = {
							has_doctrine = tenet_ancestor_worship
							has_doctrine = tenet_adorcism
						}
					}
				}
				desc = faith_creation.1001.desc.belief.spirits
			}
			#Superstition
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						OR = {
							has_doctrine = tenet_astrology
							has_doctrine = tenet_sacred_childbirth
							has_doctrine = tenet_esotericism
							has_doctrine = tenet_mystical_birthright
						}
					}
				}
				desc = faith_creation.1001.desc.belief.superstition
			}
			#Nature
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						has_doctrine = tenet_sanctity_of_nature
					}
				}
				desc = faith_creation.1001.desc.belief.nature
			}
			#Community
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						has_doctrine = tenet_communal_identity
					}
				}
				desc = faith_creation.1001.desc.belief.community
			}
			desc = faith_creation.1001.desc.belief.fallback
		}

		#Conclude with the new faith's religious leadership form.
		first_valid = {
			#Temporal Head and I am it!
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						has_doctrine = doctrine_temporal_head
						religious_head ?= root
					}
				}
				desc = faith_creation.1001.desc.temporal.personal
			}
			#Temporal Head and it is someone else...
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						has_doctrine = doctrine_temporal_head
						exists = religious_head
					}
				}
				desc = faith_creation.1001.desc.temporal
			}
			#Spiritual Head
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						has_doctrine = doctrine_spiritual_head
						exists = religious_head
					}
				}
				desc = {
					first_valid = {
						triggered_desc = {
							trigger = { scope:reformed_faith.religious_head = scope:old_hof } # Prevents the old pope from being labeled as new
							desc = faith_creation.1001.desc.spiritual.sameold
						}
						desc = faith_creation.1001.desc.spiritual
					}
				}
			}
			#No Head
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {
						has_doctrine = doctrine_no_head
					}
				}
				desc = faith_creation.1001.desc.no_head
			}
			triggered_desc = {
				trigger = {
					scope:reformed_faith = {				
						OR = {
							has_doctrine = doctrine_temporal_head
							has_doctrine = doctrine_spiritual_head
						}
						NOT = { exists = religious_head }
					}
				}
				desc = faith_creation.1001.desc.no_holy_sites
			}
		}
	}
	
	left_portrait = {
		character = scope:reformer
	}
	right_portrait = {
		trigger = {
			scope:reformed_hof != scope:reformer
		}
		character = scope:reformed_hof
		animation = personality_zealous
	}
	theme = faith
	override_background = {
		reference = temple_scope
	}

	immediate = {
		scope:reformer = {
			save_scope_as = background_temple_scope
		}
		play_music_cue = "mx_cue_epic_sacral_moment"
	}

	option = {
		name = faith_creation.1001.o.getreaction	
	}
}


# Notification event for when you create a Head of Faith.
faith_creation.1010 = {
	type = character_event
	title = faith_creation.1010.t
	desc = {
		desc = faith_creation.1010.desc.start
		first_valid = {
			# I am the new temporal HoF!
			triggered_desc = {
				trigger = {
					scope:faith = {
						has_doctrine = doctrine_temporal_head
						religious_head = root
					}					
				}
				desc = faith_creation.1010.desc.temporal.personal
			}
			# Some other person is the new temporal HoF!
			triggered_desc = {
				trigger = {
					scope:faith = {
						has_doctrine = doctrine_temporal_head
					}
				}
				desc = faith_creation.1010.desc.temporal
			}
			# No wait we like spiritual HoFs instead!
			triggered_desc = {
				trigger = {
					scope:faith = {
						has_doctrine = doctrine_spiritual_head
					}
				}
				desc = faith_creation.1010.desc.spiritual
			}
		}
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:faith = {
						has_doctrine = doctrine_temporal_head
						religious_head = root
					}					
				}
				desc = faith_creation.1010.desc.end.personal
			}
			desc = faith_creation.1010.desc.end
		}
	}
	left_portrait = {
		character = scope:HoF
		animation = personality_zealous
	}
	theme = faith
	override_background = {
		reference = temple_scope
	}

	trigger = {
		exists = scope:faith.religious_head
	}

	immediate = {
		play_music_cue = "mx_cue_epic_sacral_moment"

		scope:faith = {
			religious_head = {
				save_scope_as = HoF
				save_scope_as = background_temple_scope
			}
			show_as_tooltip = {
				change_fervor = {
					value = major_fervor_gain
					desc = fervor_gain_recreated_hof
				}
			}
		}
	}

	option = {
		name = faith_creation.1010.a

		if = {
			limit = {
				root = scope:reformer
				scope:faith = {
					has_doctrine = doctrine_spiritual_head
				}						
			}
			scope:HoF = {
				add_opinion = {
					modifier = grateful_opinion
					target = scope:reformer
					opinion = 30
				}
			}
		}		
	}
}

faith_creation.1011 = {
	hidden = yes

	trigger = {
		exists = scope:faith.religious_head
		# Limit range, so Irish players don't hear about Zhengyi religious developments
		OR = {
			scope:faith.religious_head.top_liege = root.top_liege # Same realm
			faith = scope:faith.religion # Same religion
			culture = scope:faith.religious_head.culture # Same culture
			in_diplomatic_range = scope:faith.religious_head # Fallback, actually in diplomatic range
		}
	}

	immediate = {
		scope:faith = {
			religious_head = {
				save_scope_as = HoF
			}
		}

		send_interface_message = {
			type = event_hof_established
			left_icon = scope:HoF
			title = faith_creation.1011.message.title
			desc = faith_creation.1011.message.desc
		}
	}
}
