﻿story_tai_migrations = {
	
	on_setup = {
		
	}
	
	on_end = {
		every_in_global_list = { # give all saved migration county holders the story if they don't have it already
			variable = tai_migration_target_counties
			holder = {
				if = {
					limit = {
						NOT = {
							any_owned_story = {
								type = story_tai_migrations
							}
						}
						any_realm_province = {
							culture = {
								NOT = {
									has_cultural_pillar = heritage_tai
								}
							}
						}
					}
					create_story = story_tai_migrations
				}
			}
		}
	}
	
	on_owner_death = { # pass on the story
		story_owner = {
			primary_heir ?= {
				if = {
					limit = {
						is_physically_able_adult = yes
						is_ai = yes
					}
					create_story = story_tai_migrations
				}
			}
		}
		end_story = yes
	}

	effect_group = { # Yearly validations
		days = 360
		triggered_effect = { # end the story if I have no relevant provinces for migration
			trigger = {
				story_owner = {
					NOT = {
						any_realm_province = {
							geographical_region = custom_tai_migration_target
							culture = {
								NOT = {
									has_cultural_pillar = heritage_tai
								}
							}
						}
					}
				}
			}
			effect = {
				end_story = yes
			}
		}

		triggered_effect = { # end the story if it has been 100 years from game start
			trigger = {
				years_from_game_start >= 100
			}
			effect = {
				end_story = yes
			}
		}

		triggered_effect = { # end the story if X provinces in the Tai region have already been migrated
			trigger = {
				any_county_in_region = {
				 	region = custom_tai_migration_target 
				 	count >= 14
				 	any_county_province = {
				 		culture = { has_cultural_pillar = heritage_tai }
				 	}
				}
			}
			effect = {
				end_story = yes
			}
		}

		triggered_effect = { # end the story if all provinces in the migration origin are non-tai
			trigger = {
				any_county_in_region = {
				 	region = custom_tai_migration_origin 
				 	count = 0
				 	any_county_province = {
				 		culture = { has_cultural_pillar = heritage_tai }
				 	}
				}
			}
			effect = {
				end_story = yes
			}
		}
	}

	effect_group = { # if all validations are checked, trigger event pulses
		days = 365
		chance = 13
		triggered_effect = {
			trigger = {

			}
			effect = {
				random_list = {
					3 = {
						story_owner = {
							trigger_event = tgp_tai_migration_event.1000
						}
					}
					4 = {
						story_owner = {
							trigger_event = tgp_tai_migration_event.2000
						}
					}
					1 = {
						story_owner = {
							trigger_event = tgp_tai_migration_event.3000
						}
					}
					4 = {
						story_owner = {
							trigger_event = tgp_tai_migration_event.4000
						}
					}
					4 = {
						story_owner = {
							trigger_event = tgp_tai_migration_event.5000
						}
					}
				}
			}
		}
	}
}
