﻿warden_call_warden_interaction = {
	category = interaction_category_diplomacy
	desc = warden_call_warden_interaction_desc
	interface_priority = 65
	interface = call_ally
	popup_on_receive = yes 
	pause_on_receive = yes
	common_interaction = yes
	icon = alliance

	greeting = positive
	notification_text = CALL_ALLY_NOTIFICATION

	is_shown = {
		scope:actor = {
			OR = {
				is_at_war_as_defender = yes 
				has_game_rule = warden_both_enabled 
			}
			
			is_liege_or_above_of = scope:recipient
		}
		scope:recipient = {
			vassal_contract_has_flag = has_warden_contract
		}
	} 
 
	has_valid_target = {
	}

	has_valid_target_showing_failures_only = {
		exists = scope:target

		#Set the primary enemy
		scope:target = { primary_attacker = { save_temporary_scope_as = primary_enemy } }

		scope:target = {
			is_war_leader = scope:actor
			OR = {
				is_defender = scope:actor
				has_game_rule = warden_both_enabled 
			}
		}
	}

	is_valid_showing_failures_only = {

		scope:recipient = {
			is_ruler = yes
		}

		# Only War Leaders can call allies
		scope:actor = {
			custom_description = {
				text = must_be_war_leader
				any_character_war = {
					is_war_leader = scope:actor
				}
			}
		}

		# Cannot call the same character multiple times
		scope:actor = {
			custom_description = {
				text = cannot_call_same_character_multiple_times
				subject = scope:recipient
				any_character_war = {
					ALL_FALSE = {
						was_called = scope:recipient
						is_participant = scope:recipient
					}
				}
			}
		}

		# Can only call for defensive wars
		OR = { 
			scope:actor = {
				custom_description = {
					text = must_be_war_leader
					any_character_war = {
						is_defender = scope:actor
					}
				}
			}
			has_game_rule = warden_both_enabled
		}

	}

	on_auto_accept = {
		scope:recipient = {
			trigger_event = call_ally.0001
		}
	}

	on_accept = {
		call_ally_interaction_effect = yes

		save_scope_value_as = {
			name = call_ally_interaction
			value = yes
		}

		scope:actor = {
			hidden_effect = { #To nudge friendship
				if = {
					limit = {
						NOR = {
							has_relation_friend = scope:recipient
							has_relation_potential_friend = scope:recipient
						}
					}
					set_relation_potential_friend = scope:recipient
				}
			}
			trigger_event = call_ally.0100
		}
	}

	on_decline = {
		#The war could theoretically end on the day the decline is sent
		if = {
			limit = {
				exists = scope:target
			}
			scope:target = {
				if = {
					limit = {
						is_attacker = scope:actor
					}
					scope:actor = {
						add_opinion = {
							modifier = rejected_call_to_offensive_war
							target = scope:recipient
						}
					}
				}
				else = {
					scope:actor = {
						add_opinion = {
							modifier = rejected_call_to_defensive_war
							target = scope:recipient
						}
					}
				}
				hidden_effect = {
					if = {
						limit = {
							NOT = { was_called = scope:recipient }
						}
						set_called_to = scope:recipient
					}
				}
			}
			scope:actor = {
				trigger_event = call_ally.0101
			}
		}
	}

	auto_accept = {
		OR = {
			# Always accept a call from a Spouse
			custom_description = {
				text = "recipient_is_spouse"
				subject = scope:actor
				object = scope:recipient
				scope:recipient = {
					is_spouse_of = scope:actor
					is_ai = yes
				}
			}
			# Always accept a call from the Heir
			custom_description = {
				text = "is_recipients_player_heir"
				subject = scope:actor
				object = scope:recipient

				exists = scope:recipient.player_heir
				scope:recipient = {
					player_heir = scope:actor
					is_close_or_extended_family_of = scope:actor # Only for family
					is_ai = yes
				}
			}
			# Always accept a call from the one you are heir of
			custom_description = {
				text = "is_player_heir"
				subject = scope:recipient
				object = scope:actor

				exists = scope:actor.player_heir
				scope:actor.player_heir = scope:recipient
				scope:actor = { # Shouldn't be able to force your liege to join just because you've not got any family...
					is_close_or_extended_family_of = scope:recipient
				}
				scope:recipient = {
					is_ai = yes
				}
			}
		}
	}

	ai_will_do = {
		base = 25
	}

	ai_accept = {
		base = 25

		modifier = {  # Refuse call against Heir
			add = -1000
			exists = scope:recipient.player_heir
			trigger_if = {
				limit = { scope:target.casus_belli.primary_defender = scope:actor }
				scope:target.casus_belli.primary_attacker = scope:recipient.player_heir
			}
			trigger_else = {
				scope:target.casus_belli.primary_defender = scope:recipient.player_heir
			}
			desc = WONT_FIGHT_HEIR_REASON
		}

		modifier = {  # Refuse call against Spouse
			add = -1000
			scope:recipient = {
				trigger_if = {
					limit = { scope:target.casus_belli.primary_defender = scope:actor }
					any_spouse = {
						this = scope:target.casus_belli.primary_attacker
					}
				}
				trigger_else = {
					any_spouse = {
						this = scope:target.casus_belli.primary_defender
					}
				}

			}
			desc = WONT_FIGHT_SPOUSE_REASON
		}

		opinion_modifier = { # Opinion Factor
			who = scope:recipient
			opinion_target = scope:actor
			multiplier = 1.0
			desc = AI_OPINION_REASON
		}

		# Honor factor
		ai_value_modifier = {
			ai_honor = 1
			min = -25
			max = 25
		}

		modifier = {  # Reluctant to defend against another ally
			add = -25
			scope:target.casus_belli.primary_defender = scope:actor
			scope:recipient = {
				is_allied_to = scope:target.casus_belli.primary_attacker
			}
			desc = WAR_WITH_ALLY_REASON
		}
	}
}
 