﻿je_uneasy_raj = {
	icon = "gfx/interface/icons/event_icons/event_protest.dds"

	group = je_group_historical_content

	scripted_progress_bar = sepoy_mutiny_progress_bar_bengal
	scripted_progress_bar = sepoy_mutiny_progress_bar_bombay
	scripted_progress_bar = sepoy_mutiny_progress_bar_madras

	immediate = {
		sr:region_bengal = {
			save_scope_as = bengal_sr_scope
		}
		sr:region_bombay = {
			save_scope_as = bombay_sr_scope
		}
		sr:region_madras = {
			save_scope_as = madras_sr_scope
		}
		sr:region_punjab = {
			save_scope_as = punjab_sr_scope
		}
		sr:region_central_india = {
			save_scope_as = central_india_sr_scope
		}
		set_variable = {
			name = sepoy_mutiny_timer_var
			value = 0
		}
	}

	complete = {
		custom_tooltip = {
			text = je_avert_sepoy_mutiny_tt_1
			has_variable = consolidated_colonial_rule
		}
		root.average_sol >= 10
		radical_fraction = {
			value <= 0.10
		}
	}

	fail = {
		custom_tooltip = {
			text = je_uneasy_raj_tt_2
			var:sepoy_mutiny_timer_var >= 100
		}		
	}

	timeout = 9000

	on_timeout = {
		#trigger_event = { id = sepoy_mutiny_events.3 popup = yes }
		add_journal_entry = { type = je_avert_sepoy_mutiny }
		# Bengal Presidency
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bengal)" <= uneasy_raj_stability_low_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bengal_effect_critical
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bengal)" > uneasy_raj_stability_low_lower_bound
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bengal)" <= uneasy_raj_stability_medium_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bengal_effect_high
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bengal)" > uneasy_raj_stability_medium_lower_bound
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bengal)" <= uneasy_raj_stability_high_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bengal_effect_medium
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bengal)" > uneasy_raj_stability_high_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bengal_effect_low
		}
		# Bombay Presidency
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bombay)" <= uneasy_raj_stability_low_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bombay_effect_critical
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bombay)" > uneasy_raj_stability_low_lower_bound
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bombay)" <= uneasy_raj_stability_medium_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bombay_effect_high
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bombay)" > uneasy_raj_stability_medium_lower_bound
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bombay)" <= uneasy_raj_stability_high_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bombay_effect_medium
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bombay)" > uneasy_raj_stability_high_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bombay_effect_low
		}
		# Madras Presidency
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_madras)" <= uneasy_raj_stability_low_lower_bound
				}
			}
			set_variable = sepoy_mutiny_madras_effect_critical
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_madras)" > uneasy_raj_stability_low_lower_bound
					"scripted_bar_progress(sepoy_mutiny_progress_bar_madras)" <= uneasy_raj_stability_medium_lower_bound
				}
			}
			set_variable = sepoy_mutiny_madras_effect_high
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_madras)" > uneasy_raj_stability_medium_lower_bound
					"scripted_bar_progress(sepoy_mutiny_progress_bar_madras)" <= uneasy_raj_stability_high_lower_bound
				}
			}
			set_variable = sepoy_mutiny_madras_effect_medium
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_madras)" > uneasy_raj_stability_high_lower_bound
				}
			}
			set_variable = sepoy_mutiny_madras_effect_low
		}		
	}

	invalid = {
	}

	on_complete = {
		hidden_effect = {
			set_variable_variable = easy_raj_var
			}
	}

	on_fail = {
		#trigger_event = { id = sepoy_mutiny_events.3 popup = yes }
		add_journal_entry = { type = je_avert_sepoy_mutiny }
		# Bengal Presidency
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bengal)" <= uneasy_raj_stability_low_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bengal_effect_critical
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bengal)" > uneasy_raj_stability_low_lower_bound
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bengal)" <= uneasy_raj_stability_medium_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bengal_effect_high
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bengal)" > uneasy_raj_stability_medium_lower_bound
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bengal)" <= uneasy_raj_stability_high_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bengal_effect_medium
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bengal)" > uneasy_raj_stability_high_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bengal_effect_low
		}
		# Bombay Presidency
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bombay)" <= uneasy_raj_stability_low_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bombay_effect_critical
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bombay)" > uneasy_raj_stability_low_lower_bound
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bombay)" <= uneasy_raj_stability_medium_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bombay_effect_high
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bombay)" > uneasy_raj_stability_medium_lower_bound
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bombay)" <= uneasy_raj_stability_high_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bombay_effect_medium
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_bombay)" > uneasy_raj_stability_high_lower_bound
				}
			}
			set_variable = sepoy_mutiny_bombay_effect_low
		}
		# Madras Presidency
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_madras)" <= uneasy_raj_stability_low_lower_bound
				}
			}
			set_variable = sepoy_mutiny_madras_effect_critical
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_madras)" > uneasy_raj_stability_low_lower_bound
					"scripted_bar_progress(sepoy_mutiny_progress_bar_madras)" <= uneasy_raj_stability_medium_lower_bound
				}
			}
			set_variable = sepoy_mutiny_madras_effect_high
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_madras)" > uneasy_raj_stability_medium_lower_bound
					"scripted_bar_progress(sepoy_mutiny_progress_bar_madras)" <= uneasy_raj_stability_high_lower_bound
				}
			}
			set_variable = sepoy_mutiny_madras_effect_medium
		}
		if = {
			limit = {
				je:je_uneasy_raj ?= { 
					"scripted_bar_progress(sepoy_mutiny_progress_bar_madras)" > uneasy_raj_stability_high_lower_bound
				}
			}
			set_variable = sepoy_mutiny_madras_effect_low
		}
	}

	on_weekly_pulse = {
		events = {
		}
		effect = {
		}
	}

	on_monthly_pulse = {
		effect = {
			if = {
				limit = {
					OR = {
						je:je_uneasy_raj ?= { 
							"scripted_bar_progress(sepoy_mutiny_progress_bar_bengal)" < uneasy_raj_stability_medium_upper_bound
						}
						je:je_uneasy_raj ?= { 
							"scripted_bar_progress(sepoy_mutiny_progress_bar_bombay)" < uneasy_raj_stability_medium_upper_bound
						}
						je:je_uneasy_raj ?= { 
							"scripted_bar_progress(sepoy_mutiny_progress_bar_madras)" < uneasy_raj_stability_medium_upper_bound
						}
					}
				}
				change_variable = {
					name = sepoy_mutiny_timer_var
					add = 1
				}
			}
		}
		random_events = {
			11 = 0
			1 = sepoy_mutiny_events.6
			1 = raj.4
			1 = raj.5
		}
	}

	weight = 100
	should_be_pinned_by_default = yes
}

je_uneasy_raj_tracker = {
	icon = "gfx/interface/icons/event_icons/event_protest.dds"

	group = je_group_historical_content

	scripted_progress_bar = sepoy_mutiny_progress_bar_bengal
	scripted_progress_bar = sepoy_mutiny_progress_bar_bombay
	scripted_progress_bar = sepoy_mutiny_progress_bar_madras

	is_shown_when_inactive = {
		is_princely_state = yes
        country_is_in_india = yes
		c:BIC ?= {
			has_journal_entry = je_uneasy_raj
		}
	}

	possible = {
		is_princely_state = yes
        is_subject = yes
        country_is_in_india = yes
	}

	immediate = {
		c:BIC = {
			save_scope_as = british_india_scope
		}
		sr:region_bengal = {
			save_scope_as = bengal_sr_scope
		}
		sr:region_bombay = {
			save_scope_as = bombay_sr_scope
		}
		sr:region_madras = {
			save_scope_as = madras_sr_scope
		}
		sr:region_punjab = {
			save_scope_as = punjab_sr_scope
		}
		sr:region_central_india = {
			save_scope_as = central_india_sr_scope
		}
	}

	complete = {
		c:BIC ?= {
			NOT = {
				has_journal_entry = je_uneasy_raj
			}
		}
	}

	timeout = 9000

	invalid = {
		NOT = {
			exists = c:BIC
		}
	}

	weight = 100
	should_be_pinned_by_default = yes
}

je_avert_sepoy_mutiny = {
	icon = "gfx/interface/icons/event_icons/event_protest.dds"
	group = je_group_historical_content
	
	is_shown_when_inactive = {
				exists = c:BIC
				c:BIC = THIS
	}

	possible = {
		OR = {
		has_technology_researched = rifling
		NOT = {
		has_journal_entry = je_uneasy_raj
			}
		}
	}
	
	immediate = {
		set_variable = { name = indian_states_radicals_total_bar value = 0 }
		
		every_country = {
			limit = {
				OR = {
				has_interest_marker_in_region = region_north_india
				has_interest_marker_in_region = region_south_india
				}
				NOT = {
					THIS = ROOT
				}
			}
			post_notification = bic_sepoy_mutiny_rising
		}
		trigger_event = { id = raj.3 popup = yes }
	}
	
	modifiers_while_active = {
		bic_cultural_unrest_modifier
	}
	
	on_weekly_pulse = {
		
		effect = {
			do_sepoy_math = yes
		}
	}
	
	on_monthly_pulse = {
		random_events = {
			100 = 0
			3 = raj.4 #Overseas Service
			3 = raj.5 #Suspected Conversion Project
			3 = raj.65 #Foreign Service Pay
			3 = raj.66 #Promotion then Retirement
			3 = raj.67 #Replacing Religion
			3 = raj.68 #Hindu Widows' Remarriage Act
			3 = raj.69 #At Artillery Point
			3 = raj.70 #Mangal Pandey
			3 = raj.71 #Arson
			25 = raj.72 #Drill or Jail
			}
		effect = {
			do_sepoy_math = yes
			if = {
				limit = {
					NOT = {
					has_variable = sepoy_hold_var
					}
				}
				set_variable = sepoy_hold_var				
			}
		}
			
	}
		
	complete = {				
			OR = {
			has_modifier = utilitarian_bonus
			AND = {
				NOT = {
				has_modifier = sepoy_modifier
				}
				custom_tooltip = {
					text = uf_doctrine_of_lapse_is_disabled_tt
					has_variable = doctrine_of_lapse_ended
					} 
				}
			}			
	}
	
	on_complete = {
		add_loyalists = {
		pop_type = soldiers
		value = 0.25
		}	
		add_loyalists = {
		pop_type = officers
		value = 0.25
		}
	}

	fail = {
		custom_tooltip = {
			text = historical_tt
			has_variable = sepoy_hold_var
			}
		
		OR = {
		scope:journal_entry = {
			is_goal_complete = yes  
			}
		custom_tooltip = {
			text = punjab_attacking_tt
			has_variable = punjab_attacking_var
			}
		ig:ig_armed_forces = {
			ig_approval <= -10
		}
		has_any_secessionists_broken_out = yes
		any_scope_state = {

				OR = {
					region = sr:region_bengal
		    		region = sr:region_bombay
					region = sr:region_madras
				}
				turmoil > 0.4
			}
		}
	}

	#invalid = {
		#NOT = {
		#	owns_entire_state_region = STATE_DELHI
		#}
	#}

	on_fail = {
		custom_tooltip = historical_tt
		
		if = {
			limit = {
			NOT = {
				OR = {
					has_variable = punjab_attacking_var
					has_variable = meerut_rebels
					}
				}
			}
		trigger_event = { id = sepoy_mutiny_events.3 popup = yes }
		}
	add_journal_entry = { type = je_sepoy_mutiny }
	}
	
	invalid = {
		hidden_effect = {
		OR = {
		has_variable = meerut_rebels
		has_variable_variable = easy_raj_var
			}
		}
	}
	
	current_value = {
		value = root.var:indian_states_radicals_total_bar
	}
	
	goal_add_value = {
		add = 25
	}
	
	progressbar = yes

	#timeout = 7300

	weight = 100
	
	should_be_pinned_by_default = yes
}

je_sepoy_mutiny = {
	icon = "gfx/interface/icons/event_icons/event_military.dds"
	group = je_group_historical_content
		
	immediate = {
		every_country = {
			limit = {
				OR = {
				has_interest_marker_in_region = region_north_india
				has_interest_marker_in_region = region_south_india
				}
				NOT = {
					THIS = ROOT
				}
			}
			post_notification = bic_sepoy_mutiny_explosion
		}
			random_diplomatic_play = {
			limit = {
				any_scope_play_involved = {
					
						this = c:BIC
					
				}
				target = {
					c:MUG ?= this
				}
			}
			save_scope_as = mug_diplo_play
			}
			random_diplomatic_play = {
			limit = {
				any_scope_play_involved = {
					
						this = c:BIC
					
				}
				target = {
					c:PAN ?= this
				}
			}
			save_scope_as = pan_diplo_play
			}
			random_diplomatic_play = {
			limit = {
				any_scope_play_involved = {
					
						this = c:BIC
					
				}
				target = {
					c:MAR ?= this
				}
			}
			save_scope_as = mar_diplo_play
			}
		}
	
	complete = {
		trigger_if = {
			limit = { exists = c:MUG }
			c:MUG = { is_country_alive = no }
		}
		trigger_else_if = {
			limit = {
			scope:pan_diplo_play = {
				target = {
					c:PAN ?= this
					}
				}
			}
			c:PAN = { is_country_alive = no }
			}
		trigger_else_if = {
			limit = { exists = c:MAR }
			c:MAR = { is_country_alive = no }
			}
			
		
		highest_secession_progress < 0.3
		has_any_secessionists_broken_out = no
		is_at_war = no
	}

	on_complete = {
		custom_tooltip = historical_tt
		
		hidden_effect = {
			every_country = {
				limit = {
					is_subject_of = ROOT
					has_modifier = war_for_independence
				}
				remove_modifier = war_for_independence
			}
		}
		if = {
			limit = {
				exists = c:GBR
				is_subject_of = c:GBR
				c:GBR = {
					is_country_alive = yes
					NOT = { has_variable = british_raj_var }
				}
			}
			c:GBR = {
				trigger_event = { id = raj.1 days = 0 popup = yes }
			}
		}
		else = {
			add_modifier = {
				name = company_crushed_mutiny
				months = normal_modifier_time
			}
		}
		hidden_effect = {
			every_scope_state = {
				limit = {
					owner = ROOT
					has_variable = bic_incorporated_state
				}
				set_state_type = incorporated
				remove_variable = bic_incorporated_state
			}
		}
	}
	
	fail = {
		custom_tooltip = {
			text = bic_war_with_rebels_tt
		is_diplomatic_play_committed_participant = no
		}
		trigger_if = {
			limit = {
			scope:mug_diplo_play = {
				target = {
					c:MUG ?= this
					}
				}
			}		
		c:MUG = { is_country_alive = yes }		
		}
		trigger_else_if = {
			limit = {
			scope:mar_diplo_play = {
				target = {
					c:MAR ?= this
					}
				}
			}		
		c:MAR = { is_country_alive = yes }		
		}
				
		trigger_else_if = {
			limit = {
			scope:pan_diplo_play = {
				target = {
					c:PAN ?= this
					}
				}
			}
			c:PAN = { is_country_alive = yes }
			}
		
		
	}
	
	on_fail = {
	
	custom_tooltip = {
			text = india_princes_win_freedom_tt
			every_country = {
				limit = {
					is_subject_of = ROOT
					has_modifier = war_for_independence
				}
				make_independent = yes
				remove_modifier = war_for_independence
			}
		}
		if = {
			limit = {
				exists = c:GBR
				is_subject_of = c:GBR
				c:GBR = {
					is_country_alive = yes
					NOT = { has_variable = british_raj_var }
				}
			}
			c:GBR = {
				trigger_event = { id = raj.1 days = 0 popup = yes }
			}
		}
		add_modifier = {
				name = company_failed_mutiny
				months = normal_modifier_time
			}
		hidden_effect = {
			every_scope_state = {
				limit = {
					owner = ROOT
					has_variable = bic_incorporated_state
				}
				set_state_type = incorporated
				remove_variable = bic_incorporated_state
			}
		}
	}

	weight = 100
	
	should_be_pinned_by_default = yes
}

