﻿action_no_heir = {
	type = alert

	check_create_action = {
		if = {
			limit = {
				NOT = {
					any_held_title = {
						#tier > tier_barony
						any_title_heir = {
							dynasty = root.dynasty
						}
					}
				}
				is_ruler = yes
			}
			if = {
				limit = { exists = primary_heir }
				try_create_important_action = {
					important_action_type = action_no_heir
					actor = root
					recipient = primary_heir
				}
			}
			else = {
				try_create_important_action = {
					important_action_type = action_no_heir
					actor = root
				}
			}
		}
	}

	effect = {
		scope:actor = {
			open_view_data = {
				view = character
			}
		}
		start_tutorial_lesson = reactive_advice_looming_game_over
	}
}

action_no_player_heir_but_replacement_exists_gameover_risk = {
	type = alert
	icon = action_no_heir

	check_create_action = {
		if = {
			limit = {
				NOT = { exists = player_heir }
				any_held_title = {
					#tier > tier_barony
					any_title_heir = {
						dynasty = root.dynasty
					}
				}
			}
			try_create_important_action = {
				important_action_type = action_no_player_heir_but_replacement_exists_gameover_risk
				landed_title = root.primary_title
				actor = root
			}
		}
	}

	effect = {
		scope:landed_title = {
			open_view_data = {
				view = title_view_window
			}
		}
	}
}

action_primary_heir_wrong_dynasty_no_replacement_exists = {
	type = alert
	icon = action_primary_heir_wrong_dynasty_but_replacement_exists

	check_create_action = {
		if = {
			limit = {
				exists = primary_heir
				exists = player_heir
				primary_heir = {
					NOT = { dynasty = root.dynasty }
				}

				NOT = {
					primary_title = {
						any_title_heir = {
							dynasty = root.dynasty 
						}
					}
				}
				NOT = { government_allows = administrative } # Admin doesn't care if primary heir is of another dynasty, as long as they have a player heir.
			}
			try_create_important_action = {
				important_action_type = action_primary_heir_wrong_dynasty_no_replacement_exists
				landed_title = root.primary_title
				actor = root
			}
		}
	}

	effect = {
		scope:landed_title = {
			open_view_data = {
				view = title_view_window
			}
		}
	}
}

action_primary_heir_wrong_dynasty_but_replacement_exists = {
	type = alert

	check_create_action = {
		if = {
			limit = {
				exists = primary_heir
				exists = player_heir
				primary_heir = {
					NOT = { dynasty = root.dynasty }
				}
				primary_title = {
					any_title_heir = {
						dynasty = root.dynasty
					}
				}
				NOT = { government_allows = administrative } # Admin doesn't care if primary heir is of another dynasty, as long as they have a player heir.
			}
			try_create_important_action = {
				important_action_type = action_primary_heir_wrong_dynasty_but_replacement_exists
				actor = root
				recipient = primary_heir
			}
		}
	}

	effect = {
		scope:recipient = {
			open_view_data = {
				view = character
			}
		}
	}
}


action_title_lost_from_realm_on_succession = {
	is_dangerous = yes
	combine_into_one = yes
	
	check_create_action = {
		every_vassal_or_below = {
			limit = {
				NOT = { government_allows = administrative }
				any_held_title =	{
					is_capital_barony = no
					title_will_leave_sub_realm_on_succession = root
				}
			}
			try_create_important_action = {
				important_action_type = action_title_lost_from_realm_on_succession
				actor = root
				recipient = this
			}
		}
	}

	effect = {
		scope:recipient = {
			open_view_data = {
				view = character
			}
		}
	}
}

action_title_lost_from_realm_on_foreign_ruler_succession = {
	is_dangerous = yes
	combine_into_one = yes
	
	check_create_action = {
		every_vassal_or_below = {
			limit = {
				highest_held_title_tier >= tier_county  # Barons will abdicate if they inherit most titles
				any_heir_title = {
					title_lost_from_realm_on_foreign_ruler_succession_trigger = {
						VASSAL = prev
						LIEGE = root
					}
				}
			}
			ordered_heir_title = { # Pick the most "important" one to show in the warning
				order_by = tier
				limit = {
					title_lost_from_realm_on_foreign_ruler_succession_trigger = {
						VASSAL = prev
						LIEGE = root
					}
				}
				save_scope_as = heir_title
			}
			try_create_important_action = {
				important_action_type = action_title_lost_from_realm_on_foreign_ruler_succession
				recipient = this
				landed_title = scope:heir_title
			}
		}
	}

	effect = {
		scope:recipient = {
			open_view_data = {
				view = character
			}
		}
	}
}

action_nominate_successor = {
	type = alert

	check_create_action = {
		ordered_election_title = {
			order_by = tier
			limit = {
				has_order_of_succession = election
				NOT = { has_character_nominiated = root }
			}
			try_create_important_action = {
				important_action_type = action_nominate_successor
				landed_title = this
			}
		}
	}

	effect = {
		scope:landed_title = {
			if = {
				limit = {
					has_order_of_succession = election # repeated condition so the view cannot be opened before the invalid action disappears from the screen
				}
				open_view_data = {
					view = title_election
				}
			}
		}
	}
}

action_title_election = {
	
	combine_into_one = yes
	check_create_action = {
		ordered_election_title = {
			order_by = tier

			try_create_important_action = {
				important_action_type = action_title_election
				landed_title = this
			}
		}
		every_liege_or_above = {
			every_held_title = {
				limit = {
					has_order_of_succession = election
				}
				try_create_important_action = {
					important_action_type = action_title_election
					landed_title = this
				}
			}
		}
		every_held_title = {
			limit = {
				has_order_of_succession = election
			}
			try_create_important_action = {
				important_action_type = action_title_election
				landed_title = this
			}
		}
	}

	effect = {
		scope:landed_title = {
			open_view_data = {
				view = title_election
			}
		}
	}
}

action_in_line_of_succession_for_title = {
	combine_into_one = yes
	priority = 200
	check_create_action = {
		every_pretender_title = {
			if = {
				limit = {
					is_nomad_title = no
					place_in_line_of_succession = {
						target = root
						value <= 4
					}
					tier >= tier_county
					exists = holder
					holder = {
						NOR = {
							this = root
							any_liege_or_above = { this = root }
							this = {
								is_primary_heir_of = root
								is_player_heir_of = root
								is_child_of = root
							}
						}
					}
				}
				try_create_important_action = {
					important_action_type = action_in_line_of_succession_for_title
					landed_title = this
					actor = root
				}
			}
		}
		every_heir_title = {
			if = {
				limit = {
					is_nomad_title = no
					NOT = { tier = tier_barony } # Baronies are never important
					tier >= tier_county
					exists = holder
					holder = {
						NOR = {
							this = root
							any_liege_or_above = { this = root }
							this = {
								is_primary_heir_of = root
								is_player_heir_of = root
								is_child_of = root
							}
						}
					}
					trigger_if = {
						limit = {
							tier = tier_county
							save_temporary_scope_as = county_temp
						}
						NOR = {
							# We don't generally care about inheriting counties that are already in our subrealm.
							holder = { is_vassal_of = root }
							# We don't care about sub-titles of governorships we stand to inherit
							AND = {
								OR = {
									has_title_law = acclamation_succession_law
									holder = { has_realm_law = acclamation_succession_law }
									has_title_law = appointment_succession_law
									holder = { has_realm_law = appointment_succession_law }
								}
								root = {
									any_heir_title = {
										OR = {
											this = scope:county_temp.de_jure_liege
											holder = scope:county_temp.holder
										}
									}
								}
							}
						}

					}
				}
				try_create_important_action = {
					important_action_type = action_in_line_of_succession_for_title
					landed_title = this
					actor = root
				}
			}
		}
	}

	unimportant = {
		scope:landed_title.holder = { target_is_same_character_or_above = root }
	}

	effect = {
		scope:landed_title = {
			open_view_data = {
				view = title_view_window
			}
		}
	}
}

action_heir_in_line_of_succession_for_title = {
	priority = 205
	combine_into_one = yes
	check_create_action = {
		if = {
			limit = {
				exists = player_heir
			}
			player_heir = {
				every_pretender_title = {
					if = {
						limit = {
							is_nomad_title = no
							place_in_line_of_succession = {
								target = root
								value <= 5
							}
							tier >= tier_county
							exists = holder
							holder = {
								NOR = {
									this = root
									any_liege_or_above = { this = root }
									this = {
										is_primary_heir_of = root
										is_child_of = root
									}
								}
							}
							OR = {
								tier = tier_empire
								tier = tier_kingdom # Kingdoms and empires are generally of high value, so we should inform the player even if they are several places removed.
								AND = {
									tier = tier_duchy
									place_in_line_of_succession = {
										target = root
										value <= 3
									}
									
								}
								AND = {
									tier = tier_county
									place_in_line_of_succession = { # Counties are generally of low value, so we don't need to inform the player unless they are very high in the line.
										target = root
										value <= 2
									}
									NOT = {	holder = { is_vassal_of = root } } # We don't generally care about inheriting counties that are already in our subrealm.
								}
							}
						}
						root = {
							try_create_important_action = {
								important_action_type = action_heir_in_line_of_succession_for_title
								landed_title = prev
								actor = root.player_heir
							}
						}
					}
				}
				every_heir_title = {
					if = {
						limit = {
							is_nomad_title = no
							NOT = { tier = tier_barony } # Baronies are never important
							tier >= tier_county
							exists = holder
							holder = {
								NOR = {
									this = root
									any_liege_or_above = { this = root }
									this = {
										is_primary_heir_of = root
										is_child_of = root
									}
								}
							}
							trigger_if = {
								limit = {
									tier = tier_county
								}
								NOT = {	holder = { is_vassal_of = root } } # We don't generally care about inheriting counties that are already in our subrealm.
							}
						}
						root = {
							try_create_important_action = {
								important_action_type = action_heir_in_line_of_succession_for_title
								landed_title = prev
								actor = root.player_heir
							}
						}
					}
				}
			}
		}	
	}

	unimportant = {
		scope:landed_title.holder = { target_is_same_character_or_above = root }
	}

	effect = {
		scope:landed_title = {
			open_view_data = {
				view = title_view_window
			}
		}
	}
}

