namespace = tgp_faction_events

scripted_effect faction_demand_japanese_regent_rejected_effect = {
	# This effect should always be placed here, _before_ all the other effects in the block.
	trigger_event = { on_action = on_faction_demand_rejected }
	show_as_tooltip = {
		scope:faction = {
			faction_start_war = {}
		}
	}
}

# Ceremonial Regent Faction Demand
tgp_faction_events.0101 = {
	type = letter_event
	sender = scope:faction_leader
	opening = "FACTION_JAPANESE_REPLACE_REGENT"
	desc = {
		first_valid = {
			triggered_desc = {
				trigger = { tgp_has_ceremonial_liege_title_trigger = yes }
				desc = "FACTION_JAPANESE_REPLACE_RULER_DESC"
			}
			desc = "FACTION_JAPANESE_REPLACE_REGENT_DESC"
		}
	}
	
	trigger = {
		# May have ceased to exist by the time this triggers
		exists = scope:faction
	}

	immediate = {
		# Grab our scope for loc.
		save_scope_as = faction_target
	}

	option = {
		name = "FACTION_DEMAND_ACCEPT"
		add_dread = medium_dread_loss
		# LEGITIMACY LOSS FOR ACCEPTING FACTION DEMANDS
		faction_accept_demand_legitimacy_effect = yes
		add_prestige = -500
		faction_demand_regent_transfer_effect = {
			NEW_REGENT = scope:faction_leader
			REASON = faction_demand
		}
		add_character_flag = {
			flag = recent_replace_regent_faction_war
			years = 10
		}
		scope:faction_leader = {
			trigger_event = { id = tgp_faction_events.0102 }
		}
		# This effect should always be placed here, _after_ everything except the ai_chance.
		trigger_event = { on_action = on_faction_demand_accepted }
		ai_chance = {
			base = 10
			modifier = {
				add = 90
				scope:faction = { faction_power >= 100 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 125 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 150 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 200 }
			}
			modifier = {
				add = 50
				any_character_war = {
					is_war_leader = root
					is_defender = root
				}
			}
			modifier = {
				factor = 0.1
				any_ally = {
					NOR = {
						target_is_liege_or_above = root
						target_is_vassal_or_below = root
					}
					max_military_strength > root.max_military_strength
				}
			}
			modifier = {
				add = 10000
				scope:faction_leader = { is_yes_men_enabled = yes }
			}
			modifier = { # The Kampaku always fights
				factor = 0
				has_title = title:e_japan
				government_has_flag = government_is_japan_administrative
			}
		}
	}

	option = {
		name = "FACTION_DEMAND_REFUSE"
		faction_demand_japanese_regent_rejected_effect = yes
		scope:faction_leader = {
			trigger_event = {
				id = tgp_faction_events.0103
				days = 1
			}
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 4.0
			}
			modifier = {
				add = 50
				scope:faction = { faction_power < 80 }
			}
			modifier = {
				add = 100
				scope:faction = { faction_power < 60 }
			}
			modifier = {
				add = 1000
				scope:faction = { faction_power < 40 }
			}
			modifier = {
				add = 10000
				scope:faction_leader = { is_no_men_enabled = yes }
			}
		}
	}
}

# Japanese Regent Faction Demand Accepted
tgp_faction_events.0102 = {
	type = letter_event
	sender = scope:faction_target
	opening = "FACTION_JAPANESE_REPLACE_REGENT_ACCEPTED"
	desc = "FACTION_JAPANESE_REPLACE_REGENT_ACCEPTED_DESC"

	immediate = {
		show_as_tooltip = {
			get_title = primary_title
			if = {
				limit = {
					government_has_flag = government_is_japan_administrative
					tgp_has_ceremonial_liege_title_trigger = no
					NOT = {
						has_realm_law = japanese_regency_succession_law
					}
				}
				add_realm_law_skip_effects = japanese_regency_succession_law
			}
			else = {
				add_realm_law_skip_effects = single_heir_succession_law
			}
		}
	}

	option = {
		name = "FACTION_DEMAND_ACCEPTED_OPT"
	}
}

# Japanese Regent Faction Demand Refused
tgp_faction_events.0103 = {
	type = letter_event
	sender = scope:faction_target
	opening = "FACTION_JAPANESE_REPLACE_REGENT_REFUSED"
	desc = "FACTION_JAPANESE_REPLACE_REGENT_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"
		scope:faction = {
			faction_start_war = {}
			every_faction_member = {
				limit = {
					NOT = { this = scope:faction.faction_leader }
				}
				trigger_event = tgp_faction_events.0104
			}
		}
	}
}

# Japanese Regent Faction Demand Refused Member Notice
tgp_faction_events.0104 = {
	type = letter_event
	sender = scope:faction_target
	opening = "FACTION_JAPANESE_REPLACE_REGENT_REFUSED"
	desc = "FACTION_JAPANESE_REPLACE_REGENT_REFUSED_DESC"

	option = { name = "FACTION_DEMAND_REFUSED_OPT" }
}

# Japanese Regent Faction Demand Send Notice
tgp_faction_events.0105 = {
	type = letter_event
	sender = scope:faction_leader
	opening = "FACTION_JAPANESE_REPLACE_REGENT_SEND_DEMAND_NOTIFICATION"
	desc = "FACTION_JAPANESE_REPLACE_REGENT_SEND_DEMAND_NOTIFICATION_DESC"

	option = { name = "FACTION_DEMAND_SEND_DEMAND_NOTIFICATION_OPT"	}
}

scripted_effect faction_demand_restore_emperor_rejected_effect = {
	# This effect should always be placed here, _before_ all the other effects in the block.
	trigger_event = { on_action = on_faction_demand_rejected }
	show_as_tooltip = {
		scope:faction = {
			faction_start_war = {}
		}
	}
}

# Restore Emperor Faction Demand
tgp_faction_events.0201 = {
	type = letter_event
	sender = scope:faction_leader
	opening = "FACTION_JAPANESE_RESTORE_EMPEROR"
	desc = "FACTION_JAPANESE_RESTORE_EMPEROR_DESC"
	
	trigger = {
		# May have ceased to exist by the time this triggers
		exists = scope:faction
	}

	immediate = {
		# Grab our scope for loc.
		save_scope_as = faction_target
	}

	option = {
		name = "FACTION_DEMAND_ACCEPT"
		add_dread = medium_dread_loss
		# LEGITIMACY LOSS FOR ACCEPTING FACTION DEMANDS
		faction_accept_demand_legitimacy_effect = yes
		add_prestige = -500
		scope:faction = {
			special_character = { save_scope_as = ceremonial_liege }
			faction_leader = {
				trigger_event = { id = tgp_faction_events.0202 }
			}
			if = {
				limit = { exists = this }
				destroy_faction = yes
			}
		}
		show_as_tooltip = {
			restore_ceremonial_liege_faction_combined_effect = yes
		}
		# This effect should always be placed here, _after_ everything except the ai_chance.
		trigger_event = { on_action = on_faction_demand_accepted }
		ai_chance = {
			base = 10
			modifier = {
				add = 90
				scope:faction = { faction_power >= 100 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 125 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 150 }
			}
			modifier = {
				add = 50
				scope:faction = { faction_power >= 200 }
			}
			modifier = {
				add = 50
				any_character_war = {
					is_war_leader = root
					is_defender = root
				}
			}
			modifier = {
				factor = 0.1
				any_ally = {
					NOR = {
						target_is_liege_or_above = root
						target_is_vassal_or_below = root
					}
					max_military_strength > root.max_military_strength
				}
			}
			modifier = {
				add = 10000
				scope:faction_leader = { is_yes_men_enabled = yes }
			}
		}
	}

	option = {
		name = {
			trigger = {
				scope:faction_leader = { tgp_has_ceremonial_liege_title_trigger = no }
			}
			text = tgp_faction_events.0201.b
		}
		name = {
			trigger = {
				scope:faction_leader = { tgp_has_ceremonial_liege_title_trigger = yes }
			}
			text = tgp_faction_events.0201.b.cm
		}
		faction_demand_restore_emperor_rejected_effect = yes
		scope:faction_leader = {
			trigger_event = {
				id = tgp_faction_events.0203
				days = 1
			}
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_boldness = 4.0
			}
			modifier = {
				add = 50
				scope:faction = { faction_power < 80 }
			}
			modifier = {
				add = 100
				scope:faction = { faction_power < 60 }
			}
			modifier = {
				add = 1000
				scope:faction = { faction_power < 40 }
			}
		}
	}
}

# Restore Emperor Faction Demand Accepted
tgp_faction_events.0202 = {
	type = letter_event
	sender = scope:faction_target
	opening = "FACTION_JAPANESE_RESTORE_EMPEROR_ACCEPTED"
	desc = "FACTION_JAPANESE_RESTORE_EMPEROR_ACCEPTED_DESC"

	immediate = {
		save_scope_as = faction_restoration
		scope:faction.special_character = { save_scope_as = ceremonial_monarch }
		if = {
			limit = { this = scope:ceremonial_monarch }
			restore_ceremonial_liege_faction_combined_effect = yes
		}
		else = {
			scope:faction_target = { save_scope_as = defender }
			trigger_event = tgp_faction_events.0206
		}
	}

	option = {
		name = "FACTION_DEMAND_ACCEPTED_OPT"
	}
}

# Restore Emperor Faction Demand Refused
tgp_faction_events.0203 = {
	type = letter_event
	sender = scope:faction_target
	opening = "FACTION_JAPANESE_RESTORE_EMPEROR_REFUSED"
	desc = "FACTION_JAPANESE_RESTORE_EMPEROR_REFUSED_DESC"

	option = {
		name = "FACTION_DEMAND_REFUSED_OPT"
		scope:faction = {
			faction_start_war = {}
			every_faction_member = {
				limit = {
					NOT = { this = scope:faction.faction_leader }
				}
				trigger_event = tgp_faction_events.0204
			}
		}
	}
}

# Restore Emperor Faction Demand Refused Member Notice
tgp_faction_events.0204 = {
	type = letter_event
	sender = scope:faction_target
	opening = "FACTION_JAPANESE_RESTORE_EMPEROR_REFUSED"
	desc = "FACTION_JAPANESE_RESTORE_EMPEROR_REFUSED_DESC"

	option = { name = "FACTION_DEMAND_REFUSED_OPT" }
}

# Restore Emperor Faction Demand Send Notice
tgp_faction_events.0205 = {
	type = letter_event
	sender = scope:faction_leader
	opening = "FACTION_JAPANESE_RESTORE_EMPEROR_SEND_DEMAND_NOTIFICATION"
	desc = "FACTION_JAPANESE_RESTORE_EMPEROR_SEND_DEMAND_NOTIFICATION_DESC"

	option = { name = "FACTION_DEMAND_SEND_DEMAND_NOTIFICATION_OPT"	}
}

# Restore Emperor Faction Aftermath
tgp_faction_events.0206 = {
	type = character_event
	title = tgp_faction_events.0206.t
	desc = tgp_faction_events.0206.desc
	override_background = { reference = throne_room_scope }
	theme = war
	left_portrait = {
		character = root
		animation = war_over_win
	}
	right_portrait = {
		character = scope:ceremonial_liege
		animation = war_over_tie
	}
	lower_center_portrait = scope:defender

	trigger = {
		tgp_realm_has_ceremonial_liege_trigger = yes
		tgp_has_ceremonial_liege_title_trigger = no
	}

	on_trigger_fail = {
		if = {
			limit = { tgp_realm_has_ceremonial_liege_trigger = yes }
			restore_ceremonial_liege_faction_combined_effect = yes
		}
	}

	immediate = {
		if = {
			limit = { NOT = { exists = scope:attacker } }
			save_scope_as = attacker
		}
		tgp_save_realm_ceremonial_liege_effect = yes
		scope:ceremonial_liege = { save_scope_as = background_throne_room_scope }
	}

	option = {
		name = tgp_faction_events.0206.a
		restore_ceremonial_liege_faction_combined_effect = yes
		add_hook = {
			target = scope:ceremonial_liege
			type = favor_hook
		}
		scope:ceremonial_liege = {
			add_opinion = {
				target = root
				modifier = grateful_opinion
				opinion = 50
			}
		}
		every_in_list = {
			list = attackers
			limit = {
				NOT = { this = root }
			}
			custom = faction_member_custom
			add_opinion = {
				target = root
				modifier = respect_opinion
				opinion = 25
			}
		}
		stress_impact = {
			ambitious = minor_stress_impact_gain
			arbitrary = minor_stress_impact_gain
			arrogant = minor_stress_impact_gain
		}
		ai_chance = {
			base = 100
			ai_value_modifier = {
				ai_greed = -0.5
				ai_boldness = -0.5
				ai_honor = 0.5
			}
		}
	}

	option = {
		name = tgp_faction_events.0206.b
		faction_demand_regent_transfer_effect = {
			NEW_REGENT = scope:attacker
			REASON = faction_demand
		}
		add_tyranny = major_tyranny_gain
		reverse_add_opinion = {
			target = scope:ceremonial_liege
			modifier = angry_opinion
			opinion = -50
		}
		every_in_list = {
			list = attackers
			limit = {
				NOR = {
					this = root
					house ?= root.house
				}
			}
			custom = faction_member_custom
			add_opinion = {
				target = root
				modifier = angry_opinion
				opinion = -25
			}
		}
		stress_impact = {
			honest = minor_stress_impact_gain
			just = minor_stress_impact_gain
			content = minor_stress_impact_gain
			humble = minor_stress_impact_gain
		}
		ai_chance = {
			base = -50
			ai_value_modifier = {
				ai_greed = 0.5
				ai_boldness = 0.5
				ai_honor = -0.5
			}
		}
	}
}
