﻿namespace = learn_language_outcome

######################
# Positive Outcomes
# 1000-1999
######################

# Success!
learn_language_outcome.1000 = {
	type = character_event
	window = scheme_successful_event
	title = learn_language_outcome.1000.t
	desc = learn_language_outcome.1000.desc
	theme = education
	left_portrait = {
		character = root
		animation = happiness
	}
	right_portrait = {
		trigger = { root = { is_travelling = no } }
		character = scope:target
		animation = personality_rational
	}
	lower_right_portrait = {
		trigger = { root = { is_travelling = yes } }
		character = scope:target
	}
	widget = {
		gui = "event_window_widget_scheme"
		container = "custom_widgets_container"
	}

	override_background = {
		trigger = { is_travelling = yes }
		reference = terrain_travel
	}

	immediate = {
		scope:target = {
			save_scope_as = target
		}
		scope:owner = {
			save_scope_as = owner
		}
		every_parent = {
			limit = {
				is_ai = no
			}
			send_interface_toast = {
				type = msg_child_language
				title = child_learned_language_title
				left_icon = scope:owner
				right_icon = scope:target
				custom_tooltip = child_learned_language_desc
			}
		}

		# Struggle bonus
		if = {
			limit = {
				any_character_struggle = {
					involvement = involved
					has_struggle_phase_parameter = learning_languages_gives_piety
				}
			}
			scope:owner = {
				add_piety = medium_piety_gain
			}
		}

		if = {
			limit = {
				any_character_struggle = {
					involvement = involved
					has_struggle_phase_parameter = learning_languages_gives_prestige
				}
			}
			scope:owner = {
				add_prestige = medium_prestige_gain
			}
		}


		# Struggle Catalyst
		if = {
			limit = {
				any_character_struggle = {
					involvement = involved
					activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
						CATALYST = catalyst_learned_new_language_important
						CHAR = scope:target
					}
				}
			}
			every_character_struggle = {
				involvement = involved
				limit = {
					activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
						CATALYST = catalyst_learned_new_language_important
						CHAR = scope:target
					}
				}
				activate_struggle_catalyst = {
					catalyst = catalyst_learned_new_language_important
					character = root
				}
				log_debug_variable_for_persian_struggle_effect = { VAR = stabil_catalyst_learned_new_language_important }
			}
		}
	}

	option = {
		name = learn_language_outcome.1000.a
		ai_chance = {
			base = 100
		}
	}

	option = {
		name = learn_language_outcome.1000.b
		trigger = {
			is_ruler = yes
		}
		if = {
			limit = {
				scope:target = {
					is_ai = yes
				}
			}
			scope:owner = {
				duel = {
					skill = diplomacy
					value = 12

					50 = {
						compare_modifier = {
							value = scope:duel_value
							multiplier = 3.5
							min = -49
						}

						desc = learn_language_outcome.1000.b.success
						send_interface_toast = {
							title = learn_language_outcome.1000.b.success
							left_icon = scope:target

							scope:target = {
								add_opinion = {
									target = root
									modifier = respect_opinion
									opinion = 25
								}
								if = {
									limit = {
										can_set_relation_potential_friend_trigger = { CHARACTER = root }
									}
									set_relation_potential_friend = root
								}
							}
						}
					}
					50 = {
						compare_modifier = {
							value = scope:duel_value
							multiplier = -3.5
							min = -49
						}

						desc = learn_language_outcome.1000.b.failure
						send_interface_toast = {
							title = learn_language_outcome.1000.b.failure
							left_icon = scope:target

							scope:target = {
								add_opinion = {
									target = root
									modifier = respect_opinion
									opinion = -25
								}
								if = {
									limit = {
										can_set_relation_potential_rival_trigger = { CHARACTER = root }
									}
									set_relation_potential_rival = root
								}
							}
						}
					}
				}
			}
		}
		else_if = {
			limit = {
				OR = {
					NOT = { is_at_war_with = scope:target }
					is_ai = no
				}
			}
			custom_tooltip = target_responds
			scope:target = {
				trigger_event = learn_language_outcome.1001
			}
		}
		stress_impact = {
			shy = massive_stress_impact_gain
			lazy = medium_stress_impact_gain
			paranoid = medium_stress_impact_gain
		}
		ai_chance = {
			base = 100
			modifier = {
				factor = 0
				ai_sociability <= low_negative_ai_value
			}
			modifier = {
				factor = 0
				is_at_war_with = scope:target
			}
		}
	}

	after = {
		if = {
			limit = {
				culture = {
					has_cultural_parameter = piety_from_language_schemes
				}
			}
			add_piety = massive_piety_gain
		}

		if = {
			limit = {
				dynasty ?= {
					has_dynasty_perk = ep1_culture_legacy_1
				}
			}
			culture = {
				change_cultural_acceptance = {
					target = scope:target.culture
					value = 1
					desc = cultural_acceptance_gain_event
				}
			}
		}
		scope:owner = {
			send_interface_toast = {
				title = learn_language_complete
				left_icon = root
				learn_language_of_culture = scope:target.culture
				scope:scheme = { end_scheme = yes }
			}
		}
		if = {
			limit = {
				any_vassal = {
					vassal_stance = zealot
				}
			}
			custom_tooltip = zealot_vassal_approves_learning_language
			hidden_effect = {
				every_vassal = {
					vassal_stance = zealot
					add_opinion = {
						target = scope:owner
						modifier = zealot_approves_of_learning_language_opinion
					}
				}
			}
		}
	}
}

learn_language_outcome.1001 = {
	type = letter_event
	opening = {
		desc = learn_language_outcome.1001.opening
	}
	desc = learn_language_outcome.1001.desc
	sender = scope:owner

	option = {
		name = learn_language_outcome.1001.a
		stress_impact = {
			paranoid = medium_stress_impact_gain
		}
		show_as_tooltip = {
			progress_towards_friend_effect = {
				CHARACTER = scope:owner
				OPINION = 0
				REASON = friend_language_learned
			}
		}
		scope:owner = {
			add_opinion = {
				target = scope:target
				modifier = respect_opinion
				opinion = 15
			}
			hidden_effect = {
				send_interface_toast = {
					title = learn_language_outcome.1000.b.success
					left_icon = scope:target

					scope:target = {
						add_opinion = {
							target = scope:owner
							modifier = respect_opinion
							opinion = 25
						}
						progress_towards_friend_effect = {
							CHARACTER = scope:owner
							OPINION = 0
							REASON = friend_language_learned
						}
					}
				}
			}
		}
	}

	option = {
		name = learn_language_outcome.1001.b
		stress_impact = {
			trusting = medium_stress_impact_gain
			compassionate = medium_stress_impact_gain
		}
		add_prestige = medium_prestige_gain
		show_as_tooltip = {
			progress_towards_rival_effect = {
				REASON = rival_insulted_language
				CHARACTER = scope:owner
				OPINION = 0
			}
		}
		scope:owner = {
			add_opinion = {
				target = scope:target
				modifier = insult_opinion
				opinion = -25
			}
			hidden_effect = {
				send_interface_toast = {
					title = learn_language_outcome.1000.b.failure
					left_icon = scope:target

					scope:target = {
						add_opinion = {
							target = scope:owner
							modifier = respect_opinion
							opinion = -25
						}
						progress_towards_rival_effect = {
							REASON = rival_insulted_language_corresponding
							CHARACTER = scope:owner
							OPINION = 0
						}
					}
				}
			}
		}
	}
}

#################################
# Negative Outcomes
# 2000-2999
#################################

# Failure
learn_language_outcome.2000 = {
	type = character_event
	window = scheme_failed_event
	title = learn_language_outcome.2000.t
	desc = learn_language_outcome.2000.desc
	theme = education
	left_portrait = {
		character = root
		animation = rage
	}
	widget = {
		gui = "event_window_widget_scheme"
		container = "custom_widgets_container"
	}

	override_background = {
		trigger = { is_travelling = yes }
		reference = terrain_travel
	}

	immediate = {
		scope:target = {
			save_scope_as = target
		}
		scope:owner = {
			save_scope_as = owner
		}
		every_parent = {
			limit = {
				is_ai = no
			}
			send_interface_toast = {
				type = msg_child_language
				title = child_learned_language_title
				left_icon = scope:owner
				right_icon = scope:target
				custom_tooltip = child_learned_language_desc
			}
		}
	}

	option = {
		name = learn_language_outcome.2000.c
		trigger = {
			has_trait = diplomat
		}
		trait = diplomat
		scope:target = {
			add_opinion = {
				target = root
				modifier = respect_opinion
				opinion = 25
			}
		}
		scope:scheme = {
			end_scheme = yes
		}
	}

	option = {
		name = learn_language_outcome.2000.a
		scope:scheme = {
			end_scheme = yes
		}
	}

	option = {
		name = learn_language_outcome.2000.b
		add_stress = massive_stress_gain
		custom_tooltip = scheme_loses_half_progress
		hidden_effect = {
			scope:scheme = {
				add_scheme_progress = {
					value = scheme_progress_loss
					multiply = 5
				}
			}
		}
	}
}

#################################
# Invalidation Outcomes
# 3000-3999
#################################

scripted_trigger is_valid_continuation_target = {
	culture = {
		has_same_culture_language = scope:target.culture
	}
	root = {
		can_start_scheme = {
			type = learn_language
			target_character = prev
		}
	}
}

# Invalidation
learn_language_outcome.3000 = {
	type = character_event
	title = learn_language_outcome.3000.t
	desc = learn_language_outcome.3000.desc
	theme = education
	left_portrait = {
		character = root
		animation = personality_rational
	}
	right_portrait = scope:target
	lower_left_portrait = scope:potential_continuation_target_1
	lower_center_portrait = scope:potential_continuation_target_2
	lower_right_portrait = scope:potential_continuation_target_3

	trigger = {
		OR = {
			trigger_if = {
				limit = {
					is_ai = no
				}
				any_ruler = {
					is_valid_continuation_target = yes
				}
			}
			any_neighboring_and_across_water_top_liege_realm_owner = {
				is_valid_continuation_target = yes
			}
			any_courtier_or_guest = {
				is_valid_continuation_target = yes
			}
			any_vassal_or_below = {
				is_valid_continuation_target = yes
			}
			any_liege_or_above = {
				is_valid_continuation_target = yes
			}
			scope:target = {
				any_close_family_member = {
					is_valid_continuation_target = yes
				}
			}
		}
	}

	on_trigger_fail = {
		every_parent = {
			limit = {
				is_ai = no
			}
			send_interface_toast = {
				title = child_learned_language_invalidated_title
				left_icon = scope:owner
				right_icon = scope:target
				custom_tooltip = child_learned_language_invalidated_desc
			}
		}
	}

	immediate = {
		if = {
			limit = {
				is_ai = no
			}
			every_ruler = {
				limit = {
					is_valid_continuation_target = yes
				}
				add_to_list = potential_continuation_targets
			}
		}
		every_neighboring_and_across_water_top_liege_realm_owner = {
			limit = {
				is_valid_continuation_target = yes
			}
			add_to_list = potential_continuation_targets
		}
		every_courtier_or_guest = {
			limit = {
				is_valid_continuation_target = yes
			}
			add_to_list = potential_continuation_targets
		}
		every_vassal_or_below = {
			limit = {
				is_valid_continuation_target = yes
			}
			add_to_list = potential_continuation_targets
		}
		every_liege_or_above = {
			limit = {
				is_valid_continuation_target = yes
			}
			add_to_list = potential_continuation_targets
		}
		scope:target = {
			every_close_family_member = {
				limit = {
					is_valid_continuation_target = yes
				}
				add_to_list = potential_continuation_targets
			}
		}


		random_in_list = {
			list = potential_continuation_targets
			weight = {
				base = 1
				modifier = {
					add = 1000
					is_close_family_of = scope:target
					is_ruler = yes
				}
				modifier = {
					add = 10
					is_of_major_interest_to_root_trigger = yes
				}
				modifier = {
					add = 5
					is_of_minor_interest_to_root_trigger = yes
				}
			}
			save_scope_as = potential_continuation_target_1
		}

		random_in_list = {
			list = potential_continuation_targets
			limit = {
				this != scope:potential_continuation_target_1
			}
			weight = {
				base = 1
				modifier = {
					add = 1000
					is_close_family_of = scope:target
					is_ruler = yes
				}
				modifier = {
					add = 10
					is_of_major_interest_to_root_trigger = yes
				}
				modifier = {
					add = 5
					is_of_minor_interest_to_root_trigger = yes
				}
			}
			save_scope_as = potential_continuation_target_2
		}

		random_in_list = {
			list = potential_continuation_targets
			limit = {
				NOR = {
					this = scope:potential_continuation_target_1
					this = scope:potential_continuation_target_2
				}
			}
			weight = {
				base = 1
				modifier = {
					add = 1000
					is_close_family_of = scope:target
					is_ruler = yes
				}
				modifier = {
					add = 10
					is_of_major_interest_to_root_trigger = yes
				}
				modifier = {
					add = 5
					is_of_minor_interest_to_root_trigger = yes
				}
			}
			save_scope_as = potential_continuation_target_3
		}
	}

	option = {
		name = learn_language_outcome.3000.a

		if = {
			limit = {
				can_start_scheme = {
					type = learn_language
					target_character = scope:potential_continuation_target_1
				}
			}

			start_scheme = {
				type = learn_language
				target_character = scope:potential_continuation_target_1
			}
		}

		custom_tooltip = scheme_retains_progress
		hidden_effect = {
			random_scheme = {
				type = learn_language
				limit = {
					scheme_target_character = scope:potential_continuation_target_1
				}
				if = {
					limit = {
						exists = root.var:language_progress
					}
					add_scheme_progress = root.var:language_progress
				}
			}
		}
	}

	option = {
		name = learn_language_outcome.3000.b

		trigger = {
			exists = scope:potential_continuation_target_2
		}

		if = {
			limit = {
				can_start_scheme = {
					type = learn_language
					target_character = scope:potential_continuation_target_2
				}
			}

			start_scheme = {
				type = learn_language
				target_character = scope:potential_continuation_target_2
			}
		}

		custom_tooltip = scheme_retains_progress
		hidden_effect = {
			random_scheme = {
				type = learn_language
				limit = {
					scheme_target_character = scope:potential_continuation_target_2
					exists = root.var:language_progress
				}
				if = {
					limit = {
						exists = root.var:language_progress
					}
					add_scheme_progress = root.var:language_progress
				}
			}
		}
	}

	option = {
		name = learn_language_outcome.3000.c

		trigger = {
			exists = scope:potential_continuation_target_3
		}

		if = {
			limit = {
				can_start_scheme = {
					type = learn_language
					target_character = scope:potential_continuation_target_3
				}
			}

			start_scheme = {
				type = learn_language
				target_character = scope:potential_continuation_target_3
			}
		}

		custom_tooltip = scheme_retains_progress
		hidden_effect = {
			random_scheme = {
				type = learn_language
				limit = {
					scheme_target_character = scope:potential_continuation_target_3
					exists = root.var:language_progress
				}
				if = {
					limit = {
						exists = root.var:language_progress
					}
					add_scheme_progress = root.var:language_progress
				}
			}
		}
	}

	option = {
		name = learn_language_outcome.3000.d
	}

	after = {
		remove_variable = language_progress
	}
}
