﻿namespace = tgp_china_decision

scripted_effect set_up_local_examiner_effect = {
	top_liege = { save_scope_as = hegemon }
	scope:hegemon = {
		if = {
			limit = {
				any_vassal_or_below = {
					NOT = { this = root }
					has_trait = governor
					is_available = yes
					capital_county = root.domicile.domicile_location.county
				}
			}
			random_vassal_or_below = {
				limit = {
					NOT = { this = root }
					has_trait = governor
					is_available = yes
					capital_county = root.domicile.domicile_location.county
				}
				save_scope_as = examiner
			}
		}
		else = {
			create_character = {
				template = tgp_learning_exam_entrant_template
				age = { 33 79 }
				location = root.location
				faith = scope:hegemon.faith
				culture = scope:hegemon.culture
				gender_female_chance = {
					if = {
						limit = { scope:hegemon.faith = { has_doctrine = doctrine_gender_male_dominated } }
						add = 0
					}
					else_if = {
						limit = { scope:hegemon.faith = { has_doctrine = doctrine_gender_female_dominated } }
						add = 100
					}
					else = {
						add = 50
					}
				}
				save_scope_as = examiner
			}
		}
	}
	scope:examiner = {
		hidden_effect = {
			add_trait = governor
			change_merit = massive_merit_value
		}
	}
}

tgp_china_decision.1000 = {
	type = character_event
	title = tgp_china_decision.1000.t
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { has_character_flag = failed_child_exam }
				desc = tgp_china_decision.1000.desc_returning
			}
			desc = tgp_china_decision.1000.desc_new
		}
		desc = tgp_china_decision.1000.desc_outro
	}

	theme = imperial_examination
	override_background = { reference = study }

	left_portrait = {
		character = root
		animation = worry
	}

	right_portrait = {
		character = scope:examiner
		animation = happy_teacher
	}

	immediate = {
		set_variable = {
			name = child_examination_success_chance
			value = child_examination_success_chance_value
		}
		set_up_local_examiner_effect = yes
	}

	widget = { gui = "child_examination_success_chance" container = "custom_widgets_container" }

	option = { # Recite classics
		name = tgp_china_decision.1000.a
		duel = {
			skill = learning
			value = 1
			50 = { # You succeed
				desc = provincial_exam_success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3
				}
				add_character_modifier = {
					modifier = tgp_local_exam_initial_success
					years = 12
				}
				custom_tooltip = examination_success_increase_medium_tt
			}
			50 = { # You fail
				desc = provincial_exam_duel_fail
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3
				}
				stress_impact = { base = medium_stress_impact_gain }
			}
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 1
				ai_energy = 1
			}
			modifier = {
				add = 20
				learning > medium_skill_rating
			}
			modifier = {
				add = 40
				has_trait = ambitious
			}
			modifier = {
				add = -40
				OR = {
					has_trait = content
					has_trait = lazy
					has_trait = craven
				}
			}
		}
	}

	option = { # Play it safe
		name = tgp_china_decision.1000.b
		custom_tooltip = examination_success_increase_miniscule_tt
		# Use this var to track score
		change_variable = {
			name = safe_bets
			add = 1
		}
		add_character_flag = safe_start
		add_prestige = miniscule_prestige_loss
		ai_chance = {
			base = 100
			modifier = {
				add = 40
				OR = {
					has_trait = content
					has_trait = lazy
					has_trait = craven
				}
			}
			modifier = {
				add = 40
				stress_level < 0
			}
		}
	}

	after = {
		trigger_event = tgp_china_decision.1001
	}
}

tgp_china_decision.1001 = {
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						has_character_modifier = tgp_local_exam_initial_success
						has_character_flag = safe_start
					}
				}
				desc = tgp_china_decision.1001.t_success
			}
			desc = tgp_china_decision.1001.t_fail
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					has_character_modifier = tgp_local_exam_initial_success
				}
				desc = tgp_china_decision.1001.desc_success
			}
			triggered_desc = {
				trigger = {
					var:safe_bets = 1
				}
				desc = tgp_china_decision.1001.desc_safe
			}
			desc = tgp_china_decision.1001.desc_fail
		}
		desc = tgp_china_decision.1001.desc_outro
	}

	theme = imperial_examination
	override_background = { reference = study }

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				has_character_modifier = tgp_local_exam_initial_success
			}
  			animation = personality_bold
  		}
  		triggered_animation = {
			trigger = {
				var:safe_bets = 1
			}
  			animation = shame
  		}
		animation = disappointed
	}

	right_portrait = {
		character = scope:examiner
		animation = happy_teacher
	}

	immediate = {
		set_variable = {
			name = child_examination_success_chance
			value = child_examination_success_chance_value
		}
	}

	widget = { gui = "child_examination_success_chance" container = "custom_widgets_container" }

	option = { # Recite classics
		name = tgp_china_decision.1001.a
		duel = {
			skill = learning
			value = 1
			40 = { # You succeed
				desc = provincial_exam_success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3
				}
				add_character_modifier = {
					modifier = tgp_local_exam_impressed_examiner
					years = 12
				}
				custom_tooltip = examination_success_increase_major_tt
			}
			60 = { # You fail
				desc = provincial_exam_duel_fail
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3
				}
				stress_impact = { base = medium_stress_impact_gain }
			}
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 1
				ai_energy = 1
			}
			modifier = {
				add = 20
				learning > medium_skill_rating
			}
			modifier = {
				add = 40
				has_trait = ambitious
			}
			modifier = {
				add = -40
				OR = {
					has_trait = content
					has_trait = lazy
					has_trait = craven
				}
			}
		}
	}

	option = { # Play it safe
		name = tgp_china_decision.1001.b
		custom_tooltip = examination_success_increase_miniscule_tt
		# Use this var to track score
		change_variable = {
			name = safe_bets
			add = 1
		}
		# Use this flag for loc only
		add_character_flag = safe_midpoint
		add_prestige = miniscule_prestige_loss
		ai_chance = {
			base = 100
			modifier = {
				add = 40
				OR = {
					has_trait = content
					has_trait = lazy
					has_trait = craven
				}
			}
			modifier = {
				add = 40
				stress_level < 0
			}
		}
	}

	after = {
		trigger_event = tgp_china_decision.1002
	}
}

tgp_china_decision.1002 = {
	type = character_event
	title = {
		first_valid = {
			triggered_desc = {
				trigger = {
					OR = {
						has_character_modifier = tgp_local_exam_impressed_examiner
						has_character_flag = safe_midpoint
					}
				}
				desc = tgp_china_decision.1002.t_success
			}
			desc = tgp_china_decision.1002.t_fail
		}
	}
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = {
					has_character_modifier = tgp_local_exam_impressed_examiner
				}
				desc = tgp_china_decision.1002.desc_success
			}
			triggered_desc = {
				trigger = {
					has_character_flag = safe_midpoint
				}
				desc = tgp_china_decision.1002.desc_safe
			}
			desc = tgp_china_decision.1002.desc_fail
		}
		desc = tgp_china_decision.1002.desc_outro
	}

	theme = imperial_examination
	override_background = { reference = study }

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				has_character_modifier = tgp_local_exam_impressed_examiner
			}
  			animation = happiness
  		}
  		triggered_animation = {
			trigger = {
				has_character_flag = safe_midpoint
			}
  			animation = shame
  		}
		animation = stress
	}

	right_portrait = {
		character = scope:examiner
		animation = happy_teacher
	}

	immediate = {
		set_variable = {
			name = child_examination_success_chance
			value = child_examination_success_chance_value
		}
	}

	widget = { gui = "child_examination_success_chance" container = "custom_widgets_container" }

	option = { # Recite classics
		name = tgp_china_decision.1002.a
		duel = {
			skill = learning
			value = 1
			30 = { # You succeed
				desc = provincial_exam_success
				compare_modifier = {
					value = scope:duel_value
					multiplier = 3
				}
				add_character_modifier = {
					modifier = tgp_local_exam_strong_finish
					years = 12
				}
				custom_tooltip = examination_success_increase_massive_tt
			}
			70 = { # You fail
				desc = provincial_exam_duel_fail
				compare_modifier = {
					value = scope:duel_value
					multiplier = -3
				}
				stress_impact = { base = medium_stress_impact_gain }
			}
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 1
				ai_energy = 1
			}
			modifier = {
				add = 20
				learning > medium_skill_rating
			}
			modifier = {
				add = 40
				has_trait = ambitious
			}
			modifier = {
				add = -40
				OR = {
					has_trait = content
					has_trait = lazy
					has_trait = craven
				}
			}
		}
	}

	option = { # Play it safe
		name = tgp_china_decision.1002.b
		custom_tooltip = examination_success_increase_miniscule_tt
		# Use this var to track score
		change_variable = {
			name = safe_bets
			add = 1
		}
		# Use this flag for loc only
		add_character_flag = safe_end
		add_prestige = miniscule_prestige_loss
		ai_chance = {
			base = 100
			modifier = {
				add = 40
				OR = {
					has_trait = content
					has_trait = lazy
					has_trait = craven
				}
			}
			modifier = {
				add = 40
				stress_level < 0
			}
		}
	}

	after = {
		set_variable = {
			name = child_examination_success_chance
			value = child_examination_success_chance_value
		}
		trigger_event = tgp_china_decision.1005
	}
}

# Releasing the Roll (Results)
tgp_china_decision.1005 = {
	type = character_event
	title = tgp_china_decision.1005.t
	desc = {
		# Opening
		desc = tgp_china_decision.1005.desc
		# Did you get a strong finish, play it safe, or fail?
		first_valid = {
			triggered_desc = {
				trigger = {
					has_character_modifier = tgp_local_exam_strong_finish
				}
				desc = tgp_china_decision.1005.desc.success
			}
			triggered_desc = {
				trigger = {
					has_character_flag = safe_end
				}
				desc = tgp_china_decision.1005.desc.safe
			}
			# If none of the above, you failed.
			desc = tgp_china_decision.1005.desc.failure
		}
		# And now, for the score
		desc = tgp_china_decision.1005.desc.segway
		first_valid = {
			triggered_desc = {
				trigger = {
					var:child_examination_success_chance >= 90
				}
				desc = tgp_china_decision.1005.desc.high_score
			}
			triggered_desc = {
				trigger = {
					var:child_examination_success_chance >= 60
				}
				desc = tgp_china_decision.1005.desc.good_score
			}
			triggered_desc = {
				trigger = {
					var:child_examination_success_chance >= 40
				}
				desc = tgp_china_decision.1005.desc.passing_score
			}
			desc = tgp_china_decision.1005.desc.failing
		}
	}

	theme = imperial_examination
	override_background = { reference = study }

	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				AND = {
					var:child_examination_success_chance >= 40
					var:child_examination_success_chance < 90
				}
			}
  			animation = war_over_win
  		}
  		triggered_animation = {
			trigger = {
				var:child_examination_success_chance < 40
			}
  			animation = disappointed
  		}
		animation = happiness
	}

	immediate = {
		if = {
			limit = {
				var:child_examination_success_chance >= 40
			}
			change_merit = {
				value = {
					add = minor_merit_value
					add = var:child_examination_success_chance
				}
			}
			dynasty = {
				add_dynasty_prestige = 10
			}
			# For all characters who pass the exam, we give a flag to check their overall progress at the Imperial Examinations.
			add_character_flag = passed_child_exam
			remove_character_flag ?= failed_child_exam
		}
		else = {
			add_character_flag = failed_child_exam
			add_character_modifier = {
				modifier = tgp_child_exam_cooldown_modifier
				years = 3
			}
		}
	}

	widget = { gui = "child_examination_success_chance" container = "custom_widgets_container" }

	# If you had a critical success, you do not get the option to dedicate yourself to studying.
	option = { # I have my family to thank for my success.
		name = tgp_china_decision.1005.a
		trigger = { var:child_examination_success_chance >= 90 }
		change_merit = medium_merit_value
		ai_chance = {
			base = 100
			modifier = {
				add = 40
				has_trait = loyal
			}
			modifier = {
				add = 50
				is_adult = no
			}
		}
	}

	option = { # My teachers deserve all the credit.
		name = tgp_china_decision.1005.b
		trigger = { var:child_examination_success_chance >= 90 }
		change_influence = medium_influence_gain
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_sociability = 1
			}
		}
	}

	option = { # I need to push myself harder next time!
		name = {
			text = tgp_china_decision.1005.a_success
			trigger = {
				var:child_examination_success_chance >= 40
			}
		}
		name = {
			text = tgp_china_decision.1005.a_failure
			trigger = {
				var:child_examination_success_chance < 40
			}
		}
		trigger = { var:child_examination_success_chance < 90 }
		add_character_modifier = {
			modifier = tgp_child_focused_studies_modifier
			years = 5
		}

		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 1
				ai_energy = 1
			}
			modifier = {
				add = 40
				OR = {
					has_trait = ambitious
					has_trait = diligent
				}
			}
			modifier = {
				add = -100
				stress_level < 0
			}
		}
	}

	option = { # I am pleased with this result.
		name = {
			text = tgp_china_decision.1005.b_passing
			trigger = {
				var:child_examination_success_chance >= 40
			}
		}
		name = {
			text = tgp_china_decision.1005.b_failed
			trigger = {
				var:child_examination_success_chance < 40
			}
		}
		trigger = { var:child_examination_success_chance < 90 }

		ai_chance = {
			base = 100
			modifier = {
				add = 40
				OR = {
					has_trait = content
					has_trait = lazy
				}
			}
		}
	}

	after = {
		# Time to clean up some variables
		remove_character_flag ?= safe_end
		remove_variable ?= safe_bets
		# And possibly the examiner
		scope:examiner = {
			if = {
				limit = {
					NOT = { is_vassal_or_below_of = scope:hegemon }
				}
				silent_disappearance_effect = yes
			}
		}
	}
}
