namespace = N3OW_faith_creation

# Fired when you make a faith. Holy Order rel head setup
ate_faith_creation.0001 = {
	hidden = yes
	
	trigger = {
		faith = {
			has_doctrine = doctrine_holy_order_head
			#can_create_holy_order_head_of_faith_title_trigger = yes
		}
	}
	
	immediate = {
		trigger_event = N3OW_holy_order.0003
	}
}

namespace = ate_holy_order

# this event needs localisations #fune
# Create Holy Order HoF Titles
N3OW_holy_order.0001 = {
	hidden = yes
	type = character_event

	trigger = {
		#root = faith:ursuline.religious_head
		always = yes
	}

	immediate = {
		
		# Select a random county.
		ordered_held_title = {
			limit = {
				tier = tier_county
				any_county_province = {
					has_holding = no
					barony = { is_holy_site_of = root.faith }
				}
				# any_county_province = { has_holding = no }
				# OR = {
				# 	is_holy_site_of = root.faith
				# 	any_county_province = {
				# 		barony = { is_holy_site_of = root.faith }
				# 	}
				# }
			}
			alternative_limit = {
				tier = tier_county
				any_county_province = { has_holding = no }
			}
			save_scope_as = designated_county
		}
		# Select an appropriate barony.
		## If there's an empty space in the capital, we create a castle.
		#if = {
		#	limit = {
		#		scope:designated_county = {
		#			any_county_province = { has_holding = no }
		#		}
		#	}
		scope:designated_county = {
			random_county_province = {
				limit = { has_holding = no }
				set_holding_type = castle_holding
				barony = { save_scope_as = leased_barony }
			}
		}
		#}
		## Otherwise, grab an existing non-capital castle/city.
		##TODO this doesn't work, that's why we're forcing empty provinces
		#else = {
		#	scope:designated_county = {
		#		random_county_province = {
		#			limit = {
		#				is_county_capital = no
		#				OR = {
		#					has_holding_type = castle_holding
		#					#has_holding_type = city_holding
		#				}
		#			}
		#			barony = { save_scope_as = leased_barony }
		#		}
		#	}
		#}
		
		## Create a suitable leader
		#hardcoded logic not needed anymore
		#if = {
		#	limit = { root.faith = faith:ursuline }
		#	character:canada_ladouceur_denise_1 = {
		#		save_scope_as = leader
		#		every_held_title = { # Causes issues if she has this before she becomes HO leader
		#			limit = { this = title:k_ursuline_see }
		#			root = { destroy_title = prev }
		#		}
		#	}
		#}
		#if = {
		#	limit = { root.faith = faith:cristero }
		#	character:religious_cristo_rey_hof_1 = {
		#		save_scope_as = leader
		#		every_held_title = { # Causes issues if she has this before she becomes HO leader
		#			limit = { this = title:d_prelature_cristo_rey }
		#			root = { destroy_title = prev }
		#		}
		#	}
		#}
		#if we already have one on history, use it!
		if = {
			limit = {
				exists = root.faith.religious_head
			}
			#but destroy the actual title to avoid errors
			root.faith.religious_head = {
				save_scope_as = leader
				destroy_title = root.faith.religious_head_title
			}
		}
		else = {
			create_character = {
				template = ate_holy_order_faith_leader_character
				location = scope:leased_barony.title_province
				culture = root.culture
				faith = root.faith
				save_scope_as = leader
			}
		}
		
		# Create the actual holy order.
		create_holy_order = {
			leader = scope:leader
			capital = scope:leased_barony
			save_scope_as = new_holy_order
		}
		scope:leader = {
			add_piety_level = 2
			add_gold = holy_order_starting_gold
			every_courtier = { add_trait = order_member }
		}
		
		## Give it some buildings.
		scope:leased_barony.title_province = {
			# Walls & Towers.
			if = {
				limit = {
					NOT = { has_building_or_higher = curtain_walls_01 }
				}
				# If it doesn't have a free building slot, give it one.
				if = {
					limit = { free_building_slots = 0 }
					add_province_modifier = extra_building_slot
				}
				# Add the building.
				add_building = curtain_walls_01
			}
			# Barracks.
			if = {
				limit = {
					NOT = { has_building_or_higher = barracks_01 }
				}
				# If it doesn't have a free building slot, give it one.
				if = {
					limit = { free_building_slots = 0 }
					add_province_modifier = extra_building_slot
				}
				# Add the building.
				add_building = barracks_01
			}
			# Military Camps.
			if = {
				limit = {
					NOT = { has_building_or_higher = military_camps_01 }
				}
				# If it doesn't have a free building slot, give it one.
				if = {
					limit = { free_building_slots = 0 }
					add_province_modifier = extra_building_slot
				}
				# Add the building.
				add_building = military_camps_01
			}
		}
		
		#TODO make sure this doesn't break faiths without a coa
		scope:new_holy_order.title = {
			set_coa = root.faith.religious_head_title
		}
		
		root.faith = {
			set_religious_head_title = scope:new_holy_order.title
		}

		#not needed anymore
		#if = {
		#	limit = { root.faith = faith:ursuline }
		#	scope:new_holy_order.title = {
		#		set_coa = k_ursuline_see
		#		set_title_name = k_ursuline_see # For some reason, this isnt working
		#		# copy_title_history = k_ursuline_see # This isnt working either
		#	}
		#	#scope:leased_barony = { set_title_name = b_ursuline_abbey }
		#}

	}
}

#generic Holy Order creation
N3OW_holy_order.0002 = { 
	hidden = yes
	type = character_event

	trigger = {
		#root = faith:ursuline.religious_head
		always = yes
	}

	immediate = {
		
		# Display simplified results.
		custom_tooltip = create_holy_order_decision_effect_message
		# Select a random county.
		random_held_title = {
			limit = {
				tier = tier_county
				any_county_province = {
					custom_description = {
						text = forge_the_jomsvikings_decision.tt.empty_castle_or_city
						OR = {
							has_holding = no
							has_holding_type = castle_holding
							has_holding_type = city_holding
						}
					}
				}
			}
			save_scope_as = designated_county
		}
		# Select an appropriate barony.
		## If there's an empty space in the capital, we create a castle.
		if = {
			limit = {
				scope:designated_county = {
					any_county_province = { has_holding = no }
				}
			}
			scope:designated_county = {
				random_county_province = {
					limit = { has_holding = no }
					set_holding_type = castle_holding
					barony = { save_scope_as = leased_barony }
				}
			}
		}
		## Otherwise, grab an existing non-capital castle/city.
		else = {
			scope:designated_county = {
				random_county_province = {
					limit = {
						is_county_capital = no
						OR = {
							has_holding_type = castle_holding
							has_holding_type = city_holding
						}
					}
					barony = { save_scope_as = leased_barony }
				}
			}
		}
		# Create a suitable leader for the Jomsvikings.
		create_character = {
			template = holy_order_leader_character
			location = scope:leased_barony.title_province
			culture = root.culture
			faith = root.faith
			save_scope_as = leader
		}
		# Create the actual holy order.
		create_holy_order = {
			leader = scope:leader
			capital = scope:leased_barony
			save_scope_as = new_holy_order
		}
		scope:leader = {
			add_piety_level = 2
			add_gold = holy_order_starting_gold
			every_courtier = { add_trait = order_member }
		}
		## Give it some buildings.
		scope:leased_barony.title_province = {
			# Walls & Towers.
			if = {
				limit = {
					NOT = { has_building_or_higher = curtain_walls_01 }
				}
				# If it doesn't have a free building slot, give it one.
				if = {
					limit = { free_building_slots = 0 }
					add_province_modifier = extra_building_slot
				}
				# Add the building.
				add_building = curtain_walls_01
			}
			# Barracks.
			if = {
				limit = {
					NOT = { has_building_or_higher = barracks_01 }
				}
				# If it doesn't have a free building slot, give it one.
				if = {
					limit = { free_building_slots = 0 }
					add_province_modifier = extra_building_slot
				}
				# Add the building.
				add_building = barracks_01
			}
			# Military Camps.
			if = {
				limit = {
					NOT = { has_building_or_higher = military_camps_01 }
				}
				# If it doesn't have a free building slot, give it one.
				if = {
					limit = { free_building_slots = 0 }
					add_province_modifier = extra_building_slot
				}
				# Add the building.
				add_building = military_camps_01
			}
		}
	}
}

# Fired when you make a faith (if you meet the reqs) or recreate the Head of Faith
N3OW_holy_order.0003 = {
	hidden = yes
	
	trigger = {
		faith = {
			has_doctrine = doctrine_holy_order_head
			#can_create_holy_order_head_of_faith_title_trigger = yes
		}
	}
	
	immediate = {
		faith = {
			save_scope_as = my_faith
		}

		#root.faith = {
		#	random_holy_site = {
		#		limit = {
		#			county.holder = root
		#		}
		#		alternative_limit = {
		#			county.holder = {
		#				target_is_liege_or_above = root
		#			}
		#		}
		#		county = { save_scope_as = designated_county }
		#	}
		#}
		#
		## Select an appropriate barony.
		### If there's an empty space in the capital, we create a castle.
		#if = {
		#	limit = {
		#		scope:designated_county = {
		#			any_county_province = { has_holding = no }
		#		}
		#	}
		#	scope:designated_county = {
		#		random_county_province = {
		#			limit = { has_holding = no }
		#			set_holding_type = castle_holding
		#			barony = { save_scope_as = leased_barony }
		#		}
		#	}
		#}
		### Otherwise, grab an existing non-capital castle/city.
		#else = {
		#	scope:designated_county = {
		#		random_county_province = {
		#			limit = {
		#				is_county_capital = no
		#				OR = {
		#					has_holding_type = castle_holding
		#					has_holding_type = city_holding
		#				}
		#			}
		#			barony = { save_scope_as = leased_barony }
		#		}
		#	}
		#}
		
		# Select a random county.
		ordered_held_title = {
			limit = {
				tier = tier_county
				any_county_province = {
					OR = {
						has_holding = no
					}
				}
				OR = {
					is_holy_site_of = root.faith
					any_county_province = {
						barony = {
							is_holy_site_of = root.faith
						}
					}
				}
			}
			alternative_limit = {
				tier = tier_county
				any_county_province = {
					OR = {
						has_holding = no
					}
				}
			}
			save_scope_as = designated_county
		}
		# Select an appropriate barony.
		## If there's an empty space in the capital, we create a castle.
		#if = {
		#	limit = {
		#		scope:designated_county = {
		#			any_county_province = { has_holding = no }
		#		}
		#	}
		scope:designated_county = {
			random_county_province = {
				limit = { has_holding = no }
				set_holding_type = castle_holding
				barony = { save_scope_as = leased_barony }
			}
		}
		
		
		# Create a suitable leader
		create_character = {
			template = ate_holy_order_faith_leader_character
			location = scope:leased_barony.title_province
			culture = root.culture
			faith = scope:my_faith
			save_scope_as = leader
		}
		# Create the actual holy order.
		create_holy_order = {
			leader = scope:leader
			capital = scope:leased_barony
			save_scope_as = new_holy_order
		}
		scope:leader = {
			add_trait = order_member
			add_piety_level = 2
			add_gold = holy_order_starting_gold
			every_courtier = { add_trait = order_member }
		}

		scope:leased_barony.title_province = {
			# Walls & Towers.
			if = {
				limit = {
					NOT = { has_building_or_higher = curtain_walls_01 }
				}
				# If it doesn't have a free building slot, give it one.
				if = {
					limit = { free_building_slots = 0 }
					add_province_modifier = extra_building_slot
				}
				# Add the building.
				add_building = curtain_walls_01
			}
			# Barracks.
			if = {
				limit = {
					NOT = { has_building_or_higher = barracks_01 }
				}
				# If it doesn't have a free building slot, give it one.
				if = {
					limit = { free_building_slots = 0 }
					add_province_modifier = extra_building_slot
				}
				# Add the building.
				add_building = barracks_01
			}
			# Military Camps.
			if = {
				limit = {
					NOT = { has_building_or_higher = military_camps_01 }
				}
				# If it doesn't have a free building slot, give it one.
				if = {
					limit = { free_building_slots = 0 }
					add_province_modifier = extra_building_slot
				}
				# Add the building.
				add_building = military_camps_01
			}
		}

		create_holy_order_effect = yes

		scope:my_faith = {
			set_religious_head_title = scope:new_holy_order.title
		}
	}
}