﻿acs_se_create_religion_lists = {
	every_religion_global = {
		limit = { 
			is_in_family = rf_abrahamic 
		}
		add_to_global_variable_list = { name = acs_gvl_rf_abrahamic target = this }   
	}
	every_religion_global = {
		limit = { 
			is_in_family = rf_eastern
		}
		add_to_global_variable_list = { name = acs_gvl_rf_eastern target = this }   
	}
	every_religion_global = {
		limit = { 
			is_in_family = rf_pagan 
		}
		add_to_global_variable_list = { name = acs_gvl_rf_pagan target = this }
	}
	every_religion_global = {
		limit = { 
			NOR = { 
				is_in_family = rf_eastern
				is_in_family = rf_abrahamic
				is_in_family = rf_pagan
			}
		}
		add_to_global_variable_list = { name = acs_gvl_rf_other target = this }
	}
}

acs_se_update_rf = {
	if = {
		limit = {
			is_in_family = rf_pagan
		}
		acs_set_update_rf = { FAMILY = pagan }
	}
	else_if = {
		limit = {
			is_in_family = rf_abrahamic
		}
		acs_set_update_rf = { FAMILY = abrahamic }
	}
	else_if = {
		limit = {
			is_in_family = rf_eastern
		}
		acs_set_update_rf = { FAMILY = eastern }
	}
	else = {
		acs_set_update_rf = { FAMILY = other }
	}
}

acs_set_update_rf = {
	if = {
		limit = {
			any_in_global_list = { 
				variable = acs_gvl_rf_$FAMILY$
				trigger_if = {
					limit = { has_variable = acs_v_selected }
					var:acs_v_selected = 1
				}
				trigger_else ={
					always = no
				}
				count = all
			}
		}
		global_var:acs_gv_main_filters = {
			set_variable = { name = acs_v_rf_$FAMILY$_selected value = 1 }
		}
	}
	else_if = {
		limit = {
			any_in_global_list = { 
				variable = acs_gvl_rf_$FAMILY$
				trigger_if = {
					limit = { has_variable = acs_v_selected }
					var:acs_v_selected = 2
				}
				trigger_else ={
					always = no
				}
				count = all
			}
		}
		global_var:acs_gv_main_filters = {
			set_variable = { name = acs_v_rf_$FAMILY$_selected value = 2 }
		}
	}
	else_if = {
		limit = {
			any_in_global_list = { 
				variable = acs_gvl_rf_$FAMILY$
				NOT = { has_variable = acs_v_selected }
				count = all
			}
		}
		global_var:acs_gv_main_filters = {
			remove_variable = acs_v_rf_$FAMILY$_selected
		}
		
	}
	else = {
		global_var:acs_gv_main_filters = {
			set_variable = { name = acs_v_rf_$FAMILY$_selected value = 3 }
		}
		
	}
}

acs_se_update_religion = {
	religion = {
		if = {
			limit = {
				any_faith = {
					trigger_if = {
						limit = { has_variable = acs_v_selected }
						var:acs_v_selected = 1
					}
					trigger_else ={
						always = no
					}
					count = all
				}
			}
			set_variable = { name = acs_v_selected value = 1 }
		}
		else_if = {
			limit = {
				any_faith = {
					trigger_if = {
						limit = { has_variable = acs_v_selected }
						var:acs_v_selected = 2
					}
					trigger_else ={
						always = no
					}
					count = all
				}
			}
			set_variable = { name = acs_v_selected value = 2 }
		}
		else_if = {
			limit = {
				any_faith = {
					NOT = { has_variable = acs_v_selected }
					count = all
				}
			}
			remove_variable = acs_v_selected
		}
		else = {
			set_variable = { name = acs_v_selected value = 3 }
		}
		acs_se_update_rf = yes
	}
}

acs_set_togle_religion_family_selection = {
	acs_se_save_undo = yes
	if = {
		limit = {
			NOT = { 
				global_var:acs_gv_main_filters = {
					has_variable = acs_v_rf_$FAMILY$_selected
				}
			}
		}
		every_in_global_list = {
			variable = acs_gvl_rf_$FAMILY$
			every_faith = {
				set_variable = { name = acs_v_selected value = 1 }
			}
			set_variable = { name = acs_v_selected value = 1 }	
		}
		global_var:acs_gv_main_filters = {
			set_variable = { name = acs_v_rf_$FAMILY$_selected value = 1 }
		}
	}
	else_if = {
		limit = {
			trigger_if = {
				limit = { 
					global_var:acs_gv_main_filters = {
						has_variable = acs_v_rf_$FAMILY$_selected
					} 
				}
				global_var:acs_gv_main_filters.var:acs_v_rf_$FAMILY$_selected = 1
			}
			trigger_else ={
				always = no
			}	
		}
		every_in_global_list = {
			variable = acs_gvl_rf_$FAMILY$
			every_faith = {
				set_variable = { name = acs_v_selected value = 2 }
			}
			set_variable = { name = acs_v_selected value = 2 }	
		}
		global_var:acs_gv_main_filters = {
			set_variable = { name = acs_v_rf_$FAMILY$_selected value = 2 }
		}
	}
	else = {
		every_in_global_list = {
			variable = acs_gvl_rf_$FAMILY$
			every_faith = {
				remove_variable = acs_v_selected
			}
			remove_variable = acs_v_selected	
		}
		global_var:acs_gv_main_filters = {
			remove_variable = acs_v_rf_$FAMILY$_selected
		}
		
	}
	acs_set_copy_from_all_rf_to_save_slot = { SLOT = global_var:acs_gv_main_filters }
	acs_se_auto_apply_sorting_and_filters = yes
}

acs_set_copy_from_all_rf_to_save_slot = {
	acs_set_copy_from_rf_to_save_slot = { SLOT_RF = $SLOT$ FAMILY = pagan }
	acs_set_copy_from_rf_to_save_slot = { SLOT_RF = $SLOT$ FAMILY = abrahamic }
	acs_set_copy_from_rf_to_save_slot = { SLOT_RF = $SLOT$ FAMILY = eastern }
	acs_set_copy_from_rf_to_save_slot = { SLOT_RF = $SLOT$ FAMILY = other }
	acs_se_update_religion_filter = yes
}

acs_set_copy_from_rf_to_save_slot = {
	if = {
		limit = {
			global_var:acs_gv_main_filters = {
				trigger_if = {
					limit = { has_variable = acs_v_rf_$FAMILY$_selected	}
					var:acs_v_rf_$FAMILY$_selected = 3
				}
				trigger_else ={ always = no	}
			}
		}
		every_in_global_list = {
			variable = acs_gvl_rf_$FAMILY$
			save_temporary_scope_as = acs_ts_religion_to_update
			acs_set_copy_from_religion_to_save_slot = { RELIGION = scope:acs_ts_religion_to_update SLOT_RELIGION = $SLOT_RF$ }
		}
	}
	else = {
		every_in_global_list = {
			variable = acs_gvl_rf_$FAMILY$
			every_faith = {
				save_temporary_scope_as = acs_ts_faith_to_update
				$SLOT_RF$ = {
					remove_list_variable = { name = acs_vl_has_faith target = scope:acs_ts_faith_to_update }
					remove_list_variable = { name = acs_vl_dont_have_faith target = scope:acs_ts_faith_to_update }
				}	
			}
			save_temporary_scope_as = acs_ts_religion_to_update
			$SLOT_RF$ = {
				remove_list_variable = { name = acs_vl_has_religion target = scope:acs_ts_religion_to_update }
				remove_list_variable = { name = acs_vl_dont_have_religion target = scope:acs_ts_religion_to_update }
			}	
		}	
	}
}

acs_set_copy_from_religion_to_save_slot = {
	$RELIGION$ = {
		save_temporary_scope_as = acs_ts_religion
		if = {
			limit = {
				trigger_if = {
					limit = { has_variable = acs_v_selected	}
					var:acs_v_selected = 3
				}
				trigger_else ={ always = no	}
			}
			every_faith = {
				save_temporary_scope_as = acs_ts_faith_to_update
				acs_set_copy_from_faith_to_save_slot = { FAITH = scope:acs_ts_faith_to_update SLOT_FAITH = $SLOT_RELIGION$ }
			}
			$SLOT_RELIGION$ = {
				remove_list_variable = { name = acs_vl_has_religion target = scope:acs_ts_religion }
				remove_list_variable = { name = acs_vl_dont_have_religion target = scope:acs_ts_religion }
			}	
		}
		else = {
			every_faith = {
				save_temporary_scope_as = acs_ts_faith_to_update
				$SLOT_RELIGION$ = {
					remove_list_variable = { name = acs_vl_has_faith target = scope:acs_ts_faith_to_update }
					remove_list_variable = { name = acs_vl_dont_have_faith target = scope:acs_ts_faith_to_update }
				}	
			}
			if = {
				limit = {
					trigger_if = {
						limit = { has_variable = acs_v_selected	}
						var:acs_v_selected = 1
					}
					trigger_else ={ always = no	}
				}
				$SLOT_RELIGION$ = { 
					remove_list_variable = { name = acs_vl_dont_have_religion target = scope:acs_ts_religion }
					add_to_variable_list = { name = acs_vl_has_religion target = scope:acs_ts_religion }
				}
			}
			else_if = {
				limit = {
					trigger_if = {
						limit = { has_variable = acs_v_selected	}
						var:acs_v_selected = 2
					}
					trigger_else ={ always = no	}
				}
				$SLOT_RELIGION$ = { 
					remove_list_variable = { name = acs_vl_has_religion target = scope:acs_ts_religion }
					add_to_variable_list = { name = acs_vl_dont_have_religion target = scope:acs_ts_religion }
				}
			}
			else = {
				$SLOT_RELIGION$ = { 
					remove_list_variable = { name = acs_vl_has_religion target = scope:acs_ts_religion }
					remove_list_variable = { name = acs_vl_dont_have_religion target = scope:acs_ts_religion }
				}
			}
		}
	}
}

acs_set_copy_from_faith_to_save_slot = {
	$FAITH$ = {
		save_temporary_scope_as = acs_ts_faith
		if = {
			limit = { NOT = { has_variable = acs_v_selected } }
			$SLOT_FAITH$ = {
				remove_list_variable = { name = acs_vl_has_faith target = scope:acs_ts_faith }
				remove_list_variable = { name = acs_vl_dont_have_faith target = scope:acs_ts_faith }
			}	
		}
		else_if = {
			limit = {
				trigger_if = {
					limit = { 
						has_variable = acs_v_selected
					}
					var:acs_v_selected = 1
				}
				trigger_else ={
					always = no
				}
			}
			$SLOT_FAITH$ = { 
				remove_list_variable = { name = acs_vl_dont_have_faith target = scope:acs_ts_faith }
				add_to_variable_list = { name = acs_vl_has_faith target = scope:acs_ts_faith }
			}
		}
		else = {
			$SLOT_FAITH$ = { 
				remove_list_variable = { name = acs_vl_has_faith target = scope:acs_ts_faith }
				add_to_variable_list = { name = acs_vl_dont_have_faith target = scope:acs_ts_faith }
			}
		}
	}
}

acs_set_copy_from_save_slot_to_all_rf = {
	acs_set_copy_from_save_slot_to_rf = { FAMILY = pagan }
	acs_set_copy_from_save_slot_to_rf = { FAMILY = abrahamic }
	acs_set_copy_from_save_slot_to_rf = { FAMILY = eastern }
	acs_set_copy_from_save_slot_to_rf = { FAMILY = other }
}

acs_set_copy_from_save_slot_to_rf = {
	if = {
		limit = {
			global_var:acs_gv_main_filters = {
				trigger_if = {
					limit = { has_variable = acs_v_rf_$FAMILY$_selected	}
					var:acs_v_rf_$FAMILY$_selected = 1
				}
				trigger_else ={ always = no	}
			}
		}
		every_in_global_list = {
			variable = acs_gvl_rf_$FAMILY$
			every_faith = {
				set_variable = { name = acs_v_selected value = 1 }	
			}
			set_variable = { name = acs_v_selected value = 1 }	
		}
	}
	else_if = {
		limit = {
			global_var:acs_gv_main_filters = {
				trigger_if = {
					limit = { has_variable = acs_v_rf_$FAMILY$_selected	}
					var:acs_v_rf_$FAMILY$_selected = 2
				}
				trigger_else ={ always = no	}
			}
		}
		every_in_global_list = {
			variable = acs_gvl_rf_$FAMILY$
			save_temporary_scope_as = acs_ts_religion_to_update
			every_faith = {
				set_variable = { name = acs_v_selected value = 2 }	
			}
			set_variable = { name = acs_v_selected value = 2 }	
		}
	}
	else_if = {
		limit = {
			global_var:acs_gv_main_filters = {
				NOT = { has_variable = acs_v_rf_$FAMILY$_selected	}
			}
		}
		every_in_global_list = {
			variable = acs_gvl_rf_$FAMILY$
			every_faith = {
				remove_variable = acs_v_selected		
			}
			remove_variable = acs_v_selected	
		}
	}
	else = {
		every_in_global_list = {
			variable = acs_gvl_rf_$FAMILY$
			save_temporary_scope_as = acs_ts_religion_to_update
			if = {
				limit = {
					global_var:acs_gv_main_filters = {
						any_in_list = { variable = acs_vl_has_religion this = scope:acs_ts_religion_to_update count = 1 }
					}
				}
				every_faith = {
					set_variable = { name = acs_v_selected value = 1 }	
				}
				set_variable = { name = acs_v_selected value = 1 }	
			}
			else_if = {
				limit = {
					global_var:acs_gv_main_filters = {
						any_in_list = { variable = acs_vl_dont_have_religion this = scope:acs_ts_religion_to_update count = 1 }
					}
				}
				every_faith = {
					set_variable = { name = acs_v_selected value = 2 }	
				}
				set_variable = { name = acs_v_selected value = 2 }	
			}
			else_if = {
				limit = {
					any_faith = {
						save_temporary_scope_as = acs_ts_faith_to_update
						OR = {
							global_var:acs_gv_main_filters = {
								any_in_list = { variable = acs_vl_has_faith this = scope:acs_ts_faith_to_update count = 1 }
							}
							global_var:acs_gv_main_filters = {
								any_in_list = { variable = acs_vl_dont_have_faith this = scope:acs_ts_faith_to_update count = 1 }
							}
						}
						
					}
					
				}
				every_faith = {
					save_temporary_scope_as = acs_ts_faith_to_update
					if = {
						limit = {
							global_var:acs_gv_main_filters = {
								any_in_list = { variable = acs_vl_has_faith this = scope:acs_ts_faith_to_update count = 1 }
							}
						}
						set_variable = { name = acs_v_selected value = 1 }
					}
					else_if = {
						limit = {
							global_var:acs_gv_main_filters = {
								any_in_list = { variable = acs_vl_dont_have_faith this = scope:acs_ts_faith_to_update count = 1 }
							}
						}
						set_variable = { name = acs_v_selected value = 2 }
					}
					else = {
						remove_variable = acs_v_selected
					}
						
				}
				set_variable = { name = acs_v_selected value = 3 }	
			}
			else = {
				every_faith = {
					remove_variable = acs_v_selected		
				}
				remove_variable = acs_v_selected
			}
			
		}
	}
}