##############################
##############################
##### WUW GERMANY EVENTS #####
##############################
##############################

add_namespace = wia_ger
add_namespace = wia_faction
add_namespace = kr.political
add_namespace = prca




country_event = { #The Sun Sets
	id = wia_ger.1
	title = wia_ger.1.t
	desc = wia_ger.1.desc
	picture = GFX_report_event_volkshalle

	fire_only_once = yes

	is_triggered_only = yes

	option = { 
		name = wia_ger.1.a
		ai_chance = {
			base = 10
		}		
	}
}

country_event = { #Operation Großeschweine
	id = wia_ger.2
	title = wia_ger.2.t
	desc = wia_ger.2.desc
	picture = GFX_report_erwache_isabella

	fire_only_once = yes

	is_triggered_only = yes

	option = { 
		name = wia_ger.2.a
		ai_chance = {
			base = 100
		}	
		
		add_stability = -0.05
	}

	option = { 
		name = wia_ger.2.b
		ai_chance = {
			base = 0
		}	
		
		custom_effect_tooltip = GER_groseschweine_tt

		hidden_effect = {
			country_event = {
			    id = wia_ger.3
			    days = 1
			}
		}
	}
}

country_event = { #Assasination of Himmler
	id = wia_ger.3
	title = wia_ger.3.t
	desc = wia_ger.3.desc
	picture = GFX_report_event_german_reichstag_bombed

	fire_only_once = yes

	is_triggered_only = yes

	option = { 
		name = wia_ger.3.a
		ai_chance = {
			base = 10
		}	
		
		add_stability = -0.1
		hidden_effect = {
			country_event = {
			    id = wia_ger.4
			    days = 7
			}

			retire_character = GER_heinrich_himmler

			add_country_leader_role = {
				character = GER_hermann_goring
				promote_leader = yes
				country_leader = {
					ideology = nazism
					expire = "1965.1.1.1"
				}
			}
		}
	}
}

country_event = { #German Civil War
	id = wia_ger.4
	title = wia_ger.4.t
	desc = wia_ger.4.desc
	picture = GFX_report_event_ger_civil_war

	fire_only_once = yes

	is_triggered_only = yes

	immediate = {
		hidden_effect = {
			GER = {
				dismantle_faction = yes
				end_puppet = RKS
				set_nationality = {
					target_country = SPE
					character = GER_albert_speer
				}
				set_nationality = {
					target_country = BOR
					character = GER_martin_bormann
				}
				set_nationality = {
					target_country = GOR
					character = GER_hermann_goring
				}
				set_nationality = {
					target_country = RKS
					character = GER_reinhard_heydrich
				}

				set_nationality = {
					target_country = SPE
					character = GER_erwin_von_witzleben
				}
				set_nationality = {
					target_country = SPE
					character = GER_albert_kesselring
				}
				set_nationality = {
					target_country = SPE
					character = GER_ludwig_beck
				}
				set_nationality = {
					target_country = SPE
					character = GER_alfred_jodl
				}

				set_nationality = {
					target_country = GOR
					character = GER_erich_von_manstein
				}
				set_nationality = {
					target_country = GOR
					character = GER_franz_halder
				}
				set_nationality = {
					target_country = GOR
					character = GER_georg_von_kuchler
				}
				set_nationality = {
					target_country = GOR
					character = GER_gerd_von_rundstedt
				}
				set_nationality = {
					target_country = GOR
					character = GER_johannes_blaskowitz
				}

				set_nationality = {
					target_country = RKS
					character = GER_georghans_reinhardt
				}
				set_nationality = {
					target_country = RKS
					character = GER_friedrich_paulus
				}
				set_nationality = {
					target_country = RKS
					character = GER_erich_brandenberger
				}

				set_nationality = {
					target_country = BOR
					character = GER_fedor_von_bock
				}
				set_nationality = {
					target_country = BOR
					character = GER_maximilian_von_weichs
				}
				set_nationality = {
					target_country = BOR
					character = GER_gunther_von_kluge
				}
				set_nationality = {
					target_country = BOR
					character = GER_gotthard_heinrici
				}
			}

			#CHARACTERS
			add_country_leader_role = {
				character = GER_erwin_rommel
				promote_leader = yes
				country_leader = {
					ideology = nazism
					expire = "1965.1.1.1"
				}
			}
			SPE = {
				add_country_leader_role = {
					character = GER_albert_speer
					promote_leader = yes
					country_leader = {
						ideology = nazism
						expire = "1965.1.1.1"
					}
				}
			}
			BOR = {
				add_country_leader_role = {
					character = GER_martin_bormann
					promote_leader = yes
					country_leader = {
						ideology = nazism
						expire = "1965.1.1.1"
					}
				}
			}
			GOR = {
				add_country_leader_role = {
					character = GER_hermann_goring
					promote_leader = yes
					country_leader = {
						ideology = nazism
						expire = "1965.1.1.1"
					}
				}
			}
			RKS = {
				add_country_leader_role = {
					character = GER_reinhard_heydrich
					promote_leader = yes
					country_leader = {
						ideology = nazism
						expire = "1965.1.1.1"
					}
				}
			}

			#STATES
			SPE = {
				transfer_state = 989
				transfer_state = 55
				transfer_state = 57
				transfer_state = 56
				transfer_state = 59
				transfer_state = 58
				transfer_state = 998
				transfer_state = 922
				transfer_state = 60
			}
			BOR = {
				transfer_state = 61
				transfer_state = 62
				transfer_state = 63
				transfer_state = 68
				transfer_state = 65
				transfer_state = 69
				transfer_state = 66
				transfer_state = 74
				transfer_state = 72
				transfer_state = 67
				transfer_state = 762
				transfer_state = 86
				transfer_state = 87
				transfer_state = 807
				transfer_state = 85
				transfer_state = 98
				transfer_state = 97
				transfer_state = 934
				transfer_state = 5
				transfer_state = 763
				transfer_state = 188
			}
			RKS = {
				transfer_state = 51
				transfer_state = 42
				transfer_state = 1053
				transfer_state = 8
				transfer_state = 28
				transfer_state = 965
			}
			GOR = {
				transfer_state = 923
				transfer_state = 50
				transfer_state = 54
				transfer_state = 52
				transfer_state = 53
				transfer_state = 848
				transfer_state = 153
				transfer_state = 152
				transfer_state = 1054
				transfer_state = 102
				transfer_state = 1086
				transfer_state = 4
				transfer_state = 557
				transfer_state = 1078
			}

			#PARTIES
			set_party_name = {
				ideology = fascism
				long_name = GER_nsdap_regular_long
				name = GER_nsdap_regular
			}
			GOR = {
				set_party_name = {
					ideology = fascism
					long_name = GER_nsdap_regular_long
					name = GER_nsdap_goring
				}
			}
			BOR = {
				set_party_name = {
					ideology = fascism
					long_name = GER_nsdap_regular_long
					name = GER_nsdap_bormann
				}
			}
			SPE = {
				set_party_name = {
					ideology = fascism
					long_name = GER_nsdap_regular_long
					name = GER_nsdap_speer
				}
			}

			#OOB
			GOR = {
				delete_unit = {}
				load_oob = "GER_goring"
			}
			BOR = {
				delete_unit = {}
				load_oob = "GER_bormann"
			}
			SPE = {
				delete_unit = {}
				load_oob = "GER_speer"
			}
			RKS = {
				delete_unit = {}
				load_oob = "GER_heydrich"
			}
			GER = {
				delete_unit = {}
				load_oob = "GER_germania"
			}

			#WAR
			GOR = {
				declare_war_on = {
					target = BOR
					type = annex_everything
				}
				declare_war_on = {
					target = SPE
					type = annex_everything
				}
				declare_war_on = {
					target = RKS
					type = annex_everything
				}
			}
			BOR = {
				declare_war_on = {
					target = GOR
					type = annex_everything
				}
				declare_war_on = {
					target = SPE
					type = annex_everything
				}
				declare_war_on = {
					target = RKS
					type = annex_everything
				}
			}
			SPE = {
				declare_war_on = {
					target = BOR
					type = annex_everything
				}
				declare_war_on = {
					target = GOR
					type = annex_everything
				}
				declare_war_on = {
					target = RKS
					type = annex_everything
				}
			}
			RKS = {
				declare_war_on = {
					target = BOR
					type = annex_everything
				}
				declare_war_on = {
					target = GOR
					type = annex_everything
				}
				declare_war_on = {
					target = SPE
					type = annex_everything
				}
			}


			#MISC
			set_cosmetic_tag = GER_germania_cos
			RKS = {
				set_cosmetic_tag = GER_ss_ordensstaat_cos
			}
		}
	}

	option = { 
		name = wia_ger.4.a
		ai_chance = {
			base = 10
		}	
		GOR = {
			change_tag_from = GER
		}
	}
	option = { 
		name = wia_ger.4.b
		ai_chance = {
			base = 10
		}	
		SPE = {
			change_tag_from = GER
		}
	}
	option = { 
		name = wia_ger.4.c
		ai_chance = {
			base = 10
		}	
		RKS = {
			change_tag_from = GER
		}
	}
	option = { 
		name = wia_ger.4.d
		ai_chance = {
			base = 10
		}	
		BOR = {
			change_tag_from = GER
		}
	}
}

country_event = { #End of Civil War
	id = wia_ger.5
	title = wia_ger.5.t
	desc = wia_ger.5.desc
	picture = GFX_report_event_volkshalle

	trigger = {
		OR = {
			tag = BOR
			tag = GOR
			tag = SPE
			tag = RKS
		}
		has_war = no
	}

	fire_only_once = yes

	option = { 
		name = wia_ger.5.a
		ai_chance = {
			base = 10
		}	
		
		GER = {
			annex_country = {
				target = ROOT
				transfer_troops = yes
			}
			if = {
				limit = {
					country_exists = RKS
				}

				set_cosmetic_tag = GER_ordensstaat_cos

				else = {
					set_cosmetic_tag = GER_greater_reich_cos
				}
			}
		}
		GER = {
		    change_tag_from = ROOT
		}

		ROOT = {
			every_character ={
				limit = {
					is_country_leader = yes
				}
				set_nationality = GER
				promote_character = yes
			}
			every_character ={
				limit = {
					is_country_leader = no
				}
				set_nationality = GER
			}
		}
	}
}

country_event = { #X Wants to Join Faction
	id = wia_faction.1
	title = wia_faction.1.t
	desc = wia_faction.1.desc
	picture = GFX_report_event_volkshalle

	fire_only_once = no
	is_triggered_only = yes

	option = { 
		name = wia_faction.1.a
		ai_chance = {
			base = 100
		}
		
		ROOT = {
			add_to_faction = FROM
		}
	}
	option = { 
		name = wia_faction.1.b
		ai_chance = {
			base = 0
		}	
	}
}

# unit limit calculations
country_event = { #donotlog
	id = kr.political.119

	hidden = yes
	is_triggered_only = yes

	trigger = {
		NOT = { has_global_flag = KR_no_unit_limits }
	}

	option = {
		# step one, calculate num of actual divisions
		set_variable = { num_actual_divisions = num_battalions }

		# ignore fake intel divisions
		subtract_from_variable = { num_actual_divisions = num_fake_intel_battalions }

		# expeditionaries should count towards the sender's limit, not the receiver's
		clear_variable = num_expeditionary_battalions_sent
		clear_variable = num_expeditionary_battalions_received
		for_each_scope_loop = {
			array = faction_members
			if = {
				limit = {
					received_expeditionary_forces = { sender = ROOT value > 0 }
				}
				set_temp_variable = { expeditionary_battalions_sent_temp = 0 }
				while_loop_effect = {
					limit = {
						received_expeditionary_forces = { sender = ROOT value > expeditionary_battalions_sent_temp }
					}
					add_to_temp_variable = { expeditionary_battalions_sent_temp = 1 }
				}
				add_to_variable = { ROOT.num_expeditionary_battalions_sent = expeditionary_battalions_sent_temp }
			}
			if = {
				limit = {
					ROOT = { received_expeditionary_forces = { sender = PREV value > 0 } }
				}
				set_temp_variable = { expeditionary_battalions_received_temp = 0 }
				while_loop_effect = {
					limit = {
						ROOT = { received_expeditionary_forces = { sender = PREV value > expeditionary_battalions_received_temp } }
					}
					add_to_temp_variable = { expeditionary_battalions_received_temp = 1 }
				}
				add_to_variable = { ROOT.num_expeditionary_divisions_received = expeditionary_battalions_received_temp }
			}
		}
		subtract_from_variable = { num_actual_divisions = num_expeditionary_battalions_received }
		add_to_variable = { num_actual_divisions = num_expeditionary_battalions_sent }

		# calculate the limit
		calculate_maximum_divisions = yes

		# if already suffering penalties, check if they still apply
		if = {
			limit = {
				has_dynamic_modifier = { modifier = mobilisation_penalty_modifier }
			}
			if = {
				limit = {
					OR = {
						is_ai = yes
						has_reached_maximum_divisions = no
					}
				}
				clear_variable = mobilisation_penalty_var
				clear_variable = mobilisation_penalty_var_positive
				remove_dynamic_modifier = { modifier = mobilisation_penalty_modifier }
			}
			else = {
				calculate_mobilisation_penalty = yes
			}
		}
		# if not suffering penalties, check if they should apply
		else_if = {
			limit = {
				is_ai = no
				has_reached_maximum_divisions = yes
				NOT = { has_global_flag = KR_unit_limits_ai_only }
			}
			calculate_mobilisation_penalty = yes
			add_dynamic_modifier = { modifier = mobilisation_penalty_modifier }
		}
	}
}

# Check after a war whether we should be demobilizing
country_event = {
	id = kr.political.120
	immediate = { log = "[GetDateText]: [Root.GetName]: event kr.political.120 FROM = [From.GetName]" }

	hidden = yes
	is_triggered_only = yes

	trigger = {
		is_ai = yes
		has_war = no
	    OR = { 
		    has_unsupported_economic_law_ai = yes 
		    has_unsupported_manpower_law_ai = yes  
	    }  
		NOT = {
			has_global_flag = KR_no_demob
			any_other_country = {
				OR = {
					is_justifying_wargoal_against = ROOT
					has_wargoal_against = ROOT
					ROOT = { is_justifying_wargoal_against = PREV }
					ROOT = { has_wargoal_against = PREV }
				}
			}
		}
	}

	immediate = {
		clr_country_flag = has_demobilization_check
		if = {
			limit = {
			    has_unsupported_economic_law_ai = yes  
			    has_unattained_important_goals = no  
			    OR = {  
				    has_elected_government = yes  
					has_war_support < 0.25
			    }  
			}
			if = {
				limit = { has_idea = tot_economic_mobilisation }
				add_political_power = 200
			}
			else_if = {
				limit = { has_idea = war_economy }
				add_political_power = 150
			}
			else_if = {
				limit = { has_idea = partial_economic_mobilisation }
				add_political_power = 100
			}
			else = {
				add_political_power = 50
			}
			add_ideas = civilian_economy
		}
		else_if = {
			limit = {
			    has_unsupported_economic_law_ai = yes  
			    has_unattained_important_goals = no  
				has_war_support < 0.50
			}
			if = {
				limit = { has_idea = tot_economic_mobilisation }
				add_political_power = 150
			}
			else_if = {
				limit = { has_idea = war_economy }
				add_political_power = 100
			}
			else = {
				add_political_power = 50
			}
			add_ideas = low_economic_mobilisation
		}
		else_if = {
		    limit = { has_unsupported_economic_law_ai = yes }  
			if = {
				limit = { has_idea = tot_economic_mobilisation }
				add_political_power = 100
			}
			else = {
				add_political_power = 50
			}
			add_ideas = partial_economic_mobilisation
		}

		if = {
			limit = {
			    has_unsupported_manpower_law_ai = yes  
			    has_unattained_important_goals = no  
			    OR = {  
					has_war_support < 0.25
			        has_government = democratic #social_liberal ✖ MODDED BY KEN
			       #has_government = market_liberal ✖ MODDED BY KEN 
			       #has_government = social_democrat ✖ MODDED BY KEN  
			    }  
			}
			if = {
				limit = { has_idea = scraping_the_barrel }
				add_political_power = 250
			}
			else_if = {
				limit = { has_idea = all_adults_serve }
				add_political_power = 200
			}
			else_if = {
				limit = { has_idea = service_by_requirement }
				add_political_power = 150
			}
			else_if = {
				limit = { has_idea = extensive_conscription }
				add_political_power = 100
			}
			else = {
				add_political_power = 50
			}
			add_ideas = volunteer_only
		}
		else_if = {
			limit = {
			    has_unsupported_manpower_law_ai = yes  
			    has_unattained_important_goals = no  
			    OR = { 
					has_war_support < 0.50
			        has_elected_government = yes 
			    }  
			}
			if = {
				limit = { has_idea = scraping_the_barrel }
				add_political_power = 200
			}
			else_if = {
				limit = { has_idea = all_adults_serve }
				add_political_power = 150
			}
			else_if = {
				limit = { has_idea = service_by_requirement }
				add_political_power = 100
			}
			else = {
				add_political_power = 50
			}
			add_ideas = limited_conscription
		}
		else_if = {
	        limit = { has_unsupported_manpower_law_ai = yes }  
			if = {
				limit = { has_idea = scraping_the_barrel }
				add_political_power = 150
			}
			else_if = {
				limit = { has_idea = all_adults_serve }
				add_political_power = 100
			}
			else = {
				add_political_power = 50
			}
			add_ideas = extensive_conscription
		}

		if = {
			limit = {
				NOT = { has_country_flag = has_reduce_units_check }
			}
			set_country_flag = has_reduce_units_check
			country_event = { id = kr.political.121 days = 4 random = 96 }
		}
	}
}

# Repeated check for whether an AI country should reduce its number of units
country_event = {
	id = kr.political.121
	immediate = { log = "[GetDateText]: [Root.GetName]: event kr.political.121 FROM = [From.GetName]" }

	hidden = yes
	is_triggered_only = yes

	immediate = {
		calculate_maximum_divisions = yes
		if = {
			limit = {
				is_ai = yes
				has_war = no
				num_divisions > 4
				OR = {
					has_excessive_army_size = yes
				    needs_army_reduction_for_manpower = yes  
					AND = {
						has_reached_maximum_divisions = yes
						NOT = { has_global_flag = KR_no_unit_limits }
					}
				}
				NOT = {
					any_other_country = {
						OR = {
							is_justifying_wargoal_against = ROOT
							has_wargoal_against = ROOT
							ROOT = { is_justifying_wargoal_against = PREV }
							ROOT = { has_wargoal_against = PREV }
						}
					}
				}
			}
			disband_twenty_percent_units = yes
			country_event = { id = kr.political.121 days = 7 random = 72 }
		}
		else = {
			clr_country_flag = has_reduce_units_check
		}
	}
}

country_event = {
	id = prca.1
	title = prca.1.t
	desc = prca.1.d

	picture = GFX_prca_report_event_radio_broadcast

	is_triggered_only = yes

	option = {
		name = prca.1.a
		ai_chance = {
			factor = 0
		}
		custom_effect_tooltip = prca_broadcast_to_warmonger_tt
	}

	option = {
		name = prca.1.b
		ai_chance = {
			factor = 1
		}
		set_country_flag = hostile_broadcast_event_disabled
		custom_effect_tooltip = prca.1.tt
		custom_effect_tooltip = prca_broadcast_to_warmonger_tt
	}
}

# BUFFER EVENT: Since triggers for events are only checked every 20 days,
# there is a small chance that multiple countries activate a leaflet simultaneously.
# This buffer step ensures that all leaflets are activated.
country_event = {
	id = prca.2

	hidden = yes

	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				NOT = { has_country_flag = prca_activate_leaflet }
			}
			set_country_flag = prca_activate_leaflet
			else = {
				country_event = {
					id = prca.2
					days = 10
				}
			}
		}
	}
}

# Mission Generator for Enemy Planes dropping Propaganda Leaflets
country_event = {
	id = prca.3

	hidden = yes

	trigger = {
		prca_use_default_decisions_trigger = yes
		has_country_flag = prca_activate_leaflet
	}

	mean_time_to_happen = { days = 20 }

	immediate = {
		clr_country_flag = prca_activate_leaflet
		country_event = prca.4
		random_list = {
			33 = {
				modifier = {
					factor = 0
					has_active_mission = prca_hostile_leaflet_1
				}
				activate_mission = prca_hostile_leaflet_1
				set_variable = { THIS.frame = 12 }
				THIS_activate_leaflet = yes
				set_variable = { THIS.last_activated_leaflet = 1 }
			}
			33 = {
				modifier = {
					factor = 0
					has_active_mission = prca_hostile_leaflet_2
				}
				activate_mission = prca_hostile_leaflet_2
				set_variable = { THIS.frame = 13 }
				THIS_activate_leaflet = yes
				set_variable = { THIS.last_activated_leaflet = 2 }
			}
			33 = {
				modifier = {
					factor = 0
					has_active_mission = prca_hostile_leaflet_3
				}
				activate_mission = prca_hostile_leaflet_3
				set_variable = { THIS.frame = 14 }
				THIS_activate_leaflet = yes
				set_variable = { THIS.last_activated_leaflet = 3 }
			}
		}
	}
}

# Enemy Planes dropping Propaganda Leaflets
country_event = {
	id = prca.4
	title = prca.4.t
	desc = prca.4.d

	picture = GFX_prca_report_event_airplane_leaflets

	is_triggered_only = yes

	option = {
		name = prca.4.a
		custom_effect_tooltip = prca.4.tt
	}
	option = {
		name = prca.4.b
		custom_effect_tooltip = prca.4.tt
		if = {
			limit = { has_idea = raid_on_leaflet_bombers }
			remove_ideas = raid_on_leaflet_bombers
		}
		if = {
			limit = { has_idea = police_action_on_leaflets }
			modify_timed_idea = {
				idea = police_action_on_leaflets
				days = 120
			}
			else = {
				add_timed_idea = {
					idea = police_action_on_leaflets
					days = 120
				}
			}
		}
		add_political_power = -50
	}
	option = {
		name = prca.4.c
		custom_effect_tooltip = prca.4.tt
		if = {
			limit = { has_idea = police_action_on_leaflets }
			remove_ideas = police_action_on_leaflets
		}
		if = {
			limit = { has_idea = raid_on_leaflet_bombers }
			modify_timed_idea = {
				idea = raid_on_leaflet_bombers
				days = 120
			}
			else = {
				add_timed_idea = {
					idea = raid_on_leaflet_bombers
					days = 120
				}
			}
		}
		add_political_power = -50
	}
}

# Warning: Outdated submod detected
country_event = {
	id = prca.5
	title = prca.5.t
	desc = prca.5.d

	picture = GFX_report_event_generic_rally2

	is_triggered_only = yes
	major = yes

	option = {
		name = prca.5.a
		ai_chance = {
			factor = 1
		}
	}
	option = {
		name = prca.5.b
		ai_chance = {
			factor = 0
		}
		ROOT = {
			custom_effect_tooltip = prca.5.tt
			clr_country_flag = prca_enabled
		}
	}
}

# DISPATCH EVENT: Calls an event for each country running on an outdated
# Propaganda Campaign version. This is intended to inform the player
# (and the developer) about any potential issues. Currently not used,
# since all versions from 0.8.1 on are still compatible.
country_event = {
		id = prca.6

		hidden = yes

		fire_only_once = yes
		is_triggered_only = yes

		immediate = {
				every_country = {
						limit = {
							AND = {
								always = no
								# has_country_flag = prca_enabled
								# NOT = { is_major = yes }
								# NOT = { has_country_flag = prca_v0.8.2 }
							}
						}
						country_event = prca.5
				}
		}
}

# Debug Event to reset propaganda
country_event = {
		id = prca.7
		hidden = yes
		is_triggered_only = yes
		immediate = {
				THIS_reset_propaganda = yes
		}
}
