﻿save_old_government_effect = {
	if = {
		limit = {
			government_has_mechanic = administrative
		}
		save_scope_value_as = { name = admin value = yes }
	}
	government_type ?= { save_scope_as = old_government }
}

assign_tribal_governments_effect = {
	if = {
		limit = {
			has_tgp_dlc_trigger = yes
			OR = {
				government_has_flag = government_is_wanua
				scope:old_government ?= government_type:wanua_government
				scope:capital_county.title_province ?= {
					geographical_region = world_asia_southeast_islands
				}
			}
		}
		change_government = wanua_government
	}
	else = {
		change_government = tribal_government
	}
}

assign_feudal_governments_effect = {
	if = {
		limit = {
			tgp_can_become_japanese_feudal_trigger = yes
		}
		change_government = japan_feudal_government
	}
	else_if = {
		limit = {
			ep3_is_clan_inclined_trigger = yes
		}
		change_government = clan_government
	}
	else = {
		change_government = feudal_government
	}
}

update_feudal_governments_by_religion_effect = {
	if = {
		limit = {
			faith = { religion_tag = islam_religion }
			government_has_flag = government_is_feudal
		}
		change_government = clan_government
	}
	else_if = {
		limit = {
			ep3_is_clan_inclined_trigger = no
			government_has_flag = government_is_clan
		}
		change_government = feudal_government
	}
}

assign_feudal_governments_by_religion_effect = {
	if = {
		limit = {
			faith = { religion_tag = islam_religion }
		}
		change_government = clan_government
	}
	else = { change_government = feudal_government }
}

assign_new_government_effect = {
	if = {
		limit = {
			exists = scope:new_capital
			NOT = { exists = scope:capital_county }
		}
		scope:new_capital = { save_scope_as = capital_county }
	}
	save_scope_as = new_ruler
	# Change our government according to the government of our liege.
	## Nomad.
	if = {
		limit = {
			scope:capital_county.title_province = {
				OR = {
					has_holding_type = nomad_holding
					has_holding_type = herder_holding
				}
			}
		}
		hidden_effect = {
			every_held_title = {
				title_tier = county
				limit = {
					title_province ?= { has_holding_type = herder_holding }
				}
				title_province = { set_holding_type = nomad_holding }
			}
		}
		change_government = nomad_government
	}
	## Wanua or Tribal.
	else_if = {
		limit = {
			scope:capital_county.title_province = {
				has_holding_type = tribal_holding
			}
		}
		assign_tribal_governments_effect = yes
	}
	## Mandala.
	else_if = {
		limit = {
			has_mandala_culture_trigger = yes
			has_mandala_faith_trigger = yes
			scope:capital_county.title_province = {
				has_holding_type = temple_citadel_holding
			}
		}
		change_government = mandala_government
		#Sort out the House shenanigans
		hidden_effect = {
			if = {
				limit = { is_house_head = no }
				house = {
					if = {
						limit = {
							house_head = {
								is_landed = no
								is_courtier_of = scope:new_ruler
							}
						}
						set_house_head = scope:new_ruler
					}
				}
			}
			else = {
				house ?= {
					set_house_aspiration = { type = no_aspect }
				}
			}
		}
	}
	else_if = {
		limit = {
			OR = {
				government_has_mechanic = administrative
				exists = scope:admin
			}
			trigger_if = {
				limit = {
					independent = no
					liege = {
						government_has_mechanic = administrative
					}
				}
				NOR = {
					has_character_flag = granted_title_government_blocker
					prestige_level > 2
					piety_level > 2
					AND = {
						dynasty ?= {
							dynasty_prestige_level > 2
						}
					}
				}
			}
		}
		change_to_administrative_effect = yes
	}
	# add check for non-playable govs?
	else_if = {
		limit = { 
			exists = scope:old_government 
			trigger_if = {
				limit = {
					independent = no
					liege = {
						government_has_mechanic = administrative
					}
				}
				NOR = {
					has_character_flag = granted_title_government_blocker
					prestige_level > 2
					piety_level > 2
					AND = {
						dynasty ?= {
							dynasty_prestige_level > 2
						}
					}
				}
			}
		}
		change_government = scope:old_government
	}
	## Else feudal.
	else = { assign_feudal_governments_effect = yes }
}
