﻿marriage_interaction_on_accept_effect = {
	#Use hook
	if = {
		limit = { always = scope:hook }
		scope:actor = {
			use_hook = scope:recipient
		}
		#To make sure betrothals can't be broken if a hook was used
		if = {
			limit = {
				OR = {
					scope:secondary_actor = { is_adult = no }
					scope:secondary_recipient = { is_adult = no }
				}
			}
			scope:secondary_actor = {
				set_variable = {
					name = hook_used_for_betrothal
					value = scope:secondary_recipient
				}
			}
		}
	}
	
	#Penalties for marrying a lowborn
	scope:actor = {
		if = {
			limit = {
				this = scope:secondary_actor # We check that you're marrying them yourself
				scope:secondary_recipient = { is_lowborn = yes }
			}
			# Vassal dispositions get aggy.
			## Actual effect applied in the on_marriage on_action.
			show_as_tooltip = {
				vassals_dislike_marry_lowborn_effect = {
					SPOUSE = scope:secondary_recipient
					RULER = scope:actor
				}
			}
			#Legitimacy loss for marrying a lowborn
			if = {
				limit = {
					is_valid_for_legitimacy_change = yes
					NOT = { has_variable = lowborn_legitimacy_cooldown }
					OR = {
						NOT = { exists = primary_spouse }
						primary_spouse ?= scope:secondary_recipient
					}
				}
				if = {
					limit = { is_ai = no }
					send_interface_toast = {
						type = event_toast_effect_good
						title = married_lowborn_toast
						left_icon = scope:actor
						add_legitimacy = {
							value = medium_legitimacy_loss
							multiply = scope:actor.primary_title.tier
						}
					}
				}
				else = { # To avoid destroying polygamy AIs
					send_interface_toast = {
						type = event_toast_effect_good
						title = married_lowborn_toast
						left_icon = scope:actor
						add_legitimacy = { value = minor_legitimacy_loss }
					}
					hidden_effect = {
						set_variable = {
							name = lowborn_legitimacy_cooldown
							years = 10
						}
					}
				}
			}
		}
		if = {
			limit = {
				this = scope:secondary_recipient # We check that you're marrying them yourself
				scope:secondary_actor = { is_lowborn = yes }
			}
			# Vassal dispositions get aggy.
			## Actual effect applied in the on_marriage on_action.
			show_as_tooltip = {
				vassals_dislike_marry_lowborn_effect = {
					SPOUSE = scope:secondary_actor
					RULER = scope:actor
				}
			}
			#Legitimacy loss for marrying a lowborn
			if = {
				limit = {
					is_valid_for_legitimacy_change = yes
					NOT = { has_variable = lowborn_legitimacy_cooldown }
					OR = {
						NOT = { exists = primary_spouse }
						primary_spouse ?= scope:secondary_actor
					}
				}
				if = {
					limit = { is_ai = no }
					send_interface_toast = {
						type = event_toast_effect_good
						title = married_lowborn_toast
						left_icon = scope:actor
						add_legitimacy = {
							value = medium_legitimacy_loss
							multiply = scope:actor.primary_title.tier
						}
					}
				}
				else = { # To avoid destroying polygamy AIs
					send_interface_toast = {
						type = event_toast_effect_good
						title = married_lowborn_toast
						left_icon = scope:actor
						add_legitimacy = { value = minor_legitimacy_loss }
					}
					hidden_effect = {
						set_variable = {
							name = lowborn_legitimacy_cooldown
							years = 10
						}
					}
				}
			}
		}
		else = { # If they're not a lowborn AND you and the recipient are both Nomad we may give you some Legitimacy
			if = {
				limit = {
					is_valid_for_nomadic_legitimacy_change = yes
					scope:recipient = { government_has_flag = government_is_nomadic }
					mpo_lower_nomad_authority_trigger = { CHARACTER = scope:recipient } # Only if recipient has higher Dominance than you
				}
				add_legitimacy = medium_legitimacy_gain
			}
		}
	}
	
	#No longer Herder
	if = {
		limit = {
			scope:secondary_recipient = {
				is_landed = yes
				government_has_flag = government_is_herder
			}
		}
		scope:actor = {
			switch = {
				trigger = has_government
				feudal_government = {
					scope:secondary_recipient = { change_government = feudal_government }
				}
				tribal_government = {
					scope:secondary_recipient = { change_government = tribal_government }
				}
				clan_government = {
					scope:secondary_recipient = { change_government = clan_government }
				}
				nomad_government = {
					scope:secondary_recipient = { change_government = nomad_government }
				}
				administrative_government = {
					scope:secondary_recipient = { change_government = administrative_government }
				}
				landless_adventurer_government = {
					scope:secondary_recipient = { change_government = landless_adventurer_government }
				}
			}
		}
	}

	#No longer concubine
	if = {
		limit = {
			scope:secondary_recipient = { is_concubine = yes }
		}
		scope:secondary_recipient = {
			random_consort = {
				limit = {
					any_concubine = { this = scope:secondary_recipient }
				}
				remove_concubine = scope:secondary_recipient
			}
		}
	}
	if = {
		limit = {
			scope:secondary_actor = { is_concubine = yes }
		}
		scope:secondary_actor = {
			random_consort = {
				limit = {
					any_concubine = { this = scope:secondary_actor }
				}
				remove_concubine = scope:secondary_actor
			}
		}
	}
	# A Grand Wedding was promised
	if = {
		limit = {
			scope:grand_wedding_promise = yes
		}
		set_grand_wedding_betrothal_variables = {
			SPOUSE_1 = scope:secondary_actor
			SPOUSE_2 = scope:secondary_recipient
			HOST = scope:actor
			PROMISEE = scope:recipient
		}
	}

	#Marriage notification events
	hidden_effect = {
		scope:actor = {
			if = {
				limit = { this != scope:recipient }
				trigger_event = marriage_interaction.0010
			}
			else = { #In my own court
				if = {
					limit = { #Betrothal?
						OR = {
							scope:secondary_actor = { is_adult = no }
							scope:secondary_recipient = { is_adult = no }
							scope:secondary_actor = { has_been_promised_grand_wedding = yes }
							scope:secondary_recipient = { has_been_promised_grand_wedding = yes }
						}
					}
					send_interface_toast = {
						type = event_toast_effect_good
						title = arrange_marriage_interaction_accept_betrothal_toast
						left_icon = scope:secondary_actor
						right_icon = scope:secondary_recipient
						custom_tooltip  = arrange_marriage_interaction_accept_betrothal_toast_desc
					}
				}
				else = {
					send_interface_toast = {
						type = event_toast_effect_good
						title = arrange_marriage_interaction_accept_toast
						left_icon = scope:secondary_actor
						right_icon = scope:secondary_recipient
						custom_tooltip  = arrange_marriage_interaction_accept_toast_desc
					}
				}
			}
		}

		# Apply any relevant opinion penalties for relatives of spouse.
		scope:secondary_actor = {
			if = { # Polygamy
				limit = {
					any_consort = {
						count >= 2
					}
				}
				every_consort = {
					# If the spouse themselves does not believe in polygamy...
					limit = {
						NOR = {
							culture = { has_cultural_tradition = tradition_polygamous }
							faith = { has_doctrine = doctrine_polygamy }
							faith = { has_doctrine = doctrine_concubines }
						}
					}

					#... then family members who also do not believe in polygamy aren't happy about having a relative forced into polygamous union with you.
					every_close_family_member = {
						limit = { is_close_family_of = prev }
						if = {
							limit = { 
								NOR = {
									culture = { has_cultural_tradition = tradition_polygamous }
									faith = { has_doctrine = doctrine_polygamy }
									faith = { has_doctrine = doctrine_concubines }
								}
							}
							add_opinion = {
								target = scope:secondary_actor
								modifier = relative_in_blasphemous_union_opinion
							}
						}
					}

					# NOTE: Spouse's own opinions handled in the 'on_marriage' code on_action inside of marriage_concubinage.txt, and are not included here.
				}
			}
		}
		scope:secondary_recipient = {
			if = { #Same-sex relations
				limit = {
					allowed_to_marry_same_sex_trigger = no
					sex_same_as = scope:secondary_actor
				}
				every_close_family_member = {
					limit = { is_close_family_of = scope:secondary_recipient }
					if = {
						limit = { 
							allowed_to_marry_same_sex_trigger = no
						}
						add_opinion = {
							target = scope:secondary_actor
							modifier = relative_in_blasphemous_union_opinion
						}
					}
				}
			}
		}
		# Courtly vassals like grand weddings
		scope:secondary_actor = {
			if = {
				limit = {
					any_vassal = {
						vassal_stance = courtly
					}
				}
				if = {
					limit = {
						has_been_promised_grand_wedding = yes
					}
					every_vassal = {
						vassal_stance = courtly
						add_opinion = {
							target = scope:secondary_actor
							modifier = prestigious_wedding_opinion
						}
					}
				}
			}
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					any_vassal = {
						vassal_stance = courtly
					}
				}
				if = {
					limit = {
						has_been_promised_grand_wedding = yes
					}
					every_vassal = {
						vassal_stance = courtly
						add_opinion = {
							target = scope:secondary_recipient
							modifier = prestigious_wedding_opinion
						}
					}
				}
			}
		}
	}

	###MANAGEMENT SCRIPT FOR VARIOUS EVENTS/CONTENT###
	
	#If your spouse councillor had boosted the chance of marriage, remove modifier
	if = {
		limit = {
			scope:secondary_actor = {
				has_character_modifier = heir_easier_to_marry_off_modifier
			}
			scope:actor = { player_heir = scope:secondary_actor }
		}
		scope:secondary_actor = {
			remove_character_modifier = heir_easier_to_marry_off_modifier
		}
	}
	if = {
		limit = {
			scope:secondary_actor = {
				is_child_of = scope:actor
				has_character_modifier = child_easier_to_marry_off_modifier
			}
		}
		scope:secondary_actor = {
			remove_character_modifier = child_easier_to_marry_off_modifier
		}
	}

	#AI Break up consideration after marriage
	scope:secondary_recipient = { 
		if = {
			limit = {
				is_adult = yes
				is_ai = yes
			}
			scope:secondary_actor = { save_scope_as = new_spouse_secondary_actor }
			trigger_event = lover.0205
		}
	}
	scope:secondary_actor = {
		if = {
			limit = {
				is_adult = yes
				is_ai = yes
			}
			scope:secondary_recipient = { save_scope_as = new_spouse_secondary_recipient }
			trigger_event = lover.0205
		}
	}

	# Struggle parameters

	#interfaith wedding costs piety
	if = {
		limit = {
			scope:secondary_actor = {
				faith != scope:secondary_recipient.faith
			}
			scope:actor ={
				any_character_struggle = {
					has_struggle_phase_parameter = interfaith_marriages_between_involved_characters_costs_piety
				}
			}
		}
		scope:actor = {
			add_piety = major_piety_loss
		}
	}
	#interfaith wedding provides piety
	if = {
		limit = {
			scope:secondary_actor = {
				faith != scope:secondary_recipient.faith
			}
			scope:actor ={
				any_character_struggle = {
					has_struggle_phase_parameter = interfaith_marriages_between_involved_characters_gives_piety
				}
			}
		}
		scope:actor = {
			add_piety = medium_piety_gain
		}
	}

	#other stuff 
	#Khurramites and Mazdakists gain piety from marrying lowborn
	if = {
		limit = {
			scope:secondary_recipient = {
				is_lowborn = yes 
			}
			scope:actor.faith = {
				has_doctrine = tenet_communal_possessions 
			}
		}
		scope:actor = {
			add_piety = medium_piety_gain
		}
	}

	### LMF betrothal status
	if = {
		limit = {
			NOT = { has_game_rule = lmf_no_betrothal_objection }
			OR = {
				scope:secondary_actor = { is_adult = no }
				scope:secondary_recipient = { is_adult = no }
			}
		}
		scope:secondary_actor = {
			if = {
				limit = {
					OR = {
						is_ruler = yes
						any_heir_title = {
							trigger_if = {
								limit = { has_title_law_flag = elective_succession_law }
								exists = holder
							}
							trigger_else = {
								holder ?= {
									NOR = {
										has_government = mercenary_government
										has_government = republic_government
										has_government = theocracy_government
										has_government = holy_order_government
									}
								}
							}
							count > 0
						}
					}
				}
				set_variable = { name = betrothal_ruler_or_heir value = scope:secondary_recipient }
			}
		}
		scope:secondary_recipient = {
			if = {
				limit = {
					OR = {
						is_ruler = yes
						any_heir_title = {
							trigger_if = {
								limit = { has_title_law_flag = elective_succession_law }
								exists = holder
							}
							trigger_else = {
								holder ?= {
									NOR = {
										has_government = mercenary_government
										has_government = republic_government
										has_government = theocracy_government
										has_government = holy_order_government
									}
								}
							}
							count > 0
						}
					}
				}
				set_variable = { name = betrothal_ruler_or_heir value = scope:secondary_actor }
			}
		}
	}
	else = {
		scope:secondary_actor = {
			if = {
				limit = { exists = var:betrothal_ruler_or_heir }
				remove_variable = betrothal_ruler_or_heir
			}
		}
		scope:secondary_recipient = {
			if = {
				limit = { exists = var:betrothal_ruler_or_heir }
				remove_variable = betrothal_ruler_or_heir
			}
		}
	}
}

marriage_soulmate_exclusivity_confrontation_effect = {
	$SPOUSE_1$ = { add_to_temporary_list = new_spouses }
	$SPOUSE_2$ = { add_to_temporary_list = new_spouses }

	# check to see if they break up with lovers
	$SPOUSE_1$ = {
		if = {
			limit = {
				is_ai = yes
				num_of_relation_lover > 0
				OR = {
					# too honest to lie about having a lover
					has_trait = honest
					# not the sort to cheat on their new spouse
					NOT = { might_cheat_on_partner_trigger = { PARTNER = $SPOUSE_2$ } }
					# too honorable to become an adulterer
					AND = {
						trait_is_criminal_in_faith_trigger = { TRAIT = trait:adulterer FAITH = $SPOUSE_1$.faith GENDER_CHARACTER = $SPOUSE_1$ }
						ai_honor >= low_positive_ai_value
					}
					AND = {
						trait_is_shunned_in_faith_trigger = { TRAIT = trait:adulterer FAITH = $SPOUSE_1$.faith GENDER_CHARACTER = $SPOUSE_1$ }
						ai_honor >= high_positive_ai_value
					}
				}
			}
			every_relation = {
				type = lover
				limit = {
					is_ai = yes
					NOR = {
						is_consort_of = $SPOUSE_1$
						has_relation_soulmate = $SPOUSE_1$
						is_in_list = new_spouses
					}
				}
				save_temporary_scope_as = former_lover
				lover_breakup_effect = {
					BREAKER = $SPOUSE_1$
					LOVER = scope:former_lover
				}
			}
		}
	}
	$SPOUSE_2$ = {
		if = {
			limit = {
				is_ai = yes
				num_of_relation_lover > 0
				OR = {
					# too honest to lie about having a lover
					has_trait = honest
					# not the sort to cheat on their new spouse
					NOT = { might_cheat_on_partner_trigger = { PARTNER = $SPOUSE_1$ } }
					# too honorable to become an adulterer
					AND = {
						trait_is_criminal_in_faith_trigger = { TRAIT = trait:adulterer FAITH = $SPOUSE_2$.faith GENDER_CHARACTER = $SPOUSE_2$ }
						ai_honor >= low_positive_ai_value
					}
					AND = {
						trait_is_shunned_in_faith_trigger = { TRAIT = trait:adulterer FAITH = $SPOUSE_2$.faith GENDER_CHARACTER = $SPOUSE_2$ }
						ai_honor >= high_positive_ai_value
					}
				}
			}
			every_relation = {
				type = lover
				limit = {
					is_ai = yes
					NOR = {
						is_consort_of = $SPOUSE_2$
						has_relation_soulmate = $SPOUSE_2$
						is_in_list = new_spouses
					}
				}
				save_temporary_scope_as = former_lover
				lover_breakup_effect = {
					BREAKER = $SPOUSE_2$
					LOVER = scope:former_lover
				}
			}
		}
	}

	every_in_list = {
		list = new_spouses
		limit = {
			any_relation = {
				type = soulmate
				NOT = { is_in_list = new_spouses }
			}
		}
		add_character_flag = {
			flag = can_be_confronted_about_lover_exclusivity
			days = 1825
		}
	}
}

