﻿bme_turkish_adventurer_conquest = {
	group = event

	combine_into_one = yes
	should_show_war_goal_subview = yes
	mutually_exclusive_titles = { always = yes }

	allowed_for_character = {
		is_ai = yes
	}

	allowed_against_character = {
		always = yes
	}
	target_titles = all
	target_title_tier = duchy
	target_de_jure_regions_above = yes
	ignore_effect = change_title_holder

	valid_to_start = {
		always = no
	}

	should_invalidate = {
		NOT = {
			any_in_list = {
				list = target_titles
				any_in_de_jure_hierarchy = {
					tier = tier_county
					holder = {
						target_is_same_character_or_above = scope:defender
					}
				}
			}
		}
	}

	on_invalidated_desc = msg_religious_war_invalidation_region_message
	
	on_invalidated = {
		scope:attacker = { clean_turkish_adventurer_effect = yes }
	}

	on_declaration = {
		on_declared_war = yes

		# If scope:defender is a player or a king, give them a few choices.
		scope:defender = {
			if = {
				limit = {
					OR = {
						is_ai = no
						highest_held_title_tier >= tier_kingdom
					}
				}
				trigger_event = {
					id = fp1_trade_events.1131
					days = 30
				}
			}
		}
	}

	on_victory_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:attacker = { is_local_player = yes } }
				desc = county_conquest_cb_victory_desc_attacker
			}
			desc = county_conquest_cb_victory_desc
		}
	}

	on_victory = {
		create_title_and_vassal_change = {
			type = conquest
			save_scope_as = change
			add_claim_on_loss = yes
		}

		# go through the dejure hierarchy under target titles, transfer titles with same or worse tolerance holders (their religion equally or less tolerated than the defender's),
		# take the holder as vassal otherwise and don't go deeper
		every_in_list = {
			list = target_titles
			custom_tooltip = CONQUEST_CB_TITLE
			every_in_de_jure_hierarchy = {
				continue = {
					OR = {
						NOT = {	exists = holder }
						NOT = {
							holder = { target_is_liege_or_above = scope:defender }
						}
						AND = {
							scope:attacker.faith = {
								faith_hostility_level_comparison = { prev.holder.faith <= scope:defender.faith }
							}
							holder = {
								OR = {
									this = scope:defender
									target_is_liege_or_above = scope:defender
								}
							}
						}
					}
					tier > tier_county
				}
				limit = {
					tier <= tier_duchy
					exists = holder
					holder = {
						OR = {
							this = scope:defender
							target_is_liege_or_above = scope:defender
						}
					}
				}

				if = {
					limit = {
						OR = {
							scope:attacker.faith = {
								faith_hostility_level_comparison = {
									prev.holder.faith >= scope:defender.faith
								}
							}
							holder.primary_title.tier >= scope:attacker.primary_title.tier
						}
					}
					add_to_temporary_list = titles_taken
				}
				else = {
					holder = {
						add_to_temporary_list = vassals_taken
					}
				}
			}
		}

		every_in_list = {
			list = titles_taken
			change_title_holder = {
				holder = scope:attacker
				change = scope:change
				take_baronies = yes
			}
		}

		every_in_list = {
			list = vassals_taken
			change_liege = {
				liege = scope:attacker
				change = scope:change
			}
		}

		resolve_title_and_vassal_change = scope:change

		# Prestige Progress for the Attacker
		every_in_list = {
			list = target_titles
			scope:attacker = {
				add_prestige_experience = medium_prestige_value
			}
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker
			FAME_BASE = medium_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		# Truce
		add_truce_attacker_victory_effect = yes

		#Clean-up adventurer things.
		scope:attacker = { clean_turkish_adventurer_effect = yes }

		# FP1: note the victory for future memorialisation via stele (if applicable).
		scope:attacker = { fp1_remember_recent_conquest_victory_effect = yes }
	}

	on_white_peace_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = county_conquest_cb_white_peace_desc_defender
			}
			desc = county_conquest_cb_white_peace_desc
		}
	}

	on_white_peace = {
		# Prestige loss for the attacker
		scope:attacker = {
			add_prestige = {
				value = minor_prestige_value
				multiply = -1.0
			}
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:attacker # Not important as the scales are identical
			FAME_BASE = medium_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		add_truce_white_peace_effect = yes

		#Clean-up adventurer things.
		scope:attacker = { clean_turkish_adventurer_effect = yes }

		# Since they failed to win, take away their efficacy loan.
		scope:attacker = { remove_short_term_gold = scandinavian_adventurer_efficacy_loan_value }
	}

	on_defeat_desc = {
		first_valid = {
			triggered_desc = {
				trigger = { scope:defender = { is_local_player = yes } }
				desc = county_conquest_cb_defeat_desc_defender
			}
			triggered_desc = {
				trigger = {
					scope:attacker = {
						is_local_player = yes
						has_targeting_faction = yes
					}
				}
				desc = county_conquest_cb_defeat_desc_attacker
			}
			desc = county_conquest_cb_defeat_desc
		}
	}

	on_defeat = {

		# Prestige loss for the attacker
		scope:attacker = {
			pay_short_term_gold_reparations_effect = {
				GOLD_VALUE = 5
			}
			add_prestige = {
				value = major_prestige_value
				multiply = -1.0
			}
		}

		# Prestige for Defender
		scope:defender = {
			add_prestige_war_defender_effect = {
				PRESTIGE_VALUE = major_prestige_value
			}
		}

		# Allies on both sides get full prestige value for helping in the war, based on their war participation.
		modify_allies_of_participants_fame_values = {
			WINNER = scope:defender
			FAME_BASE = medium_prestige_value
			IS_RELIGIOUS_WAR = no
			WINNER_ALLY_FAME_SCALE = 1
			LOSER_ALLY_FAME_SCALE = 1
		}

		add_truce_attacker_defeat_effect = yes

		scope:attacker = {
			save_temporary_scope_as = loser
		}

		on_lost_aggression_war_discontent_loss = yes

		# Give scope:defender some goodies.
		scope:defender = {
			# Including a break from the fighting.
			fp1_add_realm_size_appropriate_sa_reprieve_effect = yes
			# And a tally towards a nickname.
			## Which starts...
			if = {
				limit = {
					NOT = { has_variable = fp1_has_beaten_sa_progress }
				}
				set_variable = {
					name = fp1_has_beaten_sa_progress
					value = 1
				}
			}
			## ... and grows...
			else_if = {
				limit = {
					has_variable = fp1_has_beaten_sa_progress
					var:fp1_has_beaten_sa_progress <= 9
				}
				change_variable = {
					name = fp1_has_beaten_sa_progress
					add = 1
				}
			}
			## ... towards a pay-off.
			else_if = {
				limit = {
					OR = {
						has_bad_nickname = yes
						has_any_nickname = no
					}
				}
				hidden_effect = {
					random_list = {
						20 = { give_nickname = nick_the_bane_of_danes }
						20 = { give_nickname = nick_the_culture_wall }
						20 = { give_nickname = nick_nord_foe }
						20 = { give_nickname = nick_the_scourge_of_the_northerners }
						20 = { give_nickname = nick_the_burner_of_ash }
					}
				}
			}
		}

		#Clean-up adventurer things.
		scope:attacker = { clean_turkish_adventurer_effect = yes }

		# Since they lost, take away their efficacy loan.
		scope:attacker = { remove_short_term_gold = scandinavian_adventurer_efficacy_loan_value }
	}

	transfer_behavior = transfer

	on_primary_attacker_death = inherit
	on_primary_defender_death = inherit

	attacker_allies_inherit = yes
	defender_allies_inherit = yes

	war_name = "CONQUEST_WAR_NAME"
	war_name_base = "CONQUEST_WAR_NAME_BASE"
	cb_name = "CONQUEST_DUCHY_CB_NAME"

	interface_priority = 79

	use_de_jure_wargoal_only = yes

	attacker_wargoal_percentage = 0.8
}
