﻿namespace = caciquismo

# KAI: Disable election rigging for AI.
# Vanilla AI rigs election randomly, which causes democracies to lose legitimacy for no reason.
# I'm not against rigging elections strategically, but scripting it in is too much work.
caciquismo.1 = { # For Electoral Clientelism
	type = country_event
	placement = ROOT

	title = caciquismo.1.t
	desc = caciquismo.1.d
	flavor = caciquismo.1.f

	event_image = {
		video = "southamerica_election"
	}

	on_created_soundeffect = "event:/SFX/UI/Alerts/event_appear"

	icon = "gfx/interface/icons/event_icons/event_election.dds"

	duration = 3

	trigger = {
		NOT = { has_journal_entry = je_coffee_with_milk }
		in_election_campaign = yes
		NOT = { has_law_or_variant = law_type:law_single_party_state }
		any_active_party = {
			party_has_members = yes
			count > 1
		}
		modifier:country_forbid_electoral_fraud_bool = no
		owner.active_law:lawgroup_distribution_of_power ?= {
			has_amendment = amendment_type:amendment_electoral_clientelism
		}
	}

	immediate = {
		handle_possible_absent_caciquismo_variables = yes
		active_law:lawgroup_distribution_of_power ?= {
			save_scope_as = election_law_scope
		}
		ordered_active_party = {
			limit = {
				party_has_members = yes
			}
			order_by = momentum
			position = 0
			save_scope_as = party_1_scope
		}
		ordered_active_party = {
			limit = {
				party_has_members = yes
				NOT = {
					scope:party_1_scope ?= this
				}
			}
			order_by = momentum
			position = 0
			check_range_bounds = no
			save_scope_as = party_2_scope
		}
		ordered_active_party = {
			limit = {
				party_has_members = yes
				NOR = {
					scope:party_1_scope ?= this
					scope:party_2_scope ?= this
				}
			}
			order_by = momentum
			position = 0
			check_range_bounds = no
			save_scope_as = party_3_scope
		}
		ordered_active_party = {
			limit = {
				party_has_members = yes
				NOR = {
					scope:party_1_scope ?= this
					scope:party_2_scope ?= this
					scope:party_3_scope ?= this
				}
			}
			order_by = momentum
			position = 0
			check_range_bounds = no
			save_scope_as = party_4_scope
		}
	}

	cancellation_trigger = {
		OR = {
			in_election_campaign = no
			NOT = {
				modifier:country_forbid_electoral_fraud_bool = no
			}
		}
	}

	option = {
		name = caciquismo.1.a
		ai_chance = {
			base = 0
		}
		scope:party_1_scope ?= {
			add_momentum = momentum_utterly_fraudulent
		}
		every_active_party = {
			limit = {
				party_has_members = yes
				NOT = {
					scope:party_1_scope ?= this
				}
			}
			add_momentum = momentum_very_large_decrease
		}
		add_caciquismo_effect = yes
		custom_tooltip = {
			text = mutes_all_election_events_tt
			set_variable = {
				name = mute_election_events
				months = 12
			}
		}
	}

	option = {
		name = caciquismo.1.b
		ai_chance = {
			base = 0
		}
		trigger = {
			exists = scope:party_2_scope
		}
		scope:party_2_scope ?= {
			add_momentum = momentum_utterly_fraudulent
		}
		every_active_party = {
			limit = {
				party_has_members = yes
				NOT = {
					scope:party_2_scope ?= this
				}
			}
			add_momentum = momentum_very_large_decrease
		}
		add_caciquismo_effect = yes
		custom_tooltip = {
			text = mutes_all_election_events_tt
			set_variable = {
				name = mute_election_events
				months = 12
			}
		}
	}

	option = {
		name = caciquismo.1.c
		ai_chance = {
			base = 0
		}
		trigger = {
			exists = scope:party_3_scope
		}
		scope:party_3_scope ?= {
			add_momentum = momentum_utterly_fraudulent
		}
		every_active_party = {
			limit = {
				party_has_members = yes
				NOT = {
					scope:party_3_scope ?= this
				}
			}
			add_momentum = momentum_very_large_decrease
		}
		add_caciquismo_effect = yes
		custom_tooltip = {
			text = mutes_all_election_events_tt
			set_variable = {
				name = mute_election_events
				months = 12
			}
		}
	}

	option = {
		name = caciquismo.1.e
		ai_chance = {
			base = 0
		}
		trigger = {
			exists = scope:party_4_scope
		}
		scope:party_4_scope ?= {
			add_momentum = momentum_utterly_fraudulent
		}
		every_active_party = {
			limit = {
				party_has_members = yes
				NOT = {
					scope:party_4_scope ?= this
				}
			}
			add_momentum = momentum_very_large_decrease
		}
		add_caciquismo_effect = yes
		custom_tooltip = {
			text = mutes_all_election_events_tt
			set_variable = {
				name = mute_election_events
				months = 12
			}
		}
	}

	option = { # Let's not do this
		name = caciquismo.1.g
		ai_chance = { # The AI will try to avoid a legitimacy death spiral
			base = 20
			modifier = {
				trigger = {
					ROOT.electoral_confidence > 0.90
				}
				add = -2
			}
			modifier = {
				trigger = {
					ROOT.electoral_confidence > 0.70
				}
				add = -2
			}
			modifier = {
				trigger = {
					ROOT.electoral_confidence > 0.50
				}
				add = -2
			}
			modifier = {
				trigger = {
					ROOT.electoral_confidence > 0.30
				}
				add = -2
			}
		}
		trigger = {
			custom_tooltip = {
				text = caciquismo_cannot_be_abolished_tt
				var:electoral_fraud_abolish_var < 5
			}
		}
		highlighted_option = yes
		default_option = yes
		if = {
			limit = {
				var:electoral_fraud_abolish_var = 4
			}
			custom_tooltip = can_abolish_caciquismo_next_election_tt
		}
		subtract_caciquismo_effect = yes
	}

	option = { # Let's not do this - abolish
		name = caciquismo.1.h
		ai_chance = {
			base = 20
		}
		trigger = {
			custom_tooltip = {
				text = caciquismo_can_be_abolished_tt
				var:electoral_fraud_abolish_var >= 5
			}
		}
		highlighted_option = yes
		default_option = yes
		remove_electoral_fraud_effect = yes
	}
}

caciquismo.2 = { # Generic electoral fraud
	type = country_event
	placement = ROOT

	title = caciquismo.2.t
	desc = caciquismo.2.d
	flavor = caciquismo.2.f

	event_image = {
		video = "southamerica_election"
	}

	on_created_soundeffect = "event:/SFX/UI/Alerts/event_appear"

	icon = "gfx/interface/icons/event_icons/event_election.dds"

	duration = 3

	trigger = {
		NOT = { has_journal_entry = je_coffee_with_milk }
		in_election_campaign = yes
		NOT = { has_law_or_variant = law_type:law_single_party_state }
		any_active_party = {
			party_has_members = yes
			count > 1
		}
		modifier:country_forbid_electoral_fraud_bool = no
		NOT = {
			owner.active_law:lawgroup_distribution_of_power ?= {
				has_amendment = amendment_type:amendment_electoral_clientelism
			}
		}
	}

	immediate = {
		handle_possible_absent_caciquismo_variables = yes
		active_law:lawgroup_distribution_of_power ?= {
			save_scope_as = election_law_scope
		}
		ordered_active_party = {
			limit = {
				party_has_members = yes
				any_member = {
					is_in_government = yes
				}
			}
			order_by = momentum
			position = 0
			save_scope_as = party_1_scope
		}
		ordered_active_party = {
			limit = {
				party_has_members = yes
				NOT = {
					scope:party_1_scope ?= this
				}
			}
			order_by = momentum
			position = 0
			check_range_bounds = no
			save_scope_as = party_2_scope
		}
		ordered_active_party = {
			limit = {
				party_has_members = yes
				NOT = {
					any_member = {
						is_in_government = yes
					}
				}
				NOR = {
					scope:party_1_scope ?= this
					scope:party_2_scope ?= this
				}
			}
			order_by = momentum
			position = 0
			save_scope_as = party_3_scope
		}
		ordered_active_party = {
			limit = {
				party_has_members = yes
				NOR = {
					scope:party_1_scope ?= this
					scope:party_2_scope ?= this
					scope:party_3_scope ?= this
				}
			}
			order_by = momentum
			position = 0
			check_range_bounds = no
			save_scope_as = party_4_scope
		}
		ordered_active_party = {
			limit = {
				party_has_members = yes
				NOR = {
					scope:party_1_scope ?= this
					scope:party_2_scope ?= this
					scope:party_3_scope ?= this
					scope:party_4_scope ?= this
				}
			}
			order_by = momentum
			position = 0
			check_range_bounds = no
			save_scope_as = party_5_scope
		}
		ordered_scope_state = {
			limit = {
				is_incorporated = yes
			}
			order_by = "pop_type_percent_state(pop_type:peasants)"
			position = 0
			check_range_bounds = no
			save_scope_as = rural_state
		}
	}

	cancellation_trigger = {
		OR = {
			in_election_campaign = no
			NOT = {
				modifier:country_forbid_electoral_fraud_bool = no
			}
		}
	}

	option = {
		name = caciquismo.2.a
		ai_chance = {
			base = 0
		}
		trigger = {
			exists = scope:party_1_scope
		}
		scope:party_1_scope ?= {
			add_momentum = momentum_utterly_fraudulent
		}
		every_active_party = {
			limit = {
				party_has_members = yes
				NOT = {
					scope:party_1_scope ?= this
				}
			}
			add_momentum = momentum_very_large_decrease
		}
		add_caciquismo_effect = yes
		custom_tooltip = {
			text = mutes_all_election_events_tt
			set_variable = {
				name = mute_election_events
				months = 12
			}
		}
	}

	option = {
		name = caciquismo.2.b
		ai_chance = {
			base = 0
		}
		trigger = {
			exists = scope:party_2_scope
		}
		scope:party_2_scope ?= {
			add_momentum = momentum_utterly_fraudulent
		}
		every_active_party = {
			limit = {
				party_has_members = yes
				NOT = {
					scope:party_2_scope ?= this
				}
			}
			add_momentum = momentum_very_large_decrease
		}
		add_caciquismo_effect = yes
		custom_tooltip = {
			text = mutes_all_election_events_tt
			set_variable = {
				name = mute_election_events
				months = 12
			}
		}
	}

	option = {
		name = caciquismo.2.c
		ai_chance = {
			base = 0
		}
		trigger = {
			exists = scope:party_3_scope
		}
		scope:party_3_scope ?= {
			add_momentum = momentum_utterly_fraudulent
		}
		every_active_party = {
			limit = {
				party_has_members = yes
				NOT = {
					scope:party_3_scope ?= this
				}
			}
			add_momentum = momentum_very_large_decrease
		}
		add_caciquismo_effect = yes
		custom_tooltip = {
			text = mutes_all_election_events_tt
			set_variable = {
				name = mute_election_events
				months = 12
			}
		}
	}

	option = {
		name = caciquismo.2.e
		ai_chance = {
			base = 0
		}
		trigger = {
			exists = scope:party_4_scope
		}
		scope:party_4_scope ?= {
			add_momentum = momentum_utterly_fraudulent
		}
		every_active_party = {
			limit = {
				party_has_members = yes
				NOT = {
					scope:party_4_scope ?= this
				}
			}
			add_momentum = momentum_very_large_decrease
		}
		add_caciquismo_effect = yes
		custom_tooltip = {
			text = mutes_all_election_events_tt
			set_variable = {
				name = mute_election_events
				months = 12
			}
		}
	}

	option = {
		name = caciquismo.2.g
		ai_chance = {
			base = 0
		}
		trigger = {
			exists = scope:party_5_scope
		}
		scope:party_5_scope ?= {
			add_momentum = momentum_utterly_fraudulent
		}
		every_active_party = {
			limit = {
				party_has_members = yes
				NOT = {
					scope:party_5_scope ?= this
				}
			}
			add_momentum = momentum_very_large_decrease
		}
		add_caciquismo_effect = yes
		custom_tooltip = {
			text = mutes_all_election_events_tt
			set_variable = {
				name = mute_election_events
				months = 12
			}
		}
	}

	option = { # Let's not do this
		name = caciquismo.2.h
		ai_chance = { # The AI will try to avoid a legitimacy death spiral
			base = 20
			modifier = {
				trigger = {
					ROOT.electoral_confidence > 0.90
				}
				add = -2
			}
			modifier = {
				trigger = {
					ROOT.electoral_confidence > 0.70
				}
				add = -2
			}
			modifier = {
				trigger = {
					ROOT.electoral_confidence > 0.50
				}
				add = -2
			}
			modifier = {
				trigger = {
					ROOT.electoral_confidence > 0.30
				}
				add = -2
			}
		}
		trigger = {
			custom_tooltip = {
				text = caciquismo_cannot_be_abolished_tt
				var:electoral_fraud_abolish_var < 5
			}
		}
		highlighted_option = yes
		default_option = yes
		if = {
			limit = {
				var:electoral_fraud_abolish_var = 4
			}
			custom_tooltip = can_abolish_caciquismo_next_election_tt
		}
		subtract_caciquismo_effect = yes
	}

	option = { # Let's not do this - abolish
		name = caciquismo.2.i
		ai_chance = {
			base = 20
		}
		trigger = {
			custom_tooltip = {
				text = caciquismo_can_be_abolished_tt
				var:electoral_fraud_abolish_var >= 5
			}
		}
		highlighted_option = yes
		default_option = yes
		remove_electoral_fraud_effect = yes
	}
}
