# This story cycle send the event a character should see when they discover a lover's/spouse's lover secret, or when a lover's/spouse's lover secret is exposed.

# It should be started by the secret_lover secret with these scripted effects:
# - start_infidelity_confrontation_story_discovery_effect
# - start_infidelity_confrontation_story_exposure_effect

# To get some special texts in the first event infidelity_confrontation.1000, you can save a variable on the secret before revealing/exposing it, and use the scripted effects to apply that variable to the story. Current special texts:
# - revealed_through_investigation_by_desc
# - revealed_through_confession_to_desc

story_cycle_infidelty_confronter = {

	on_setup = {
		set_variable = {
			name = ongoing_confrontation
			value = no
		}
	}

	on_end = {
		
	}

	on_owner_death = {
		end_story = yes
	}

	# Send the next confrontation chain
	effect_group = {
		days = { 2 3 }
		chance = 100
	
		triggered_effect = {
			trigger = {
				var:ongoing_confrontation = no
				has_variable_list = confronting_partners
			}
			effect = {
				#Save some scopes
				save_scope_as = story
				var:lover_1 = { save_scope_as = lover_1 }
				var:lover_2 = { save_scope_as = lover_2 }

				#Make sure there is no one in the list that shouldn't be there anymore
				every_in_list = {
					variable = confronting_partners
					limit = {
						OR = {
							NOR = {
								has_relation_soulmate = scope:lover_1
								has_relation_soulmate = scope:lover_2
								is_consort_of = scope:lover_1
								is_consort_of = scope:lover_2
								has_relation_lover = scope:lover_1
								has_relation_lover = scope:lover_2
							}
						}
					}
					save_temporary_scope_as = remove_from_list
					scope:story = {
						remove_list_variable = {
							name = confronting_partners
							target = scope:remove_from_list
						}
					}
				}

				#In order of importance, each partner of the two lovers will get to make their choice/demand
				random_in_list = {
					variable = confronting_partners
					limit = {
						is_ai = no
					}
					alternative_limit = {
						OR = {
							has_relation_soulmate = scope:lover_1
							has_relation_soulmate = scope:lover_2
						}
					}
					alternative_limit = {
						OR = {
							is_consort_of = scope:lover_1
							is_consort_of = scope:lover_2
						}
					}
					alternative_limit = {
						OR = {
							has_relation_lover = scope:lover_1
							has_relation_lover = scope:lover_2
						}
					}
					save_scope_as = confronting_partner
				}

				#Did we find someone? We might have removed the last character from the list above
				if = {
					limit = { exists = scope:confronting_partner }

					#Who is confronting_partner confronting? If involved with both, pick the "most important" one
					random_in_list = {
						variable = infidelity_lovers
						limit = { has_relation_soulmate = scope:confronting_partner }
						alternative_limit = { is_consort_of = scope:confronting_partner }
						alternative_limit = { has_relation_lover = scope:confronting_partner }
						save_scope_as = infidelity_partner
					}
					random_in_list = {
						variable = infidelity_lovers
						limit = {
							NOT = { this = scope:infidelity_partner }
						}
						save_scope_as = infidelity_partners_lover
					}

					#Did confronting_partner know? (exposed secrets only)
					if = {
						limit = {
							has_variable_list = knowing_confronting_partners
							is_target_in_variable_list = {
								name = knowing_confronting_partners
								target = scope:confronting_partner
							}
						}
						save_scope_value_as = {
							name = confronting_partner_knew
							value = yes
						}
					}
					else_if = {
						limit = {
							has_variable_list = knowing_confronting_partners
						}
						save_scope_value_as = {
							name = confronting_partner_knew
							value = no
						}
					}

					#Save exposer (if exposed)
					if = {
						limit = { exists = var:exposer }
						var:exposer = { save_scope_as = exposer }
					}

					#Save the secret (if discovery)
					scope:infidelity_partner = {
						random_secret = {
							limit = {
								secret_type = secret_lover
								secret_target = scope:infidelity_partners_lover
							}
							save_scope_as = secret
						}
					}
				
					#Remove current confronting_partner from list
					remove_list_variable = {
						name = confronting_partners
						target = scope:confronting_partner
					}

					#Block other choices while the current one is evaluating
					set_variable = {
						name = ongoing_confrontation
						value = yes
					}

					#Send event!
					scope:confronting_partner = { 
						if = {
							limit = {
								has_trait = magister_trait_group
								scope:lover_1 = {
										has_trait = devoted_trait_group
								}
							}
							scope:lover_1 = {
								save_scope_as = lover_spouse
							}
							trigger_event = regula_adultery.0002
						}
						else_if = {
							limit = {
								has_trait = magister_trait_group
								scope:lover_2 = {
									has_trait = devoted_trait_group
								}
							}
							scope:lover_2 = {
								save_scope_as = lover_spouse
							}
							trigger_event = regula_adultery.0002
						}
						else = {
							trigger_event = infidelity_confrontation.1000
						}
					}
				}
			}
		}

		triggered_effect = {
			trigger = {
				NOT = { has_variable_list = confronting_partners }
				var:ongoing_confrontation = no #If it's yes, we still have an unresolved confrontation going on
			}

			effect = {
				end_story = yes
				#todo end the story with a happy summary if you're still together
			}
		}
	}

	#100 days self destruct
	effect_group = {
		days = 100
		chance = 100

		triggered_effect = {
			trigger = { always = yes }
			effect = {
				end_story = yes
			}
		}
	}
}
