﻿namespace = accolade

#Accolade rank gain notification
# by Jason Cantalini
accolade.0001 = {
	hidden = yes

	trigger = {
		exists = acclaimed_knight
		has_dlc_feature = accolades
	}

	immediate = {
		scope:changing_accolade = {
			acclaimed_knight = {
				save_scope_as = acclaimed_knight
			}
		}
		if = {
			limit = {
				scope:changing_accolade = {
					accolade_rank >= 5
				}
			}
			send_interface_message = {
				type = msg_accolade_gained_rank
				title = accolade.0001.notification_high
				desc = accolade_rank_unlock_notification_tooltip_high
				left_icon = scope:acclaimed_knight
			}
		}
		else = {
			send_interface_message = {
				type = msg_accolade_gained_rank
				title = accolade.0001.notification_low
				desc = accolade_rank_unlock_notification_tooltip_low
				left_icon = scope:acclaimed_knight
			}
		}
	}
}

#Accolade rank loss notification
# by Jason Cantalini
accolade.0002 = {
	hidden = yes

	trigger = {
		has_dlc_feature = accolades
	}

	immediate = {
		if = {
			limit = {
				exists = scope:changing_accolade.acclaimed_knight
			}
			scope:changing_accolade = {
				acclaimed_knight = {
					save_scope_as = acclaimed_knight
				}
			}
			if = {
				limit = {
					scope:changing_accolade = {
						accolade_rank >= 5
					}
				}
				send_interface_message = {
					type = msg_accolade_lost_rank
					title = accolade.0002.notification_high
					left_icon = scope:acclaimed_knight
					desc = {
						first_valid = {
							triggered_desc = {
								trigger = {
									scope:acclaimed_knight = {
										is_alive = yes
									}
								}
								desc = accolade_rank_loss_notification_tooltip_high
							}
							desc = accolade_rank_loss_notification_tooltip_high_dead
						}
					}
				}
			}
			else_if = {
				limit = {
					scope:changing_accolade = {
						accolade_rank >= 1
					}
				}
				send_interface_message = {
					type = msg_accolade_lost_rank
					title = accolade.0002.notification_low
					left_icon = scope:acclaimed_knight
					desc = {
						first_valid = {
							triggered_desc = {
								trigger = {
									scope:acclaimed_knight = {
										is_alive = yes
									}
								}
								desc = accolade_rank_loss_notification_tooltip_low
							}
							desc = accolade_rank_loss_notification_tooltip_low_dead
						}
					}
				}
			}
			else = {
				send_interface_message = {
					type = msg_accolade_lost_rank
					title = accolade.0002.notification_low
					left_icon = scope:acclaimed_knight
					desc = {
						first_valid = {
							triggered_desc = {
								trigger = {
									scope:acclaimed_knight = {
										is_alive = yes
									}
								}
								desc = accolade_rank_loss_notification_tooltip_no_rank
							}
							desc = accolade_rank_loss_notification_tooltip_no_rank_dead
						}
					}
				}
			}
		}
		else_if = {
			limit = {
				NOT = { exists = scope:changing_accolade.acclaimed_knight }
				scope:changing_accolade = { accolade_rank >= 1 }
			}
			send_interface_message = {
				type = msg_accolade_lost_rank
				title = accolade.0002.notification_empty
				desc = accolade_rank_loss_notification_tooltip_empty
			}
		}
		else = {
			send_interface_message = {
				type = msg_accolade_lost_rank
				title = accolade.0002.notification_low
				desc = accolade_rank_loss_notification_tooltip_empty_no_rank
			}
		}
	}
}

#Accolade becomes idle
# by Jason Cantalini
accolade.0003 = {
	hidden = yes

	trigger = {
		has_dlc_feature = accolades
	}

	immediate = {
		send_interface_toast = {
			type = msg_accolade_active
			title = accolade.0003.accolade_becomes_idle.tt
			custom_tooltip = accolade.0003.accolade_becomes_idle_effects.tt
		}
	}
}

#Accolade succession with unchanged types
# by Jason Cantalini
accolade.0004 = {
	hidden = yes

	trigger = {
		has_dlc_feature = accolades
	}

	immediate = {
		send_interface_toast = {
			type = msg_accolade_succession
			title = accolade.0004.new_acclaimed_knight.tt
			left_icon = scope:new_acclaimed_knight
			custom_tooltip = accolade.0004.new_acclaimed_knight_name.tt
			custom_tooltip = accolade.glory_loss.tt
		}
	}
}

#Accolade succession with new secondary type
# by Jason Cantalini
accolade.0005 = {
	hidden = yes

	trigger = {
		has_dlc_feature = accolades
	}

	immediate = {
		send_interface_toast = {
			type = msg_accolade_succession_toast
			title = accolade.0005.new_acclaimed_knight_new_type.tt
			left_icon = scope:new_acclaimed_knight
			custom_tooltip = accolade.0005.new_accolade_type.tt
			custom_tooltip = accolade.0005.new_acclaimed_knight_succeeds.tt
			custom_tooltip = accolade.glory_loss.tt
		}
	}
}

#Find Accolade Successor knight creation event
accolade.0006 = {
	hidden = yes

	trigger = {
		exists = scope:accolade_in_need
		exists = root.capital_province
		highest_held_title_tier >= tier_county
		NOT = { government_has_flag = government_is_theocracy }
	}

	immediate = {
		# try to find a pool character first
		every_courtier_or_guest = {
			limit = { 
				ep2_can_be_accolade_successor_base_trigger = yes
				can_be_knight_trigger = { ARMY_OWNER = root }
				is_valid_successor_for_accolade = scope:accolade_in_need
				faith = {
					faith_hostility_level = {
						target = root.faith
						value <= faith_astray_level
					}
				}
				is_clergy = no
				is_healthy = yes
				save_temporary_scope_as = temp_knight
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = scope:temp_knight
				}
			}
			add_to_list = potential_knights
		}
		every_pool_character = {
			province = root.capital_province
			limit = {
				ep2_can_be_accolade_successor_base_trigger = yes
				can_be_knight_trigger = { ARMY_OWNER = root }
				is_valid_successor_for_accolade = scope:accolade_in_need
				faith = {
					faith_hostility_level = {
						target = root.faith
						value <= faith_astray_level
					}
				}
				is_clergy = no
				is_healthy = yes
				save_temporary_scope_as = temp_knight
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = scope:temp_knight
				}
			}
			add_to_list = potential_knights
		}
		random_in_list = {
			list = potential_knights
			save_scope_as = chosen_knight
		}

		# create a character if needed
		if = {
			limit = {
				NOT = { exists = scope:chosen_knight }
			}
			#if = { # Balance testing variables
			#	limit = {
			#		NOT = {
			#			exists = global_var:accolade_fail
			#		}
			#	}
			#	set_global_variable = {
			#		name = accolade_fail
			#		value = 1
			#	}
			#}
			#else = {
			#	change_global_variable = {
			#		name = accolade_fail
			#		add = 1
			#	}
			#}
			accolade_character_creation_effect = yes
		}
		#else = { # Balance testing variables
		#	if = {
		#		limit = {
		#			NOT = {
		#				exists = global_var:accolade_success
		#			}
		#		}
		#		set_global_variable = {
		#			name = accolade_success
		#			value = 1
		#		}
		#	}
		#	else = {
		#		change_global_variable = {
		#			name = accolade_success
		#			add = 1
		#		}
		#	}
		#}
		
		if = {
			limit = {
				exists = scope:chosen_knight
			}
			if = {
				limit = {
					can_recruit_character_to_court_trigger = {
						RECRUITER = root
						RECRUITEE = scope:chosen_knight
					}
					# make sure the chosen knight is not already a courtier
					scope:chosen_knight ?= {
						NOT = { is_courtier_of = root }
					}
				}
				add_courtier = scope:chosen_knight
			}

			trigger_event = {
				id = accolade.0007
				days = 1
			}
		}

	}
}

#Find Accolade Successor join event
accolade.0007 = {
	hidden = yes

	immediate = {
		if = {
			limit = {
				exists = scope:accolade_in_need
				exists = scope:chosen_knight
				exists = root.capital_province
				highest_held_title_tier >= tier_county
				NOT = { government_has_flag = government_is_theocracy }
			}		
			
			#SEND A TOAST NOTIFICATION
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = accolade.0007.notification
				left_icon = scope:chosen_knight
				custom_tooltip = accolade_call_acclaimed_knight_tooltip
				hidden_effect = {
					scope:chosen_knight = {
						if = {
							limit = {
								NOT = { is_knight_of = root }
							}
						}
					}
				}
			}
		}
		else = {
			send_interface_message = {
				type = event_accolade_bad_text
				title = accolade.0007.notification_fail
				desc = accolade_find_successor_fail_notification_tooltip
			}
		}
	}
}

#Call Acclaimed Knight first knight
accolade.0008 = {
	hidden = yes

	trigger = {
		exists = scope:empty_accolade_1
		exists = root.capital_province
		highest_held_title_tier >= tier_county
		NOT = { government_has_flag = government_is_theocracy }
	}

	immediate = {
		# try to find a pool character first
		scope:empty_accolade_1 = {
			save_scope_as = accolade_in_need
		}
		every_courtier_or_guest = {
			limit = { 
				ep2_can_be_accolade_successor_base_trigger = yes
				can_be_knight_trigger = { ARMY_OWNER = root }
				is_valid_successor_for_accolade = scope:accolade_in_need
				faith = {
					faith_hostility_level = {
						target = root.faith
						value <= faith_astray_level
					}
				}
				is_clergy = no
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = this
				}
				is_healthy = yes
			}
			add_to_list = potential_knights
		}
		every_pool_character = {
			province = root.capital_province
			limit = {
				ep2_can_be_accolade_successor_base_trigger = yes
				can_be_knight_trigger = { ARMY_OWNER = root }
				is_valid_successor_for_accolade = scope:accolade_in_need
				faith = {
					faith_hostility_level = {
						target = root.faith
						value <= faith_astray_level
					}
				}
				is_clergy = no
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = this
				}
				is_healthy = yes
			}
			add_to_list = potential_knights
		}
		random_in_list = {
			list = potential_knights
			save_scope_as = chosen_knight
		}

		# create a character if needed
		if = {
			limit = {
				NOT = { exists = scope:chosen_knight }
			}
			accolade_character_creation_effect = yes
		}

		if = {
			limit = {
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = scope:chosen_knight
				}
			}
			add_courtier = scope:chosen_knight
		}

		trigger_event = {
			id = accolade.0013
			days = 1
		}
	}
}

#Call Acclaimed Knight second knight
accolade.0009 = {
	hidden = yes

	trigger = {
		exists = scope:empty_accolade_2
		exists = root.capital_province
		highest_held_title_tier >= tier_county
		NOT = { government_has_flag = government_is_theocracy }
	}

	immediate = {
		# try to find a pool character first
		scope:empty_accolade_2 = {
			save_scope_as = accolade_in_need
		}
		every_courtier_or_guest = {
			limit = { 
				ep2_can_be_accolade_successor_base_trigger = yes
				can_be_knight_trigger = { ARMY_OWNER = root }
				is_valid_successor_for_accolade = scope:accolade_in_need
				faith = {
					faith_hostility_level = {
						target = root.faith
						value <= faith_astray_level
					}
				}
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = this
				}
				is_clergy = no
				is_healthy = yes
			}
			add_to_list = potential_knights
		}
		every_pool_character = {
			province = root.capital_province
			limit = {
				ep2_can_be_accolade_successor_base_trigger = yes
				can_be_knight_trigger = { ARMY_OWNER = root }
				is_valid_successor_for_accolade = scope:accolade_in_need
				faith = {
					faith_hostility_level = {
						target = root.faith
						value <= faith_astray_level
					}
				}
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = this
				}
				is_clergy = no
				is_healthy = yes
			}
			add_to_list = potential_knights
		}
		random_in_list = {
			list = potential_knights
			save_scope_as = chosen_knight
		}

		# create a character if needed
		if = {
			limit = {
				NOT = { exists = scope:chosen_knight }
			}
			accolade_character_creation_effect = yes
		}

		if = {
			limit = {
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = scope:chosen_knight
				}
			}
			add_courtier = scope:chosen_knight
		}
		trigger_event = {
			id = accolade.0013
			days = 1
		}
	}
}

#Call Acclaimed Knight third knight
accolade.0010 = {
	hidden = yes

	trigger = {
		exists = scope:empty_accolade_3
		exists = root.capital_province
		highest_held_title_tier >= tier_county
		NOT = { government_has_flag = government_is_theocracy }
	}

	immediate = {
		# try to find a pool character first
		scope:empty_accolade_3 = {
			save_scope_as = accolade_in_need
		}
		every_courtier_or_guest = {
			limit = { 
				ep2_can_be_accolade_successor_base_trigger = yes
				can_be_knight_trigger = { ARMY_OWNER = root }
				is_valid_successor_for_accolade = scope:accolade_in_need
				faith = {
					faith_hostility_level = {
						target = root.faith
						value <= faith_astray_level
					}
				}
				is_clergy = no
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = this
				}
				is_healthy = yes
			}
			add_to_list = potential_knights
		}
		every_pool_character = {
			province = root.capital_province
			limit = {
				ep2_can_be_accolade_successor_base_trigger = yes
				can_be_knight_trigger = { ARMY_OWNER = root }
				is_valid_successor_for_accolade = scope:accolade_in_need
				faith = {
					faith_hostility_level = {
						target = root.faith
						value <= faith_astray_level
					}
				}
				is_clergy = no
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = this
				}
				is_healthy = yes
			}
			add_to_list = potential_knights
		}
		random_in_list = {
			list = potential_knights
			save_scope_as = chosen_knight
		}

		# create a character if needed
		if = {
			limit = {
				NOT = { exists = scope:chosen_knight }
			}
			accolade_character_creation_effect = yes
		}

		if = {
			limit = {
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = scope:chosen_knight
				}
			}
			add_courtier = scope:chosen_knight
		}
		trigger_event = {
			id = accolade.0013
			days = 1
		}
	}
}

#Call Acclaimed Knight fourth knight
accolade.0011 = {
	hidden = yes

	trigger = {
		exists = scope:empty_accolade_4
		exists = root.capital_province
		highest_held_title_tier >= tier_county
		NOT = { government_has_flag = government_is_theocracy }
	}

	immediate = {
		# try to find a pool character first
		scope:empty_accolade_4 = {
			save_scope_as = accolade_in_need
		}
		every_courtier_or_guest = {
			limit = { 
				ep2_can_be_accolade_successor_base_trigger = yes
				can_be_knight_trigger = { ARMY_OWNER = root }
				is_valid_successor_for_accolade = scope:accolade_in_need
				faith = {
					faith_hostility_level = {
						target = root.faith
						value <= faith_astray_level
					}
				}
				is_clergy = no
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = this
				}
				is_healthy = yes
			}
			add_to_list = potential_knights
		}
		every_pool_character = {
			province = root.capital_province
			limit = {
				ep2_can_be_accolade_successor_base_trigger = yes
				can_be_knight_trigger = { ARMY_OWNER = root }
				is_valid_successor_for_accolade = scope:accolade_in_need
				faith = {
					faith_hostility_level = {
						target = root.faith
						value <= faith_astray_level
					}
				}
				is_clergy = no
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = this
				}
				is_healthy = yes
			}
			add_to_list = potential_knights
		}
		random_in_list = {
			list = potential_knights
			save_scope_as = chosen_knight
		}

		# create a character if needed
		if = {
			limit = {
				NOT = { exists = scope:chosen_knight }
			}
			accolade_character_creation_effect = yes
		}

		
		if = {
			limit = {
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = scope:chosen_knight
				}
			}
			add_courtier = scope:chosen_knight
		}
		trigger_event = {
			id = accolade.0013
			days = 1
		}
	}
}

#Call Acclaimed Knight fifth knight
accolade.0012 = {
	hidden = yes

	trigger = {
		exists = scope:empty_accolade_5
		exists = root.capital_province
		highest_held_title_tier >= tier_county
		NOT = { government_has_flag = government_is_theocracy }
	}

	immediate = {
		# try to find a pool character first
		scope:empty_accolade_5 = {
			save_scope_as = accolade_in_need
		}
		every_courtier_or_guest = {
			limit = { 
				ep2_can_be_accolade_successor_base_trigger = yes
				can_be_knight_trigger = { ARMY_OWNER = root }
				is_valid_successor_for_accolade = scope:accolade_in_need
				faith = {
					faith_hostility_level = {
						target = root.faith
						value <= faith_astray_level
					}
				}
				is_clergy = no
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = this
				}
				is_healthy = yes
			}
			add_to_list = potential_knights
		}
		every_pool_character = {
			province = root.capital_province
			limit = {
				ep2_can_be_accolade_successor_base_trigger = yes
				can_be_knight_trigger = { ARMY_OWNER = root }
				is_valid_successor_for_accolade = scope:accolade_in_need
				faith = {
					faith_hostility_level = {
						target = root.faith
						value <= faith_astray_level
					}
				}
				is_clergy = no
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = this
				}
				is_healthy = yes
			}
			add_to_list = potential_knights
		}
		random_in_list = {
			list = potential_knights
			save_scope_as = chosen_knight
		}

		# create a character if needed
		if = {
			limit = {
				NOT = { exists = scope:chosen_knight }
			}
			accolade_character_creation_effect = yes
		}

		
		if = {
			limit = {
				can_recruit_character_to_court_trigger = {
					RECRUITER = root
					RECRUITEE = scope:chosen_knight
				}
			}
			add_courtier = scope:chosen_knight
		}
		trigger_event = {
			id = accolade.0013
			days = 1
		}
	}
}

#Call Accolade Knight join event
accolade.0013 = {
	hidden = yes

	immediate = {
		if = {
			limit = {
				exists = scope:accolade_in_need
				exists = scope:chosen_knight
				exists = root.capital_province
				highest_held_title_tier >= tier_county
				NOR = {
					government_has_flag = government_is_theocracy
					has_character_flag = had_accolade_successor_notification
				}
			}		
			
			#SEND A TOAST NOTIFICATION
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = accolade.0013.notification
				left_icon = scope:chosen_knight
				custom_tooltip = accolade_call_acclaimed_knight_tooltip
				hidden_effect = {
					scope:chosen_knight = {
						if = {
							limit = {
								NOT = { is_knight_of = root }
							}
							set_knight_status = force
						}
					}
				}
			}
			add_character_flag = {
				flag = had_accolade_successor_notification
				months = 6
			}
		}
		else_if = {
			limit = {
				exists = scope:accolade_in_need
				exists = scope:chosen_knight
				exists = root.capital_province
				highest_held_title_tier >= tier_county
				NOT = { government_has_flag = government_is_theocracy }
			}		
			
			#SEND A FEED NOTIFICATION FOR NEXT KNIGHTS
			send_interface_message = {
				type = event_accolade_good_with_text
				title = accolade.0013.notification
				desc = accolade_call_acclaimed_knight_tooltip
				left_icon = scope:chosen_knight
				scope:chosen_knight = { 
					if = {
						limit = {
							NOT = { is_knight_of = root }
						}
						set_knight_status = force
					}
				}
			}
		}
		else = {
			send_interface_message = {
				type = event_accolade_bad_text
				title = accolade.0013.notification_fail
				desc = accolade_call_acclaimed_knight_fail_notification_tooltip
			}
		}
	}
}
