namespace = Plus
#reserved: Plus.4150 to Plus.4199

#######################################
# ISRAELI EVENTS
# Written by Zeress
#######################################

# Jewish community returns to Israel
character_event = {
	id = Plus.4150
	desc = "EVTDESC_Plus_4150"
	picture = GFX_evt_market
	
	only_rulers = yes
	war = no

	trigger = {
		OR = {
			has_global_flag = israel_remade
			has_global_flag = greater_israel_founded
		}
		OR = {
			AND = {
				k_israel = { 
					has_holder = yes
					holder_scope = {
						realm_size = 30
						religion_group = jewish_group
					}
				}
			}
			AND = {
				e_greater_israel = { 
					has_holder = yes
					holder_scope = {
						religion_group = jewish_group
					}
				}
			}
		}
		OR = {
			AND = {
				tier = baron
				is_republic = yes
			}
			is_patrician = yes
		}	
		any_realm_province = {
			any_province_holding = {
				has_building = ct_jewish_community_1
			}
			NOT = { has_province_modifier = brm_revolt }
		}
	}

	mean_time_to_happen = {
		months = 1000
		modifier = {
			factor = 0.5
			OR = { higher_tier_than = baron is_patrician = yes }
			trait = zealous
		}
		modifier = {
			factor = 0.75
			OR = { higher_tier_than = baron is_patrician = yes }
			trait = cruel
		}
		modifier = {
			factor = 0.75
			OR = { higher_tier_than = baron is_patrician = yes }
			trait = arbitrary
		}
		modifier = {
			factor = 1.5
			OR = { higher_tier_than = baron is_patrician = yes }
			trait = just
		}
		modifier = {
			factor = 1.5
			OR = { higher_tier_than = baron is_patrician = yes }
			trait = charitable
		}
		modifier = {
			factor = 0.5
			tier = baron is_patrician = no
			liege = { trait = zealous }
		}
		modifier = {
			factor = 0.75
			tier = baron is_patrician = no
			liege = { trait = cruel }
		}
		modifier = {
			factor = 0.75
			tier = baron is_patrician = no
			liege = { trait = arbitrary }
		}
		modifier = {
			factor = 1.5
			tier = baron is_patrician = no
			liege = { trait = just }
		}
		modifier = {
			factor = 1.5
			tier = baron is_patrician = no
			liege = { trait = charitable }
		}
		modifier = {
			factor = 1.5 # Bigger Community less likely to move
			any_demesne_province = {
				any_province_holding = {
					has_building = ct_jewish_community_2
				}				
			}
		}
		modifier = {
			factor = 1.5 # Bigger Community less likely to move
			any_demesne_province = {
				any_province_holding = {
					has_building = ct_jewish_community_3
				}
			}
		}
		modifier = {
			factor = 1.33 # Prosperous community less likely to move
			OR = {
				AND = {
					any_demesne_province = {
						any_province_holding = {
							has_building = ct_jewish_community_1
						}
						has_province_modifier = province_prosperity_2
					}
					NOT = { has_dlc = "Reapers" }
				}
				AND = {
					any_demesne_province = {
						any_province_holding = {
							has_building = ct_jewish_community_1
						}
					}
					has_dlc = "Reapers"
				}
			}
		}
		modifier = {
			factor = 0.75 # Greater Israel Exists, more likely to move
			e_greater_israel = {
				has_holder = yes
			}
		}
	}

	option = {
		name = "EVTOPTA_Plus_4150" # I see
		any_realm_province = {
			random_province_holding = {
				limit = {
					has_building = ct_jewish_community_1
					PREV = {
						NOT = { has_province_modifier = brm_revolt }
					}
				}
				PREV = { save_event_target_as = old_jewish_province }
				if = {
					limit = { has_building = ct_jewish_community_3 }
					event_target:old_jewish_province = { set_province_flag = had_jewish_community_3 }
				}
				if = {
					limit = {
						has_building = ct_jewish_community_2
						NOT = { has_building = ct_jewish_community_3 }
					}
					event_target:old_jewish_province = { set_province_flag = had_jewish_community_2 }
				}
				remove_building = ct_jewish_community_1
				
				ROOT = {
					if = {
						limit = {
							tier = baron
							NOT = { demesne_size = 2 }
						}
						if = {
							limit = {
								top_liege = {
									NOR = { 
										character = ROOT 
										has_character_flag = jewish_comm_left 
									}
								}
							}
							top_liege = {
								set_character_flag = jewish_comm_left
								letter_event = { id = Plus.4151 days = 3 }
							}
						}
						if = {
							limit = {
								liege = {
									NOR = { 
										character = ROOT 
										has_character_flag = jewish_comm_left 
										top_liege = { character = PREV } #I'm the top liege so I already got the event
									}
								}
							}
							liege = {
								set_character_flag = jewish_comm_left
								letter_event = { id = Plus.4151 days = 3 }
							}
						}
					}
				}
			}
		}
		hidden_tooltip = { # Return to Israel
			top_liege = {
				clr_character_flag = jewish_comm_left
			}
			if = {
				limit = { e_greater_israel = { has_holder = yes } }
				e_greater_israel = {
					holder_scope = {
						character_event = { id = Plus.4152 days = 3 }
					}
				}
			}
			if = {
				limit = { e_greater_israel = { has_holder = no } }
				k_israel = {
					holder_scope = {
						character_event = { id = Plus.4152 days = 3 }
					}
				}
			}
		}
	}
}

# Liege informed that jewish community has left
letter_event = {
	id = Plus.4151
	desc = "EVTDESC_Plus_4151"

	is_triggered_only = yes

	ai = no

	option = {
		name = "EVTOPTA_Plus_4151"
		FROM = {
			capital_holding = {
				tooltip = { remove_building = ct_jewish_community_1 }
			}
		}
	}
}

# Jewish community returns to Israel
character_event = {
	id = Plus.4152
	desc = "EVTDESC_Plus_4152"
	picture = GFX_evt_market
	
	is_triggered_only = yes

	immediate = { # Set character flag immediately so that only one province is converted. Demesne if available, otherwise realm
		if = {
			limit = {
				any_demesne_province = {
					NAND = {
						culture_group = israelite
						religion_group = jewish_group
					}
				}
			}
			ROOT = { set_character_flag = convert_demesne_israeli }
		}		
	}
	
	option = {
		name = "EVTOPTA_Plus_4152"
		if = {
			limit = { has_character_flag = convert_demesne_israeli } # Convert Demesne
			random_demesne_province = {
				limit = {
					NAND = {
						culture_group = israelite
						religion_group = jewish_group
					}
				}
				random = {
					chance = 50
					modifier = {
						factor = 2 # 100%
						event_target:old_jewish_province = { has_province_flag = had_jewish_community_3 }
					}
					modifier = {
						factor = 1.5 # 75%
						event_target:old_jewish_province = { has_province_flag = had_jewish_community_2 }
					}					
					israel_culture_religion_effect = yes # Convert to ROOT's religion and a culture based on FROM's origin
					
					event_target:old_jewish_province = { # Clear out reference flags
						clr_province_flag = had_jewish_community_3
						clr_province_flag = had_jewish_community_2
					}
					hidden_tooltip = { 
						if = {
							limit = { owner = { has_ambition = obj_civilize_province } }
							owner = { set_character_flag = civilized_province }
						}
					}
				}
			}
		}
		if = {
			limit = { NOT = { has_character_flag = convert_demesne_israeli } } # Convert outside of Demesne
			random_realm_province = {
				limit = {
					holder_scope = { NOT = { character = ROOT } }
					NAND = {
						culture_group = israelite
						religion_group = jewish_group
					}
				}
				random = {
					chance = 50
					modifier = {
						factor = 2 # 100%
						event_target:old_jewish_province = { has_province_flag = had_jewish_community_3 }
					}
					modifier = {
						factor = 1.5 # 75%
						event_target:old_jewish_province = { has_province_flag = had_jewish_community_2 }
					}					
					israel_culture_religion_effect = yes # Convert to ROOT's religion and a culture based on FROM's origin
					event_target:old_jewish_province = { # Clear out reference flags
						clr_province_flag = had_jewish_community_3
						clr_province_flag = had_jewish_community_2
					}
					hidden_tooltip = { 
						if = {
							limit = { owner = { has_ambition = obj_civilize_province } }
							owner = { set_character_flag = civilized_province }
						}
					}
				}
			}
		}
		clr_character_flag = convert_demesne_israeli
	}
}

# Greater Israel is born
narrative_event = {
	id = Plus.4153
	title = EVTNAME_Plus_4153
	desc = EVTDESC_Plus_4153
	picture = GFX_evt_jerusalem_captured_jews
	border = GFX_event_narrative_frame_religion
	
	is_triggered_only = yes

	option = {
		trigger = {
			religion_group = jewish_group
		}
		name = "EVTOPTA_Plus_4153"
	}
	
	option = {
		trigger = {
			NOT = { religion_group = jewish_group }
		}
		name = "EVTOPTB_Plus_4153"
	}
}