﻿######################
# Legitimacy
######################

#standard_activity_participation_legitimacy_effect
#legendary_hunt_participation_legitimacy_effect
#grand_activity_participation_legitimacy_effect
#war_end_legitimacy_effect
#faction_war_end_legitimacy_effect
#faction_accept_demand_legitimacy_effect
#excommunication_legitimacy_effect
#hostile_scheme_exposure_legitimacy_effect
#epidemic_outbreak_legitimacy_effect
#epidemic_endemic_capital_legitimacy_effect
#disburse_legend_protagonist_legitimacy_effect
#base_legitimacy_intent_reward - Give a small amount of legitimacy, just in case players didn't get any events relating to it

add_legitimacy_effect = {
	if = {
		limit = { is_valid_for_legitimacy_change = yes }
		add_legitimacy = $LEGITIMACY$
	}
}

# LEGITIMACY GAIN FOR STANDARD ACTIVITY PARTICIPATION
standard_activity_participation_legitimacy_effect = {
	if = {
		limit = { is_valid_for_legitimacy_change = yes }
		if = {
			limit = {
				this = scope:host
			}
			add_legitimacy = minor_legitimacy_gain
		}
		else = {
			add_legitimacy = miniscule_legitimacy_gain
		}
	}
}

# LEGITIMACY GAIN FOR LEGENDARY HUNT PARTICIPATION
legendary_hunt_participation_legitimacy_effect = {
	if = {
		limit = { is_valid_for_legitimacy_change = yes }
		if = {
			limit = {
				this = scope:host
			}
			add_legitimacy = major_legitimacy_gain
		}
		else = {
			add_legitimacy = medium_legitimacy_gain
		}
	}
}

# LEGITIMACY GAIN FOR GRAND ACTIVITY PARTICIPATION
grand_activity_participation_legitimacy_effect = {
	if = {
		limit = { is_valid_for_legitimacy_change = yes }
		if = {
			limit = {
				scope:activity = {
					has_activity_type = activity_tour
					has_activity_option = {
						category = special_type
						option = tour_type_majesty
					}
				}
			}
			add_legitimacy = major_legitimacy_gain
		}
		else_if = {
			limit = {
				this = scope:host
			}
			add_legitimacy = medium_legitimacy_gain
		}
		else = {
			add_legitimacy = minor_legitimacy_gain
		}
	}
}

# LEGITIMACY GAIN FOR WINNING A WAR
add_legitimacy_attacker_victory_effect = {
	war_end_legitimacy_effect = {
		WINNER = scope:attacker
		LOSER = scope:defender
	}
}

add_legitimacy_attacker_defeat_effect = {
	war_end_legitimacy_effect = {
		WINNER = scope:defender
		LOSER = scope:attacker
	}
}

war_end_legitimacy_effect = {
	# Special rewards for Temüjin vs Jamukha
	if = {
		limit = {
			always = no
			#scope:attacker = {
			#	OR = {
			#		this = character:125501
			#		this = character:mpo_mongol_1
			#	}
			#	has_variable = temujin_jamukha_war_var
			#}
			#scope:defender = {
			#	OR = {
			#		this = character:125501
			#		this = character:mpo_mongol_1
			#	}
			#}
		}
		scope:attacker = {
			add_legitimacy = 500
		}
	}
	else = {
		if = {
			limit = {
				any_in_list = { list = target_titles }
			}
			ordered_in_list = {
				list = target_titles
				order_by = tier
				save_scope_as = target_title
				save_scope_value_as = {
					name = title_tier_minus_1
					value = {
						value = scope:target_title.tier
						subtract = 1
					}
				}
			}
		}
		$WINNER$ = {
			if = {
				limit = {
					is_valid_for_legitimacy_change = yes
					primary_title.tier <= $LOSER$.primary_title.tier
				}
				send_interface_toast = {
					type = msg_legitimacy_gain
					title = legitimacy_gain_toast
					left_icon = $WINNER$
					right_icon = $LOSER$
					
					add_legitimacy = {
						value = {
							value = 0
							
							if = { # You get a lot of Legitimacy for punching above your weight
								limit = { # Specially if you are a Duke/Count against an Emperor
									OR = {
										primary_title.tier = tier_county
										primary_title.tier = tier_duchy
									}
									$LOSER$.primary_title.tier = tier_empire
								}
								value = 200
							}
							else_if = { # Or a Count against a King
								limit = {
									primary_title.tier = tier_county
									$LOSER$.primary_title.tier = tier_kingdom
								}
								value = 150
							}
							else_if = { # Or a Count vs a Duke
								limit = {
									primary_title.tier = tier_county
									$LOSER$.primary_title.tier = tier_duchy
								}
								value = 100
							}
							else_if = { # Or a Duke vs a King
								limit = {
									primary_title.tier = tier_duchy
									$LOSER$.primary_title.tier = tier_kingdom
								}
								value = 100
							}
							else_if = { # Or King vs Emperor
								limit = {
									primary_title.tier = tier_kingdom
									$LOSER$.primary_title.tier = tier_empire
								}
								value = 100
							}
							else_if = { # Special Legitimacy gain for same tier
								limit = {
									primary_title.tier = $LOSER$.primary_title.tier
								}
								value = 50
							}
						}
						
						if = { # We give you extra Legitimacy for Religious Wars
							limit = {
								root.war ?= {
									OR = {
										using_cb = minor_religious_war
										using_cb = religious_war
									}
								}
							}
							multiply = 2
						}
						else_if = { # And even more for Holy Wars
							limit = {
								root.war ?= {
									OR = {
										using_cb = major_religious_war
										using_cb = undirected_great_holy_war
										using_cb = directed_great_holy_war
									}
								}
							}
							multiply = 3
						}
						
						#if = { # We give you extra Legitimacy for special On Start Wars
						#	limit = {
						#		root.war ?= {
						#			OR = {
						#				using_cb = norman_conquest_cb
						#				using_cb = norwegian_invasion_cb
						#				using_cb = sons_of_lothbrok_invasion_cb
						#			}
						#		}
						#	}
						#	multiply = 4
						#}
						
						if = { # And we 50% increase whatever you have if you are a nomad
							limit = {
								government_has_flag = government_is_nomadic
							}
							multiply = 1.5
						}
					}
				}
			}
			# Special extra Legitimacy gain for Byzantine rulers since their procedence didn't matter as much, to prevent them from always having 0 Legitimacy basically
			#hidden_effect = {
			#	if = {
			#		limit = {
			#			exists = scope:target_title
			#			scope:target_title = title:e_byzantium
			#			$WINNER$ = { is_valid_for_legitimacy_change = yes }
			#		}
			#		add_legitimacy = 1000
			#	}
			#}
		}
	}
}

# LEGITIMACY FOR LOSING A FACTION WAR
faction_war_end_defeat_legitimacy_effect = {
	scope:attacker = {
		if = {
			limit = { is_valid_for_legitimacy_change = yes }
			send_interface_toast = {
				type = msg_legitimacy_gain
				title = legitimacy_gain_toast
				left_icon = scope:attacker
				right_icon = scope:defender
				add_legitimacy = minor_legitimacy_gain
			}
		}
	}
	scope:defender = {
		if = {
			limit = { is_valid_for_legitimacy_change = yes }
			if = {
				limit = {
					scope:attacker = {
						OR = {
							is_leading_faction_type = peasant_faction
							is_leading_faction_type = claimant_faction
							is_leading_faction_type = populist_faction
						}
					}
				}
				send_interface_toast = {
					type = event_toast_effect_bad
					title = legitimacy_loss_toast
					left_icon = scope:defender
					right_icon = scope:attacker
					add_legitimacy = major_legitimacy_loss
				}
			}
			else = {
				send_interface_toast = {
					type = event_toast_effect_bad
					title = legitimacy_loss_toast
					left_icon = scope:defender
					right_icon = scope:attacker
					if = {
						limit = {
							has_nomadic_legitimacy = yes
						}
						add_legitimacy = {
							value = medium_legitimacy_loss
							multiply = 2
						}
					}
					else = {
						add_legitimacy = medium_legitimacy_loss
					}
				}
			}
		}
	}
}

# LEGITIMACY FOR WINNING A FACTION WAR
faction_war_end_victory_legitimacy_effect = {
	scope:defender = {
		if = {
			limit = { is_valid_for_legitimacy_change = yes }
			send_interface_toast = {
				type = msg_legitimacy_gain
				title = legitimacy_gain_toast
				left_icon = scope:attacker
				right_icon = scope:defender
				add_legitimacy = minor_legitimacy_gain
			}
		}
	}
}

# LEGITIMACY LOSS FOR ACCEPTING FACTION DEMANDS
faction_accept_demand_legitimacy_effect = {
	if = {
		limit = { is_valid_for_legitimacy_change = yes }
		add_legitimacy = minor_legitimacy_loss
	}
}

# LEGITIMACY LOSS FROM EXCOMMUNICATION
excommunication_legitimacy_effect = {
	if = {
		limit = { is_valid_for_legitimacy_change = yes }
		send_interface_toast = {
			type = msg_legitimacy_loss
			title = legitimacy_loss_toast
			left_icon = scope:target_character
			right_icon = scope:requesting_character
			add_legitimacy = major_legitimacy_loss
		}
	}
}

# LEGITIMACY LOSS FROM HOSTILE SCHEME EXPOSURE
hostile_scheme_exposure_legitimacy_effect = {
	if = {
		limit = { is_valid_for_legitimacy_change = yes }
		add_legitimacy = minor_legitimacy_loss
	}
}

# LEGITIMACY LOSS FROM EPIDEMIC OUTBREAK
epidemic_outbreak_legitimacy_effect = {
	save_scope_as = epidemic
	$PROVINCE$.county.holder = {
		save_scope_as = outbreak_top_liege
		if = { 
			#EK EDIT: Exclusion for the Veneration of Disease tenet
			limit = { 
				is_valid_for_legitimacy_change = yes
				NOT = { faith = { has_doctrine_parameter = celebrated_disease } }
			}
			#EK EDIT END
			if = {
				limit = {
					scope:epidemic = { outbreak_intensity = major }
				}
				add_legitimacy = {
					value = miniscule_legitimacy_loss
					multiply = $PROVINCE$.county.holder.primary_title.tier
				}
			}
			else_if = {
				limit = {
					scope:epidemic = { outbreak_intensity = apocalyptic }
				}
				add_legitimacy = {
					value = minor_legitimacy_loss
					multiply = $PROVINCE$.county.holder.primary_title.tier
				}
			}
		}
	}
}

# LEGITIMACY LOSS FROM EPIDEMIC OUTBREAK
epidemic_endemic_capital_legitimacy_effect = {
	save_scope_as = endemic_capital
	province_owner = {
		if = {
			limit = { is_valid_for_legitimacy_change = yes }
			send_interface_toast = {
				type = msg_legitimacy_loss
				title = legitimacy_loss_toast
				left_icon = scope:endemic_capital.province_owner
				right_icon = scope:endemic_capital.barony
				custom_tooltip = legitimacy_loss_endemic_capital_tt
				add_legitimacy = miniscule_legitimacy_loss
			}
		}
	}
}

base_legitimacy_intent_reward = {
	if = {
		limit = {
			has_activity_intent = legitimacy_intent
		}
		add_legitimacy = minor_legitimacy_gain
	}
}

# Achievements
claim_title_achievement_effect = {
	if = {
		limit = {
			is_ai = no
			scope:actor.legitimacy_level >= scope:recipient.legitimacy_level
		}
		add_achievement_global_variable_effect = {
			VARIABLE = ce1_upward_mobility_achievement_unlocked
			VALUE = yes
		}
	}
}

# Legitimacy Protagonist gain from Legends
disburse_legend_protagonist_legitimacy_effect = {
	# Legitimacy gain based on Protagonist
	if = {
		limit = {
			scope:protagonist = {
				NOT = { this = root } # The legend owner
				num_virtuous_traits >= 3
			}
		}
		custom_tooltip = {
			text = legend_protagonist_legitimacy_major_tt
			add_legitimacy = major_legitimacy_gain
		}
	}
	else_if = {
		limit = {
			scope:protagonist = {
				NOT = { this = root } # The legend owner
				num_virtuous_traits >= 2
			}
		}
		custom_tooltip = {
			text = legend_protagonist_legitimacy_medium_tt
			add_legitimacy = medium_legitimacy_gain
		}
	}
	else_if = {
		limit = {
			scope:protagonist = {
				NOT = { this = root } # The legend owner
				num_virtuous_traits >= 1
			}
		}
		custom_tooltip = {
			text = legend_protagonist_legitimacy_minor_tt
			add_legitimacy = minor_legitimacy_gain
		}
	}
	else_if = { # No Virtuous traits but at least you didn't choose yourself
		limit = {
			scope:protagonist = {
				NOT = { this = root }
			}
		}
		custom_tooltip = {
			text = legend_protagonist_legitimacy_miniscule_tt
			add_legitimacy = miniscule_legitimacy_gain
		}
	}
}
