﻿
change_to_administrative_hereditary_effect = {
	scope:recipient = {
		save_scope_as = petition_vassal
		every_held_title = {
			limit = {
				tier = scope:petition_vassal.highest_held_title_tier
				is_landless_type_title = no
				is_noble_family_title = no
			}
			custom_tooltip = {
				text = petition_liege_house_governorship_rights_tt
				set_variable = {
					name = petition_house_rights
					value = scope:petition_vassal.house
					years = 100
				}
			}
		}
	}
}

change_to_administrative_effect = {
	save_scope_as = governor
	top_liege = { save_scope_as = governor_liege }
	# Set the correct government
	if = {
		limit = {
			OR = {
				NOT = { government_allows = administrative }
				AND = {
					government_allows = administrative
					scope:governor_liege = { government_has_flag = government_is_japan_administrative }
					NOT = { government_has_flag = government_is_japan_administrative }
				}
				AND = {
					government_allows = administrative
					scope:governor_liege = { government_has_flag = government_is_meritocratic }
					NOT = { government_has_flag = government_is_meritocratic }
				}
				AND = {
					government_allows = administrative
					scope:governor_liege = { government_has_flag = government_is_celestial }
					NOT = { government_has_flag = government_is_celestial }
				}
				AND = {
					government_allows = administrative
					scope:governor_liege = { government_has_flag = government_is_steppe_admin }
					NOT = { government_has_flag = government_is_steppe_admin }
				}
			}
		}
		# Only case where Estate type is shared between Admin and non-Admin
		if = {
			limit = {
				NAND = {
					government_has_flag = government_is_japan_feudal
					scope:governor_liege = { government_has_flag = government_is_japan_administrative }
				}
			}
			save_scope_as = new_admin
		}
		# If we are greek, let's ensure we become administrative
		if = {
			limit = {
				NOT = { exists = scope:has_selected_a_government }
				top_liege = {
					OR = {
						culture = culture:greek
						culture = {
							any_parent_culture_or_above = { this = culture:greek }
						}
					}
				}
			}
			save_scope_value_as = {
				name = has_selected_a_government
				value = flag:administrative
			}
		}
		# Change government
		# We first check if you have a selected government, then if you are independent, then what your liege has
		if = { # Ritsuryo
			limit = {
				trigger_if = {
					limit = {
						exists = scope:has_selected_a_government
					}
					scope:has_selected_a_government = flag:japan_administrative
				}
				trigger_else_if = {
					limit = {
						top_liege = this
					}
					has_tgp_dlc_trigger = yes
					tgp_can_become_japan_administrative_trigger = yes
				}
				trigger_else = {
					scope:governor_liege = { government_has_flag = government_is_japan_administrative }
				}
			}
			tgp_domicile_conversion_when_changing_government_type = {
				NEW_DOMICILE_TYPE = japanese_manor
				NEW_GOVERNMENT_TYPE = japan_administrative_government
			}
			change_government = japan_administrative_government
		}
		else_if = { # Celestial
			limit = {
				trigger_if = {
					limit = {
						exists = scope:has_selected_a_government
					}
					scope:has_selected_a_government = flag:celestial
				}
				trigger_else_if = {
					limit = {
						top_liege = this
					}
					has_tgp_dlc_trigger = yes
					OR = {
						tgp_can_become_celestial_trigger = yes
						#This is Mongol breakup and they are hegemon
						AND = {
							exists = scope:great_yuan_ruler
							this = scope:great_yuan_ruler
							primary_title = title:h_china
						}
					}
				}
				trigger_else = {
					scope:governor_liege = { government_has_flag = government_is_celestial }
				}
			}
			capital_province = {
				if = {
					limit = {
						OR = {
							has_holding_type = tribal_holding
							has_holding_type = herder_holding
							has_holding_type = nomad_holding
						}
					}
					set_holding_type = castle_holding
				}
			}
			tgp_domicile_conversion_when_changing_government_type = {
				NEW_DOMICILE_TYPE = east_asian_estate
				NEW_GOVERNMENT_TYPE = celestial_government
			}
			change_government = celestial_government
		}
		else_if = { # Meritocratic Khanate
			limit = {
				trigger_if = {
					limit = {
						exists = scope:has_selected_a_government
					}
					# There's a separate decision to go from Nomad to Meritocratic Khanate, so you cannot pick it from here
					always = no
				}
				trigger_else = {
					scope:governor_liege = { government_has_flag = government_is_steppe_admin }
				}
			}
			capital_province = {
				if = {
					limit = {
						OR = {
							has_holding_type = tribal_holding
							has_holding_type = herder_holding
							has_holding_type = nomad_holding
						}
					}
					set_holding_type = castle_holding
				}
			}
			tgp_domicile_conversion_when_changing_government_type = {
				NEW_DOMICILE_TYPE = east_asian_estate
				NEW_GOVERNMENT_TYPE = steppe_admin_government
			}
			change_government = steppe_admin_government
		}
		else_if = { # Meritocratic
			limit = {
				trigger_if = {
					limit = {
						exists = scope:has_selected_a_government
					}
					scope:has_selected_a_government = flag:meritocratic
				}
				trigger_else_if = {
					limit = {
						top_liege = this
					}
					has_tgp_dlc_trigger = yes
					tgp_should_become_meritocratic_trigger = yes
				}
				trigger_else = {
					scope:governor_liege = { government_has_flag = government_is_meritocratic }
				}
			}
			capital_province = {
				if = {
					limit = {
						OR = {
							has_holding_type = tribal_holding
							has_holding_type = herder_holding
							has_holding_type = nomad_holding
						}
					}
					set_holding_type = castle_holding
				}
			}
			tgp_domicile_conversion_when_changing_government_type = {
				NEW_DOMICILE_TYPE = east_asian_estate
				NEW_GOVERNMENT_TYPE = meritocratic_government
			}
			change_government = meritocratic_government
		}
		else = { # Administrative
			capital_province = {
				if = {
					limit = {
						OR = {
							has_holding_type = tribal_holding
							has_holding_type = herder_holding
							has_holding_type = nomad_holding
						}
					}
					set_holding_type = castle_holding
				}
			}
			add_character_flag = new_government_is_byz_admin
			tgp_domicile_conversion_when_changing_government_type = {
				NEW_DOMICILE_TYPE = estate
				NEW_GOVERNMENT_TYPE = administrative_government
			}
			change_government = administrative_government 
			remove_character_flag = new_government_is_byz_admin
		}
	}
	# Set up a noble family title
	if = {
		limit = {
			primary_title.tier >= min_appointment_tier
			liege = {
				scope:governor_liege = this
				government_allows = administrative
			}
			house.house_head = {
				top_liege = scope:governor_liege
				NOT = {
					any_held_title = { is_noble_family_title = yes }
				}
			}
		}
		create_noble_family_effect = { GOVERNMENT_GIVER = top_liege }
	}
	# Add the governor trait if applicable
	hidden_effect = {
		if = {
			limit = {
				is_governor = yes
				NOT = { has_trait = governor }
			}
			add_trait = governor
		}
	}
	# Set up the domicile
	if = {
		limit = {
			exists = scope:new_admin
			exists = domicile
		}
		domicile ?= {
			set_up_domicile_estate_effect = yes
		}
	}
	if = {
		limit = {
			has_treasury = yes
		}
		add_treasury = {
			value = gold
			min = 100
		}
	}
}

change_to_administrative_interaction_effect = {
	scope:actor = {
		if = {
			limit = { has_treasury = yes }
			pay_short_term_treasury = {
				target = scope:recipient
				treasury = {
					value = 50
					scope:recipient = {
						if = {
							limit = { highest_held_title_tier >= tier_kingdom }
							multiply = 10
						}
						else_if = {
							limit = { highest_held_title_tier >= tier_duchy }
							multiply = 6
						}
						else_if = {
							limit = { highest_held_title_tier >= tier_county }
							multiply = 3
						}
					}
					if = {
						limit = { scope:gold ?= yes }
						add = scope:actor.medium_gold_value
					}
					if = {
						limit = {
							scope:actor = { has_realm_law_flag = admin_change_vassal_gov_cheaper }
						}
						multiply = 0.5
					}
				}
			}
		}
		else = {
			pay_short_term_gold = {
				target = scope:recipient
				gold = {
					value = 50
					scope:recipient = {
						if = {
							limit = { highest_held_title_tier >= tier_kingdom }
							multiply = 10
						}
						else_if = {
							limit = { highest_held_title_tier >= tier_duchy }
							multiply = 6
						}
						else_if = {
							limit = { highest_held_title_tier >= tier_county }
							multiply = 3
						}
					}
					if = {
						limit = { scope:gold ?= yes }
						add = scope:actor.medium_gold_value
					}
					if = {
						limit = {
							scope:actor = { has_realm_law_flag = admin_change_vassal_gov_cheaper }
						}
						multiply = 0.5
					}
				}
			}
		}
	}
	scope:recipient = {
		change_to_administrative_effect = yes

		#And now we also convert all the administrative vassals below so they also match the actor's form of admin.
		#For non admin sub-vassals the newly converted main vassal can still ask using the same interaction but we 
		hidden_effect = {
			every_vassal_or_below = {
				limit = {
					government_allows = administrative
					is_ai = yes #Humans get to be asked directly
				}
				change_to_administrative_effect = yes
			}
		}
	}
	if = {
		limit = {
			scope:hook = yes
			scope:actor = { has_usable_hook = scope:recipient }
		}
		scope:actor = { use_hook = scope:recipient }
	}
	if = {
		limit = { scope:hereditary = yes }
		scope:recipient = { save_scope_as = petition_vassal }
		change_to_administrative_hereditary_effect = yes
	}
	if = {
		limit = { scope:influence = yes }
		scope:actor = {
			change_influence = {
				value = massive_influence_loss
			}
		}
	}
}

set_up_domicile_estate_effect = {
	if = {
		limit = { is_domicile_type = estate }
		# Intentionally one level lower than what you can get
		if = {
			limit = {
				NOT = { has_domicile_building_or_higher = estate_main_02 }
				owner.culture ?= { has_innovation = innovation_city_planning }
			}
			add_domicile_building = estate_main_02
		}
		if = {
			limit = {
				has_domicile_building = estate_main_02
				NOT = { has_domicile_building_or_higher = estate_main_03 }
				owner.culture ?= { has_innovation = innovation_manorialism }
			}
			add_domicile_building = estate_main_03
		}
		hidden_effect = {
			switch = {
				trigger = has_domicile_building
				estate_main_03 = {
					while = {
						count = 2
						add_random_internal_estate_building = yes
					}
				}
				estate_main_02 = {
					add_random_internal_estate_building = yes
				}
			}
		}
		fill_external_estate_building_effect = yes
	}
	else_if = {
		limit = { is_domicile_type = japanese_manor }
		# Intentionally one level lower than what you can get
		if = {
			limit = {
				NOT = { has_domicile_building_or_higher = japanese_manor_main_02 }
				owner.culture ?= { has_innovation = innovation_city_planning }
			}
			add_domicile_building = japanese_manor_main_02
		}
		if = {
			limit = {
				has_domicile_building = japanese_manor_main_02
				NOT = { has_domicile_building_or_higher = japanese_manor_main_03 }
				owner.culture ?= { has_innovation = innovation_manorialism }
			}
			add_domicile_building = japanese_manor_main_03
		}
		hidden_effect = {
			switch = {
				trigger = has_domicile_building
				japanese_manor_main_03 = {
					while = {
						count = 2
						add_random_internal_japanese_manor_building = yes
					}
				}
				japanese_manor_main_02 = {
					add_random_internal_japanese_manor_building = yes
				}
			}
		}
		fill_external_japanese_manor_building_effect = yes
	}
	else_if = {
		limit = { is_domicile_type = east_asian_estate }
		# Intentionally one level lower than what you can get
		if = {
			limit = {
				NOT = { has_domicile_building_or_higher = east_asian_estate_main_02 }
				owner.culture ?= { has_innovation = innovation_city_planning }
			}
			add_domicile_building = east_asian_estate_main_02
		}
		if = {
			limit = {
				has_domicile_building = east_asian_estate_main_02
				NOT = { has_domicile_building_or_higher = east_asian_estate_main_03 }
				owner.culture ?= { has_innovation = innovation_manorialism }
			}
			add_domicile_building = east_asian_estate_main_03
		}
		hidden_effect = {
			switch = {
				trigger = has_domicile_building
				east_asian_estate_main_03 = {
					while = {
						count = 2
						add_random_internal_east_asian_estate_building = yes
					}
				}
				east_asian_estate_main_02 = {
					add_random_internal_east_asian_estate_building = yes
				}
			}
		}
		fill_external_east_asian_estate_building_effect = yes
	}
}

noble_family_title_realm_setup_effect = {
	save_scope_as = top_liege
	every_noble_family = {
		save_scope_as = nf_title
		holder ?= {
			save_scope_as = nf_holder
			house ?= {
				save_scope_as = nf_house
				# Ensure holders of historical noble family titles are the default house heads
				if = {
					limit = { house_head != scope:nf_holder }
					set_house_head = scope:nf_holder
				}
			}
		}
		scope:nf_title = {
			set_color_from_title = scope:nf_holder.capital_county
			# Ensure Noble Family CoA match House
			set_coa = scope:nf_house
		}
	}
}
