
namespace = tgp_east_asia_decision_events

#You adopted Mandala
tgp_east_asia_decision_events.0010 = {
	type = character_event
	title = tgp_east_asia_decision_events.0010.t
	desc = tgp_east_asia_decision_events.0010.desc
	theme = mandala
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = { has_trait = zealous }
			animation = personality_zealous
		}
		animation = eccentric
	}
	immediate = {
		if = {
			limit = { has_variable = vying_for_mandala_divinity }
			custom_description_no_bullet = { text = because_of_your_divine_happenstance_tt }
			divine_happenstance_adopt_mandala_effect = yes
			remove_variable = vying_for_mandala_divinity
		}
	}
	#Alright, and I'll take a Temple Citadel too, please
	option = {
		name = tgp_east_asia_decision_events.0010.a
		trigger = {
			any_directly_owned_province = { has_holding_type = tribal_holding }
		}
		#This changes all holdings for Tribals, or only the Capital for everyone else
		if = {
			limit = {
				culture = {
					has_cultural_parameter = mandala_tributaries 
				}
				confederation ?= { is_house_based = no }
			}
			hidden_effect = {
				confederation = {
					every_confederation_member = {
						limit = {
							this != root
						}
						add_to_list = tributaries
					}
				}
			}
		}
		change_to_mandala_government_and_citadel_effect = yes
		if = {
			limit = {
				culture = {
					has_cultural_parameter = mandala_tributaries 
				}
				any_in_list = {
					list = tributaries
					is_alive = yes
				}
			}
			hidden_effect = {
				every_in_list = {
					list = tributaries
					confederation = {
						remove_confederation_member = prev
					}
					start_tributary_interaction_effect = {
						TRIBUTARY = this
						SUZERAIN = root
					}
				}
			}
			custom_tooltip = confederates_to_tributaries_tt
		}
		ai_chance = {
			base = 100
		}
	}
	#Only the government for me thanks
	option = {
		name = {
			text = {
				first_valid = {
					triggered_desc = {
						trigger = {
							capital_province = { has_holding_type = temple_citadel_holding }
						}
						desc = tgp_east_asia_decision_events.0010.b_temple_citadel
					}
					desc = tgp_east_asia_decision_events.0010.b
				}
			}
		}
		trigger = {
			#Sorry, Tribal Holdings need to go
			NOR = {
				capital_province = { has_holding_type = tribal_holding }
				any_directly_owned_province = { has_holding_type = tribal_holding }
			}
			#You need to either have a Temple Citadel or somewhere to build a Temple Citadel to avoid noob trap
			OR = {
				any_realm_province = {
					OR = {
						has_holding = no
						has_holding_type = temple_citadel_holding
					}
				}
				capital_province = { has_holding_type = temple_citadel_holding }
			}
			
		}
		if = {
			limit = {
				culture = {
					has_cultural_parameter = mandala_tributaries 
				}
				confederation ?= { is_house_based = no }
			}
			hidden_effect = {
				confederation = {
					every_confederation_member = {
						limit = {
							this != root
						}
						add_to_list = tributaries
					}
				}
			}
		}
		change_to_mandala_government_effect = yes
		if = {
			limit = {
				culture = {
					has_cultural_parameter = mandala_tributaries 
				}
				any_in_list = {
					list = tributaries
					is_alive = yes
				}
			}
			hidden_effect = {
				every_in_list = {
					list = tributaries
					confederation = {
						remove_confederation_member = prev
					}
					start_tributary_interaction_effect = {
						TRIBUTARY = this
						SUZERAIN = root
					}
				}
			}
			custom_tooltip = confederates_to_tributaries_tt
		}
		ai_chance = {
			base = 0
		}
	}
}

#Vassals get notified
tgp_east_asia_decision_events.0015 = {
	type = letter_event
	opening = tgp_east_asia_decision_events.0015.opening
	desc = tgp_east_asia_decision_events.0015.desc
	sender = {
		character = scope:liege
		animation = personality_zealous
	}
	immediate = {
		liege = {
			save_scope_as = liege
			show_as_tooltip = { change_government = mandala_government }
		}
	}
	#What does this mean
	option = {
		name = tgp_east_asia_decision_events.0015.a
	}
}


#You assimilated to your Overlord being Mandala
tgp_east_asia_decision_events.0020 = {
	type = character_event
	title = tgp_east_asia_decision_events.0020.t
	desc = tgp_east_asia_decision_events.0020.desc
	theme = mandala
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = { has_trait = zealous }
			animation = personality_zealous
		}
		animation = eccentric
	}
	lower_right_portrait = {
		character = scope:overlord
	}
	immediate = {
		overlord = { save_scope_as = overlord }
		if = {
			limit = {
				NOR = { 
					faith = overlord.faith 
					faith.religious_head ?= root
				}
			}
			set_character_faith = overlord.faith
			custom_tooltip = tgp_east_asia_decision_events.0020.close_family_post_tt
			hidden_effect = {
				# Family at court also convert
				every_close_family_member = {
					limit = {
						is_courtier_of = root
						faith != root.overlord.faith
					}
					set_character_faith = root.overlord.faith
				}
			}
		}
	}
	#Alright, and I'll take a Temple Citadel too, please
	option = {
		name = tgp_east_asia_decision_events.0020.a	
		if = {
			limit = {
				NOT = { government_has_flag = government_is_mandala }
			}
			#This changes all holdings for Tribals, or only the Capital for everyone else
			change_to_mandala_government_and_citadel_effect = yes
		}
		ai_chance = {
			base = 100
		}
	}
	#Only the government for me thanks
	option = {
		name = tgp_east_asia_decision_events.0020.b
		trigger = {
			NOR = {
				government_has_flag = government_is_mandala
				#Sorry, Tribal Holdings need to go
				capital_province = { has_holding_type = tribal_holding }
			}
			#You need to either have a Temple Citadel or somewhere to build a Temple Citadel to avoid noob trap
			any_realm_province = {
				OR = {
					has_holding = no
					has_holding_type = temple_citadel_holding
				}
			}
		}
		change_to_mandala_government_effect = yes
		ai_chance = {
			base = 0
		}
	}
	after = {
		#Change the tributary type to Mandala
		if = {
			limit = {
				is_tributary_of = scope:overlord
				NOT = { has_subject_contract_group = tributary_mandala }
			}
			hidden_effect = {
				end_tributary = yes
				#This still triggers the invalidation toast, somehow — should be reverted when code Jira TIT-68838 is fixed
				trigger_event = {
					id = tgp_east_asia_decision_events.0021
					delayed = yes
				}
			}
		}
	}
}

#Ful-fix to make the tributary update not invalidate
tgp_east_asia_decision_events.0021 = {
	hidden = yes
	immediate = {
		start_tributary_interaction_effect = {
			TRIBUTARY = root
			SUZERAIN = scope:overlord
		}
	}
}

#Vassals get notified
tgp_east_asia_decision_events.0025 = {
	type = letter_event
	opening = tgp_east_asia_decision_events.0025.opening
	desc = tgp_east_asia_decision_events.0025.desc
	sender = {
		character = scope:liege
		animation = personality_zealous
	}
	immediate = {
		liege = {
			save_scope_as = liege
			overlord = { save_scope_as = overlord }
			show_as_tooltip = { mandala_assimilation_effect = yes }
		}
	}
	#What does this mean
	option = {
		name = tgp_east_asia_decision_events.0025.a
	}
}


#Your Mandala Liege turned Clan
tgp_east_asia_decision_events.0030 = {
	type = character_event
	title = tgp_east_asia_decision_events.0030.t
	desc = tgp_east_asia_decision_events.0030.desc
	theme = court
	override_background = { reference = throne_room }
	left_portrait = {
		character = root
		animation = worry
	}
	lower_right_portrait = {
		character = scope:liege
	}
	immediate = {
		liege = { 
			save_scope_as = liege
			show_as_tooltip = { change_government = clan_government }
		}
		show_as_tooltip = {
			if = {
				limit = {
					capital_province = {
						NOR = { 
							has_holding_type = temple_citadel_holding
							has_holding_type = castle_holding
						}
					}
				}
				capital_province = { set_holding_type = castle_holding }
			}
			change_government = clan_government
			#Start off crown authorities
			if = {
				limit = {
					NOT = { has_realm_law = crown_authority_0 }
				}
				add_realm_law_skip_effects = crown_authority_0
			}
		}
	}
	#Okidoki
	option = {
		name = tgp_east_asia_decision_events.0030.a
		ai_chance = {
			base = 100
		}
	}
}


#Assimilation Payout Event
tgp_east_asia_decision_events.0100 = {
	type = character_event
	title = tgp_east_asia_decision_events.0100.t
	desc = tgp_east_asia_decision_events.0100.desc
	theme = mandala
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = { has_trait = zealous }
			animation = personality_zealous
		}
		animation = admiration
	}
	right_portrait = {
		character = scope:overlord
		animation = personality_zealous
	}
	trigger = {
		this != top_overlord
		government_has_flag = government_is_mandala
		AND = {
			has_variable = mandala_assimilation_devaraja
			var:mandala_assimilation_devaraja = {
				government_has_flag = government_is_mandala
				this = { is_alive = yes }
				this = overlord
			}
		}
		AND = {
			has_variable = mandala_assimilation_devaraja_piety_level
			var:mandala_assimilation_devaraja_piety_level >= overlord.piety_level
		}
	}
	on_trigger_fail = { trigger_event = tgp_east_asia_decision_events.0110 }
	#Did they level up?
	override_effect_2d = {
		trigger = { overlord.piety_level > var:mandala_assimilation_devaraja_piety_level }
		reference = legend_glow
	}
	immediate = {
		overlord = { save_scope_as = overlord }
	}
	#Cool!
	option = {
		name = tgp_east_asia_decision_events.0100.a
		if = {
			limit = { has_character_flag = assimilation_payout_t3 }
			add_piety = assimilation_piety_payout_t3
			add_legitimacy_effect = { LEGITIMACY = assimilation_legitimacy_payout_t3 }
		}
		else_if = {
			limit = { has_character_flag = assimilation_payout_t2 }
			add_piety = assimilation_piety_payout_t2
			add_legitimacy_effect = { LEGITIMACY = assimilation_legitimacy_payout_t2 }
		}
		else_if = {
			limit = { has_character_flag = assimilation_payout_t1 }
			add_piety = assimilation_piety_payout_t1
			add_legitimacy_effect = { LEGITIMACY = assimilation_legitimacy_payout_t1 }
		}
	}
	after = { mandala_clean_up_variables_effect = yes }
}

#Whelp, either you or your Devaraja have lost your way
tgp_east_asia_decision_events.0110 = {
	hidden = yes

	immediate = {
		var:mandala_assimilation_devaraja ?= { save_scope_as = saved_devaraja }
		send_interface_message = {
			type = event_conversion_bad_text
			title = failed_assimilation_payout_msg
			desc = failed_assimilation_payout_msg_desc
			left_icon = root
			right_icon = scope:saved_devaraja
		}
		mandala_clean_up_variables_effect = yes
	}
}

scripted_trigger base_court_brahmin_triggers = {
	is_adult = yes
	has_any_disease_trigger = no
}

scripted_effect recruit_mandala_court_position_effect = {
	pay_short_term_gold = {
		target = $RECRUITEE$
		gold = $GOLD_VALUE$
	}
	add_courtier = $RECRUITEE$
	appoint_court_position = {
		recipient = $RECRUITEE$
		court_position = $COURT_POSITION$
	}
	hidden_effect = {
		$RECRUITEE$ = {
			add_opinion = {
				modifier = grateful_opinion
				target = root
				opinion = 25
			}
		}
	}
}

#Hire a Court Brahmin
tgp_east_asia_decision_events.0200 = {
	type = character_event
	title = tgp_east_asia_decision_events.0200.t
	desc = tgp_east_asia_decision_events.0200.desc
	theme = mandala
	left_portrait = {
		character = scope:great_brahmin_option
		animation = personality_zealous
	}
	right_portrait = {
		character = scope:good_brahmin_option 
		animation = thinking
	}
	immediate = {
		#Good character
		if = {
			limit = {
				any_pool_character = {
					province = root.capital_province
					base_court_brahmin_triggers = yes
					court_brahmin_validity_trigger = { EMPLOYER = root }
					"aptitude(court_brahmin_court_position)" = 3
				}
			}
			random_pool_character = {
				province = root.capital_province
				limit = {
					base_court_brahmin_triggers = yes
					court_brahmin_validity_trigger = { EMPLOYER = root }
					"aptitude(court_brahmin_court_position)" = 3
				}
				save_scope_as = good_brahmin_option
			}
		}
		else = {
			create_character = {
				location = root.capital_province
				template = tgp_court_brahmin_template
				age = { 25 40 }
				random_traits_list = {
					count = 1
					education_learning_1 = {}
					education_learning_2 = {}
				}
				save_scope_as = good_brahmin_option
				after_creation = { save_scope_as = goodly_created }
			}
			hidden_effect = {
				random_list = {
					50 = {
						religion:buddhism_religion = {
							random_faith = {
								save_scope_as = brahmin_faith
							}
						}
					}
					50 = {
						religion:hinduism_religion = {
							random_faith = {
								save_scope_as = brahmin_faith
							}
						}
					}
				}
				scope:good_brahmin_option = {
					set_character_faith = scope:brahmin_faith
				}
			}
		}
		#Great character
		if = {
			limit = {
				any_pool_character = {
					province = root.capital_province
					base_court_brahmin_triggers = yes
					court_brahmin_validity_trigger = { EMPLOYER = root }
					"aptitude(court_brahmin_court_position)" = 5
				}
			}
			random_pool_character = {
				province = root.capital_province
				limit = {
					base_court_brahmin_triggers = yes
					court_brahmin_validity_trigger = { EMPLOYER = root }
					"aptitude(court_brahmin_court_position)" = 5
				}
				save_scope_as = great_brahmin_option
			}
		}
		else = {
			create_character = {
				location = root.capital_province
				gender_female_chance = root_faith_clergy_gender_female_chance
				template = tgp_court_brahmin_template
				age = { 40 50 }
				trait = devoted
				trait = compassionate
				trait = education_learning_4
				random_traits_list = {
					count = 1
					theologian = {}
					avaricious = {}
				}
				save_scope_as = great_brahmin_option
				after_creation = { save_scope_as = greatly_created }
			}
			hidden_effect = {
				random_list = {
					50 = {
						religion:buddhism_religion = {
							random_faith = {
								save_scope_as = brahmin_faith
							}
						}
					}
					50 = {
						religion:hinduism_religion = {
							random_faith = {
								save_scope_as = brahmin_faith
							}
						}
					}
				}
				scope:great_brahmin_option = {
					set_character_faith = scope:brahmin_faith
				}
			}
		}
	}
	#Great option
	option = {
		name = tgp_east_asia_decision_events.0200.a
		custom_tooltip = tgp_east_asia_decision_events.0200.a.aptitude
		recruit_mandala_court_position_effect = {
			RECRUITEE = scope:great_brahmin_option
			GOLD_VALUE = medium_gold_value
			COURT_POSITION = court_brahmin_court_position
		}
		ai_chance = {
			base = 100
			modifier = {
				short_term_gold > medium_gold_value
			}
		}
	}
	#Good option
	option = {
		name = tgp_east_asia_decision_events.0200.b
		custom_tooltip = tgp_east_asia_decision_events.0200.b.aptitude
		recruit_mandala_court_position_effect = {
			RECRUITEE = scope:good_brahmin_option
			GOLD_VALUE = minor_gold_value
			COURT_POSITION = court_brahmin_court_position
		}
	}
	#Nah
	option = {
		name = tgp_east_asia_decision_events.0200.c
	}
	#Sweep, sweep
	after = {
		if = {
			limit = {
				exists = scope:goodly_created
				NOT = {
					scope:goodly_created = { is_courtier_of = root }
				}
			}
			scope:goodly_created = { silent_disappearance_effect = yes }
		}
		if = {
			limit = {
				exists = scope:greatly_created
				NOT = {
					scope:greatly_created = { is_courtier_of = root }
				}
			}
			scope:greatly_created = { silent_disappearance_effect = yes }
		}
	}
}

#Hire a Khlon Glan
tgp_east_asia_decision_events.0210 = {
	type = character_event
	title = tgp_east_asia_decision_events.0210.t
	desc = tgp_east_asia_decision_events.0210.desc
	theme = mandala
	override_background = armory
	left_portrait = {
		character = scope:great_khlon_glan_option
		animation = survey
	}
	right_portrait = {
		character = scope:good_khlon_glan_option 
		animation = chess_cocky
	}
	immediate = {
		#Good character
		if = {
			limit = {
				any_pool_character = {
					province = root.capital_province
					base_court_brahmin_triggers = yes
					khlon_glan_validity_trigger = { EMPLOYER = root }
					"aptitude(khlon_glan_court_position)" = 3
				}
			}
			random_pool_character = {
				province = root.capital_province
				limit = {
					base_court_brahmin_triggers = yes
					khlon_glan_validity_trigger = { EMPLOYER = root }
					"aptitude(khlon_glan_court_position)" = 3
				}
				save_scope_as = good_khlon_glan_option
			}
		}
		else = {
			create_character = {
				location = root.capital_province
				template = tgp_khlon_glan_template
				age = { 30 40 }
				random_traits_list = {
					count = 1
					education_stewardship_1 = {}
					education_stewardship_2 = {}
					education_martial_1 = {}
					education_martial_2 = {}
				}
				save_scope_as = good_khlon_glan_option
				after_creation = { save_scope_as = goodly_created }
			}
			scope:good_khlon_glan_option = {
				if = {
					limit = { martial < 10 }
					add_martial_skill = { 4 6 }
				}
				if = {
					limit = { stewardship < 10 }
					add_stewardship_skill = { 4 6 }
				}
			} 
		}
		#Great character
		if = {
			limit = {
				any_pool_character = {
					province = root.capital_province
					base_court_brahmin_triggers = yes
					khlon_glan_validity_trigger = { EMPLOYER = root }
					"aptitude(khlon_glan_court_position)" = 5
				}
			}
			random_pool_character = {
				province = root.capital_province
				limit = {
					base_court_brahmin_triggers = yes
					khlon_glan_validity_trigger = { EMPLOYER = root }
					"aptitude(khlon_glan_court_position)" = 5
				}
				save_scope_as = great_khlon_glan_option
			}
		}
		else = {
			create_character = {
				location = root.capital_province
				template = tgp_khlon_glan_template
				age = { 40 50 }
				random_traits_list = {
					count = 1
					education_stewardship_3 = {}
					education_stewardship_4 = {}
					education_martial_3 = {}
					education_martial_4 = {}
				}
				random_traits_list = {
					count = 1
					administrator = {}
					overseer = {}
				}
				trait = organizer
				save_scope_as = great_khlon_glan_option
				after_creation = { save_scope_as = greatly_created }
			}
			scope:great_khlon_glan_option = {
				if = {
					limit = { martial < 10 }
					add_martial_skill = { 6 8 }
				}
				if = {
					limit = { stewardship < 10 }
					add_stewardship_skill = { 6 8 }
				}
			} 
		}
	}
	#Great option
	option = {
		name = tgp_east_asia_decision_events.0210.a
		custom_tooltip = tgp_east_asia_decision_events.0210.a.aptitude
		recruit_mandala_court_position_effect = {
			RECRUITEE = scope:great_khlon_glan_option
			GOLD_VALUE = medium_gold_value
			COURT_POSITION = khlon_glan_court_position
		}
		ai_chance = {
			base = 100
			modifier = {
				short_term_gold > medium_gold_value
			}
		}
	}
	#Good option
	option = {
		name = tgp_east_asia_decision_events.0210.b
		custom_tooltip = tgp_east_asia_decision_events.0210.b.aptitude
		recruit_mandala_court_position_effect = {
			RECRUITEE = scope:good_khlon_glan_option
			GOLD_VALUE = minor_gold_value
			COURT_POSITION = khlon_glan_court_position
		}
	}
	#Nah
	option = {
		name = tgp_east_asia_decision_events.0210.c
	}
	#Sweep, sweep
	after = {
		if = {
			limit = {
				exists = scope:goodly_created
				NOT = {
					scope:goodly_created = { is_courtier_of = root }
				}
			}
			scope:goodly_created = { silent_disappearance_effect = yes }
		}
		if = {
			limit = {
				exists = scope:greatly_created
				NOT = {
					scope:greatly_created = { is_courtier_of = root }
				}
			}
			scope:greatly_created = { silent_disappearance_effect = yes }
		}
	}
}

#Create Bunga Mas
tgp_east_asia_decision_events.0300 = {
	type = character_event
	title = tgp_east_asia_decision_events.0300.t
	desc = {
		desc = tgp_east_asia_decision_events.0300.desc
		first_valid = {
			triggered_desc = {
				trigger = {
					scope:newly_created_bunga_mas = { rarity = illustrious }
				}
				desc = tgp_east_asia_decision_events.0300.outro.illustrious
			}
			triggered_desc = {
				trigger = {
					scope:newly_created_bunga_mas = { rarity = famed }
				}
				desc = tgp_east_asia_decision_events.0300.outro.famed
			}
			triggered_desc = {
				trigger = {
					scope:newly_created_bunga_mas = { rarity = masterwork }
				}
				desc = tgp_east_asia_decision_events.0300.outro.masterwork
			}
			desc = tgp_east_asia_decision_events.0300.outro
		}
	}
	theme = mandala
	left_portrait = {
		character = root
		triggered_animation = {
			trigger = {
				scope:newly_created_bunga_mas = { rarity = illustrious }
			}
			animation = ecstasy
		}
		triggered_animation = {
			trigger = {
				scope:newly_created_bunga_mas = { rarity = famed }
			}
			animation = admiration
		}
		triggered_animation = {
			trigger = {
				scope:newly_created_bunga_mas = { rarity = masterwork }
			}
			animation = interested
		}
		animation = thinking
	}
	right_portrait = {
		character = scope:local_artisan
		animation = admiration
	}
	artifact = {
	    target = scope:newly_created_bunga_mas
	    position = lower_center_portrait
	}
	immediate = {
		overlord = { save_scope_as = overlord }

		random_list = {
			50 = {
				get_artifact_quality_effect = yes
				get_artifact_wealth_effect = yes
			}
			25 = { set_artifact_rarity_famed = yes }
			10 = {
				trigger = { has_relation_best_friend = scope:overlord }
				set_artifact_rarity_illustrious = yes
			}
		}

		scope:local_artisan = {
			hidden_effect_new_object = {
				create_artifact = {
					name = bunga_mas
					description = common_bunga_mas_desc
					modifier = artifact_placeholder_modifier
					wealth = scope:wealth
					quality = scope:quality
					type = pedestal
					visuals = bunga_mas
					save_scope_as = newly_created_bunga_mas
				}
				scope:newly_created_bunga_mas = {
					remove_artifact_modifier = artifact_placeholder_modifier
					if = {
						limit = { rarity = illustrious }
						set_artifact_description = illustrious_bunga_mas_desc
						add_artifact_modifier = artifact_monthly_piety_gain_per_legitimacy_level_mult_3_modifier
						add_artifact_modifier = artifact_legitimacy_loss_mult_5_modifier
					}
					else_if = {
						limit = { rarity = famed }
						set_artifact_description = famed_bunga_mas_desc
						add_artifact_modifier = artifact_monthly_piety_gain_per_legitimacy_level_mult_2_modifier
						add_artifact_modifier = artifact_legitimacy_loss_mult_4_modifier
					}
					else_if = {
						limit = { rarity = masterwork }
						set_artifact_description = masterwork_bunga_mas_desc
						add_artifact_modifier = artifact_monthly_piety_gain_per_legitimacy_level_mult_2_modifier
						add_artifact_modifier = artifact_legitimacy_loss_mult_3_modifier
					}
					else = { 
						add_artifact_modifier = artifact_monthly_piety_gain_per_legitimacy_level_mult_1_modifier
						add_artifact_modifier = artifact_legitimacy_loss_mult_2_modifier
					}
					set_variable = {
						name = bunga_mas_created_by
						value = root
					}
				}
			}
		}

	}

	#Nah
	option = {
		name = tgp_east_asia_decision_events.0300.a
		scope:newly_created_bunga_mas = {
			set_owner = root
			if = {
				limit = { rarity = illustrious }
				custom_tooltip = illustrious_bunga_mas_desc.tt
			}
			else_if = {
				limit = { rarity = famed }
				custom_tooltip = famed_bunga_mas_desc.tt
			}
			else_if = {
				limit = { rarity = masterwork }
				custom_tooltip = masterwork_bunga_mas_desc.tt
			}
			else = { custom_tooltip = common_bunga_mas_desc.tt }
		}
		ai_chance = {
			base = 50
		}
	}
}
