create_holy_order_decision = {
	picture = "gfx/interface/illustrations/decisions/holy_order.dds"
	major = yes

	ai_goal = yes

	is_shown = {
		is_landed = yes
		NOT = {
			faith = {
				any_faith_holy_order = {
					holy_order_patron = root
					save_temporary_scope_as = order
				}
			}
		}
	}

	widget = {
		controller = create_holy_order
		barony_valid = {
			trigger_if = {
				limit = { exists = this }
				barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = scope:ruler }
			}
			trigger_else = {
				custom_description = {
					text = "has_holy_order_barony_selected"
					always = no
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		is_available_adult = yes
		is_at_war = no
	}

	is_valid = {
		custom_description = {
			text = "is_king_or_emperor"
			highest_held_title_tier >= tier_kingdom
		}
		custom_description = {
			text = "has_two_castle_county"
			any_held_county = {
				any_in_de_jure_hierarchy = {
					barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = scope:ruler }
				}
			}
		}
		trigger_if = {
			limit = {
				exists = faith.religious_head
				NOT = { root = root.faith.religious_head }
			}
			faith.religious_head = {
				opinion = {
					target = root
					value >= high_positive_opinion
				}
			}
		}
		piety_level >= 3
	}

	cost = {
		gold = {
			value = holy_order_gold_cost
		}
		piety = {
			value = holy_order_piety_cost
		}
	}

	effect = {
		custom_tooltip = create_holy_order_decision_effect_message

		hidden_effect = {
			if = {
				limit = { NOT = { scope:barony.holder = root } }
				create_title_and_vassal_change = {
					type = leased_out
					save_scope_as = change
					add_claim_on_loss = no
				}
				scope:barony = {
					change_title_holder_include_vassals = {
						holder = root
						change = scope:change
					}
				}
				resolve_title_and_vassal_change = scope:change
			}
		}

		create_character = {
			template = holy_order_leader_character
			location = scope:barony.title_province
			save_scope_as = leader
		}

		create_holy_order = {
			leader = scope:leader
			capital = scope:barony
			save_scope_as = new_holy_order
		}

		hidden_effect = {
			scope:leader = {
				add_gold = 100 #So that they have some money to lend out
				add_piety_level = 2
				add_gold = holy_order_starting_gold
				every_courtier = {
					add_trait = order_member
				}
			}
			
			# Messages
			send_interface_toast = {
				type = holy_order_founded_message
				desc = i_created_holy_order_message
				left_icon = scope:leader
				right_icon = scope:new_holy_order.title
			}
			save_scope_as = holy_order_creator
			every_ruler = {
				limit = {
					faith = scope:holy_order_creator.faith
					NOT = { this = scope:holy_order_creator }
				}
				send_interface_message = {
					type = holy_order_founded_message
					desc = someone_created_holy_order_message
					left_icon = scope:holy_order_creator
					right_icon = scope:new_holy_order.title
				}
			}
			every_neighboring_top_liege_realm_owner = {
				limit = {
					NOT = { faith = scope:holy_order_creator.faith }
				}
				send_interface_message = {
					type = enemy_holy_order_founded_message
					desc = other_faith_neighbor_created_holy_order_message
					left_icon = scope:holy_order_creator
					right_icon = scope:new_holy_order.title
				}
			}

			add_achievement_flag = { FLAG = achievement_non_nobis_domine_flag }

			hidden_effect = {
				if = {
					limit = { exists = global_var:started_give_a_dog_a_bone_achievement }
					add_achievement_global_variable = {
						VARIABLE = holy_order_give_a_dog_a_bone_achievement
						VALUE = yes
					}
				}
			}
		}
	}
	
	ai_potential = {
		highest_held_title_tier >= tier_kingdom
		is_landed = yes
		OR = {
			any_held_title = {
				tier = tier_barony
				barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = root }
			}
			any_vassal = {
				primary_title.tier = tier_barony
				any_held_title = {
					barony_is_valid_for_holy_order_lease_trigger = { CHARACTER = root }
				}
			}
		}
	}

	ai_will_do = {
		base = 100
		modifier = {
			add = {
				faith = {
					# Don't create more than 5 holy orders for a faith
					every_faith_holy_order = {
						subtract = 20
					}
				}
			}
		}
	}
	
	ai_priority = 100
}

borrow_from_holy_order_decision = { #by Linnéa Thimrén
	picture = "gfx/interface/illustrations/decisions/decision_realm.dds"

	desc = borrow_from_holy_order_decision_desc
	selection_tooltip = borrow_from_holy_order_decision_tooltip

	ai_check_interval = 72

	cost = {
		piety = 50
	}

	cooldown = { days = 5475 }

	is_shown = {
		is_landed = yes
		highest_held_title_tier > tier_barony
		faith = {
			any_faith_holy_order = { #Your faith must have a Holy Order
				always = yes
			}
		}
		NOR = {
			exists = var:loan_amount_owed
			exists = var:loan_holder
		}
	}

	is_valid_showing_failures_only = {
		is_available = yes
		custom_description = {
			text = borrow_from_holy_order_gold_condition
			faith = {
				any_faith_holy_order = {
					leader = {
						gold >= holy_order_gold_value
					}
				}
			}
		}
	}

	effect = {
		hidden_effect = {
			set_variable = {
				name = amount_to_loan
				value = holy_order_gold_value
				days = 1
			}
			add_character_flag = {
				flag = borrow_from_holy_order
				days = 3560
			}
			faith = {
				random_faith_holy_order = {
					limit = {
						leader = { gold >= holy_order_gold_value }
					}
					leader = {
						save_scope_as = recipient
					}
					save_scope_as = order
				}
			}
			trigger_event = holy_order.0200
		}
		custom_tooltip = borrow_from_holy_order_decision_effect_tooltip

	}
	
	ai_potential = {
		always = yes
	}

	ai_will_do = {
		base = 50
		ai_value_modifier = {
			ai_zeal = -0.5
		}
		ai_value_modifier = {
			ai_greed = 0.5
		}
	}
}

cancel_holy_order_lease_decision = {
	picture = "gfx/interface/illustrations/decisions/decision_major_religion.dds"

	desc = {
		first_valid = {
			triggered_desc = {
			    trigger = {
					faith = {
						faith_hostility_level = {
							target = scope:barony.lessee_title.holder.faith
							value >= faith_hostile_level
						}
					}
			    }
			    desc = cancel_holy_order_lease_hostile_decision_desc
			}
			desc = cancel_holy_order_lease_decision_desc
		}
	}

	ai_check_interval = 360

	is_shown = {
		is_landed = yes
		any_held_title = {
			tier = tier_barony
			barony_is_valid_for_holy_order_lease_cancellation_trigger = yes
		}
	}

	widget = {
		controller = revoke_holy_order_lease
		barony_valid = {
			trigger_if = {
				limit = { exists = this }
				barony_is_valid_for_holy_order_lease_cancellation_trigger = yes
			}
			trigger_else = {
				custom_description = {
					text = "has_holy_order_barony_selected"
					always = no
				}
			}
		}
	}

	is_valid_showing_failures_only = {
		is_available_adult = yes
		is_at_war = no
	}

	is_valid = {
		
	}

	cost = {
		piety = {
			value = cancel_holy_order_piety_cost
		}
	}

	effect = {
		scope:barony.lessee_title.holder = {
			faith = {
				if = {
					limit = {
						exists = religious_head
						NOT = { religious_head = root }
					}
					religious_head = {
						add_opinion = {
							modifier = angry_opinion
							target = root
							opinion = -20
						}
					}
				}
				random_faith_holy_order = {
					limit = {
						leader = { location = scope:barony.title_province } #So you save the correct order
					}
					save_temporary_scope_as = holy_order
					if = {
						limit = {
							NOT = { leader = root }
						}
						leader = {
							add_opinion = {
								modifier = angry_opinion
								target = root
								opinion = -40
							}
						}
					}
				}
			}
		}

		scope:barony = {
			revoke_lease = yes
		}

		#show_as_tooltip = { #To give warning about disbanding the holy order
		#	if = {
		#		limit = {
		#			scope:holy_order = {
		#				num_leased_titles = 1
		#			}
		#		}
		#		custom_tooltip = destroy_holy_order_warning
		#	}
		#}

		hidden_effect = {

			#Message is sent through the on_holy_order_destroyed on_action instead!
			#send_interface_toast = {
			#	type = holy_order_founded_message
			#	desc = i_created_holy_order_message
			#	left_icon = scope:leader
			#	right_icon = scope:new_holy_order.title
			#}
			#save_scope_as = holy_order_creator
			#every_ruler = {
			#	limit = {
			#		faith = scope:holy_order_creator.faith
			#		NOT = { this = scope:holy_order_creator }
			#	}
			#	send_interface_message = {
			#		type = holy_order_founded_message
			#		desc = someone_created_holy_order_message
			#		left_icon = scope:holy_order_creator
			#		right_icon = scope:new_holy_order.title
			#	}
			#}
			#every_neighboring_top_liege_realm_owner = {
			#	limit = {
			#		NOT = { faith = scope:holy_order_creator.faith }
			#	}
			#	send_interface_message = {
			#		type = enemy_holy_order_founded_message
			#		desc = other_faith_neighbor_created_holy_order_message
			#		left_icon = scope:holy_order_creator
			#		right_icon = scope:new_holy_order.title
			#	}
			#}
		}
	}
	
	ai_potential = {
		is_landed = yes
		highest_held_title_tier >= tier_county
		any_held_title = {
			tier = tier_barony
			is_leased_out = yes
		}
	}

	ai_will_do = {
		base = 0
		# revoke titles leased to holy orders of the wrong faith
		modifier = {
			add = 100
			exists = scope:barony.lessee_title.holder
			NOT = {
				scope:barony.lessee_title.holder.faith = root.faith
			}
		}
	}
	
	ai_priority = 100
}