namespace = loanPlus

character_event = {
	id = loanPlus.0001
	picture = GFX_evt_council

	desc = {
		text = EVTDESC38002 # TODO: move to correct namespace
		trigger = {
			NOT = { has_character_modifier = borrowed_from_jews }
		}
	}
	desc = {
		text = EVTDESC_Plus_625 # TODO: move to correct namespace
		trigger = { has_character_modifier = borrowed_from_jews }
	}

	only_rulers = yes

	trigger = {
		check_variable = {
			which = loan_amount
			value > 0
		}

		NOT = { has_character_modifier = loan_timer }
	}

	mean_time_to_happen = {
		days = 1
	}

	immediate = {
		add_character_modifier = {
			name = loan_timer
			years = 2
		}

		## Calculate interest
		set_variable = {
			which = local_loan_interest
			which = loan_amount
		}

		multiply_variable = {
			which = local_loan_interest
			value = 0.08 # 4% APR
		}

		# Calculate new loan amount, for full payment
		set_variable = {
			which = local_loan_plus_interest
			which = loan_amount
		}

		change_variable = {
			which = local_loan_plus_interest
			which = local_loan_interest
		}

		## Setup payment variables

		# Full payment
		set_variable = {
			which = local_loan_plus_interest_payment
			which = local_loan_plus_interest
		}

		multiply_variable = {
			which = local_loan_plus_interest_payment
			value = -1
		}

		# Interest payment
		set_variable = {
			which = local_loan_interest_negative
			which = local_loan_interest
		}

		multiply_variable = {
			which = local_loan_interest_negative
			value = -1
		}

		# Prestige 'payment'
		set_variable = {
			which = local_prestige_payment
			which = loan_amount
		}

		divide_variable = {
			which = local_prestige_payment
			value = -2
		}
	}

	option = {
		name = EVTOPTA38002 # Return the money

		trigger = { wealth >= local_loan_plus_interest }

		wealth = local_loan_plus_interest_payment

		set_variable = {
			which = loan_amount
			value = 0
		}
	}

	option = {
		name = EVTOPTB38002 # Pay the interest

		trigger = { wealth >= local_loan_interest }

		wealth = local_loan_interest_negative
	}

	option = {
		name = EVTOPTC38002 # Cannot afford it

		trigger = { wealth < local_loan_interest }

		prestige = local_prestige_payment

		change_variable = {
			which = loan_amount
			which = local_loan_interest
		}
	}

	option = {
		name = EVTOPTD38002 # Get rid of them!

		prestige = local_prestige_payment
		dishonorable_plus_1_effect = yes

		if = {
			limit = { has_character_modifier = borrowed_from_jews }

			add_character_modifier = {
				name = expelled_jewish
				duration = -1
				inherit = yes
			}

			custom_tooltip = {
				text = jews_are_expelled

				character_event = { id = SoA.105 }

				add_character_modifier = {
					name = expelled_jews_timer
					years = 20
					hidden = yes
				}
			}

			any_friend = {
				show_scope_change = no
				limit = { religion_group = jewish_group }
				remove_friend = ROOT
			}
		}
		else = {
			set_character_flag = offended_moneylenders
		}

		any_demesne_province = {
			show_scope_change = no

			limit = { owned_by = PREV }

			add_province_modifier = {
				name = offended_moneylenders
				years = 10
			}
		}

		set_variable = {
			which = loan_amount
			value = 0
		}

		pf_prosperity_minus8_effect = yes

		ai_chance = {
			factor = 1

			trigger = {
				OR = {
					trait = wroth
					trait = lunatic
					trait = possessed
				}
			}
		}
	}

	after = {
		hidden_effect = {
			if = {
				limit = {
					check_variable = {
						which = loan_amount
						value <= 0
					}
				}

				remove_character_modifier = loan_timer
			}
		}
	}
}