#Realm Maintenance events

namespace = realm_maintenance



#Gain county corruption
realm_maintenance.0002 = {
	hidden = yes

	trigger = {
		scope:county = {
			immune_to_county_corruption_trigger = no
		}
		#Holder still needs to be the same
		scope:county.holder = scope:corruption_holder
	}

	immediate = {
		scope:county = {
			set_variable = {
				name = corruption_event_happening
				days = 1
			}
			add_random_county_corruption_in_debt_effect = yes
		}
	}
}

#Gain county corruption
realm_maintenance.0003 = {
	hidden = yes

	trigger = {
		scope:county = {
			county_control < low_county_control_limit
			immune_to_county_corruption_trigger = no
		}
		#Holder still needs to be the same
		scope:county.holder = scope:corruption_holder
	}

	immediate = {
		scope:county = {
			set_variable = {
				name = corruption_event_happening
				days = 1
			}
			add_random_county_corruption_low_control_effect = yes
		}
	}
}


#Remove county corruption
realm_maintenance.0004 = {
	hidden = yes

	trigger = {
		scope:county = {
			county_control > high_county_control_limit
			has_county_corruption_trigger = yes
		}
		#Holder still needs to be the same
		scope:county.holder = scope:corruption_holder
	}

	immediate = {
		scope:county = {
			set_variable = {
				name = corruption_event_happening
				days = 1
			}
			remove_random_county_corruption_modifier_effect = yes
		}
	}
}

# I have gained new vassals (inform them with toasts)!
realm_maintenance.1000 = {
	hidden = yes
	
	trigger = {
		has_any_landed_title_trigger = yes
	}

	immediate = {
		save_scope_as = new_liege
		
		scope:vassal = {
			if = {
				limit = {
					# Only players care about notifications
					is_ai = no 

					# Don't send a notification if a player just died and their heir is replacing them under the same liege.
					trigger_if = {
						limit = {
							exists = scope:old_liege
							scope:transfer_type = flag:inheritance
						}
						NOT = {
							scope:old_liege = {
								is_alive = no
								top_liege != this
								liege = scope:new_liege
							}
						}
					}
				}
				trigger_event = realm_maintenance.1001
			}
			# Petition liege invalidation
			if = {
				limit = {
					exists = var:petition_liege_scope
					NOT = { var:petition_liege_scope = scope:new_liege }
				}
				trigger_event = { on_action = petition_liege_travel_planner_exit }
				current_travel_plan = { 
					if = {
						limit = { can_cancel = yes }
						cancel_travel_plan = yes
					}
				}
			}
			# Pay homage invalidation
			if = {
				limit = {
					exists = var:homage_liege_scope
					NOT = { var:homage_liege_scope = scope:new_liege }
				}
				trigger_event = { on_action = pay_homage_travel_planner_exit }
				current_travel_plan ?= { 
					if = {
						limit = { can_cancel = yes }
						cancel_travel_plan = yes
					}
				}
			}
			# Bow Before Liege invalidation
			if = {
				limit = {
					exists = var:pledge_loyalty_to_liege_scope
					NOT = { var:pledge_loyalty_to_liege_scope = scope:new_liege }
				}
				trigger_event = { on_action = pledge_loyalty_to_liege_travel_planner_exit }
				current_travel_plan = { 
					if = {
						limit = { can_cancel = yes }
						cancel_travel_plan = yes
					}
				}
			}
		}
	}
}

# I have a new liege toast!
scripted_effect send_new_liege_toast_effect = {
	if = {
		limit = {
			scope:new_liege = { tgp_is_ceremonial_regent_trigger = yes } # Not 'actual' ruler
		}
		send_interface_toast = {
			type = msg_liege_changed
			title = realm_maintenance.1001.t.ceremonial_regent
			left_icon = scope:new_liege
			right_icon = scope:old_liege
			custom_tooltip = realm_maintenance.1001.$TRANSFER_TYPE$
		}
	}
	else = {
		send_interface_toast = {
			type = msg_liege_changed
			title = realm_maintenance.1001.t
			left_icon = scope:new_liege
			right_icon = scope:old_liege
			custom_tooltip = realm_maintenance.1001.$TRANSFER_TYPE$
		}
	}
}

realm_maintenance.1001 = {
	hidden = yes
	 
	immediate = {
		if = {
			limit = { always = no }
			send_new_liege_toast_effect = { TRANSFER_TYPE = conquest }
		}
		else_if = {
			limit = { scope:transfer_type = flag:conquest_holy_war }
			send_new_liege_toast_effect = { TRANSFER_TYPE = conquest_holy_war }
		}
		else_if = {
			limit = { scope:transfer_type = flag:conquest_claim }
			send_new_liege_toast_effect = { TRANSFER_TYPE = conquest_claim }
		}
		else_if = {
			limit = { scope:transfer_type = flag:conquest_populist }
			send_new_liege_toast_effect = { TRANSFER_TYPE = conquest_populist }
		}
		else_if = {
			limit = { scope:transfer_type = flag:inheritance }
			send_new_liege_toast_effect = { TRANSFER_TYPE = inheritance }
		}
		else_if = {
			limit = { scope:transfer_type = flag:abdication }
			send_new_liege_toast_effect = { TRANSFER_TYPE = abdication }
		}
		else_if = {
			limit = { scope:transfer_type = flag:destroyed }
			send_new_liege_toast_effect = { TRANSFER_TYPE = destroyed }
		}
		else_if = {
			limit = { scope:transfer_type = flag:created }
			send_new_liege_toast_effect = { TRANSFER_TYPE = created }
		}
		else_if = {
			limit = { scope:transfer_type = flag:usurped }
			send_new_liege_toast_effect = { TRANSFER_TYPE = usurped }
		}
		else_if = {
			limit = { scope:transfer_type = flag:granted }
			send_new_liege_toast_effect = { TRANSFER_TYPE = granted }
		}
		else_if = {
			limit = { scope:transfer_type = flag:revoked }
			send_new_liege_toast_effect = { TRANSFER_TYPE = revoked }
		}
		else_if = {
			limit = { scope:transfer_type = flag:election }
			send_new_liege_toast_effect = { TRANSFER_TYPE = election }
		}
		else_if = {
			limit = { scope:transfer_type = flag:independency }
			send_new_liege_toast_effect = { TRANSFER_TYPE = independency }
		}
		else_if = {
			limit = { scope:transfer_type = flag:returned }
			send_new_liege_toast_effect = { TRANSFER_TYPE = returned }
		}
		else_if = {
			limit = { scope:transfer_type = flag:leased_out }
			send_new_liege_toast_effect = { TRANSFER_TYPE = leased_out }
		}
		else_if = {
			limit = { scope:transfer_type = flag:lease_revoked }
			send_new_liege_toast_effect = { TRANSFER_TYPE = lease_revoked }
		}
		else_if = {
			limit = { scope:transfer_type = flag:faction_demand }
			send_new_liege_toast_effect = { TRANSFER_TYPE = faction_demand }
		}
		else_if = {
			limit = { scope:transfer_type = flag:swear_fealty }
			send_new_liege_toast_effect = { TRANSFER_TYPE = swear_fealty }
		}
		else_if = {
			limit = { scope:transfer_type = flag:appointment }
			send_new_liege_toast_effect = { TRANSFER_TYPE = appointment }
		}
		else_if = {
			limit = { scope:transfer_type = flag:appointment_succession }
			send_new_liege_toast_effect = { TRANSFER_TYPE = appointment_succession }
		}
		else_if = {
			limit = { scope:transfer_type = flag:stepped_down }
			send_new_liege_toast_effect = { TRANSFER_TYPE = stepped_down }
		}
		else = {
			send_new_liege_toast_effect = { TRANSFER_TYPE = error }
		}
	}
}


# You just inherited an Emperor title!!
# by Linnéa Thimrén
realm_maintenance.2001 = {
    type = character_event
    title = {
    	first_valid = {
    		triggered_desc = {
    		    trigger = { tgp_is_ceremonial_regent_trigger = yes }
    		    desc = realm_maintenance.2001.t_ceremonial_regent
    		}
    		desc = realm_maintenance.2001.t
    	}
    }
    desc = {
    	first_valid = {
    		triggered_desc = {
    		    trigger = {
    				exists = scope:murder
    		    }
    		    desc = realm_maintenance.2001.desc_murdered
    		}
    		triggered_desc = {
    		    trigger = {
    				scope:previous_holder = { is_alive = no }
    		    }
    		    desc = realm_maintenance.2001.desc_dead
    		}
    		desc = realm_maintenance.2001.desc_alive
    	}
    	first_valid = {
    		triggered_desc = {
    			trigger = { tgp_is_ceremonial_regent_trigger = yes }
    			desc = realm_maintenance.2001.desc_ceremonial_regent
    		}
    		desc = realm_maintenance.2001.desc
    	}
    }
    theme = crown
    left_portrait = {
    	character = root
    	triggered_animation = {
			trigger = {
				exists = scope:murder
			}
			animation = schadenfreude
		}
		triggered_animation = {
			trigger = { always = yes }
			animation = personality_bold
		}
    }
    right_portrait = {
    	character = primary_title.var:administrative_ui_special_title.holder
    	animation = holding_hu
    	trigger = {
    		NOT = { this = root }
    	}
    }
    lower_right_portrait = scope:previous_holder
    cooldown = { days = 1 }
    
    trigger = {
    	scope:title.tier = tier_empire
    	primary_title = scope:title
    	exists = scope:previous_holder
    	NOT = { #You don't already have an empire tier title
    		any_held_title = {
    			title_tier = empire
    			this != scope:title
    		}
    	}
    	NAND = {
    		scope:title = title:e_japan
    		exists = top_liege.primary_title.var:administrative_ui_special_title
			has_title = top_liege.primary_title.var:administrative_ui_special_title
    	}
    }

    immediate = {
		if = {
			limit = {
				has_ach_dlc_trigger = yes
			}
			coronation_reset_realm_law_effect = yes #we need to reset the crown laws immediately, not wait for the next tick
		}
		add_character_flag = had_coronation_prompt
		assign_quirk_effect = yes
		if = {
    		limit = {
    			OR = {
    				any_secret = {
    					type = secret_murder
    					secret_target = scope:previous_holder
    				}
    				scope:previous_holder = {
						is_alive = no
						killer ?= root
					}
    			}
    		}
    		save_scope_value_as = {
    			name = murder
    			value = yes
    		}
    	}
    	show_as_tooltip = {
    		get_title = scope:title
    	}
    }

    option = {
        name = {
        	text = realm_maintenance.2001.a
        	trigger = { tgp_is_ceremonial_regent_trigger = no }
        }
        name = {
        	text = realm_maintenance.2001.a.ceremonial_regent
        	trigger = { tgp_is_ceremonial_regent_trigger = yes }
        }
        
		if = {
			limit = {
				has_ach_dlc_trigger = yes
				has_realm_law = uncrowned
				tgp_is_ceremonial_regent_trigger = no
				NOT = { has_character_flag = had_coronation_prompt }
			}
			if = {
				limit = {
					coronation_has_proper_artifact_trigger = yes
				}
				custom_tooltip = fund_inspiration.1001.b.coronation_ready
				open_view_data = {
					view = activity_list_detail_host_window
					data = activity_type:activity_coronation
					player = root
				}
			}
			else = {
				custom_tooltip = court_maintenance.0011.coronation.tooltip
				create_proper_coronation_artifact = yes
			}
		}
    }
}
