﻿@ai_skill_diff_upper_threshold = 8
@ai_skill_diff_lower_threshold = 2

sas_embezzle_interaction = {
	category = interaction_category_hostile
	icon = sas_embezzle
	interface_priority = 12
	common_interaction = yes
	cooldown = { years = 3 }

	desc = sas_embezzle_interaction_desc

	is_shown = {
		OR = {
			has_game_rule = sas_all_modified_perks
			has_game_rule = spy_and_interactions
		}
		NOT = { scope:actor = scope:recipient }
		# Make sure scope:recipient is suitable.
		scope:actor = {
			has_perk = golden_obligations_perk
			is_landed = yes
			OR = {
				any_neighboring_and_across_water_top_liege_realm_owner = {
					this = scope:recipient
				}
				liege = scope:recipient
				scope:actor.liege = scope:recipient.liege
				any_vassal = {
					this = scope:recipient
				}
			}
		}
	}
	
	is_valid_showing_failures_only = {
		# Target must be at least count
		scope:recipient = {
			highest_held_title_tier >= tier_county
		}
		# Cannot be at war with the target
		NOT = {
			scope:actor = { is_at_war_with = scope:recipient }
		}
		scope:actor = {
			stewardship >= 8
		}
		# Must have selected an amount to embezzle.
		trigger_if = {
			# AI won't exploit and need this exception, as they don't actually open the windows, so they'll be blocked from taking the interaction otherwise.
			limit = {
				scope:actor = { is_ai = no }
			}
				custom_tooltip = {
				text = sas_embezzle_interaction.tt.must_embezzle_something
				OR = {
					scope:sas_embezzle_sum_small = yes
					scope:sas_embezzle_sum_medium = yes
					scope:sas_embezzle_sum_large = yes
					scope:sas_embezzle_sum_huge = yes
				}
			}
		}
	}

	# Attempt Methods
	send_options_exclusive = yes
	## Steal a small sum.
	### Bonus to scope:actor.
	send_option = {
		flag = sas_embezzle_sum_small
		# We're doing gross overrides here so that we don't need to relocalise some perfectly good stuff.
		localization = sas_embezzle_sum_small_name
		current_description = sas_embezzle_sum_small
	}
	## Steal a moderate sum.
	### No bonus, no malus.
	send_option = {
		flag = sas_embezzle_sum_medium
		# We're doing gross overrides here so that we don't need to relocalise some perfectly good stuff.
		localization = sas_embezzle_sum_medium_name
		current_description = sas_embezzle_sum_medium
		starts_enabled = { always = yes }
	}
	## Steal a large sum.
	### Malus to scope:actor
	send_option = {
		flag = sas_embezzle_sum_large
		# We're doing gross overrides here so that we don't need to relocalise some perfectly good stuff.
		localization = sas_embezzle_sum_large_name
		current_description = sas_embezzle_sum_large
	}
	## Steal a _huge_sum.
	### _Huge_ malus to scope:actor.
	send_option = {
		flag = sas_embezzle_sum_huge
		# We're doing gross overrides here so that we don't need to relocalise some perfectly good stuff.
		localization = sas_embezzle_sum_huge_name
		current_description = sas_embezzle_sum_huge
	}

	on_accept = {
		# Time for a skill duel!
		scope:actor = {
			duel = {
				skill = stewardship
				value = scope:recipient.stewardship
				# Victory! Gold for scope:actor, maluses for the liege.
				50 = {
					compare_modifier = {
						value = scope:duel_value
						multiplier = 3.5
						min = -49
					}
					# Bonus for smaller amounts.
					modifier = {
						add = 25
						always = scope:sas_embezzle_sum_small
					}
					# You always have some kind of a chance.
					min = 10
					desc = sas_embezzle_interaction.tt.success
					# Now we sort the actual effects.
					## A nice toast to tell us about our new gold.
					send_interface_toast = {
						type = event_toast_effect_good
						title = sas_embezzle_interaction.tt.success
						left_icon = scope:recipient
						# Transfer gold.
						## Small amount.
						if = {
							limit = { always = scope:sas_embezzle_sum_small }
							give_or_update_embezzler_secret_effect = {
								TARGET = scope:recipient
								STAKE = syphon_treasury_gold_gain_low
							}
							add_gold = syphon_treasury_gold_gain_low
						}
						## Medium amount.
						else_if = {
							limit = { always = scope:sas_embezzle_sum_medium }
							give_or_update_embezzler_secret_effect = {
								TARGET = scope:recipient
								STAKE = syphon_treasury_gold_gain_medium
							}
							add_gold = syphon_treasury_gold_gain_medium
						}
						## Large amount.
						else_if = {
							limit = { always = scope:sas_embezzle_sum_large }
							give_or_update_embezzler_secret_effect = {
								TARGET = scope:recipient
								STAKE = syphon_treasury_gold_gain_high
							}
							add_gold = syphon_treasury_gold_gain_high
						}
						## Huge amount.
						else_if = {
							limit = { always = scope:sas_embezzle_sum_huge }
							give_or_update_embezzler_secret_effect = {
								TARGET = scope:recipient
								STAKE = syphon_treasury_gold_gain_very_high
							}
							add_gold = syphon_treasury_gold_gain_very_high
						}
					}
				}
				# Defeat! No gold, just strife & a crime opinion.
				50 = {
					compare_modifier = {
						value = scope:duel_value
						multiplier = -3.5
						min = -49
					}
					# Malus for higher amounts.
					modifier = {
						add = 25
						always = scope:sas_embezzle_sum_large
					}
					# Malus for tribal
					modifier = {
						add = 25
					}
					# Chungus malus for chungus higher amounts.
					modifier = {
						add = 100
						always = scope:sas_embezzle_sum_huge
					}
					# You can always fail.
					min = 20
					desc = sas_embezzle_interaction.tt.failure.actor
					send_interface_toast = {
						type = event_toast_effect_bad
						title = sas_embezzle_interaction.tt.failure.actor
						left_icon = scope:recipient
						show_as_tooltip = { sas_embezzle_interaction_caught_embezzling_effect = yes }
					}
					hidden_effect = {
						scope:recipient = {
							send_interface_toast = {
								type = event_toast_effect_neutral
								title = sas_embezzle_interaction.tt.failure.recipient
								left_icon = scope:actor
								sas_embezzle_interaction_caught_embezzling_effect = yes
							}
						}
					}
					if = {
						limit = { 
							scope:actor = {
								any_vassal_or_below = {
									this = scope:recipient
								}
							}
						}
						add_tyranny = major_tyranny_value
					}
				}
			}
		}
	}

	auto_accept = yes

	# AI
	ai_targets = {
		ai_recipients = liege
		ai_recipients = vassals
		ai_recipients = neighboring_rulers
	}

	ai_frequency_by_tier = {
		barony = 0
		county = 36
		duchy = 36
		kingdom = 36
		empire = 36
		hegemony = 36
	}

	ai_potential = {
		# Honest and generous characters never embezzle.
		NOR = {
			has_trait = honest
			has_trait = generous
		}
		has_perk = golden_obligations_perk
	}

	ai_will_do = {
		# Embezzlement is serious, so you need actual reasons for it.
		base = 0

		# Additions.
		## Weight for opinion.
		opinion_modifier = {
			opinion_target = scope:recipient
			multiplier = -1
		}
		## Weight for personality.
		ai_value_modifier = {
			ai_boldness = 0.25
			ai_greed = 1
			ai_honor = -0.5
		}
		## Almost never against own subjects
		modifier = {
			add = -500
			scope:actor = {
				any_vassal_or_below = {
					this = scope:recipient
				}
			}
		}
		## Flag Management.
		### The AI never goes for a huge sum, that's just there to tempt the player.
		### We prefer a large sum if we've got a massive skill disparity.
		modifier = {
			add = 100
			scope:sas_embezzle_sum_large = yes
			stewardship_diff = {
				target = scope:recipient
				value >= @ai_skill_diff_upper_threshold
			}
		}
		### But a moderate sum does well if we're more evenly matched.
		modifier = {
			add = 50
			scope:sas_embezzle_sum_medium = yes
			stewardship_diff = {
				target = scope:recipient
				value < @ai_skill_diff_upper_threshold
			}
			stewardship_diff = {
				target = scope:recipient
				value >= @ai_skill_diff_lower_threshold
			}
		}
		### Aaaand otherwise we'll accept a small amount.
		modifier = {
			add = 25
			scope:sas_embezzle_sum_small = yes
			stewardship_diff = {
				target = scope:recipient
				value < @ai_skill_diff_lower_threshold
			}
		}
		## Less likely in tribal societies with less bookwork to hide theft in.
		modifier = {
			add = -50
			scope:recipient = { government_has_flag = government_is_tribal }
		}
		## Debt is a great motivator for theft.
		### Is slightly in debt.
		modifier = {
			add = 10
			debt_level = 1
		}
		### Is one year in debt.
		modifier = {
			add = 25
			debt_level = 2
		}
		### Is two years in debt.
		modifier = {
			add = 40
			debt_level = 3
		}
		### Is three years in debt.
		modifier = {
			add = 60
			debt_level = 4
		}
		### Is four years in debt.
		modifier = {
			add = 100
			debt_level = 5
		}
		### Is over five years in debt.
		modifier = {
			add = 200
			debt_level >= 6
		}
		## Certain traits can affect likelihood directly.
		### It's intentional that these don't sync up value-wise with their opposite traits.
		### Honest/generous excluded because they're hard locked out of the interaction for the AI.
		### +++ Greedy.
		modifier = {
			add = 50
			has_trait = greedy
		}
		### ++ Deceitful.
		modifier = {
			add = 30
			has_trait = deceitful
		}
		### + Arbitrary.
		modifier = {
			add = 10
			has_trait = arbitrary
		}
		### -- Just.
		modifier = {
			add = 30
			has_trait = just
		}
		## Skill considerations.
		### Very low negative comparative stewardship.
		modifier = {
			add = -50
			stewardship_diff = {
				target = scope:recipient
				value <= 10
			}
			# Filter out arrogant characters, who can't believe they suck.
			NOT = { has_trait = arrogant }
		}
		### Low negative comparative stewardship.
		modifier = {
			add = -25
			stewardship_diff = {
				target = scope:recipient
				value <= 5
			}
			# Filter out arrogant characters, who can't believe they suck.
			NOT = { has_trait = arrogant }
		}
		### High positive comparative stewardship.
		modifier = {
			add = 25
			stewardship_diff = {
				target = scope:recipient
				value >= 5
			}
			# Filter out humble characters, who give their liege too much credit.
			NOT = { has_trait = humble }
		}
		### Very high positive comparative stewardship.
		modifier = {
			add = 50
			stewardship_diff = {
				target = scope:recipient
				value >= 10
			}
			# Filter out humble characters, who give their liege too much credit.
			NOT = { has_trait = humble }
		}
		# Factors.
		## + Do pick on people we hate.
		modifier = {
			factor = 1.5
			should_prioritise_hostile_action_against_due_to_personal_relations_trigger = {
				ACTOR = scope:actor
				RECIPIENT = scope:recipient
			}
		}
		## x0 Don't pick on our friends or allies.
		modifier = {
			factor = 0
			should_avoid_hostile_action_against_due_to_personal_relations_trigger = {
				ACTOR = scope:actor
				RECIPIENT = scope:recipient
			}
		}
		## We're happy to bully players here, since we're not taking their land.
	}
}