tribute_mission_clean_up_variables_effect = {
	remove_variable = tribute_mission_target_scope
	remove_variable = offered_gold_value
	remove_variable = tribute_mission_type
	remove_variable = offered_herd_value
	remove_variable = offered_concubine
	remove_variable = offered_eunuch
	remove_variable = offered_artifact
	remove_variable = tribute_reward_fascination_progress

	if = {
		limit = {
			has_character_flag = tribute_mission_character_flag # Under select circumstances it's possible this will be rerun, so this avoids an error
		}
		remove_character_flag = tribute_mission_character_flag
	}

	if = {
		limit = { exists = scope:receiving_character }
		scope:receiving_character = {
			if = {
				limit = { has_character_flag = created_attendant }
				silent_disappearance_effect = yes
			}
		}
	}
}

tribute_mission_clean_up_request_variables_effect = {
	remove_variable = requested_tribute_mission
	remove_variable = requested_tribute_mission_type_gold
	remove_variable = requested_tribute_mission_type_herd
	remove_variable = requested_tribute_mission_type_artifact
	remove_variable = requested_tribute_mission_type_concubine
	remove_variable = requested_tribute_mission_type_eunuch
	remove_variable = declined_requested_tribute_recently
	remove_variable = predetermined_concubine_tribute
	remove_variable = predetermined_eunuch_tribute
	remove_variable = predetermined_artifact_tribute
}

requested_tribute_mission_clean_up_variables_effect = {
	remove_variable = requested_tribute_mission
	remove_variable = invalidation_should_fail_ongoing_requested_tribute_mission
}

tribute_mission_decision_effect = {
	if = {
		#Are you a tributary of a Hegemon?
		limit = { subject_standing >= 0 }
		if = {
			limit = { is_tributary = yes }
			#If it's China, flavorize Imperial Grace
			if = {
				limit = { suzerain = { primary_title = title:h_china } }
				custom_tooltip = tribute_mission_interaction_anticipated_rewards_tt_imperial_grace
			}
			#Else standard stuff
			else = {
				custom_tooltip = tribute_mission_interaction_anticipated_rewards_tt_generic_hegemon
			}
		}
	}
	else = {
		if = {
			limit = { $TARGET$ = { primary_title = title:h_china } }
			custom_tooltip = tribute_mission_interaction_anticipated_rewards_tt_china
		}
		else_if = {
			limit = { $TARGET$ = { primary_title = title:h_roman_empire } }
			custom_tooltip = tribute_mission_interaction_anticipated_rewards_tt_roman_empire
		}
		else_if = {
			limit = { $TARGET$ = { primary_title = title:h_eastern_roman_empire } }
			custom_tooltip = tribute_mission_interaction_anticipated_rewards_tt_eastern_roman_empire
		}
		else_if = {
			limit = { $TARGET$ = { primary_title = title:h_dar_al_islam } }
			custom_tooltip = tribute_mission_interaction_anticipated_rewards_tt_dar_al_islam
		}
		else_if = {
			limit = { $TARGET$ = { primary_title = title:h_india } }
			custom_tooltip = tribute_mission_interaction_anticipated_rewards_tt_india
		}
		else = { custom_tooltip = tribute_mission_interaction_anticipated_rewards_tt }
	}

	hidden_effect = { 
		tribute_mission_clean_up_variables_effect = yes

		if = {
			limit = { is_tributary = yes }
			suzerain = { 
				save_scope_as = tribute_mission_target
				add_character_flag = {
					flag = pay_tribute_cooldown
					years = 3
				}
			}
		}
		else = {
			 $TARGET$ = {
				save_scope_as = tribute_mission_target
			}
		}

		set_variable = { # persist for later interactions
			name = tribute_mission_target_scope
			value = scope:tribute_mission_target
		}
	}

	#Your Suzerain requested this specifically
	if = {
		limit = {	
			has_variable = requested_tribute_mission
			is_ai = yes
		}
		switch = {
			trigger = has_variable
			requested_tribute_mission_type_gold = { # Gold
				run_interaction = {
					interaction = tribute_mission_gold_interaction
					actor = root
					recipient = scope:tribute_mission_target
					execute_threshold = accept
				}
			}
			requested_tribute_mission_type_herd = { # Herd
				run_interaction = {
					interaction = tribute_mission_herd_interaction
					actor = root
					recipient = scope:tribute_mission_target
					execute_threshold = accept
				}
			}
			requested_tribute_mission_type_artifact = { # Artifact
				var:predetermined_artifact_tribute ?= { save_scope_as = predetermined_artifact_tribute_scope }
				#Do the things from the interaction
				scope:predetermined_artifact_tribute_scope = {
					set_variable = is_tribute_mission_artifact
				}
				#Save the Artifact
				set_variable = {
					name = tribute_mission_type
					value = flag:artifact_tribute
				}
				set_variable = {
					name = offered_artifact
					value = scope:predetermined_artifact_tribute_scope
				}
				#Travel
				tribute_mission_set_up_tribute_travel_effect = yes
			}
			requested_tribute_mission_type_concubine = { # Concubine
				var:predetermined_concubine_tribute ?= { 
					save_scope_as = predetermined_concubine_tribute_scope 
				}
				scope:predetermined_concubine_tribute_scope = {
					add_character_flag = {
						flag = cannot_be_diarch # just enough to make sure they don't succeed to the diarchy while en route
						years = 2
					}
				}
				run_interaction = {
					interaction = tribute_mission_concubine_interaction
					actor = root
					recipient = scope:tribute_mission_target
					secondary_recipient = scope:predetermined_concubine_tribute_scope
					execute_threshold = accept
				}
			}
			requested_tribute_mission_type_eunuch = { # Eunuch	
				var:predetermined_eunuch_tribute ?= { save_scope_as = predetermined_eunuch_tribute_scope }
				run_interaction = {
					interaction = tribute_mission_eunuch_interaction
					actor = root
					recipient = scope:tribute_mission_target
					secondary_recipient = scope:predetermined_eunuch_tribute_scope
					execute_threshold = accept
				}
			}
		}
	}
	else_if = {
		limit = { is_ai = yes }
		switch = {
			trigger = yes

			scope:tribute_mission_gold = { # Gold
				run_interaction = {
					interaction = tribute_mission_gold_interaction
					actor = root
					recipient = scope:tribute_mission_target
					execute_threshold = accept
				}
			}
			
			scope:tribute_mission_herd = { # Herd
				run_interaction = {
					interaction = tribute_mission_herd_interaction
					actor = root
					recipient = scope:tribute_mission_target
					execute_threshold = accept
				}
			}

			scope:tribute_mission_artifact = { # Artifact
				random_character_artifact = { 
					limit = { is_suitable_artifact_tribute_trigger = yes }
					save_scope_as = decision_predetermined_artifact_tribute
				}
				#Do the things from the interaction
				scope:decision_predetermined_artifact_tribute = {
					set_variable = is_tribute_mission_artifact
				}
				#Save the Artifact
				set_variable = {
					name = tribute_mission_type
					value = flag:artifact_tribute
				}
				set_variable = {
					name = offered_artifact
					value = scope:decision_predetermined_artifact_tribute
				}
				#Travel
				tribute_mission_set_up_tribute_travel_effect = yes
			}

			scope:tribute_mission_concubine = { # Concubine
				every_courtier_or_guest = {
					limit = {
						can_be_offered_as_concubine_to_character_trigger = {
							GIVER = root
							CHARACTER = scope:tribute_mission_target
						}
						tribute_mission_is_available_concubine_trigger = yes
					}
					add_to_list = potential_concubine_tribute_list
					random_in_list = {
						list = potential_concubine_tribute_list
						weight = {
							#Let's try to make a sane choice here
							base = 0
							#No marginally relevant court people
							modifier = {
								add = 10
								NOR = {
									is_councillor = yes
									has_any_court_position = yes
								}
							}
							#No knights
							modifier = {
								add = 5
								is_knight = no
							}
							#No inspired people?
							modifier = {
								add = 20
								NOT = { exists = inspiration }
							}
							#No close family
							modifier = {
								add = 15
								NOT = { is_close_or_extended_family_of = root }
							}
							#No friends?
							modifier = {
								add = 20
								NOT = { has_relation_friend = root }
							}
							#No children
							modifier = {
								add = 25
								NOT = { is_child_of = root }
							}
						}
						save_scope_as = decision_predetermined_concubine_tribute
					}
				}
				scope:decision_predetermined_concubine_tribute = {
					add_character_flag = {
						flag = cannot_be_diarch # just enough to make sure they don't succeed to the diarchy while en route
						years = 2
					}
				}
				run_interaction = {
					interaction = tribute_mission_concubine_interaction
					actor = root
					recipient = scope:tribute_mission_target
					secondary_recipient = scope:decision_predetermined_concubine_tribute
					execute_threshold = accept
				}
			}

			scope:tribute_mission_eunuch = { # Eunuch
				every_courtier = {
					limit = { 
						tribute_mission_is_available_eunuch_trigger = yes
						NOT = { is_heir_of = root }
					}
					add_to_list = potential_eunuch_tribute_list
					random_in_list = {
						list = potential_eunuch_tribute_list
						weight = {
							#Let's try to make a sane choice here
							base = 0
							#No marginally relevant court people
							modifier = {
								add = 10
								NOR = {
									is_councillor = yes
									has_any_court_position = yes
								}
							}
							#No knights
							modifier = {
								add = 5
								is_knight = no
							}
							#No inspired people?
							modifier = {
								add = 20
								NOT = { exists = inspiration }
							}
							#No close family
							modifier = {
								add = 15
								NOT = { is_close_or_extended_family_of = scope:tribute_mission_target }
							}
							#No friends?
							modifier = {
								add = 20
								NOT = { has_relation_friend = scope:tribute_mission_target }
							}
							#No children
							modifier = {
								add = 25
								NOT = { is_child_of = scope:tribute_mission_target }
							}
						}
						save_scope_as = decision_predetermined_eunuch_tribute
					}
				}
				run_interaction = {
					interaction = tribute_mission_eunuch_interaction
					actor = root
					recipient = scope:tribute_mission_target
					secondary_recipient = scope:decision_predetermined_eunuch_tribute
					execute_threshold = accept
				}
			}

			scope:tribute_mission_bunga_mas = { # Bunga Mas
				random_character_artifact = { 
					limit = { var:bunga_mas_created_by ?= root }
					save_scope_as = decision_predetermined_bunga_mas_tribute
				}
				#Do the things from the interaction
				scope:decision_predetermined_bunga_mas_tribute = {
					set_variable = is_tribute_mission_artifact
				}
				#Save the Artifact
				set_variable = {
					name = tribute_mission_type
					value = flag:bunga_mas_tribute
				}
				set_variable = {
					name = offered_bunga_mas
					value = scope:decision_predetermined_bunga_mas_tribute
				}
				#Travel
				tribute_mission_set_up_tribute_travel_effect = yes
			}
		}
	}
	#Natural flow of the decision
	else_if = {
		limit = { is_ai = no }
		switch = {
			trigger = yes

			scope:tribute_mission_gold = { # Gold
				open_interaction_window = {
					interaction = tribute_mission_gold_interaction
					actor = root
					recipient = scope:tribute_mission_target
				}
			}
			
			scope:tribute_mission_herd = { # Herd
				open_interaction_window = {
					interaction = tribute_mission_herd_interaction
					actor = root
					recipient = scope:tribute_mission_target
				}
			}

			scope:tribute_mission_artifact = { # Artifact
				open_interaction_window = {
					interaction = tribute_mission_artifact_interaction
					actor = root
					recipient = scope:tribute_mission_target
				}
			}

			scope:tribute_mission_concubine = { # Concubine 
				open_interaction_window = {
					interaction = tribute_mission_concubine_interaction
					actor = root
					recipient = scope:tribute_mission_target
				}
			}

			scope:tribute_mission_eunuch = { # Eunuch
				open_interaction_window = {
					interaction = tribute_mission_eunuch_interaction
					actor = root
					recipient = scope:tribute_mission_target
				}
			}

			scope:tribute_mission_bunga_mas = { # Bunga Mas
				open_interaction_window = {
					interaction = tribute_mission_bunga_mas_interaction
					actor = root
					recipient = scope:tribute_mission_target
				}
			}
		}
	}

	if = {
		limit = { 
			is_ai = yes
			scope:tribute_mission_target = { highest_held_title_tier = tier_hegemony }
		}
		scope:tribute_mission_target = {
			set_variable = {
				name = ai_tribute_mission_cooldown
				years = 1
			}
		}
	}
}

set_or_change_offered_tribute_value_effect = {
	if = {
		limit = { has_variable = offered_$TYPE$_value }
		change_variable = {
			name = offered_$TYPE$_value
			add = $VALUE$
		}
	}
	else = {
		set_variable = {
			name = offered_$TYPE$_value
			value = $VALUE$
		}
	}
}


tribute_mission_set_up_tribute_travel_effect = {
	#Recover the tribute_mission_target scope from saved var if it doesn't already exist
	if = {
		limit = { exists = var:tribute_mission_target_scope }
		var:tribute_mission_target_scope = {
			save_scope_as = tribute_mission_target
		}
	}
	else = {
		error_log = "Tribute mission target scope variable not set on character intending to travel!"
	}
	#Travel posted first should look neater in tooltip
	hidden_effect = { # ensure we subtract everything we've offered
		if = {
			limit = { has_variable = offered_gold_value }
			remove_short_term_gold = var:offered_gold_value
		}
		if = {
			limit = { has_variable = offered_herd_value }
			domicile ?= {
				change_herd = { subtract = prev.var:offered_herd_value }
			}
		}	
	}
	trigger_event = { on_action = tribute_mission_start }
	if = {
		limit = { location = scope:tribute_mission_target.capital_province }
		trigger_event = tribute_mission.0002
	}
	#Add the concubine
	else_if = {
		limit = { has_variable = offered_concubine }
		start_travel_plan = {
			destination = scope:tribute_mission_target.capital_province
			companion = var:offered_concubine
			on_travel_planner_cancel_on_action = tribute_mission_travel_planner_exit 
			on_arrival_event = tribute_mission.0002
		}
	}
	#Add the eunuch
	else_if = {
		limit = { has_variable = offered_eunuch }
		start_travel_plan = {
			destination = scope:tribute_mission_target.capital_province
			companion = var:offered_eunuch
			on_travel_planner_cancel_on_action = tribute_mission_travel_planner_exit 
			on_arrival_event = tribute_mission.0002
		}
	}
	else = {
		custom_tooltip = tribute_mission.travel_to.tt
		start_travel_plan = {
			destination = scope:tribute_mission_target.capital_province
			on_travel_planner_cancel_on_action = tribute_mission_travel_planner_exit 
			on_arrival_event = tribute_mission.0002
		}
	}
}

tribute_mission_invalidation_effect = {
	if = {
		limit = { has_variable = tribute_mission_type }
		if = {
			limit = { # sanity check to ensure we're still a valid tribute provider
				OR = {
					is_independent_ruler = no # if we have a liege we cannot also pay tribute to someone else
					AND = { # if we're not a tributary we are only valid if we're paying tribute to a hegemony
						is_tributary = no
						OR = { 
							NOT = { exists = var:tribute_mission_target_scope }
							var:tribute_mission_target_scope = { NOT = { highest_held_title_tier = tier_hegemony } }
						}
					}
				}
			}
			#Return home and refund
			trigger_event = tribute_mission.9800
		}
		else_if = { #If our offerings have perished or been imprisoned along the way, maybe swap to gold
			limit = {
				has_variable = tribute_mission_type
				OR = {
					AND = {
						has_variable = offered_concubine
						OR = {
							var:offered_concubine = { is_alive = no }
							var:offered_concubine = { is_imprisoned = yes }
						}
					}
					AND = {
						has_variable = offered_eunuch
						OR = {
							var:offered_eunuch = { is_alive = no }
							var:offered_eunuch = { is_imprisoned = yes }
						}
					}
				}
			}
			trigger_event = tribute_mission.9750
		}
		else_if = {
			#We're still a tributary on our way to pay tribute, but our suzerain is not the person we were going to pay tribute to
			limit = {
				has_variable = tribute_mission_target_scope
				OR = {
					AND = {
						is_tributary = yes
						var:tribute_mission_target_scope != suzerain
					}
					AND = {
						is_tributary = no
						OR = { 
							NOT = { exists = var:tribute_mission_target_scope }
							var:tribute_mission_target_scope = { NOT = { highest_held_title_tier = tier_hegemony } }
						}
					}	
				}
			}
			var:tribute_mission_target_scope = { save_scope_as = old_overlord }
			suzerain = { save_scope_as = new_overlord }
			#Clean up Requested Tribute Mission variables
			requested_tribute_mission_clean_up_variables_effect = yes
			#The new Overlord is in the same place and can accept the tribute
			if = {
				limit = { 
					#Make sure they can still accept our offerings; if so, just set the new target
					current_travel_plan.next_destination_province = scope:new_overlord.capital_province
					has_variable = tribute_mission_type
					trigger_if = {
						limit = {
							OR = {
								has_variable = offered_concubine
								has_variable = offered_eunuch
							}
						}
						OR = {
							AND = {
								has_variable = offered_concubine
								var:offered_concubine = {
									can_become_concubine_of_character_valid_trigger = { CHARACTER = scope:new_overlord }
								}
							}
							AND = {
								has_variable = offered_eunuch
								scope:new_overlord = { 
									culture = { has_cultural_parameter = can_appoint_chief_eunuch } 
								}
							}
						}
					}	
				}
				send_interface_message = {
					type = msg_mandala_neutral_text
					title = tribute_mission_notification.new_overlord.title
					left_icon = scope:new_overlord
					desc = tribute_mission_notification.new_overlord.desc		
				}
				set_variable = {
					name = tribute_mission_target_scope
					value = scope:new_overlord
				}		
			}
			#The Overlord is somewhere else; please try again
			else_if = {
				limit = {
					current_travel_plan.next_destination_province != scope:new_overlord.capital_province
				}
				set_variable = new_overlord_somewhere_else
				#Return home and refund
				trigger_event = tribute_mission.9800
			}
			else = {
				set_variable = new_overlord_unable_to_accept_tribute
				#Return home and refund
				trigger_event = tribute_mission.9800
			}
		}
	}	
}

tribute_mission_mandala_piety_effect = {
	if = {
		limit = { government_has_flag = government_is_mandala }
		overlord = { add_piety = minor_piety_gain }
	}
}

requested_tribute_mission_penalty_effect = {
	if = {
		limit = {
			has_variable = invalidation_should_fail_ongoing_requested_tribute_mission
		}
		trigger_event = {
			id = tribute_mission.9500
			delayed = yes
		}
	}
}
