﻿feudalize_tribal_holding_interaction = {
	category = interaction_category_uncategorized
	hidden = yes
	special_interaction = feudalize_tribal_holding

	desc = feudalize_tribal_holding_interaction_desc

	target_type = title
	target_filter = actor_domain_titles

	auto_accept = yes

	is_shown = {
		scope:actor = {
			this = scope:recipient
			NOT = { government_has_flag = government_is_tribal }
		}
	}

	can_be_picked_title = {
		scope:target = {
			holder = scope:actor
			is_capital_barony = no # we'll use the county in that case
			trigger_if = {
				limit = { tier <= tier_county }
				title_province = {
					has_holding_type = tribal_holding
				}
			}
			trigger_else = {
				custom_description = {
					text = "feudalize_tribal_holding_interaction_title_not_county_or_below"
					always = no
				}
			}
		}
	}

	has_valid_target_showing_failures_only = {
		scope:target = {
			trigger_if = {
				limit = { tier <= tier_county }
				title_province = {
					barony_controller = scope:actor
					has_ongoing_construction = no
				}
			}
		}
	}

	cost = {
		gold = {
			scope:actor = {
				add = {
					value = feudalize_tribal_holding_interaction_cost
					desc = BASE
				}
				if = {
					limit = { # More expensive for admin to make barbarians civilized...
						government_has_flag = government_is_administrative
					}
					add = {
						value = feudalize_tribal_holding_interaction_cost
						multiply = 0.25
						desc = has_admin_government
					}
					if = {
						limit = { # Admin get a bonus for high bureaucracy levels
							OR = {
								has_realm_law = imperial_bureaucracy_2
								has_realm_law = imperial_bureaucracy_3
							}
						}
						subtract = {
							value = feudalize_tribal_holding_interaction_cost
							multiply = {
								value = 0.1
								if = {
									limit = {
										has_realm_law = imperial_bureaucracy_3
									}
									add = 0.15
								}
							}
							desc = imperial_bureaucracy
						}
					}
				}
			}
		}
	}

	on_accept = {
		scope:actor = {
			send_interface_toast = {
				type = event_toast_effect_good
				title = feudalize_tribal_holding_interaction_notification

				right_icon = scope:actor

				if = {
					limit = { exists = scope:target.title_province }
					scope:target.title_province = {
						set_holding_type = castle_holding
					}
				}
			}
		}
	}

	ai_will_do = {
		base = 1500
	}
}

support_feudalize_tribal_holding_interaction = {
	category = interaction_category_vassal
	icon = request_funding_interaction

	desc = support_feudalize_tribal_holding_interaction_desc

	target_type = title
	target_filter = recipient_domain_titles

	auto_accept = yes

	is_shown = {
		scope:actor = {
			NOT = { government_has_flag = government_is_tribal }
		}
		scope:recipient = {
			target_is_liege_or_above = scope:actor
			NOT = { government_has_flag = government_is_tribal }
			NOT = { this = scope:actor }
			any_held_title = {
				tier <= tier_county
				title_province = {
					has_holding_type = tribal_holding
				}
			}
		}
	}

	can_be_picked_title = {
		scope:target = {
			is_capital_barony = no # we'll use the county in that case
			
			trigger_if = {
				limit = { tier <= tier_county }
				title_province = {
					barony_controller = scope:recipient
					has_ongoing_construction = no
				}
			}
		}
	}

	has_valid_target_showing_failures_only = {
		scope:target = {
			trigger_if = {
				limit = { tier <= tier_county }
				title_province = {
					has_holding_type = tribal_holding
				}
			}
			trigger_else = {
				custom_description = {
					text = "feudalize_tribal_holding_interaction_title_not_county_or_below"
					always = no
				}
			}
		}
	}

	cost = {
		gold = {
			scope:actor = {
				add = {
					value = feudalize_tribal_holding_interaction_cost
					desc = BASE
				}
				if = {
					limit = { # More expensive for admin to make these barbarians civilized...
						government_has_flag = government_is_administrative
					}
					add = {
						value = feudalize_tribal_holding_interaction_cost
						multiply = 0.25
						desc = has_admin_government
					}
					if = {
						limit = { # Admin get a bonus for high bureaucracy levels
							OR = {
								has_realm_law = imperial_bureaucracy_2
								has_realm_law = imperial_bureaucracy_3
							}
						}
						subtract = {
							value = feudalize_tribal_holding_interaction_cost
							multiply = {
								value = 0.1
								if = {
									limit = {
										has_realm_law = imperial_bureaucracy_3
									}
									add = 0.15
								}
							}
							desc = imperial_bureaucracy
						}
					}
				}
			}
		}
	}
	
	is_highlighted = {
		# Highlight the interaction if we have the gold to spare
		scope:actor = {
			gold >= {
				value = feudalize_tribal_holding_interaction_cost
				if = {
					limit = { # More expensive for admin to convert these... Barbarians...
						government_has_flag = government_is_administrative
					}
					multiply = {
						value = 1.25
						desc = has_admin_government
					}
				}
			}
		}
	}

	on_accept = {
		scope:recipient = {
			# Add some opinion to make it worthwhile
			add_opinion = {
				target = scope:actor
				modifier = grateful_opinion
				opinion = 15
			}
			
			# Notify actor and recipient
			scope:actor = {
				send_interface_toast = {
					type = event_toast_effect_good
					title = feudalize_tribal_holding_interaction_notification

					left_icon = scope:actor
					right_icon = scope:target

					if = {
						limit = { exists = scope:target.title_province }
						scope:target.title_province = {
							set_holding_type = castle_holding
						}
					}
				}
			}
		}
		scope:actor = {
			# If you are admin, you get some influence for your efforts
			if = {
				limit = { government_has_flag = government_is_administrative }
				change_influence = medium_influence_gain
			}
		}
	}
	
	# The AI doesn't check this very often - So if they have more important expenses, they will put their gold to better use first.
	ai_frequency = 72
	
	ai_targets = {
		ai_recipients = vassals
		max = 10
	}
	
	ai_potential = {
		# Some basic triggers
		is_at_war = no
		is_adult = yes
		# The AI only does this if they have plenty of gold to spare
		gold >= { value = feudalize_tribal_holding_interaction_cost multiply = 4 }
		# Warring AI don't do this - They want to spend their gold on war!
		ai_has_warlike_personality = no
		ai_has_conqueror_personality = no
	}

	ai_will_do = {
		base = 25
		modifier = {
			scope:actor = { ai_has_economical_boom_personality = yes }
			add = 50
		}
	}
}
