﻿revoke_leased_title_interaction = {
	category = interaction_category_uncategorized
	hidden = yes

	desc = revoke_leased_title_interaction_desc

	target_type = title
	target_filter = recipient_lessee_titles
	
	auto_accept = yes

	is_shown = {
		scope:actor = {
			any_sub_realm_barony = {
				barony_is_valid_for_holy_order_lease_cancellation_trigger = { ACTOR = scope:actor }
				lessee ?= scope:recipient
			}
		}
	}

	#is_valid = {
	#	scope:actor = {
	#		piety >= holy_order_revoke_lease_piety_cost
	#	}
	#}

	# Unop: In order to make the piety cost display in the decision panel, add this entry
	# Unop: but because revoke_holy_order_lease_effect will also remove the piety cost, it will be re-added in the on_accept block
	cost = {
		piety = {
			value = 0
			if = {
				limit = {
					 exists = scope:recipient
					 exists = scope:recipient.faith
				}
				if = {
					limit = { scope:actor.faith = scope:recipient.faith }
					add = cancel_holy_order_piety_cost
				}
				else_if = {
					limit = {
						scope:actor.faith = {
							faith_hostility_level = {
								target = scope:recipient.faith
								value <= faith_astray_level
							}
						}
					}
					add = 100
				}
				else = {
					add = 0
				}
			}
		}
	}

	has_valid_target_showing_failures_only = {
		NOT = { scope:actor = { is_at_war_with = scope:recipient } }
		#scope:target = {
		#	NOT = {
		#		recent_history = {
		#			type = leased_out
		#			years = 1
		#		}
		#	}
		#}
		scope:target = {
			has_revokable_lease = yes
			lessee = scope:recipient
			OR = {
				county.holder = { is_ai = yes }
				county.holder = scope:actor
			}
		}
		scope:recipient = { NOT = { has_strong_hook = scope:actor } }
	}

	on_accept = {
		revoke_holy_order_lease_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			BARONY = scope:target
		}
		#Unop: The piety cost of the decision is taken twice, so re-give half of it
		hidden_effect = {
			scope:actor = {
				add_piety = {
					value = 0
					if = {
						limit = {
							exists = scope:recipient
							exists = scope:recipient.faith
						}
						if = {
							limit = { scope:actor.faith = scope:recipient.faith }
							add = cancel_holy_order_piety_cost
						}
						else_if = {
							limit = {
								scope:actor.faith = {
									faith_hostility_level = {
										target = scope:recipient.faith
										value <= faith_astray_level
									}
								}
							}
							add = 100
						}
						else = {
							add = 0
						}
					}
				}
			}
		}
	}

	# AI
	ai_targets = {
		ai_recipients = vassals
	}
	ai_frequency_by_tier = {
		barony = 0
		county = 60
		duchy = 12
		kingdom = 12
		empire = 12
		hegemony = 12
	}

	ai_potential = {
		is_playable_character = yes
		piety >= cancel_holy_order_piety_cost #Prevent AI to go into negative piety
	}

	ai_will_do = {
		base = 0

		# revoke titles leased to holy orders of the wrong faith
		modifier = {
			add = 100
			scope:recipient = {
				government_has_flag = government_is_holy_order
				faith != scope:actor.faith
			}
		}
	}
}
