﻿activity_playdate = {
	expiration_days = 300
	has_activity_window = yes
	days_until_auto_start = 20
	color = { 0.853906 0.867188 0.105469 1 }
	valid = {
		activity_owner = {
			is_alive = yes
			is_imprisoned = no
		}
	}
	on_spawn = {
		invite_character_to_activity = activity_owner
		accept_invitation_for_character = activity_owner
		
		activity_owner = {
			if = {
				limit = {
					exists = liege
					NOT = { liege = this }
					liege = {
						playdate_potential_guest_check_trigger = yes
					}
				}
				liege = {
					add_to_temporary_list = potential_guests
				}
			}
			every_sibling = {
				limit = {
					playdate_potential_guest_check_trigger = yes
				}
				add_to_temporary_list = potential_guests
			}
			every_vassal = {
				limit = {
					playdate_potential_guest_check_trigger = yes
				}
				add_to_temporary_list = potential_guests	
			}
			every_relation = {
				type = friend
				limit = {
					playdate_potential_guest_check_trigger = yes
				}
				add_to_temporary_list = potential_guests	
			}
			every_relation = {
				type = crush
				limit = {
					playdate_potential_guest_check_trigger = yes
				}
				add_to_temporary_list = potential_guests	
			}
			every_scheme = {
				limit = {
					OR = {
						scheme_type = befriend
						scheme_type = sway
					}
					exists = scheme_target
					scheme_target = {
						playdate_potential_guest_check_trigger = yes
					}
				}
				scheme_target = {
					add_to_temporary_list = potential_guests
				}
			}
			if = { #If we don't have enough people attending then we'll add some more
				limit = {
					any_in_list = {
						list = potential_guests
						count < playdate_participants
					}
					any_vassal = {
						any_child = {
							playdate_potential_guest_check_trigger = yes
						}
					}
				}
				while = {
					limit = {
						any_in_list = {
							list = potential_guests
							count < playdate_participants
						}
						any_vassal = {
							any_child = {
								playdate_potential_guest_check_trigger = yes
							}
						}
					}
					every_vassal = { #Prioritize powerful vassal's children, and heirs
						limit = {
							is_powerful_vassal = yes
							any_child = {
								playdate_potential_guest_check_trigger = yes
							}
						}
						every_child = {
							limit = {
								playdate_potential_guest_check_trigger = yes
								is_primary_heir_of = prev
							}
							add_to_temporary_list = potential_guests	
						}
						every_child = {
							limit = {
								playdate_potential_guest_check_trigger = yes
							}
							add_to_temporary_list = potential_guests	
						}
					}
					every_vassal = {
						limit = {
							any_child = {
								playdate_potential_guest_check_trigger = yes
							}
						}
						every_child = {
							limit = {
								playdate_potential_guest_check_trigger = yes
							}
							add_to_temporary_list = potential_guests	
						}
					}
				}
			}

			if = { #If we don't have enough people attending then we'll add liege´s kids
				limit = {
					any_in_list = {
						list = potential_guests
						count < playdate_participants
					}
					exists = liege
					NOT = { this = liege }
					liege = {
						any_child = {
							playdate_potential_guest_check_trigger = yes
						}
					}
				}
				while = {
					limit = {
						any_in_list = {
							list = potential_guests
							count < playdate_participants
						}
						liege = {
							any_child = {
								playdate_potential_guest_check_trigger = yes
							}
						}
					}
					liege = {
						every_child = {
							limit = {
								playdate_potential_guest_check_trigger = yes
							}
							add_to_temporary_list = potential_guests
						}
					}
				}
			}

			if = { #If we don't have enough people attending then we'll add fellow vassals
				limit = {
					any_in_list = {
						list = potential_guests
						count < playdate_participants
					}
					exists = liege
					NOT = { this = liege }
					liege = {
						any_vassal = {
							playdate_potential_guest_check_trigger = yes
						}
					}
				}
				while = {
					limit = {
						any_in_list = {
							list = potential_guests
							count < playdate_participants
						}
						liege = {
							any_vassal = {
								playdate_potential_guest_check_trigger = yes
							}
						}
					}
					liege = {
						every_vassal = {
							limit = {
								playdate_potential_guest_check_trigger = yes
							}
							add_to_temporary_list = potential_guests
						}
					}
				}
			}

			if = { #If we don't have enough people attending then we'll add fellow vassal's kids
				limit = {
					any_in_list = {
						list = potential_guests
						count < playdate_participants
					}
					exists = liege
					NOT = { this = liege }
					liege = {
						any_vassal = {
							any_child = {
								playdate_potential_guest_check_trigger = yes
							}
						}
					}
				}
				while = {
					limit = {
						any_in_list = {
							list = potential_guests
							count < playdate_participants
						}
						liege = {
							any_vassal = {
								any_child = {
									playdate_potential_guest_check_trigger = yes
								}
							}
						}
					}
					liege = {
						every_vassal = {
							limit = {
								any_child = {
									playdate_potential_guest_check_trigger = yes
								}
							}
							every_child = {
								limit = {
									playdate_potential_guest_check_trigger = yes
								}
								add_to_temporary_list = potential_guests	
							}
						}
					}
				}
			}
			

			every_in_list = {
				list = potential_guests
				if = { #Are we sending a message to the player about their kid being invited?
					limit = {
						is_ai = yes
						is_landed = no
						save_temporary_scope_as = child_scope_check
						any_parent = {
							is_ai = no
							any_courtier = {
								this = scope:child_scope_check
							}
						}
					}
					save_scope_as = child_scope
					scope:activity = {
						invite_character_to_activity = scope:child_scope
					}
					random_parent = { #Save the parent that's going to get the invite
						limit = {
							is_ai = no
                            #Don't Invite My Children ###############################
                            NOT = { has_character_flag = do_not_invite_my_children }
                            #########################################################
							any_courtier = {
								this = scope:child_scope
							}
						}
						trigger_event = playdate.1002
					}
				}
				else = {
					scope:activity = {
						invite_character_to_activity = prev
					}
					trigger_event = playdate.1001
				}
			}
	
		}
	}
	on_activate = {
		if = {
			limit = {
				# Check that there are guests apart from the playdate host.
				NOT = {
					any_participant = {
						NOT = {	this = scope:activity.activity_owner }
					}
				}
			}
			# If there are no other guests, throw this event.
			activity_owner = {
				trigger_event = playdate.2003
			}
		}
		else = {
		# If there are guests present
			activity_owner = { # The host gets their events
				# The key event is chosen for the host, and fires other events from there to every participant:
				trigger_event = playdate.0001
				trigger_event = {
					on_action = playdate_event_selection
					days = 15
				}
			}
			every_participant = {
				limit = { NOT = { this = scope:activity.activity_owner } }
				#Remove blocker for invitations to playdates (the playdate is now active and will work as a blocker)
				remove_variable = booked_for_a_party
			}
		}
	}

	on_complete = {
		#We inform player parents that their kid is home
		activity_owner = {
			save_scope_as = host
		}
		every_participant = {
			limit = {
				is_ai = yes
				is_landed = no
				save_temporary_scope_as = child_scope_check
				any_parent = {
					is_ai = no
					any_courtier = {
						this = scope:child_scope_check
					}
				}
			}
			save_scope_as = child_scope
			random_parent = {
				limit = {
					is_ai = no
					any_courtier = {
						this = scope:child_scope
					}
				}
				send_interface_message = {
					type = event_generic_good_text
					title = child_return_from_playdate_title
					desc = child_return_from_playdate_desc

					left_icon = scope:child_scope
					right_icon = scope:host
				}
			}
		}
	}
	
	on_participant_death = {
	}

	on_invalidated = {
		activity_owner = {
			save_scope_as = host
		}
		every_participant = {
			limit = {
				NOT = { this = scope:host }
			}
			add_to_list = playdate_participants
		}
		complete_activity = yes
		if = {
			limit = {
				scope:host = {
					is_alive = no
				}
			}
			#Host died
			every_in_list = {
				list = playdate_participants
				trigger_event = {
					id = playdate.0020
					days = 2 #So you don't get spammed
				}
			}
		}
		else_if = {
			limit = {
				scope:host = {
					is_imprisoned = yes
				}
			}
			#Host has been imprisoned
			every_in_list = {
				list = playdate_participants
				trigger_event = playdate.0022
			}
			scope:host = {
				trigger_event = playdate.0021
			}
		}
	}
}