﻿loaa_give_artifact_effect = {
	if = {
		limit = { exists = scope:task_contract_target }
		scope:task_contract_target ?= { save_temporary_scope_as = recipient }
	}
	if = {
		limit = { exists = scope:escorted_artifact }
		scope:escorted_artifact ?= { save_temporary_scope_as = target }
	}
	if = {
		limit = { exists = scope:task_contract_employer }
		scope:task_contract_employer ?= { save_temporary_scope_as = actor }
	}
	# Struggle Catalyst
	if = { # Struggle Catalyst
		limit = {
			scope:task_contract_employer = {
				any_character_struggle = {
					involvement = involved
					activate_struggle_catalyst_secondary_character_involvement_involved_trigger = {
						CATALYST = catalyst_gift_independent_ruler
						CHAR = scope:task_contract_target
					}
				}
			}
		}
		hidden_effect = {
			scope:task_contract_employer = {
				every_character_struggle = {
					involvement = involved
					activate_struggle_catalyst = {
						catalyst = catalyst_gift_independent_ruler
						character = scope:task_contract_employer
					}
				}
			}
		}
	}
	else_if = {
		limit = {
			NOT = { exists = global_var:AGOT_is_loaded }
			fp3_struggle_involves_one_supporter_and_one_detractor = {
				FIRST = scope:task_contract_employer
				SECOND = scope:task_contract_target
			}
			scope:task_contract_employer = {
				any_character_struggle = {
					#involvement = involved
					activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
						CATALYST = catalyst_gift_supporter_detractor_ruler
						CHAR = scope:task_contract_target
					}
				}
			}
		}
		hidden_effect = {
			scope:task_contract_employer = {
				every_character_struggle = {
					#involvement = involved
					limit = {
						activate_struggle_catalyst_secondary_character_involvement_either_trigger = {
							CATALYST = catalyst_gift_supporter_detractor_ruler
							CHAR = scope:task_contract_target
						}
					}
					activate_struggle_catalyst = {
						catalyst = catalyst_gift_supporter_detractor_ruler
						character = scope:task_contract_employer
					}
					log_debug_variable_for_persian_struggle_effect = { VAR = concession_catalyst_gift_supporter_detractor_ruler }
				}
			}
		}
	}

	scope:escorted_artifact = {
		set_variable = {
			name = suppress_artifact_notifications
			value = yes
			days = 1
		}
		set_variable = { name = gifted_artifact value = yes days = 9125 }
	}
	scope:task_contract_target = {
		# Verify that they could become friend
		if = {
			limit = {
				NAND = {
					has_relation_friend = scope:task_contract_employer
					has_relation_lover = scope:task_contract_employer
					has_relation_soulmate = scope:task_contract_employer
					has_relation_best_friend = scope:task_contract_employer
				}
			}
			gifting_leads_towards_friendship_effect = yes
		}
	}
	scope:task_contract_employer = {
		send_interface_toast = {
			type = event_toast_effect_neutral
			title = gift_artifact_interaction_notification
			left_icon = scope:task_contract_target
			right_icon = scope:escorted_artifact
			scope:task_contract_target = {
				if = {
					limit = { scope:escorted_artifact = { can_be_claimed_by = scope:task_contract_target } }
					if = {
						limit = { any_personal_claimed_artifact = { this = scope:escorted_artifact } }
						show_as_tooltip = { remove_personal_artifact_claim = scope:escorted_artifact }
					}
					else = { show_as_tooltip = { house = { remove_house_artifact_claim = scope:escorted_artifact } } }
				}
			}

			scope:escorted_artifact = { set_owner = scope:task_contract_target }


			if = {
				limit = {
					scope:escorted_artifact = { is_unique = yes }
				}
				add_clan_unity_interaction_effect = {
					CHARACTER = scope:task_contract_employer
					TARGET = scope:task_contract_target
					VALUE = medium_unity_gain
					DESC = clan_unity_given_artifact.desc
					REVERSE_NON_HOUSE_TARGET = no
				}
				stress_impact = {
					greedy = major_stress_impact_gain
					generous = major_stress_impact_loss
				}
			}
			else_if = {# Unity goes here, too!
				limit = { scope:escorted_artifact = { rarity = masterwork } }
				add_clan_unity_interaction_effect = {
					CHARACTER = scope:task_contract_employer
					TARGET = scope:task_contract_target
					VALUE = minor_unity_gain
					DESC = clan_unity_given_artifact.desc
					REVERSE_NON_HOUSE_TARGET = no
				}
				stress_impact = {
					greedy = minor_stress_impact_gain
					generous = minor_stress_impact_loss
				}
			}
			else_if = {
				limit = { scope:escorted_artifact = { rarity = famed } }
				add_clan_unity_interaction_effect = {
					CHARACTER = scope:task_contract_employer
					TARGET = scope:task_contract_target
					VALUE = minor_unity_gain
					DESC = clan_unity_given_artifact.desc
					REVERSE_NON_HOUSE_TARGET = no
				}
				stress_impact = {
					greedy = medium_stress_impact_gain
					generous = medium_stress_impact_loss
				}
			}
			else_if = {
				limit = { scope:escorted_artifact = { rarity = illustrious } }
				add_clan_unity_interaction_effect = {
					CHARACTER = scope:task_contract_employer
					TARGET = scope:task_contract_target
					VALUE = minor_unity_gain
					DESC = clan_unity_given_artifact.desc
					REVERSE_NON_HOUSE_TARGET = no
				}
				stress_impact = {
					greedy = major_stress_impact_gain
					generous = major_stress_impact_loss
				}
			}
			if = {
				limit = { scope:task_contract_target = { is_ai = yes } }
				reverse_add_opinion = {
					target = scope:task_contract_target
					modifier = gift_artifact_opinion
					opinion = gift_artifact_opinion
				}
			}
			#FP3 Tenet Communal Possessions Perk - piety gain for gift giving.
			if = {
				limit = {
					NOT = { exists = global_var:AGOT_is_loaded }
				}
				if = {
					limit = {
						scope:task_contract_employer = {
							faith = {
								has_doctrine_parameter = piety_from_gifts_active
							}
						}
					}
					scope:task_contract_employer = {
						if = {
							limit = { #the better the artifact, the more piety you gain
								scope:escorted_artifact = { rarity = illustrious }
							}
							add_piety = massive_piety_gain
						}
						else_if = {
							limit = {
								scope:escorted_artifact = { rarity = famed }
							}
							add_piety = major_piety_gain
						}
						else_if = {
							limit = {
								scope:escorted_artifact = { rarity = masterwork }
							}
							add_piety = medium_piety_gain
						}
						else = {
							add_piety = minor_piety_gain
						}
					}
				}
			}
		}

		# Warning for low opinion gain
		if = {
			limit = {
				scope:escorted_artifact = { artifact_durability <= define:NInventory|ARTIFACT_LOW_DURABILITY }
			}
			custom_tooltip = artifact_gift_low_durability_tt
		}
		if = {
			limit = {
				scope:escorted_artifact = { artifact_durability <= define:NInventory|ARTIFACT_VERY_LOW_DURABILITY }
			}
			custom_tooltip = artifact_gift_very_low_durability_tt
		}
		if = {
			limit = {
				scope:escorted_artifact = {
					OR = {
						AND = {
							has_variable = banner_house
							NOT = { var:banner_house = scope:task_contract_target.house }
						}
						AND = {
							has_variable = banner_dynasty
							NOT = { var:banner_dynasty = scope:task_contract_target.dynasty }
						}
					}
				}
			}
			custom_tooltip = artifact_gift_useless_banner_tt
		}
		else_if = {
			limit = {
				scope:task_contract_target = {
					NOR = {
						can_equip_artifact = scope:escorted_artifact
						can_benefit_from_artifact = scope:escorted_artifact
					}
				}
			}
			custom_tooltip = artifact_gift_useless_tt
		}
		else_if = {
			limit = {
				scope:task_contract_target = {
					NOT = { can_equip_artifact = scope:escorted_artifact }
				}
			}
			custom_tooltip = artifact_gift_equip_tt
		}
		else_if = {
			limit = {
				scope:task_contract_target = {
					NOT = { can_benefit_from_artifact = scope:escorted_artifact }
				}
			}
			custom_tooltip = artifact_gift_benefit_tt
		}
		if = {
			limit = { scope:task_contract_target.liege = scope:task_contract_employer }
			custom_tooltip = artifact_gift_vassal_tt
		}
		if = {
			limit = { scope:task_contract_target = scope:task_contract_employer.faith.religious_head }
			custom_tooltip = artifact_gift_religious_head_tt
		}
	}
	scope:task_contract_target = {
		hidden_effect = {
			send_interface_toast = {
				type = event_toast_effect_neutral
				title = gift_artifact_interaction_notification
				left_icon = scope:task_contract_employer
				right_icon = scope:escorted_artifact

				show_as_tooltip = {
					scope:escorted_artifact = { set_owner = scope:task_contract_target }
					if = {
						limit = { is_ai = yes }
						add_opinion = {
							target = scope:task_contract_employer
							modifier = gift_artifact_opinion
							opinion = gift_artifact_opinion
						}
					}

					# Unity stuff, scales with tier # If we're a clan this interaction affects unity
					if = { # In a better world, this would be a switch
						limit = { scope:escorted_artifact = { rarity = Masterwork } } # todo_cd_polish, make the UI show this before you gift
						add_clan_unity_interaction_effect = {
							CHARACTER = scope:task_contract_employer
							TARGET = scope:task_contract_target
							VALUE = minor_unity_gain
							DESC = clan_unity_given_artifact.desc
							REVERSE_NON_HOUSE_TARGET = no
						}
					}
					else_if = {
						limit = { scope:escorted_artifact = { rarity = Famed } }
						add_clan_unity_interaction_effect = {
							CHARACTER = scope:task_contract_employer
							TARGET = scope:task_contract_target
							VALUE = medium_unity_gain
							DESC = clan_unity_given_artifact.desc
							REVERSE_NON_HOUSE_TARGET = no
						}
					}
					else_if = {
						limit = { scope:escorted_artifact = { rarity = Illustrious } }
						add_clan_unity_interaction_effect = {
							CHARACTER = scope:task_contract_employer
							TARGET = scope:task_contract_target
							VALUE = major_unity_gain
							DESC = clan_unity_given_artifact.desc
							REVERSE_NON_HOUSE_TARGET = no
						}
					}
				}
			}
			if = {
				limit = {
					is_ruler = no
					trigger_if = {
						limit = {
							scope:escorted_artifact = {
								artifact_slot_type = primary_armament
							}
						}
						OR = {
							NOT = {
								any_equipped_character_artifact = {
									artifact_slot_type = primary_armament
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = illustrious
								}
								any_equipped_character_artifact = {
									artifact_slot_type = primary_armament
									NOT = {
										rarity = illustrious
									}
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = famed
								}
								any_equipped_character_artifact = {
									artifact_slot_type = primary_armament
									NOR = {
										rarity = illustrious
										rarity = famed
									}
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = masterwork
								}
								any_equipped_character_artifact = {
									artifact_slot_type = primary_armament
									NOR = {
										rarity = illustrious
										rarity = famed
										rarity = masterwork
									}
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = common
								}
								any_equipped_character_artifact = {
									artifact_slot_type = primary_armament
									NOR = {
										rarity = illustrious
										rarity = famed
										rarity = masterwork
										rarity = common
									}
								}
							}
						}
					}
					trigger_if = {
						limit = {
							scope:escorted_artifact = {
								artifact_slot_type = armor
							}
						}
						OR = {
							NOT = {
								any_equipped_character_artifact = {
									artifact_slot_type = armor
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = illustrious
								}
								any_equipped_character_artifact = {
									artifact_slot_type = armor
									NOT = {
										rarity = illustrious
									}
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = famed
								}
								any_equipped_character_artifact = {
									artifact_slot_type = armor
									NOR = {
										rarity = illustrious
										rarity = famed
									}
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = masterwork
								}
								any_equipped_character_artifact = {
									artifact_slot_type = armor
									NOR = {
										rarity = illustrious
										rarity = famed
										rarity = masterwork
									}
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = common
								}
								any_equipped_character_artifact = {
									artifact_slot_type = armor
									NOR = {
										rarity = illustrious
										rarity = famed
										rarity = masterwork
										rarity = common
									}
								}
							}
						}
					}
					trigger_if = {
						limit = {
							scope:escorted_artifact = {
								artifact_slot_type = regalia
							}
						}
						OR = {
							NOT = {
								any_equipped_character_artifact = {
									artifact_slot_type = regalia
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = illustrious
								}
								any_equipped_character_artifact = {
									artifact_slot_type = regalia
									NOT = {
										rarity = illustrious
									}
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = famed
								}
								any_equipped_character_artifact = {
									artifact_slot_type = regalia
									NOR = {
										rarity = illustrious
										rarity = famed
									}
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = masterwork
								}
								any_equipped_character_artifact = {
									artifact_slot_type = regalia
									NOR = {
										rarity = illustrious
										rarity = famed
										rarity = masterwork
									}
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = common
								}
								any_equipped_character_artifact = {
									artifact_slot_type = regalia
									NOR = {
										rarity = illustrious
										rarity = famed
										rarity = masterwork
										rarity = common
									}
								}
							}
						}
					}
					trigger_if = {
						limit = {
							scope:escorted_artifact = {
								artifact_slot_type = helmet
							}
						}
						OR = {
							NOT = {
								any_equipped_character_artifact = {
									artifact_slot_type = helmet
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = illustrious
								}
								any_equipped_character_artifact = {
									artifact_slot_type = helmet
									NOT = {
										rarity = illustrious
									}
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = famed
								}
								any_equipped_character_artifact = {
									artifact_slot_type = helmet
									NOR = {
										rarity = illustrious
										rarity = famed
									}
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = masterwork
								}
								any_equipped_character_artifact = {
									artifact_slot_type = helmet
									NOR = {
										rarity = illustrious
										rarity = famed
										rarity = masterwork
									}
								}
							}
							AND = {
								scope:escorted_artifact = {
									rarity = common
								}
								any_equipped_character_artifact = {
									artifact_slot_type = helmet
									NOR = {
										rarity = illustrious
										rarity = famed
										rarity = masterwork
										rarity = common
									}
								}
							}
						}
					}
				}
				scope:escorted_artifact = {
					equip_artifact_to_owner_replace = yes
				}
			}
		}
	}
}