### Pledge Loyalty to the [GetPlayer.GetLiege.GetTitleTierName|U] ###
pledge_loyalty_to_liege_decision = {
	ai_check_interval_by_tier = {
		barony = 0
		county = 45
		duchy = 45
		kingdom = 45
		empire = 45
		hegemony = 0
	}
	picture = {
		trigger = {
			culture = { has_cultural_parameter = must_pledge_loyalty_to_liege }
			top_liege ?= {
				culture = { has_cultural_parameter = must_pledge_loyalty_to_liege }
			}
		}
		reference = "gfx/interface/illustrations/event_story/tgp_japanese_shogunate.dds"
	}
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_knight_kneeling.dds"
	}
	decision_group_type = major

	cost = {
		gold = {
			value = 0
			if = {
				limit = {
					exists = scope:pledge_loyalty_to_liege_gold
					scope:pledge_loyalty_to_liege_gold = yes
				}
				add = medium_gold_value
			}
		}
	}

	is_shown = {
		is_landed = yes
		primary_title.tier >= tier_county
		is_independent_ruler = no
		liege ?= { has_royal_court = yes }
		culture = { has_cultural_parameter = must_pledge_loyalty_to_liege }
		OR = {
		 	government_has_flag = government_is_feudal
		 	government_has_flag = government_is_clan
		 	government_has_flag = government_is_japan_feudal
		}
	}

	is_valid = {
		custom_tooltip = {
			text = pledge_loyalty_to_liege_grace_valid_tt
			trigger_if = { # Has not Pledged Loyalty to this Liege before
				limit = {
					has_variable = pledge_loyalty_to_liege_grace
				}
				NOT = { var:pledge_loyalty_to_liege_grace = liege }
			}
		}
	}

	is_valid_showing_failures_only = {
		is_available_adult = yes
		liege ?= { is_available_adult = yes }
		is_at_war_with_liege = no
		trigger_if = {
			limit = {
				is_ai = no
				has_royal_court = yes
			}
			has_spawned_court_events = no # should only be used for human players
		}
		trigger_if = { # Has not Pledged Loyalty to this Liege before
			limit = { 
				has_variable = pledge_loyalty_to_liege_grace
			}
			custom_tooltip = {
				text = pledge_loyalty_to_liege_grace_valid_tt
				NOT = { var:pledge_loyalty_to_liege_grace = liege }
			}
		}
	}

	widget = {
		gui = "decision_view_widget_pay_homage"
		controller = decision_option_list_controller
		decision_to_second_step_button = "pledge_loyalty_to_liege_DECISION_NEXT_STEP_BUTTON"

		item = { # Submission
			value = pledge_loyalty_to_liege_submission
			is_valid = {}
			current_description = pledge_loyalty_to_liege_submission_desc
			localization = pledge_loyalty_to_liege_decision_option_submission
			icon = "gfx/interface/icons/icon_liege.dds"
			ai_chance = { # Always likely
				value = 50
				if = { # Generous vassals want to give gold if they can!
					limit = {
						ai_greed <= -50
						gold >= medium_gold_value
					}
					add = -50
				}
			}
		}

		item = { # Hook
			value = pledge_loyalty_to_liege_hook
			is_valid = {
				custom_tooltip = { # Liege does not already have a hook
					text = pledge_loyalty_to_liege_hook_tt
					NOT = {
						liege ?= { has_hook = prev } 
					}
				}
			}
			current_description = pledge_loyalty_to_liege_hook_desc
			localization = pledge_loyalty_to_liege_decision_option_hook
			icon = "gfx/interface/icons/message_feed/hook.dds"
			ai_chance = { # More likely if family
				value = 25
				if = {
					limit = {
						liege ?= { is_close_or_extended_family_of = prev }
					}
					add = 25
				}
				if = { # Certain AI's really do not want to be indebted
					limit = {
						OR = {
							has_trait = paranoid
							has_trait = arrogant
							ai_greed >= 50
						}
					}
					add = -50
				}
			}
		}

		item = { # Contract
			value = pledge_loyalty_to_liege_contract
			is_shown = { government_has_flag = government_is_feudal }
			is_valid = {
				custom_tooltip = { # Contract can be changed
					text = pledge_loyalty_to_liege_contract_modifiable_tt
					vassal_contract_has_modifiable_obligations = yes
				}
				custom_tooltip = { # Contract not at highest
					text = pledge_loyalty_to_liege_contract_increasable_tt
					OR = {
						vassal_contract_obligation_level_can_be_increased = feudal_government_taxes
						vassal_contract_obligation_level_can_be_increased = feudal_government_levies
					}
				}
			}
			current_description = pledge_loyalty_to_liege_contract_desc
			localization = pledge_loyalty_to_liege_decision_option_contract
			icon = "gfx/interface/icons/message_feed/a_catch_all_councillor_icon.dds"
			ai_chance = { # More likely if contract is already lenient
				value = 10
				if = {
					limit = {
						OR = {
							vassal_contract_obligation_level:feudal_government_levies = feudal_levies_low_level
							vassal_contract_obligation_level:feudal_government_taxes = feudal_tax_low_level
						}
					}
					add = 40
				}
				else_if = {
					limit = {
						OR = {
							vassal_contract_obligation_level:feudal_government_levies = feudal_levies_exempt_level
							vassal_contract_obligation_level:feudal_government_taxes = feudal_tax_exempt_level
						}
					}
					add = 90
				}
				
				if = { # Certain AI's really do not want increased obligations
					limit = {
						OR = {
							has_trait = ambitious
							has_trait = arrogant
							ai_greed >= 50
						}
					}
					add = -50
				}
			}
		}

		item = { # Gold
			value = pledge_loyalty_to_liege_gold
			is_valid = {
				custom_tooltip = { # Can afford gold homage
					text = pledge_loyalty_to_liege_gold_tt
					gold >= medium_gold_value # Can afford gold homage
				}
			}
			current_description = pledge_loyalty_to_liege_gold_desc
			localization = pledge_loyalty_to_liege_decision_option_gold
			icon = "gfx/interface/icons/message_feed/money.dds"
			ai_chance = { # More likely if rich
				value = 50
				if = {
					limit = { gold > major_gold_value }
					add = 50
				}
				
				if = { # Certain AI's really do not want to part with gold
					limit = {
						ai_greed >= 50
					}
					add = -100
				}
			}
		}
	}

	effect = {
		set_variable = {
			name = pledge_loyalty_to_liege_scope
			value = liege
		}
		custom_tooltip = pledge_loyalty_to_liege_decision_effects
		custom_description_no_bullet = { text = pledge_loyalty_to_liege_gift_warning_effect }
		if = { # Gift
			limit = { scope:pledge_loyalty_to_liege_gold = yes }
			set_variable = {
				name = homage_type
				value = flag:pledge_loyalty_to_liege_gold
			}
			# used for refunding if travel is canceled mid-way
			set_variable = {
				name = pledge_loyalty_to_liege_gold_value 
				value = medium_gold_value
			}
			custom_tooltip = pledge_loyalty_to_liege_decision_effects_gold
		}
		else_if = { # Hook
			limit = { scope:pledge_loyalty_to_liege_hook = yes }
			set_variable = {
				name = homage_type
				value = flag:pledge_loyalty_to_liege_hook
			}
			custom_tooltip = pledge_loyalty_to_liege_decision_effects_hook
		}
		else_if = { # Contract
			limit = { scope:pledge_loyalty_to_liege_contract = yes }
			set_variable = {
				name = homage_type
				value = flag:pledge_loyalty_to_liege_contract
			}
			if = {
				limit = {
					vassal_contract_obligation_level:feudal_government_levies < vassal_contract_obligation_level:feudal_government_taxes
					vassal_contract_obligation_level_can_be_increased = feudal_government_levies
				}
				custom_tooltip = pledge_loyalty_to_liege_decision_effects_contract_levies
			}
			else_if = {
				limit = {
					vassal_contract_obligation_level:feudal_government_taxes < vassal_contract_obligation_level:feudal_government_levies
					vassal_contract_obligation_level_can_be_increased = feudal_government_taxes
				}
				custom_tooltip = pledge_loyalty_to_liege_decision_effects_contract_gold
			}
			else = {
				if = {
					limit = { vassal_contract_obligation_level_can_be_increased = feudal_government_taxes }
					custom_tooltip = pledge_loyalty_to_liege_decision_effects_contract_gold
				}
				else = { custom_tooltip = pledge_loyalty_to_liege_decision_effects_contract_gold }
			}
			hidden_effect = { set_vassal_contract_modification_blocked = yes }
		}
		else = { # Submission
			set_variable = {
				name = homage_type
				value = flag:pledge_loyalty_to_liege_submission
			}
		}
		show_as_tooltip = {
			switch = {
				trigger = has_trait
				shy = { add_stress = minor_stress_impact_gain }
				arrogant = { add_stress = minor_stress_impact_gain }
			}
		}
		save_scope_as = pledge_loyalty_to_liege_vassal
		if = {
			limit = { exists = liege } # To stop errors
			liege = { save_scope_as = pledge_loyalty_to_liege_liege }
		}
		start_travel_plan = {
			destination = liege.capital_province
			on_start_on_action = pledge_loyalty_to_liege_start
			on_travel_planner_cancel_on_action = pledge_loyalty_to_liege_travel_planner_exit
			on_arrival_event = pledge_loyalty_to_liege.9000
			on_arrival_destinations = all_but_last
		}
	}
	
	ai_potential = {
		is_at_war = no
	}

	ai_will_do = {
		base = 0
		modifier = {
			add = 60
			culture = { has_cultural_parameter = must_pledge_loyalty_to_liege }
		}
		modifier = {
			add = 50
			prestige > standard_activity_base_cost
		}
		modifier = { # AI's who can give gold are more likely
			add = 50
			gold >= medium_gold_value
			ai_greed < 50
			primary_title.tier > tier_county
		}
		modifier = {
			add = -50
			has_relation_rival = liege
		}
		modifier = {
			add = -25
			opinion = {
				target = liege
				value < 0
			}
		}
		modifier = { # Dukes/kings should be more weighted
			add = 25
			primary_title.tier >= tier_duchy
		}
		modifier = { # Barons should be less weighted
			add = -25
			primary_title.tier < tier_county
		}
	}
}

### Petition the Ceremonial Monarch ###
petition_ceremonial_liege_decision = {
	ai_check_interval = 0
	sort_order = 199

	selection_tooltip = {
		first_valid = {
			triggered_desc = {
				trigger = {
					liege = { tgp_is_ceremonial_regent_trigger = yes }
				}
				desc = petition_liege_decision_tooltip_non_royal
			}
			desc = petition_liege_decision_tooltip
		}
	}
	
	title = {
		first_valid = {
			triggered_desc = {
				trigger = { exists = top_liege.primary_title.var:administrative_ui_special_title.holder }
				desc = petition_ceremonial_liege_decision_title
			}
			desc = petition_ceremonial_liege_decision
		}
	}
	desc = petition_ceremonial_liege_decision_desc
	picture = {
		trigger = {
			culture = { has_graphical_east_asia_culture_group_trigger = yes }
		}
		reference = "gfx/interface/illustrations/decisions/tgp_kowtow.dds"
	}
#	picture = {
#		trigger = {
#			NOR = {
#				government_has_flag = government_is_tribal
#				government_has_flag = government_is_nomadic
#				government_has_flag = government_is_theocracy
#				top_liege ?= { government_has_flag = government_is_tribal }
#				top_liege ?= { government_has_flag = government_is_nomadic }
#				top_liege ?= { government_has_flag = government_is_theocracy }
#			}
#			OR = {
#				culture = { has_cultural_pillar = heritage_byzantine }
#				top_liege ?= {
#					OR = {
#						culture = { has_cultural_pillar = heritage_byzantine }
#						capital_barony ?= title:b_constantinople
#						is_roman_emperor_primary_title_trigger = yes
#					}
#				}
#			}
#		}
#		reference = "gfx/interface/illustrations/decisions/ep3_cerimonial_decision.dds"
#	}
	picture = {
		reference = "gfx/interface/illustrations/decisions/decision_knight_kneeling.dds"
	}

	cost = {
		prestige = {
			value = 0
			if = {
				limit = { petition_liege_admin_valid_trigger = no }
				add = standard_activity_base_cost
			}
		}
		influence = {
			value = 0
			if = {
				limit = { petition_liege_admin_valid_trigger = yes }
				add = standard_activity_base_cost
			}
		}
	}

	cooldown = {
		days = standard_petition_liege_cooldown_time
	}

	is_shown = {
		is_playable_character = yes
		has_dlc_feature = royal_court
		tgp_is_ceremonial_petition_shown_trigger = yes
	}

	is_valid_showing_failures_only = {
		is_available_adult = yes
		tgp_is_ceremonial_petition_shown_trigger = yes
		tgp_is_ceremonial_petition_valid_trigger = yes
	}

	widget = {
		gui = "decision_view_widget_petition_liege"
		controller = decision_option_list_controller
		decision_to_second_step_button = "CHOOSE_PETITION_DECISION_NEXT_STEP_BUTTON"
		
		item = { # Regent endorsement
			value = petition_ceremonial_liege_regent
			is_valid = {	
				government_allows = administrative
				is_independent_ruler = no
				trigger_if = {
					limit = { exists = top_liege.primary_title.var:administrative_ui_special_title.holder }
					custom_tooltip = {
						text = petition_ceremonial_liege_regent_already_given_tt
						NOT = { var:petition_liege_regent_support ?= top_liege.primary_title.var:administrative_ui_special_title.holder }
					}
				}
				trigger_else = { always = no }
			}
			current_description = petition_ceremonial_liege_regent_desc
			localization = petition_ceremonial_liege_decision_option_regent
			icon = "gfx/interface/icons/message_feed/tier_up.dds"
			ai_chance = { value = 0 } # Currently unused
		}
		item = { # Appointment support
			value = petition_ceremonial_liege_appointment
			is_valid = { government_allows = administrative }
			current_description = petition_ceremonial_liege_appointment_desc
			localization = petition_ceremonial_liege_decision_option_appointment
			icon = "gfx/interface/icons/message_feed/icon_scheme_promote.dds"
			ai_chance = { value = 0 } # Currently unused
		}
		item = { # Royal wedding
			value = petition_ceremonial_liege_wedding
			is_valid = {
				tgp_is_in_ceremonial_house_trigger = no
			}
			current_description = petition_ceremonial_liege_wedding_desc
			localization = petition_ceremonial_liege_decision_option_wedding
			icon = "gfx/interface/icons/message_feed/marriage.dds"
			ai_chance = { value = 0 } # Currently unused
		}
		item = { # Privilege
			value = petition_ceremonial_liege_privilege
			is_valid = {
				tgp_is_in_ceremonial_house_trigger = no
			}
			current_description = petition_ceremonial_liege_privilege_desc
			localization = petition_ceremonial_liege_decision_option_privilege
			icon = "gfx/interface/icons/message_feed/vassal_contract.dds"
			ai_chance = { value = 0 } # Currently unused
		}
	}

	effect = {
		top_liege.primary_title.var:administrative_ui_special_title.holder = { save_scope_as = ceremonial_liege }
		custom_tooltip = petition_ceremonial_liege_decision_effects
		if = {
			limit = {
				liege = { is_ai = yes }
			}
			custom_description_no_bullet = { text = petition_liege_warning_effect }
		}
		show_as_tooltip = {
			switch = {
				trigger = has_trait 
				shy = { add_stress = medium_stress_impact_gain }
				arrogant = { add_stress = medium_stress_impact_gain }
			}
		}
		switch = {
			trigger = yes
			scope:petition_ceremonial_liege_regent = {
				set_variable = { name = petition_type value = flag:regent }
			}
			scope:petition_ceremonial_liege_appointment = {
				set_variable = { name = petition_type value = flag:appointment }
			}
			scope:petition_ceremonial_liege_wedding = {
				set_variable = { name = petition_type value = flag:wedding }
			}
			scope:petition_ceremonial_liege_privilege = {
				set_variable = { name = petition_type value = flag:privilege }
			}
		}
		if = {
			limit = { location = scope:ceremonial_liege.capital_province }
			trigger_event = { on_action = petition_ceremonial_liege_start }
			trigger_event = petition_liege.9020
		}
		else = {
			trigger_event = { on_action = petition_ceremonial_liege_start }
			start_travel_plan = {
				destination = scope:ceremonial_liege.capital_province
				on_travel_planner_cancel_on_action = petition_ceremonial_liege_travel_planner_exit
				on_arrival_event = petition_liege.9020
				on_arrival_destinations = all_but_last
			}
		}
		add_character_flag = {
			flag = petition_ceremonial_liege_character_flag
			weeks = 2
		}
	}
	
	ai_potential = { always = no } # Only players can do this. AI vassals go through the normal court events/hold court activity.
}
