﻿je_peru_bolivia = {
	icon = "gfx/interface/icons/event_icons/waving_flag.dds"

	group = je_group_historical_content

	immediate = {
		cu:quechua = {
			save_scope_as = quechua_culture
		}
		cu:south_andean = {
			save_scope_as = south_andean_culture
		}
		c:BOL = {
			save_scope_as = bolivia_scope
		}
		set_variable = pbc_member
	}
	on_monthly_pulse = {
		effect = {
			if = {
				limit = {
					c:BOL ?= this
					government_legitimacy >= 50
				}
				change_global_variable = {
					name = peru_bolivia_var
					add = 1
				}
			}
		}
	}

	complete = {
		hidden_trigger = {
			c:BOL = this
		}
		scope:journal_entry = { is_goal_complete = yes }
		OR = {
			c:SPU ?= {
				is_subject_of = c:BOL
			}
			c:NPU ?= {
				is_subject_of = c:BOL
			}
			c:IQU ?= {
				is_subject_of = c:BOL
			}
			c:PEU ?= {
				is_subject_of = c:BOL
			}
		}
		custom_tooltip = {
			text = bol_no_revolutions_tt
			NOT = {
				any_country = {
					is_subject_of = c:BOL
					OR = {
						country_has_primary_culture = cu:south_andean
						country_has_primary_culture = cu:quechua
						country_has_primary_culture = cu:peruvian
					}
					has_revolution = yes
				}
			}
		}
	}

	on_complete = {
		custom_tooltip = {
			text = peru_bolivia_completion_nonbolivia_tt
			if = {
				limit = {
					c:BOL ?= this
				}
				trigger_event = {
					id = peru_bolivia_events.1
				}
			}
		}
	}

	is_progressing = {
		c:BOL ?= {
			government_legitimacy >= 50
		}
		OR = {
			c:SPU ?= {
				is_subject_of = c:BOL
			}
			c:NPU ?= {
				is_subject_of = c:BOL
			}
			c:IQU ?= {
				is_subject_of = c:BOL
			}
			c:PEU ?= {
				is_subject_of = c:BOL
			}
		}
		NOT = {
			any_country = {
				is_subject_of = c:BOL
				has_revolution = yes
			}
		}
	}

	fail = {
		OR = {
			NOT = {
				exists = c:BOL
			}
			AND = {
				OR = {
					NOT = {
						exists = c:SPU
					}
					c:SPU ?= {
						NOT = { is_subject_of = c:BOL }
					}
				}
				OR = {
					NOT = {
						exists = c:NPU
					}
					c:NPU ?= {
						NOT = { is_subject_of = c:BOL }
					}
				}
				OR = {
					NOT = {
						exists = c:IQU
					}
					c:IQU ?= {
						NOT = { is_subject_of = c:BOL }
					}
				}
				OR = {
					NOT = {
						exists = c:PEU
					}
					c:PEU ?= {
						NOT = { is_subject_of = c:BOL }
					}
				}
			}
			NOT = {
				country_has_primary_culture = cu:south_andean
			}
			c:BOL ?= {
				is_subject = yes
			}
		}
	}

	on_fail = {
		if = { #Why? So that [somehow] this doesn't cause four fail events to fire at once. That should be impossible, but you never know.
			limit = {
				c:BOL ?= this
			} 
			if = {
				limit = {
					any_country = {
						has_variable = pbc_member
						is_player = yes
					}
				}
				random_country = {
					limit = {
						has_variable = pbc_member
						is_player = yes
					}
					trigger_event = { id = peru_bolivia_events.3 popup = yes }
				}
			}
			else = {
				ordered_country = {
					limit = {
						has_variable = pbc_member
					}
					order_by = gdp
					trigger_event = { id = peru_bolivia_events.3 popup = yes }
				}
			}
		}
		else_if = {
			limit = {
				NOT = {
					exists = c:BOL
				}
			}
			if = {
				limit = {
					any_country = {
						has_variable = pbc_member
						is_player = yes
					}
				}
				random_country = {
					limit = {
						has_variable = pbc_member
						is_player = yes
					}
					trigger_event = { id = peru_bolivia_events.3 popup = yes }
				}
			}
			else = {
				ordered_country = {
					limit = {
						has_variable = pbc_member
					}
					order_by = gdp
					trigger_event = { id = peru_bolivia_events.3 popup = yes }
				}
			}
		}
	}

	invalid = {
		OR = {
			AND = {
				OR = {
					c:IQU ?= this
					c:SPU ?= this
					c:NPU ?= this
					c:PEU ?= this
				}
				NOT = {
					is_subject_of = c:BOL
				}
			}
			NOT = {
				exists = c:BOL
			}
			AND = {
				exists = c:BOL 
				c:BOL = {
					NOT = {
						has_journal_entry = je_peru_bolivia
					}
				}
			}
		}
	}

	current_value = {
		value = global_var:peru_bolivia_var #This variable is initialised in Argentina's country file, because of load orders.
	}

	goal_add_value = {
		value = 120
	}

	progressbar = yes

	weight = 10000

	should_be_pinned_by_default = yes
}

je_war_of_the_confederation = {
	icon = "gfx/interface/icons/event_icons/event_military.dds"

	group = je_group_historical_content

	immediate = {
		cu:quechua = {
			save_scope_as = quechua_culture
		}
		cu:south_andean = {
			save_scope_as = south_andean_culture
		}
		c:BOL = {
			save_scope_as = bolivia_scope
		}
		root = {
			save_scope_as = home_country_scope
		}
	}

	on_weekly_pulse = {
		effect = {
			if = {
				limit = {
					exists = global_var:peru_bolivia_var
				}
				set_variable = { name = peru_bolivia_opposition_var value = global_var:peru_bolivia_var }
			}
		}
	}

	complete = {
		OR = {
			custom_tooltip = {
				text = peru_bolivia_dead_tt
				NOT = {
					exists = c:PBC
				}
				OR = {
					NOT = {
						exists = c:BOL
					}
					AND = {
						OR = {
							NOT = {
								exists = c:SPU
							}
							c:SPU ?= {
								NOT = { is_subject_of = c:BOL }
							}
						}
						OR = {
							NOT = {
								exists = c:NPU
							}
							c:NPU ?= {
								NOT = { is_subject_of = c:BOL }
							}
						}
						OR = {
							NOT = {
								exists = c:IQU
							}
							c:IQU ?= {
								NOT = { is_subject_of = c:BOL }
							}
						}
						OR = {
							NOT = {
								exists = c:PEU
							}
							c:PEU ?= {
								NOT = { is_subject_of = c:BOL }
							}
						}
					}
					c:BOL ?= {
						is_subject = yes
					}
				}
			}
			AND = {
				c:BOL ?= {
					relations:root >= relations_threshold:amicable
				}
				c:BOL ?= {
					OR = {
						is_in_customs_union_with = root
						has_diplomatic_pact = {
							who = root
							type = alliance
						}
						has_diplomatic_pact = {
							who = root
							type = defensive_pact
						}								
					}
				}
			}
		}
	}

	on_complete = {
		custom_tooltip = {
			text = peru_bolivia_complete_tt
			if = {
				limit = {
					AND = {
						c:BOL ?= {
							relations:root >= relations_threshold:amicable
						}
						c:BOL ?= {
							OR = {
								is_in_customs_union_with = root
								has_diplomatic_pact = {
									who = root
									type = alliance
								}
								has_diplomatic_pact = {
									who = root
									type = defensive_pact
								}								
							}
						}
					}
				}
				trigger_event = { id = peru_bolivia_events.6 popup = yes }
			}
			else = {
				trigger_event = { id = peru_bolivia_events.5 popup = yes }
			}
		}
	}

	fail = {
		custom_tooltip = {
			text = peru_bolivia_success_tt
			exists = c:PBC
		}
	}

	on_fail = {
		trigger_event = { id = peru_bolivia_events.7 popup = yes }
	}

	invalid = {
		OR = {
			AND = {
				OR = {
					c:IQU ?= this
					c:SPU ?= this
					c:NPU ?= this
					c:PEU ?= this
				}
				NOT = {
					is_subject_of = c:BOL
				}
			}
			NOR = {
				exists = c:BOL
				exists = c:PBC
			}
			AND = {
				NOT = {
					exists = c:PBC
				}
				c:BOL = {
					NOT = {
						has_journal_entry = je_peru_bolivia
					}
				}
			}
		}
	}

	current_value = {
		value = var:peru_bolivia_opposition_var
	}

	goal_add_value = {
		value = 120
	}

	progressbar = yes

	weight = 10000

	should_be_pinned_by_default = yes
}