﻿task_foreign_affairs = {
	default_task = yes
	position = councillor_chancellor
	task_type = task_type_general
	task_progress = task_progress_infinite

	effect_desc = {
		### Prestige tooltip
		desc = task_foreign_affairs_prestige_effect_desc
		# Adaptive Traditions tooltip
		triggered_desc = {
			trigger = { liege = { has_perk = adaptive_traditions_perk } }
			desc = task_foreign_affairs_effect_desc_adaptive_traditions_bonus_prestige
		}
		# Bureaucrats tooltip
		triggered_desc = {
			trigger = {
				exists = liege.dynasty
				liege.dynasty = { has_dynasty_perk = erudition_legacy_5 } }
			desc = task_foreign_affairs_effect_desc_bureaucrats_bonus_prestige
		}
		# Opinion tooltip
		first_valid = {
			triggered_desc = {
				trigger = { liege = { is_independent_ruler = no } }
				desc = {
					desc = task_foreign_affairs_fallback_effect_desc
					desc = task_foreign_affairs_not_independent_effect_desc
				}
			}
			desc = task_foreign_affairs_independent_effect_desc
		}
		# Adaptive Traditions tooltip
		triggered_desc = {
			trigger = { liege = { has_perk = adaptive_traditions_perk } }
			desc = task_foreign_affairs_effect_desc_adaptive_traditions_bonus_opinion
		}
		# Bureaucrats tooltip
		triggered_desc = {
			trigger = {
				exists = liege.dynasty
				liege.dynasty = { has_dynasty_perk = erudition_legacy_5 } }
			desc = task_foreign_affairs_effect_desc_bureaucrats_bonus_opinion
		}

		desc = {
			desc = council_task_possible_side_effects
			triggered_desc = {
				trigger = {
					diplomacy > mediocre_skill_rating
				}
				desc = task_foreign_affairs_shorten_truce
			}
			triggered_desc = {
				trigger = {
					diplomacy < high_skill_rating
					NOT = { liege = { has_government = patrician_government } }
				}
				desc = task_foreign_affairs_enemy_claim
			}
			triggered_desc = {
				trigger = {
					diplomacy < high_skill_rating
				}
				desc = task_foreign_shorten_enemy_truce
			}
			triggered_desc = {
				trigger = {
					diplomacy < high_skill_rating
				}
				desc = task_foreign_neighbor_opinion_loss
			}
		}
	}

	on_start_task = {
		set_variable = {
			name = chancellor_foreign_affairs_opinion_value
			value = 0
		}
		liege = {
			add_character_flag = {
				flag = no_chancellor_side_effects_first_month
				days = 30
			}
		}
	}

	on_cancel_task = {
		remove_variable = chancellor_foreign_affairs_opinion_value
	}

	council_owner_modifier = {
		name = task_foreign_affairs_prestige_modifier
		monthly_prestige = 1
		scale = chancellor_foreign_affairs_prestige_gain_base
	}

	council_owner_modifier = {
		name = task_foreign_affairs_prestige_perk_bonus_modifier
		monthly_prestige = 1
		scale = chancellor_foreign_affairs_prestige_gain_perk_bonus_scale
	}

	council_owner_modifier = {
		name = task_foreign_affairs_prestige_dynasty_perk_bonus_modifier
		monthly_prestige = 1
		scale = chancellor_foreign_affairs_prestige_gain_erudition_modifier
	}

	council_owner_modifier = {
		name = task_foreign_affairs_opinion_modifier
		independent_ruler_opinion = 1
		scale = chancellor_foreign_affairs_independent_opinion_scale
	}

	council_owner_modifier = {
		name = task_foreign_affairs_opinion_modifier
		fellow_vassal_opinion = 1
		scale = chancellor_foreign_affairs_realm_opinion_scale
	}

	on_monthly = {
		if = {
			limit = {
				liege = { is_ai = no }
			}
			if = {
				limit = {
					liege = { has_variable = chancellor_foreign_affairs_opinion_value }
				}
				if = {
					limit = {
						liege.var:chancellor_foreign_affairs_opinion_value < chancellor_foreign_affairs_opinion_max
					}
					liege = {
						change_variable = {
							name = chancellor_foreign_affairs_opinion_value
							add = root.chancellor_foreign_affairs_monthly_increase
						}
					}
				}
				if = {
					limit = {
						liege.var:chancellor_foreign_affairs_opinion_value > chancellor_foreign_affairs_opinion_max
					}
					liege = {
						set_variable = {
							name = chancellor_foreign_affairs_opinion_value
							value = root.chancellor_foreign_affairs_opinion_max
						}
					}
				}
			}
			else = {
				liege = {
					set_variable = {
						name = chancellor_foreign_affairs_opinion_value
						value = root.chancellor_foreign_affairs_monthly_increase
					}
				}
			}
		}
		liege = {
			if = {
				limit = {
					has_character_flag = no_chancellor_side_effects_first_month
				}
				remove_character_flag = no_chancellor_side_effects_first_month
			}
			else = {
				#Side effects
				trigger_event = {
					on_action = task_foreign_affairs_side_effects
					days = { 1 30 }
				}
			}
		}
	}

	ai_will_do = {
		value = 1 # Always a good backup
	}
}


task_domestic_affairs = {
	position = councillor_chancellor
	task_type = task_type_general
	task_progress = task_progress_infinite
	
	is_shown = {
		NOT = { liege = { has_government = patrician_government } }
	}

	effect_desc = {
		desc = task_domestic_affairs_effect_desc
		triggered_desc = {
			trigger = {
				liege = { has_perk = chains_of_loyalty_perk }
			}
			desc = task_domestic_affairs_chains_of_loyalty_bonus_desc
		}
		triggered_desc = {
			trigger = {
				exists = liege.dynasty
				liege.dynasty = { has_dynasty_perk = erudition_legacy_5 }	
			}
			desc = task_domestic_affairs_bureaucrats_bonus_desc
		}
		desc = {
			desc = council_task_possible_side_effects
			triggered_desc = {
				trigger = {
					diplomacy > mediocre_skill_rating
				}
				desc = task_increase_control_improved_vassal_opinion
			}
			triggered_desc = {
				trigger = {
					diplomacy > mediocre_skill_rating
					liege = {
						any_vassal = {
							has_government = feudal_government
							primary_title.tier > tier_barony
						}
					}
				}
				desc = task_increase_control_improve_vassal_contract
			}
			triggered_desc = {
				trigger = {
					diplomacy > mediocre_skill_rating
				}
				desc = task_increase_control_end_internal_war
			}
			triggered_desc = {
				trigger = {
					diplomacy > mediocre_skill_rating
				}
				desc = task_increase_control_force_vassal_partition
			}
			triggered_desc = {
				trigger = {
					diplomacy < high_skill_rating
				}
				desc = task_increase_control_lose_vassal_opinion
			}
			triggered_desc = {
				trigger = {
					diplomacy < high_skill_rating
					liege = {
						any_vassal = {
							has_government = feudal_government
							primary_title.tier > tier_barony
						}
					}
				}
				desc = task_increase_control_worsen_contract
			}
			triggered_desc = {
				trigger = {
					diplomacy < high_skill_rating
				}
				desc = task_increase_control_loss_of_crown_authority
			}
		}
	}

	on_start_task = {
		liege = {
			add_character_flag = {
				flag = no_chancellor_side_effects_first_month
				days = 30
			}
		}
	}

	council_owner_modifier = {
		name = task_domestic_affairs_opinion_moidifier
		direct_vassal_opinion = 1
		scale = chancellor_domestic_affairs_opinion_scale
	}

	on_monthly = {
		if = {
			limit = {
				liege = { is_ai = no }
			}
			if = {
				limit = {
					liege = { has_variable = chancellor_domestic_affairs_opinion_value }
				}
				if = {
					limit = {
						liege.var:chancellor_domestic_affairs_opinion_value <chancellor_domestic_affairs_opinion_max
					}
					liege = {
						change_variable = {
							name = chancellor_domestic_affairs_opinion_value
							add = root.chancellor_domestic_affairs_monthly_increase
						}
					}
				}
				if = {
					limit = {
						liege.var:chancellor_domestic_affairs_opinion_value > chancellor_domestic_affairs_opinion_max
					}
					liege = {
						set_variable = {
							name = chancellor_domestic_affairs_opinion_value
							value = root.chancellor_domestic_affairs_opinion_max
						}
					}
				}
			}
			else = {
				liege = {
					set_variable = {
						name = chancellor_domestic_affairs_opinion_value
						value = root.chancellor_domestic_affairs_monthly_increase
					}
				}
			}
		}
		liege = {
			if = {
				limit = {
					has_character_flag = no_chancellor_side_effects_first_month
				}
				remove_character_flag = no_chancellor_side_effects_first_month
			}
			else = {
				#Side effects
				trigger_event = {
					on_action = task_domestic_affairs_side_effects
					days = { 1 30 }
				}
			}
		}
	}

	ai_will_do = {
		value = 0
		if = { # Do this if you have a faction against you, every little bit helps
			limit = {
				scope:councillor_liege = {
					any_targeting_faction = {
						OR = {
							faction_is_type = independence_faction
							faction_is_type = liberty_faction
							faction_is_type = claimant_faction
						}
					}
				}
			}
			add = 1000
		}
	}
}


task_integrate_title = {
	position = councillor_chancellor
	task_type = task_type_county
	county_target = realm
	task_progress = task_progress_value
	task_current_value = scope:councillor_liege.chancellor_integrate_title_current_progress

	task_max_value = define:NTitle|DRIFT_PROGRESS_LIMIT
	
	is_shown = {
		NOT = { liege = { has_government = patrician_government } }
	}

	effect_desc = {
		desc = task_integrate_title_effect_desc
		desc = {
			desc = council_task_possible_side_effects
			triggered_desc = {
				trigger = {
					diplomacy > mediocre_skill_rating
				}
				desc = task_integrate_title_nobles_support_integration
			}
			triggered_desc = {
				trigger = {
					diplomacy > mediocre_skill_rating
				}
				desc = task_integrate_title_culture_supports_integration
			}
			triggered_desc = {
				trigger = {
					diplomacy < high_skill_rating
				}
				desc = task_integrate_title_nobles_resist_integration
			}
			triggered_desc = {
				trigger = {
					diplomacy < high_skill_rating
				}
				desc = task_integrate_title_culture_resists_integration
			}
		}
	}

	on_start_task = {
		liege = {
			add_character_flag = {
				flag = no_chancellor_side_effects_first_month
				days = 30
			}
		}
	}

	is_valid_showing_failures_only =  {
		liege = {
			custom_description = {
				text = "is_king_or_emperor"
				highest_held_title_tier >= tier_kingdom
			}
		}
	}

	on_finish_task_county = {
		start_default_task = yes
	}

	potential_county = {
		scope:county = {
			de_jure_drifting_towards = scope:councillor_liege.primary_title
		}
	}

	progress = {
		value = 0
		add = {
			value = define:NTitle|DRIFT_MONTHLY_PROGRESS_INCREASE
			desc = PASSIVE_INTEGRATION_PROGRESS_VALUE
		}
		add = {
			value = scope:councillor.chancellor_integrate_title_progress_gain
			desc = SCALED_COUNCILLOR_DIPLOMACY_VALUE
		}
		# Relation Bonuses/Penalties
		if = { # Friend
			limit = {
				scope:councillor_liege = {
					has_relation_friend = scope:councillor
					NOT = { has_relation_best_friend = scope:councillor }
				}
			}
			add = {
				value = scope:councillor.chancellor_integrate_title_monthly_increase_friend_bonus
				desc = COUNCILLOR_IS_YOUR_FRIEND
			}
		}
		if = { # Best Friend
			limit = {
				scope:councillor_liege = {
					has_relation_best_friend = scope:councillor
				}
			}
			add = {
				value = scope:councillor.chancellor_integrate_title_monthly_increase_best_friend_bonus
				desc = COUNCILLOR_IS_YOUR_BEST_FRIEND
			}
		}
		if = { # Rival
			limit = {
				scope:councillor_liege = {
					has_relation_rival = scope:councillor
					NOT = { has_relation_nemesis = scope:councillor }
				}
			}
			add = {
				value = scope:councillor.chancellor_integrate_title_monthly_increase_rival_bonus
				desc = COUNCILLOR_IS_YOUR_RIVAL
			}
		}
		if = { # Nemesis
			limit = {
				scope:councillor_liege = {
					has_relation_nemesis = scope:councillor
				}
			}
			add = {
				value = scope:councillor.chancellor_integrate_title_monthly_increase_nemesis_bonus
				desc = COUNCILLOR_IS_YOUR_NEMESIS
			}
		}
		if = {
			limit = {
				exists = scope:councillor.liege.dynasty
				scope:councillor.liege.dynasty = { has_dynasty_perk = erudition_legacy_5 }
			}
			add = {
				value = scope:councillor.chancellor_integrate_title_erudition_bonus
				desc = INTEGRATE_TITLE_DYNASTY_PERK_BONUS_VALUE
			}
		}
	}

	on_monthly_county = {
		if = {
			# Due to code setup, it might not have invalidated yet if this is not the case
			limit = {
				scope:county = {
					de_jure_drifting_towards = scope:councillor_liege.primary_title
				}
			}
			scope:county = {
				change_de_jure_drift_progress = {
					target = scope:councillor_liege.primary_title
					value = scope:councillor.chancellor_integrate_title_progress_gain
				}
			}
		}

		scope:councillor_liege = {
			if = {
				limit = {
					has_character_flag = no_chancellor_side_effects_first_month
				}
			}
			else = {
				#Side effects
				trigger_event = {
					on_action = task_integrate_title_side_effects
					days = { 1 30 }
				}
			}
		}
	}

	ai_will_do = {
		value = 10 # Prefer this if available
	}
}
