﻿join_war_interaction = {
	category = interaction_category_diplomacy
	interface = interfere_in_war
	special_interaction = interfere_in_war_interaction
	popup_on_receive = yes
	desc = join_war_interaction_desc

	greeting = positive
	notification_text = join_war_interaction_notification

	is_shown = {
		scope:recipient = {
			is_at_war = yes
			NOR = {
				this = scope:actor
				is_at_war_with = scope:actor
			}
		}
	}

	is_valid_showing_failures_only = {

		trigger_if = {
			limit = { exists = liege }
			custom_description = {
				text = join_war_interaction_recipient_warring_with_my_liege
				subject = scope:recipient
				scope:actor = {
					NOT = {
						any_liege_or_above = {
							is_at_war_with = scope:recipient
						}
					}
				}
			}
		}

		# special exception for liege trying to put down his vassal's peasant revolt
		trigger_if = {
			limit = {
				scope:actor = {
					target_is_vassal_or_below = scope:recipient
				}
			}
			custom_description = {
				text = join_war_interaction_recipient_not_fighting_peasant_faction
				subject = scope:recipient
				OR = {
					scope:recipient = {
						any_war_enemy = {
							is_leading_faction_type = peasant_faction
						}
					}
					AND = {
						scope:recipient = {
							is_allied_to = scope:actor
						}
						scope:recipient = {
							any_war_enemy = {
								target_is_liege_or_above = scope:recipient
							}
						}
					}					
				}
			}
		}	
		# recipient is ally or defending against a holy war
		trigger_else_if = {
			limit = {
				scope:actor = {
					NOT = { target_is_liege_or_above = scope:recipient }
				}
			}
			trigger_if = {
				limit = {
					scope:recipient = {
						any_character_war = {
							scope:recipient.faith.religion = scope:actor.faith.religion
							any_war_defender = { this = scope:recipient }
						}
					}
				}
				# Deliberately blank; we've passed the trigger and making a desc out of this doesn't make sense
			}
			trigger_else = {
				custom_description = {
					text = join_war_interaction_recipient_not_ally
					subject = scope:recipient
					scope:recipient = {
						is_allied_to = scope:actor
					}
				}
			}
		}
		# recipient is liege
		trigger_else = {
			custom_description = {
				text = join_war_interaction_recipient_not_liege
				subject = scope:recipient
				scope:actor = {
					target_is_liege_or_above = scope:recipient
				}
			}
		}

		# recipient is not in (any) war against your liege(s)
		trigger_if = {
			limit = { exists = liege }
			custom_description = {
				text = join_war_interaction_recipient_warring_with_my_liege
				subject = scope:recipient
				scope:actor = {
					NOT = {
						any_liege_or_above = {
							is_at_war_with = scope:recipient
						}
					}
				}
			}
		}
	}
	
	can_be_picked = {
		scope:target = {

			# recipient is war leader
			is_war_leader = scope:recipient

			# not already in target war
			custom_description = {
				text = join_war_interaction_already_in_target_war
				NOR = {
					any_war_attacker = { this = scope:actor }
					any_war_defender = { this = scope:actor }
				}
			}

			# if holy war, recipient is defender of same faith
			# and not too far away
			trigger_if = {
				limit = { is_religious_war = yes }
				custom_description = {
					text = join_war_interaction_recipient_not_def_GHW
					subject = scope:recipient
					scope:recipient.faith.religion = scope:actor.faith.religion
					any_war_defender = { this = scope:recipient }
				}
				custom_description = {
					text = join_war_interaction_recipient_too_distant
					subject = scope:recipient
					scope:actor.capital_province = {
						squared_distance = {
							target = scope:recipient.capital_province
							value <= squared_distance_large
						}
					}
				}
			}

			# If not holy war, special exception for liege trying to put down his vassal's peasant revolt.	
			trigger_if = {
				limit = {
					is_religious_war = no
					scope:actor = { target_is_vassal_or_below = scope:recipient }
					primary_defender = scope:recipient
					primary_attacker = {
						is_leading_faction_type = peasant_faction
					}
				}
				always = yes
			}

			# ...otherwise, recipient must be an ally...
			trigger_else_if = {
				limit = {
					is_religious_war = no
					scope:actor = { NOT = { target_is_liege_or_above = scope:recipient } }
				}
				custom_description = {
					text = join_war_interaction_recipient_not_ally
					subject = scope:recipient
					scope:actor = {
						is_allied_to = scope:recipient
					}
				}
				trigger_if = {
					limit = {
						scope:actor = {
							target_is_vassal_or_below = scope:recipient
							NOT = { is_allied_to = scope:recipient }
						}
					}
					# If they are an unallied vassal, inform the player that we could have aided them if this was a peasant revolt.
					custom_description = {
						text = join_war_interaction_recipient_not_fighting_peasant_faction
						subject = scope:recipient
						primary_defender = scope:recipient
						primary_attacker = {
							is_leading_faction_type = peasant_faction
						}
					}
				}
			}

			# ...or our liege.
			trigger_else_if = {
				limit = {
					is_religious_war = no
					scope:actor = { NOT = { is_allied_to = scope:recipient } }
				}
				# recipient is liege
				custom_description = {
					text = join_war_interaction_recipient_not_liege
					subject = scope:recipient
					scope:actor = {
						target_is_liege_or_above = scope:recipient
					}
				}
			}
			# making the default behavior explicit if no other trigger_(else_)ifs are valid
			trigger_else = {
				always = yes
			}
		}

		scope:actor = {
			# not already at war with primary enemy
			custom_description = {
				text = join_war_interaction_already_warring_against_enemy
				subject = scope:recipient
				NOR = {
					any_war_enemy = { this = scope:target.primary_attacker }
					any_war_enemy = { this = scope:target.primary_defender }
				}
			}
		}
	}

	on_accept = {
		scope:target = {
			hidden_effect = {
				set_called_to = scope:actor
			}
			if = {
				limit = {
					is_attacker = scope:recipient
				}
				add_attacker = scope:actor
			}
			else = {
				add_defender = scope:actor
			}

			### invalidate wars between vassals and the side you're joining

			# create list of recipient-side combatants
			if = {
				limit = { any_war_attacker = { this = scope:recipient } }
				every_war_attacker = {
					limit = {
						NOR = {
							this = scope:recipient
							this = scope:actor
						}
					}
					add_to_list = join_war_interaction_combatants
				}
			}
			else = {
				every_war_defender = {
					limit = {
						NOR = {
							this = scope:recipient
							this = scope:actor
						}
					}
					add_to_list = join_war_interaction_combatants
				}
			}
		}

		scope:actor = {

			# check list against vassal war enemies
			if = {
				limit = {
					any_vassal = {
						any_in_list = {
							list = join_war_interaction_combatants
							is_at_war_with = prev # could be a temp saved scope? Feels wrong.
						}
					}
				}
				every_vassal = {
					limit = {
						any_in_list = {
							list = join_war_interaction_combatants
							is_at_war_with = prev # prev = vassal
						}
					}

					### end wars
					every_character_war = {
						limit = {
							# vassal is attacker, recipient-side character is defender
							trigger_if = {
								limit = { is_attacker = prev } # prev = vassal
								any_in_list = {
									list = join_war_interaction_combatants
									is_defender_in_war = prev # prev = war
									save_temporary_scope_as = invalid_combatant
								}
							}
							# vassal is defender, recipient-side character is attacker
							trigger_else_if = {
								limit = { is_defender = prev } # prev = vassal
								any_in_list = {
									list = join_war_interaction_combatants
									is_attacker_in_war = prev # prev = war
									save_temporary_scope_as = invalid_combatant
								}
							}
							# making the default behavior explicit if no other trigger_(else_)ifs are valid
							trigger_else = {
								always = yes
							}
						}
						
						end_war = invalidated

						# send message to stood-down vassals
						prev = { # prev = vassal
							send_interface_message = {
								type = msg_war_ended_invalid
								desc = join_war_interaction_vassal_war_invalidated_message
								right_icon = scope:invalid_combatant
								left_icon = this
							}
						}
					}
				}
			}

			stress_impact = {
				craven = medium_stress_impact_gain
				shy = minor_stress_impact_gain
			}
		}
		scope:recipient = {
			if = {
				limit = {
					is_ai = yes
				}
				progress_towards_friend_effect = {
					CHARACTER = scope:actor
					OPINION = no
				}
			}
			else = {
				hidden_effect = { #To nudge friendship
					if = {
						limit = {
							NOR = {
								has_relation_friend = scope:actor
								has_relation_potential_friend = scope:actor
							}
						}
						set_relation_potential_friend = scope:actor
					}
				}
			}
		}
	}

	on_decline = {
		scope:actor = {
			trigger_event = char_interaction.0236
		}
	}

	auto_accept = yes

	ai_accept = {
		base = 100 # everyone wants help
	}
}
