namespace = ambrosian_republic

# ending event
# text depending on resolution
country_event = {
	id = ambrosian_republic.1
	title = ambrosian_republic.1.t
	desc = {
		trigger = {
			has_reform = ambrosian_republic
			is_subject = no
		}
		desc = ambrosian_republic.1.d
	}
	desc = {
		trigger = {
			has_reform = military_dictatorship_reform
			is_subject = no
		}
		desc = ambrosian_republic.1.d2
	}
	desc = {
		trigger = {
			government = monarchy
			is_subject = no
		}
		desc = ambrosian_republic.1.d3
	}
	desc = {
		trigger = {
			is_subject = yes
		}
		desc = ambrosian_republic.1.d4
	}
	picture = {	
		trigger = {
			has_dlc = "Emperor"
		}
		picture = ITALIAN_wars_eventPicture
	}
	picture = {	
		trigger = {
			NOT = { has_dlc = "Emperor" }
		}
		picture = DEBATE_REPUBLICAN_eventPicture
	}
	
	fire_only_once = yes
	is_triggered_only = yes
	
	immediate = {
		on_non_estate_disaster_end = yes
		hidden_effect = {
			set_country_flag = ambrosian_republic_completely_resolved
		}
	}
	
	option = {
		name = ambrosian_republic.1.a
		add_stability = 1
		add_prestige = 20
	}
}

# France and the Emperor react
country_event = {
	id = ambrosian_republic.2
	title = ambrosian_republic.2.t
	desc = ambrosian_republic.2.d
	picture = {	
		trigger = {
			has_dlc = "Emperor"
		}
		picture = ITALIAN_wars_eventPicture
	}
	picture = {	
		trigger = {
			NOT = { has_dlc = "Emperor" }
		}
		picture = DEBATE_REPUBLICAN_eventPicture
	}
	
	fire_only_once = yes
	is_triggered_only = yes
	
	trigger = {
		had_country_flag = { flag = had_ambrosian_republic days = 365 }
		has_reform = ambrosian_republic
		exists = FRA
		FRA = {
			is_subject = no
			government = monarchy
		}
		hre_size = 1
		emperor = {
			government = monarchy
		}
	}
	
	option = {
		name = ambrosian_republic.2.a
		FRA = {
			country_event = { id = ambrosian_republic.3 }
		}
		emperor = {
			country_event = { id = ambrosian_republic.4 }
		}
	}
}

# for France
country_event = {
	id = ambrosian_republic.3
	title = ambrosian_republic.2.t
	desc = ambrosian_republic.3.d
	picture = {	
		trigger = {
			has_dlc = "Emperor"
		}
		picture = ITALIAN_wars_eventPicture
	}
	picture = {	
		trigger = {
			NOT = { has_dlc = "Emperor" }
		}
		picture = DEBATE_REPUBLICAN_eventPicture
	}
	
	fire_only_once = yes
	is_triggered_only = yes
	
	option = {
		name = ambrosian_republic.3.a
		ai_chance = { factor = 75 }
		if = {
			limit = {
				valid_for_personal_unions_trigger = yes
				MLO = { valid_for_personal_unions_trigger = yes }
			}
			add_casus_belli = {
				type = cb_restore_personal_union
				months = 300
				target = MLO
			}
		}
		else = {
			add_casus_belli = {
				type = cb_vassalize_mission
				months = 300
				target = MLO
			}
		}
	}
	option = {
		name = ambrosian_republic.3.b
		ai_chance = { factor = 25 }
		add_prestige = 5
	}
}

# for emperor
country_event = {
	id = ambrosian_republic.4
	title = ambrosian_republic.2.t
	desc = ambrosian_republic.4.d
	picture = {	
		trigger = {
			has_dlc = "Emperor"
		}
		picture = ITALIAN_wars_eventPicture
	}
	picture = {	
		trigger = {
			NOT = { has_dlc = "Emperor" }
		}
		picture = DEBATE_REPUBLICAN_eventPicture
	}
	
	fire_only_once = yes
	is_triggered_only = yes
	
	option = {
		name = ambrosian_republic.3.a
		ai_chance = { factor = 75 }
		if = {
			limit = {
				valid_for_personal_unions_trigger = yes
				MLO = { valid_for_personal_unions_trigger = yes }
			}
			add_casus_belli = {
				type = cb_restore_personal_union
				months = 300
				target = MLO
			}
		}
		else = {
			add_casus_belli = {
				type = cb_vassalize_mission
				months = 300
				target = MLO
			}
		}
	}
	option = {
		name = ambrosian_republic.3.b
		ai_chance = { factor = 25 }
		add_prestige = 5
	}
}

# Sforza defects to Milan
country_event = {
	id = ambrosian_republic.5
	title = ambrosian_republic.5.t
	desc = {
		trigger = {
			war_with = VEN
		}
		desc = ambrosian_republic.5.d
	}
	desc = {
		trigger = {
			is_at_war = yes
			NOT = { war_with = VEN }
		}
		desc = ambrosian_republic.5.d2
	}
	desc = {
		trigger = {
			num_of_rebel_armies = 1
			is_at_war = no
		}
		desc = ambrosian_republic.5.d3
	}
	picture = {	
		trigger = {
			has_dlc = "Emperor"
		}
		picture = ITALIAN_wars_eventPicture
	}
	picture = {	
		trigger = {
			NOT = { has_dlc = "Emperor" }
		}
		picture = DEBATE_REPUBLICAN_eventPicture
	}
	fire_only_once = yes
	is_triggered_only = yes
	
	trigger = {
		has_reform = ambrosian_republic
		OR = {
			is_at_war = yes
			num_of_rebel_armies = 1
		}
	}
	
	immediate = {
		hidden_effect = {
			if = {
				limit = {
					is_at_war = yes
					NOT = { war_with = VEN }
				}
				random_country = {
					limit = {
						is_subject = no
						OR = {
							is_in_war = {
								attackers = ROOT
								defender_leader = this
							}
							is_in_war = {
								defenders = ROOT
								attacker_leader = this
							}
						}
					}
					save_event_target_as = sforza_origin_country
				}
			}
			if = {
				limit = {
					any_owned_province = {
						region = italy_region
						is_capital = no
					}
				}
				random_owned_province = {
					limit = {
						is_capital = no
						region = italy_region
					}
					save_event_target_as = sforza_promised_province
				}
			}
		}
	}
	
	option = { # recruit Sforza
		name = ambrosian_republic.5.a
		set_country_flag = sforza_hired
		define_general = {
			name = "00000000000000"
			shock = 5
			fire = 3
			manuever = 4
			siege = 2
		}
		if = {
			limit = {
				num_of_cities = 3
				any_owned_province = {
					region = italy_region
					is_capital = no
				}
			}
			event_target:sforza_promised_province = {
				add_local_autonomy = 50
			}
		}
		if = {
			limit = {
				is_at_war = yes
				NOT = { war_with = VEN }
			}
			event_target:sforza_origin_country = {
				add_opinion = {
					who = ROOT
					modifier = opinion_swayed_sforza
				}
			}
		}
		else_if = {
			limit = {
				war_with = VEN
			}
			VEN = {
				add_opinion = {
					who = ROOT
					modifier = opinion_swayed_sforza
				}
			}
		}
	}
	option = { # we don't need him
		name = ambrosian_republic.5.b
		ai_chance = {
			factor = 0
		}
		add_stability = -1
		if = {
			limit = {
				war_with = VEN
			}
			VEN = {
				define_general = {
					name = "00000000000000"
					shock = 5
					fire = 3
					manuever = 4
					siege = 2
				}
				country_event = { id = ambrosian_republic.9 }
			}
		}
		else_if = {
			limit = {
				is_at_war = yes
			}
			event_target:sforza_origin_country = {
				define_general = {
					name = "00000000000000"
					shock = 5
					fire = 3
					manuever = 4
					siege = 2
				}
				country_event = { id = ambrosian_republic.9 }
			}
		}
		else = {
			random_owned_province = {
				limit = {
					region = italy_region
				}
				spawn_rebels = {
					type = pretender_rebels
					size = 2
					leader = "Francesco"
					leader_dynasty = "Sforza"
				}
			}
		}
	}
}

# Sforza clashes with political rivals
country_event = {
	id = ambrosian_republic.6
	title = ambrosian_republic.6.t
	desc = ambrosian_republic.6.d
	picture = {	
		trigger = {
			has_dlc = "Emperor"
		}
		picture = ITALIAN_wars_eventPicture
	}
	picture = {	
		trigger = {
			NOT = { has_dlc = "Emperor" }
		}
		picture = DEBATE_REPUBLICAN_eventPicture
	}
	
	fire_only_once = yes
	is_triggered_only = yes
	
	trigger = {
		government = republic
		had_country_flag = { flag = sforza_hired days = 700 }
		has_leader = "00000000000000"
		any_owned_province = {
			region = italy_region
		}
	}
	
	option = { # Sforza establishes military rule
		name = ambrosian_republic.6.a
		ai_chance = { factor = 75 }
		add_stability = -1
		add_government_reform = military_dictatorship_reform
		set_country_flag = mlo_military_rule
		define_leader_to_ruler = {
			name = "00000000000000"
			ADM = 5
			DIP = 5
			MIL = 5
			fixed = yes
		}
	}
	option = { # we don't need him
		name = ambrosian_republic.6.b
		ai_chance = { factor = 25 }
		add_stability = -2
		kill_leader = { type = "Francesco Sforza" }
		random_owned_province = {
			limit = {
				region = italy_region
			}
			spawn_rebels = {
				type = pretender_rebels
				size = 2
				leader = "Francesco"
				leader_dynasty = "Sforza"
			}
		}
	}
}
	

# Sforza declares himself Duke
country_event = {
	id = ambrosian_republic.7
	title = ambrosian_republic.7.t
	desc = ambrosian_republic.7.d
	picture = {	
		trigger = {
			has_dlc = "Emperor"
		}
		picture = ITALIAN_wars_eventPicture
	}
	picture = {	
		trigger = {
			NOT = { has_dlc = "Emperor" }
		}
		picture = DEBATE_REPUBLICAN_eventPicture
	}
	
	fire_only_once = yes
	is_triggered_only = yes
	
	trigger = {
		has_reform = military_dictatorship_reform
		had_country_flag = { flag = mlo_military_rule days = 365 }
		has_ruler = "00000000000000"
	}
	
	immediate = {
		hidden_effect = {
			set_country_flag = ambrosian_republic_resolved
		}
	}
	
	option = { # all hail Duke Sforza
		name = "ambrosian_republic.7.a"
		ai_chance = { factor = 90 }
		change_government_to_monarchy = yes
		add_government_reform = feudalism_reform
		add_legitimacy = 50
		define_heir = {
			name = "Galeazzo Maria"
			dynasty = "000000"
			birth_date = 1444.1.24
			claim = 100
		}
		add_stability = 1	
		custom_tooltip = ambrosian_republic_resolved_tt		
	}
	option = { # Appoint a new dictator
		name = "ambrosian_republic.7.b"
		ai_chance = { factor = 5 }
		define_ruler = {
			name = "Jacopo Piccinino"
		}
		random_owned_province = {
			limit = {
				region = italy_region
			}
			spawn_rebels = {
				type = pretender_rebels
				size = 2
				leader = "Francesco"
				leader_dynasty = "Sforza"
			}
		}
		custom_tooltip = ambrosian_republic_resolved_tt
	}
	option = { # restore ambrosian republic under Lampugnano's republican direction
		name = "ambrosian_republic.7.c"
		ai_chance = { factor = 5 }
		add_government_reform = ambrosian_republic
		define_ruler = {
			name = "Giorgio Lampugnano"
		}
		random_owned_province = {
			limit = {
				region = italy_region
			}
			spawn_rebels = {
				type = pretender_rebels
				size = 2
				leader = "Francesco"
				leader_dynasty = "Sforza"
			}
		}
		custom_tooltip = ambrosian_republic_resolved_tt
	}
}

# Sforza dies after becoming dictator but before seizing the throne
country_event = {
	id = ambrosian_republic.8
	title = ambrosian_republic.8.t
	desc = ambrosian_republic.8.d
	picture = {	
		trigger = {
			has_dlc = "Emperor"
		}
		picture = ITALIAN_wars_eventPicture
	}
	picture = {	
		trigger = {
			NOT = { has_dlc = "Emperor" }
		}
		picture = DEBATE_REPUBLICAN_eventPicture
	}
	
	fire_only_once = yes
	is_triggered_only = yes
	
	trigger = {
		NOT = { has_country_flag = ambrosian_republic_resolved }
		has_reform = military_dictatorship_reform
		NOT = { has_ruler = "00000000000000" }
	}
	
	immediate = {
		hidden_effect = {
			set_country_flag = ambrosian_republic_resolved
		}
	}
	
	option = { # all hail the new dictator
		name = "ambrosian_republic.8.a"
		ai_chance = { factor = 25 }
		add_republican_tradition = -5
		custom_tooltip = ambrosian_republic_resolved_tt
		random_owned_province = {
			limit = {
				region = italy_region
			}
			spawn_rebels = {
				type = particularist_rebels
				size = 1
				leader = "Giorgio"
				leader_dynasty = "Lampugnano"
			}
		}
	}
	option = { # restore the ambrosian republic
		name = "ambrosian_republic.8.b"
		ai_chance = { factor = 75 }
		add_government_reform = ambrosian_republic
		add_republican_tradition = 20
		define_ruler = {
			name = "Giorgio Lampugnano"
		}
		custom_tooltip = ambrosian_republic_resolved_tt
		random_owned_province = {
			limit = {
				region = italy_region
			}
			spawn_rebels = {
				type = particularist_rebels
				size = 1
				leader = "Jacopo"
				leader_dynasty = "Piccinino"
			}
		}
	}
}

# Sforza joins enemy country
country_event = {
	id = ambrosian_republic.9
	title = ambrosian_republic.9.t
	desc = ambrosian_republic.9.d
	picture = {	
		trigger = {
			has_dlc = "Emperor"
		}
		picture = ITALIAN_wars_eventPicture
	}
	picture = {	
		trigger = {
			NOT = { has_dlc = "Emperor" }
		}
		picture = DEBATE_REPUBLICAN_eventPicture
	}
	
	is_triggered_only = yes
	
	option = {
		name = "ambrosian_republic.9.a"
		tooltip = {
			define_general = {
				name = "00000000000000"
				shock = 5
				fire = 3
				manuever = 4
				siege = 2
			}
		}
	}
}