﻿grant_independence_interaction = {
	category = interaction_category_vassal
	force_notification = yes
	greeting = positive
	notification_text = GRANT_INDEPENDENCE_INTERACTION_NOTIFICATION
	icon = independence
	interface_priority = 120

	desc = grant_independence_interaction_desc

	is_shown = {
		scope:actor = {
			is_confederation_member = no
		}
		scope:recipient = {
			is_vassal_of = scope:actor
		}
		NOT = { scope:recipient = scope:actor }
	}

	is_valid_showing_failures_only = {
		NOT = { scope:actor = { is_at_war_with = scope:recipient } }
		scope:actor = {
			is_independent_ruler = yes
			NOT = {
				is_at_war = yes
			}
		}
		custom_description = {
			text = is_at_war_with_another_vassal
			object = scope:recipient
			NOT = {
				scope:actor = {
					any_vassal_or_below = {
						is_at_war_with = scope:recipient
					}
				}
			}
		}
		#custom_description = {
			#text = is_de_jure_vassal_check
			#object = scope:recipient
			
			#trigger_if = { # Admin rulers can grant tribal vassals independence regardless of de jure
				#limit = {
					#scope:actor = { government_has_flag = government_is_administrative }
					#scope:recipient = { government_has_flag = government_is_tribal }
				#}
				# Always possible
			#}
			#trigger_else = {
				#NOT = { #Cannot be used against de jure vassals
					#scope:recipient.primary_title = {
						#any_this_title_or_de_jure_above = {
							#holder = scope:actor
						#}
					#}
				#}
			#}
			
		#}
		custom_tooltip = {
			text = can_only_remove_diarch_by_elevating_them.tt
			NOT = { scope:recipient ?= scope:actor.diarch }
		}
	}

	auto_accept = yes

	on_accept = {
		scope:actor = {
			stress_impact = {
				ambitious = medium_stress_impact_gain
				arrogant = minor_stress_impact_gain
				greedy = minor_stress_impact_gain
			}
			hidden_effect = {
				send_interface_toast = {
					type = event_toast_effect_neutral
					title = grant_vassal_independence_interaction_toast
					left_icon = scope:actor
					right_icon = scope:recipient

					custom_tooltip = grant_vassal_independence_interaction_toast_desc

					# Struggle Parameter
					if = {
						limit = {
							exists = dynasty
							NOT = {
								any_held_title = {
 									is_de_jure_liege_or_above_target = scope:recipient.primary_title
								}
							}
							any_character_struggle = {
								involvement = involved
								has_struggle_phase_parameter = granting_independence_to_non_dejure_gives_renown
							}
						}
						dynasty = {
							add_dynasty_prestige = medium_dynasty_prestige_value
						}
					}
				}
			}
		}

		scope:recipient = {
			#Send notifications
			if = {
				limit = {
					any_liege_or_above = {
						is_ai = no
						NOT = { this = scope:actor }
					}
				}
				every_liege_or_above = {
					limit = {
						is_ai = no
						NOT = { this = scope:actor }
					}
					trigger_event = vassal_interaction.0023
				}
			}
			if = {
				limit = {
					any_liege_or_above = {
						any_vassal = {
							is_ai = no
							NOR = {
								this = scope:recipient
								this = scope:actor
							}
						}
					}
				}
				every_liege_or_above = {
					limit = {
						any_vassal = {
							is_ai = no
							NOR = {
								this = scope:recipient
								this = scope:actor
							}
						}
					}
					every_vassal = {
						limit = {
							is_ai = no
							NOT = { this = scope:recipient }
						}
						trigger_event = {
							id = vassal_interaction.0022
							days = 3
						}
					}
				}
			}

			add_opinion = {
				target = scope:actor
				modifier = granted_independence_opinion
			}

			add_truce_both_ways = {
				character = scope:actor
				days = 3650
				name = TRUCE_GRANT_INDEPENDENCE
			}

			create_title_and_vassal_change = {
				type = independency
				save_scope_as = change
				add_claim_on_loss = yes
			}
			becomes_independent = {
				change = scope:change
			}

			resolve_title_and_vassal_change = scope:change
		}

		# Struggle Catalysts.
		scope:actor = {
			if = {
				limit = {
					catalyst_gave_independence_to_powerful_diff_faith_culture_vassal_preliminary_trigger = {
						CHAR1 = scope:actor
						CHAR2 = scope:recipient
					}
					any_character_struggle = {
						involvement = involved
						activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
							CATALYST = catalyst_gave_independence_to_powerful_diff_faith_culture_vassal
							CHAR = scope:recipient
						}
					}
				}
				every_character_struggle = {
					involvement = involved
					limit = {
						activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
							CATALYST = catalyst_gave_independence_to_powerful_diff_faith_culture_vassal
							CHAR = scope:recipient
						}
					}
					activate_struggle_catalyst = {
						catalyst = catalyst_gave_independence_to_powerful_diff_faith_culture_vassal
						character = scope:actor
					}
				}
			}

			if = {
				limit = {
					NOT = {
						any_held_title = {
							is_de_jure_liege_or_above_target = scope:recipient.primary_title
						}
					}
					any_character_struggle = {
						involvement = involved
						activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
							CATALYST = catalyst_independence_from_non_dejure_vassal
							CHAR = scope:recipient
						}
					}
				}
				every_character_struggle = {
					involvement = involved
					limit = {
						activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
							CATALYST = catalyst_independence_from_non_dejure_vassal
							CHAR = scope:recipient
						}
					}
					activate_struggle_catalyst = {
						catalyst = catalyst_independence_from_non_dejure_vassal
						character = scope:actor
					}
				}
			}
		}

		# Grant independence to non de_jure


		# If we're a clan this interaction affects unity
		add_clan_unity_interaction_effect = {
			CHARACTER = scope:actor
			TARGET = scope:recipient
			VALUE = medium_unity_loss
			DESC = clan_unity_grant_independence.desc
			REVERSE_NON_HOUSE_TARGET = no
		}
	}

	ai_will_do = {
		base = 0 #The AI should never do this!

		# Except in the context of Struggle
		modifier = {
			scope:recipient = {
				any_character_struggle = {
					involvement = involved
				}
			}
			scope:actor = {
				any_character_struggle = {
					involvement = involved
				}
				NOT = {
					any_held_title = {
						is_de_jure_liege_or_above_target = scope:recipient.primary_title
					}
				}
			}
			add = {
				value = 0
				if = {
					limit = {
						scope:actor = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_independence_from_non_dejure_vassal
							}
							has_character_flag = agenda_towards_escalation
						}
					}
					add = -50
				}
				else_if = {
					limit = {
						scope:actor = {
							any_character_struggle = {
								phase_has_catalyst = catalyst_independence_from_non_dejure_vassal
							}
						}
					}
					add = 100
				}
			}
		}

		# Unity modifiers
		evaluate_action_decreasing_house_unity = {
			VALUE = 10
		}
	}
}