﻿culture_number_of_counties = {
	scope = county

	effect = {
		clear_variable_list = cultures

		if = {									##### Create the list with only this heritage and language, sort it by acceptance.
			limit = {
				root = {
					has_variable = sortby_acceptance
				}		
				has_variable = heritage_only	
				has_variable = language_only
			}
			ordered_culture_global = {
				min = 0
				order_by = "cultural_acceptance(root.culture)"

				limit = {
					has_same_culture_heritage = scope:target
					has_same_culture_language = scope:target			
				}

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}			
			}
		}
		else_if = {										##### Create the list with only this heritage, sort it by acceptance.
			limit = {
				root = {
					has_variable = sortby_acceptance
				}			
				has_variable = heritage_only
			}
			ordered_culture_global = {
				min = 0
				order_by = "cultural_acceptance(root.culture)"

				limit = {
					has_same_culture_heritage = scope:target			
				}

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}			
			}
		}
		else_if = {									##### Create the list with only this language, sort it by acceptance.
			limit = {
				root = {
					has_variable = sortby_acceptance
				}			
				has_variable = language_only
			}
			ordered_culture_global = {
				min = 0
				order_by = "cultural_acceptance(root.culture)"

				limit = {
					has_same_culture_language = scope:target			
				}

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}			
			}
		}
		else_if = {									##### Create the list with only this heritage and language, sort it by number of counties.
			limit = {
				root = {
					has_variable = sortby_counties
				}	
				has_variable = heritage_only		
				has_variable = language_only
			}
			ordered_culture_global = {
				min = 0
				order_by = culture_number_of_counties

				limit = {
					has_same_culture_heritage = scope:target
					has_same_culture_language = scope:target			
				}

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}			
			}
		}
		else_if = {									##### Create the list with only this heritage, sort it by number of counties.
			limit = {
				root = {
					has_variable = sortby_counties
				}			
				has_variable = heritage_only
			}
			ordered_culture_global = {
				min = 0
				order_by = culture_number_of_counties

				limit = {
					has_same_culture_heritage = scope:target			
				}

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}			
			}
		}
		else_if = {									##### Create the list with only this language, sort it by number of counties.
			limit = {
				root = {
					has_variable = sortby_counties
				}			
				has_variable = language_only
			}
			ordered_culture_global = {
				min = 0
				order_by = culture_number_of_counties

				limit = {
					has_same_culture_language = scope:target			
				}

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}			
			}
		}
		else = {									##### Create the list...
			ordered_culture_global = {
				min = 0
				order_by = {
					if = {							##### ...Sort it by acceptance
						limit = {
							root = {
								has_variable = sortby_acceptance
							}
						}
						value = "cultural_acceptance(root.culture)"
					}
					else = {						##### ...Sort it by number of counties
						value = culture_number_of_counties
					}
				}

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}
			}			
		}
	}
}

culture_sortby_counties = {
	effect = {
		if = {
			limit = {
				has_variable = sortby_acceptance
			}
			remove_variable = sortby_acceptance
			set_variable = sortby_counties
		}
		else = {
			set_variable = sortby_counties
		}		
	}
}

culture_sortby_acceptance = {
	effect = {
		if = {
			limit = {
				has_variable = sortby_counties
			}
			remove_variable = sortby_counties
			set_variable = sortby_acceptance
		}
		else = {
			set_variable = sortby_acceptance
		}		
	}
}

culture_this_heritage_only = {
	effect = {
		clear_variable_list = cultures
		set_variable = sortby_counties

		if = {
			limit = {
				has_variable = heritage_only
				NOT = {
					has_variable = language_only
				}
			}
			remove_variable = heritage_only
			ordered_culture_global = {
				min = 0
				order_by = culture_number_of_counties

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}
			}			
		}
		else_if = {
			limit = {
				has_variable = heritage_only
				has_variable = language_only
			}
			remove_variable = heritage_only
			ordered_culture_global = {
				min = 0
				order_by = culture_number_of_counties

				limit = {
					has_same_culture_language = scope:target			
				}

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}
			}			
		}		
		else_if = {
			limit = {
				has_variable = language_only
				NOT = {
					has_variable = heritage_only
				}				
			}		
			set_variable = heritage_only
			ordered_culture_global = {
				min = 0
				order_by = culture_number_of_counties

				limit = {
					has_same_culture_heritage = scope:target
					has_same_culture_language = scope:target			
				}

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}			
			}			
		}		
		else = {
			set_variable = heritage_only
			ordered_culture_global = {
				min = 0
				order_by = culture_number_of_counties

				limit = {
					has_same_culture_heritage = scope:target			
				}

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}			
			}			
		}
	}
}

culture_this_language_only = {
	effect = {
		clear_variable_list = cultures
		set_variable = sortby_counties

		if = {
			limit = {
				has_variable = language_only
				NOT = {
					has_variable = heritage_only
				}
			}
			remove_variable = language_only
			ordered_culture_global = {
				min = 0
				order_by = culture_number_of_counties

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}
			}			
		}
		else_if = {
			limit = {
				has_variable = heritage_only
				has_variable = language_only
			}
			remove_variable = language_only
			ordered_culture_global = {
				min = 0
				order_by = culture_number_of_counties

				limit = {
					has_same_culture_heritage = scope:target			
				}

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}
			}			
		}		
		else_if = {
			limit = {
				has_variable = heritage_only
				NOT = {
					has_variable = language_only
				}				
			}		
			set_variable = language_only
			ordered_culture_global = {
				min = 0
				order_by = culture_number_of_counties

				limit = {
					has_same_culture_heritage = scope:target
					has_same_culture_language = scope:target			
				}

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}			
			}			
		}		
		else = {
			set_variable = language_only
			ordered_culture_global = {
				min = 0
				order_by = culture_number_of_counties

				limit = {
					has_same_culture_language = scope:target			
				}

			  	root = {
		    		add_to_variable_list = {
		      			name = cultures
		      			target = prev
					}
				}			
			}			
		}
	}
}

culture_variable_cleanup = {
	effect = {
		if = {
			limit = {
				has_variable = language_only
			}
			remove_variable = language_only
		}
		else_if = {
			limit = {
				has_variable = heritage_only
			}
			remove_variable = heritage_only
		}
		else = {
			remove_variable = language_only
			remove_variable = heritage_only
		}
	}
}