
tgp_is_japanese_kampaku_trigger = {
	has_title = title:e_japan
	NOR = {
		has_title = title:k_chrysanthemum_throne
		has_global_variable = tenno_restored
		has_global_variable = shogunate_established
	}
}

tgp_is_japanese_governor_trigger = {
	custom_tooltip = {
		text = tgp_is_japanese_governor_trigger
		government_has_flag = government_is_japan_administrative
		is_landed = yes
		is_ruler = yes
		highest_held_title_tier >= tier_county
		is_independent_ruler = no
		tgp_is_ceremonial_liege_trigger = no
	}
}

can_have_kampaku_acclamation_succession_law_trigger = {
	has_tgp_dlc_trigger = yes
	government_allows = administrative
	is_independent_ruler = yes
}

can_keep_kampaku_acclamation_succession_law_trigger = {
	has_tgp_dlc_trigger = yes
	trigger_if = {
		limit = {
			has_realm_law = japanese_regency_succession_law
		}
		OR = {
			can_have_kampaku_acclamation_succession_law_trigger = yes
			has_title = title:e_japan
		}
	}
}

any_descendants_are_governors = {
	# A direct descendant is currently a Governor
	any_child = { # Children
		OR = {
			tgp_is_japanese_governor_trigger = yes
			any_child = { # Grandchildren
				OR = {
					tgp_is_japanese_governor_trigger = yes
					any_child = { # Great-grandchildren
						OR = {
							tgp_is_japanese_governor_trigger = yes
							any_child = { # Great-great-grandchildren
								tgp_is_japanese_governor_trigger = yes
							}
						}
					}
				}
			}
		}
	}
}

any_liege_or_above_is_descendant = {
	any_liege_or_above = {
		is_liege_or_above_of = root
		OR = {
			is_child_of = root
			is_grandchild_of = root
			is_great_grandchild_of = root
		}
	}
}

is_a_previous_title_holder_trigger = {
	save_temporary_scope_as = previous_holder_compare
	$TITLE$ ?= {
		# Easy out
		trigger_if = {
			limit = { previous_holder ?= scope:previous_holder_compare }
			always = yes
		}
		# Hard out
		trigger_else = {
			any_past_holder = { this = scope:previous_holder_compare }
		}
	}
}

# Filter out dynasties of Emperors further away from you
# e.g. your father's brother's son
target_shares_nearest_related_title_holder_trigger = {
	trigger_if = {
		limit = {
			any_parent = {
				even_if_dead = yes
				is_a_previous_title_holder_trigger = { TITLE = $TITLE$ }
			}
		}
		is_child_of = $COMPARE$
	}
	trigger_else_if = {
		limit = {
			any_parent = {
				even_if_dead = yes
				any_parent = {	
					even_if_dead = yes
					is_a_previous_title_holder_trigger = { TITLE = $TITLE$ }
				}
			}
		}
		is_grandchild_of = $COMPARE$
	}
	trigger_else = { is_great_grandchild_of = $COMPARE$ }
}

is_culture_or_descended_from_trigger = {
	culture = {
		OR = {
			this = $CULTURE$
			any_parent_culture_or_above = { this = $CULTURE$ }
		}
	}
}

tgp_japan_offensive_wars_ban_trigger = {
	trigger_if = {
		limit = {
			exists = scope:defender.top_liege
			scope:attacker.top_liege ?= {
				has_title = title:e_japan
				scope:defender.top_liege != this
				government_is_japanese_trigger = yes
				realm_law_use_imperial_policy_trigger = yes
			}
		}
		custom_tooltip = {
			text = japan_offensive_wars_ban_tt
			scope:attacker.top_liege = { has_realm_law = imperial_expansion_law }
		}
	}
}

tgp_is_ceremonial_liege_or_direct_heir_trigger = {
	save_temporary_scope_as = char_temp
	top_liege.primary_title.var:administrative_ui_special_title ?= {
		OR = {
			holder ?= {
				OR = {
					this = scope:char_temp
					is_parent_of = scope:char_temp
				}
			}
			place_in_line_of_succession = {
				target = scope:char_temp
				value <= 5
			}
		}
	}
}

tgp_ceremonial_candidacy_restriction_trigger = {
	trigger_if = {
		limit = {
			exists = scope:target
			scope:actor ?= { tgp_realm_has_ceremonial_liege_trigger = yes }
			scope:secondary_recipient ?= { tgp_is_in_ceremonial_house_trigger = yes }
		}
		trigger_if = { # Top title only valid for ceremonial liege
			limit = {
				exists = scope:target.var:administrative_ui_special_title
				scope:target.holder = {
					OR = {
						has_realm_law = japanese_regency_succession_law
						has_realm_law = meritocratic_regency_succession_law
					}
				}
			}
			trigger_if = {
				limit = {
					top_liege.primary_title.var:administrative_ui_special_title.holder = { tgp_has_ceremonial_liege_title_trigger = yes }
				}
				custom_tooltip = {
					text = ceremonial_liege_only_heir_can_be_ruler_trigger
					scope:secondary_recipient = { tgp_is_ceremonial_liege_or_direct_heir_trigger = yes }
				}
			}
			trigger_else_if = {
				limit = {
					scope:actor = { is_independent_ruler = yes }
				}
				custom_tooltip = {
					text = ceremonial_liege_only_heir_can_be_ruler_trigger
					scope:secondary_recipient = scope:actor.player_heir
				}
			}
			trigger_else = {
				custom_tooltip = {
					text = ceremonial_liege_house_can_not_be_ruler_trigger
					scope:secondary_recipient = { tgp_is_ceremonial_liege_trigger = yes }
				}
			}
		}
		trigger_else = { # Governor titles only valid for non-ceremonial liege
			custom_tooltip = {
				text = ceremonial_liege_cannot_be_governor_trigger
				scope:secondary_recipient = { tgp_is_ceremonial_liege_trigger = no }
			}
		}
	}
}

tgp_japan_grant_titles_restriction_trigger = {
	trigger_if = {
		limit = {
			$GRANTER$ ?= { tgp_realm_has_ceremonial_liege_trigger = yes }
			exists = $GRANTEE$.house
		}
		custom_tooltip = {
			text = ceremonial_liege_cannot_be_governor_trigger
			$GRANTEE$ = { tgp_is_ceremonial_liege_trigger = no }
		}
	}
}

tgp_ceremonial_title_revoke_restriction_trigger = {
	trigger_if = {
		limit = {
			exists = scope:recipient.house
			scope:actor.top_liege.primary_title.var:administrative_ui_special_title.holder ?= scope:recipient
		}
		custom_tooltip = {
			text = tgp_blocked_ceremonial_title_revocation_trigger
			NOT = { scope:target ?= scope:actor.top_liege.primary_title.var:administrative_ui_special_title }
		}
	}
}

can_have_japanese_regency_succession_law_trigger = {
	has_tgp_dlc_trigger = yes
	is_independent_ruler = yes
	government_has_flag = government_is_japan_administrative
	OR = {
		has_title = title:k_chrysanthemum_throne
		any_vassal_or_below = { has_title = title:k_chrysanthemum_throne }
	}
	NOT = {
		has_global_variable = tenno_restored
	}
}

can_have_japanese_appointment_succession_law_trigger = {
	has_tgp_dlc_trigger = yes
	government_has_flag = government_is_japan_administrative
	is_independent_ruler = no
	NOT = { has_title = title:k_chrysanthemum_throne }
}

can_keep_japanese_appointment_succession_law_trigger = {
	has_tgp_dlc_trigger = yes
	trigger_if = {
		limit = { has_realm_law = japanese_appointment_succession_law }
		can_have_japanese_appointment_succession_law_trigger = yes
	}
}

can_change_japanese_appointment_succession_law_trigger = {
	has_tgp_dlc_trigger = yes
}

tgp_japan_single_heir_succession_override_trigger = {
	save_temporary_scope_as = holder_temp
	OR = {
		title:k_chrysanthemum_throne.holder ?= scope:holder_temp
		AND = {
			has_global_variable = tenno_restored
			title:e_japan.holder ?= scope:holder_temp
		}
	}
}

tgp_blocked_action_against_tenno_trigger = {
	custom_description = {
		text = tgp_blocked_ruler_against_ceremonial_action_trigger
		NAND = {
			$TARGET$ = { has_title = title:k_chrysanthemum_throne }
			$TARGET$.top_liege = $ACTOR$.top_liege
		}
	}
}

tgp_blocked_ruler_against_ceremonial_action_trigger = {
	custom_description = {
		text = tgp_blocked_ruler_against_ceremonial_action_trigger
		NAND = {
			$TENNO$ = { has_title = title:k_chrysanthemum_throne }
			$REGENT$ = $TENNO$.liege
		}
	}
}

#japan_house_name_county_trigger = {
#	exists = $TITLE$
#	save_temporary_scope_as = new_head_temp
#	trigger_if = {
#		limit = {
#			OR = {
#				$TITLE$.holder ?= scope:new_head_temp
#				scope:new_head_temp.domicile.domicile_location.county ?= $TITLE$
#			}
#		}
#		always = yes
#	}
#	trigger_else_if = {
#		limit = { exists = scope:old_head }
#		OR = {
#			$TITLE$.holder ?= scope:old_head
#			scope:old_head.domicile.domicile_location.county ?= $TITLE$
#		}
#	}
#	trigger_else_if = {
#		limit = { exists = scope:new_head_temp.house.house_head }
#		OR = {
#			$TITLE$.holder ?= scope:new_head_temp.house.house_head
#			scope:new_head_temp.house.house_head.domicile.domicile_location.county ?= $TITLE$
#		}
#	}
#	trigger_else_if = {
#		limit = { exists = scope:old_head.house.house_head }
#		OR = {
#			$TITLE$.holder ?= scope:old_head.house.house_head
#			scope:old_head.house.house_head.domicile.domicile_location.county ?= $TITLE$
#		}
#	}
#	trigger_else = { always = yes }
#	NOT = {
#		is_target_in_variable_list = { name = japanese_house_names target = $FLAG$ }
#	}
#}
#
japan_house_name_trigger = {
	NOT = {
		is_target_in_variable_list = { name = japanese_house_names target = $FLAG$ }
	}
}

tgp_japan_internal_soryo_war_is_crime_trigger = {
	scope:defender = {
		government_has_flag = government_is_japan_administrative
		top_liege = {
			government_has_flag = government_is_japan_administrative
			this = scope:attacker.top_liege
		}
	}
}

japan_imperial_expansion_cb_allowed_for_character_trigger = {
	# DLC check
	has_tgp_dlc_trigger = yes
	# Must have a Japanese government form with policies
	realm_law_use_imperial_policy_trigger = yes
	# Must be part of Japan with functional imperial family
	tgp_realm_has_ceremonial_liege_trigger = yes
}

government_is_japanese_trigger = {
	OR = {
		government_has_flag = government_is_japan_administrative
		government_has_flag = government_is_japan_feudal
	}
}

japan_imperial_expansion_cb_allowed_against_character_trigger = {
	# Cannot also be part of Japan
	NOT = { top_liege ?= scope:attacker.top_liege }
	# Must own Japanese non-de-jure land
	any_sub_realm_county = {
		NOT = { empire = title:e_japan }
		title_province = {
			OR = {
				geographical_region = world_asia_japan
				geographical_region = world_asia_sakhalin_hokkaido
			}
		}
	}
}

japan_imperial_reconquest_cb_allowed_for_character_trigger = {
	# DLC check
	has_tgp_dlc_trigger = yes
	# Must have a Japanese government form with policies
	realm_law_use_imperial_policy_trigger = yes
	# Must be part of Japan with functional imperial family
	tgp_realm_has_ceremonial_liege_trigger = yes
}

japan_imperial_reconquest_cb_allowed_against_character_trigger = {
	# Cannot also be part of Japan
	scope:defender.top_liege != scope:attacker.top_liege
	# Must own Japanese de-jure land
	any_sub_realm_county = { empire = title:e_japan }
}

realm_law_use_imperial_policy_trigger = {
	top_liege = { 
		highest_held_title_tier >= tier_empire
		government_is_japanese_trigger = yes 
	}
}

tgp_japan_defense_mobilization_valid_trigger = {
	custom_tooltip = {
		text = defense_mobilization_law_valid_trigger
		OR = {
			AND = {
				is_at_war = yes
				save_temporary_scope_as = liege_temp
				any_character_war = {
					is_civil_war = no
					save_temporary_scope_as = war_temp
					scope:liege_temp = {
						is_leader_in_war = scope:war_temp
						is_defender_in_war = scope:war_temp
					}
					OR = {
						# CURRENT ATTACKERS COMBINED ARE A THREAT
						war_attacker_total_strength_value > scope:liege_temp.max_strength_fifty_percent_value
						# PRIMARY ATTACKER WITH ALLIES IS A THREAT
						primary_attacker.max_strength_with_allies_value > scope:liege_temp.max_strength_fifty_percent_value
					}
					primary_attacker = {
						OR = {
							capital_county.empire != scope:liege_temp.capital_county.empire
							NOT = { government_is_japanese_trigger = yes }
						}
					}
				}
			}
			capital_county.empire = {
				any_de_jure_county = { holder.top_liege != root.top_liege }
			}
		}
	}
}

tgp_japan_valid_restore_monarchy_scion_trigger = {
	is_landed = no
	can_be_combatant_based_on_gender_trigger = { ARMY_OWNER = scope:kampaku }
	is_healthy = yes
}

tgp_japan_imperial_expansion_internal_peace_trigger = {
	custom_tooltip = {
		text = imperial_expansion_internal_peace_trigger
		top_liege = {
			NOT = {
				any_vassal_or_below = {
					any_primary_war_enemy = {
						top_liege = prev.top_liege
					}
				}
			}
		}
	}
}

tgp_install_regent_faction_target_valid_trigger = {
	is_independent_ruler = yes
	tgp_realm_has_ceremonial_liege_trigger = yes
}

tgp_install_regent_faction_can_create_trigger = {
	has_tgp_dlc_trigger = yes
	scope:target ?= { tgp_install_regent_faction_target_valid_trigger = yes }
	custom_tooltip = {
		text = replace_ceremonial_regent_faction_kampaku_trigger
		NOR = {
			has_global_variable = shogunate_established
			has_global_variable = tenno_restored
		}
	}
	is_adult = yes
	custom_tooltip = {
		text = is_faith_dominant_gender_tt
		is_faith_dominant_gender = yes
	}
	japan_faction_cohesion_hard_trigger = yes
	trigger_if = {
		limit = {
			scope:target ?= {
				government_is_japanese_trigger = yes # am I a compatible government type?
			}
		}
		government_is_japanese_trigger = yes
	}
	trigger_if = {
		limit = {
			scope:target ?= {
				has_government = meritocratic_government # am I a compatible government type?
			}
		}
		has_government = meritocratic_government
	}
}

japan_faction_cohesion_trigger = {
	trigger_if = {
		limit = { 
			government_has_flag = government_has_house_blocs 
			is_confederation_member = yes
		}
		custom_tooltip = {
			text = house_head_create_faction_cohesion_tt
			OR = {
				house.house_confederation ?= { cohesion >= 50 }
				joined_faction ?= {
					OR = {
						faction_is_at_war = yes
						any_faction_member = {
							OR = {
								this = root.house.house_confederation.leading_house.house_head
								is_ai = no
							}
						}
					}
				}
			}
		}
	}
}

japan_faction_cohesion_hard_trigger = {
	trigger_if = {
		limit = { 
			government_has_flag = government_has_house_blocs 
			is_confederation_member = yes
		}
		custom_tooltip = {
			text = house_head_create_faction_cohesion_hard_tt
			OR = {
				house.house_confederation ?= { cohesion >= 75 }
				joined_faction ?= {
					OR = {
						faction_is_at_war = yes
						any_faction_member = {
							OR = {
								this = root.house.house_confederation.leading_house.house_head
								is_ai = no
							}
						}
					}
				}
			}
		}
	}
}

restore_ceremonial_liege_faction_can_create_trigger = {
	tgp_realm_has_ceremonial_liege_trigger = yes
	trigger_if = {
		limit = { government_is_japanese_trigger = yes }
		custom_tooltip = {
			text = house_head_create_faction_cohesion_hard_tt
			OR = {
				house.house_confederation ?= { cohesion >= 75 }
				joined_faction ?= {
					faction_is_at_war = yes
				}
			}
		}
		custom_tooltip = {
			text = bloc_leader_unlocks_restore_emperor_faction_tt
			OR = {
				house.house_confederation ?= { has_cohesion_level_parameter = bloc_leader_unlocks_restore_emperor_faction }
				joined_faction ?= {
					faction_is_at_war = yes
				}
			}
		}
	}
	OR = {
		custom_tooltip = {
			text = is_house_head_of_noble_family_tt
			is_house_head = yes
			any_held_title = { is_noble_family_title = yes }
		}
		is_landed = yes
	}
	scope:target ?= {
		is_independent_ruler = yes
		tgp_realm_has_ceremonial_liege_trigger = yes
		tgp_has_ceremonial_liege_title_trigger = no
	}
}

ceremonial_claimant_faction_can_create_trigger = {
	scope:target ?= {
		is_independent_ruler = yes
		tgp_realm_has_ceremonial_liege_trigger = yes
		tgp_has_ceremonial_liege_title_trigger = no
	}
	japan_faction_cohesion_hard_trigger = yes
}

imperial_policy_faction_can_create_trigger = {
	realm_law_use_imperial_policy_trigger = yes
	scope:target = {
		is_independent_ruler = yes
		this = root.liege
	}
	japan_faction_cohesion_trigger = yes
}

tgp_ritsuryo_bloc_house_head_trigger = {
	top_liege = root.top_liege
	government_has_flag = government_is_japan_administrative
}

tgp_house_bloc_interaction_valid_trigger = {
	is_ruler = yes
	tgp_uses_house_blocs_trigger = yes
	highest_held_title_tier >= tier_county
	top_liege = { tgp_uses_house_blocs_trigger = yes }
}

tgp_house_bloc_interaction_valid_showing_failures_trigger = {
	custom_tooltip = {
		text = tgp_house_bloc_house_head_tt
		is_house_head = yes
		any_held_title = { is_noble_family_title = yes }
	}
}

tgp_has_house_relation_level_trigger = {
	exists = $HOUSE_1_MEMBER$.house
	exists = $HOUSE_2_MEMBER$.house
	$HOUSE_1_MEMBER$.house != $HOUSE_2_MEMBER$.house
	$HOUSE_1_MEMBER$.house = {
		any_house_relation = {
			has_house_relation_level = $LEVEL$
			any_relation_house = { this = $HOUSE_2_MEMBER$.house }
		}
	}
}

## Check who can join a House Bloc
#
# TODO_TGP_CD: Remove this? Functionality (if any) would do better on house confederation type).
#
# root: possibly viable character
#
tgp_should_join_house_bloc_trigger = {
	# Not in a Bloc
	NOT = { exists = confederation }
	# Not vassal - TODO_CD_TGP
	#NOT = { liege ?= house.house_head }
	# Same liege
	$BLOC$.leading_house.house_head.liege ?= liege
	# General checks
	tgp_house_bloc_interaction_valid_trigger = yes
	is_landed = yes # TODO_CD_TGP UNLANDED CANNOT JOIN YET
	# Government TODO_CD_TGP disabled for now
	#trigger_if = {
	#	limit = {
	#		$BLOC$.leading_house.house_head ?= { government_has_flag = government_is_japan_administrative }
	#	}
	#	government_has_flag = government_is_japan_administrative
	#}
	#trigger_else = { government_has_flag = government_is_japan_feudal }
}

tgp_house_bloc_inviter_or_leader_trigger = {
	$JOINER$ = { save_temporary_scope_as = joiner_temp }
	trigger_if = {
		limit = { exists = $INVITER$.confederation.leading_house.house_head }
		$INVITER$.confederation.leading_house.house_head = { save_temporary_scope_as = inviter_temp }
	}
	trigger_else = { $INVITER$ = { save_temporary_scope_as = inviter_temp } }
}

tgp_japan_cadet_creates_dynasty_trigger = {
	NOR = {
		this = dynasty.dynast
		this = house.house_head
	}
	dynasty.dynast ?= { government_allows = administrative }
}

tgp_soryo_or_bushido_trigger = {
	custom_tooltip = {
		text = soryo_or_bushido_trigger
		OR = {
			government_has_flag = government_is_japan_feudal
			culture = { has_cultural_tradition = tradition_tgp_bushido }
		}
	}
}

has_house_aspiration_trigger = {
	OR = {
		has_house_aspiration_parameter = aspiration_level_1
		has_house_aspiration_parameter = aspiration_level_2
		has_house_aspiration_parameter = aspiration_level_3
	}
}

should_learn_chinese_trigger = {
	save_temporary_scope_as = child_learned_chinese
	#Make sure they don't already know Chinese
	NOT = {
		knows_language = language_chinese
	}
	num_of_known_languages < language_soft_cap
	exists = house
	age >= 10 
	is_faith_dominant_gender = yes
	probably_unintelligent_trigger = no
	house.house_head ?= {
		OR = {
			government_is_japanese_trigger = yes
			#This will account for korea and friends
			government_allows = merit 
		}
		OR = {
			#Cultural acceptance will be our guiding light for teaching young, noblemen (dominate gender) adults Chinese. These values will be set in game_start.txt 
			culture = {
				OR = {
					cultural_acceptance = { 
						target = culture:han
						value >= 65
					}
					any_parent_culture_or_above = { this = culture:han }
				}	
			}
			top_liege = {
				highest_held_title_tier >= tier_kingdom 
				has_dlc_feature = royal_court
				has_court_language_of_culture = culture:han	
			}
		}
	}
}

japan_government_japan_kingdom_restriction_trigger = {
	trigger_if = {
		limit = {
			government_is_japanese_trigger = yes
			OR = {
				is_independent_ruler = no
				AND = {
					NOT = { highest_held_title_tier = tier_kingdom }
					has_title = title:e_japan
				}
			}
		}
		custom_tooltip = {
			text = japan_government_japan_kingdom_restriction_tt
			has_global_variable = tenno_restored
		}
	}
}

japan_government_japan_duchy_restriction_trigger = {
	trigger_if = {
		limit = {
			OR = {
				government_has_flag = government_is_japan_administrative
				AND = { # Soryo can create duchies if they're already a duke
					government_has_flag = government_is_japan_feudal
					primary_title.tier = tier_county
				}
			}
			NAND = {
				is_independent_ruler = yes
				government_has_flag = government_is_japan_feudal
			}
		}
		custom_tooltip = {
			text = japan_government_japan_duchy_restriction_tt
			has_global_variable = tenno_restored
		}
	}
}

can_upgrade_house_aspiration_trigger = {
	is_house_head = yes
	trigger_if = {
		limit = {
			exists = house.house_confederation.leading_house
			house.house_confederation.leading_house != house
		}
		custom_tooltip = {
			text = cannot_upgrade_mismatched_bloc_aspiration_tt
			house.house_confederation.leading_house = { has_same_house_aspiration_as = root.house }
		}
	}
}

japan_castle_construction_trigger = {
	trigger_if = {
		limit = {
			county.holder ?= {
				government_has_flag = government_is_japan_administrative
				realm_law_use_imperial_policy_trigger = yes
			}
		}
		custom_tooltip = {
			text = japan_castle_construction_trigger_tt
			county.holder.top_liege ?= { has_realm_law_flag = disarmament_law }
		}
	}
}

tgp_bloc_war_transfer_trigger = {
	$BLOC_LEADER$ = { save_temporary_scope_as = leader_temp }
	save_temporary_scope_as = member_head_temp
	NOR = {
		# Not leading house
		scope:leader_temp.house.house_confederation.leading_house ?= house
		# Not sub vassal
		trigger_if = {
			limit = {
				scope:leader_temp = { is_independent_ruler = no }
			}
			is_vassal_or_below_of = scope:leader_temp
		}
		# Not friend
		has_relation_friend = scope:leader_temp
		# Not ally
		is_allied_to = scope:leader_temp
		# Not hooked
		scope:leader_temp = { has_hook = scope:member_head_temp }
		# Not terrified
		has_dread_level_towards = {
			target = scope:leader_temp
			level >= 2
		}
		# Not loyal
		has_trait = loyal
	}
}

has_japanese_house_aspiration_trigger = {
	house ?= {
		OR = {
			has_house_aspiration_parameter = aspiration_ceremony
			has_house_aspiration_parameter = aspiration_determination
			has_house_aspiration_parameter = aspiration_humility
			has_house_aspiration_parameter = aspiration_prosperity
			has_house_aspiration_parameter = aspiration_service
			has_house_aspiration_parameter = aspiration_strength
		}
	}
}
