﻿namespace = jannabid_invasion

### Spawn invader with the associated story...
jannabid_invasion.0001 = {
	scope = none
	hidden = yes
	
	trigger = {
		exists = title:k_jannabid # update compatibility - new titles in old saves return false
		# only trigger the uprising if every county in the start region is not only controlled by a player, to prevent the script from removing player held demesne. player-ai-vassals are fine though.
		any_county_in_region = {
			region = special_jannabid_conquest_region_start
			count > 0
			holder = { is_ai = yes }
		}
	}

	on_trigger_fail = {
		debug_log = "jannabid story has failed to trigger, because a player directly controls all counties within the starting region; trying again next year"
		debug_log_date = yes
	}

	immediate = {
		debug_log = "jannabid story owner has appeared"
		debug_log_date = yes
		
		spawn_said_invader_effect = {
			INVADER_DYNN = jannabid
		}
		scope:invader = {
			create_story = story_jannabid_invasion
		}
	}
}

# Event for handling new army spawning on succession
jannabid_invasion.0100 = {
	hidden = yes
	immediate = {
		spawn_jannabid_troops_effect = yes
	}
}

# Event for declaring the very first war; values like current_ / max_military_strength aren't immediately filled with a value and return 0 instead
jannabid_invasion.0200 = {
	hidden = yes
	trigger = { is_ai = yes } # don't force a player into a war
	immediate = {
		war_target_evaluation_and_declaration_effect = {
			INVADER_DYNN = jannabid
			INVADER_CHAR = scope:story.story_owner
			INVADER_REGION = start
			INVADER_TARGET_TITLE_TIER = duchy
			INVADER_RANGE = neighboring
			INVADER_LIEGE_RESTRICTIONS = ""
		}
	}
}

##################
# Notification Events
# 1000-1999
##################

### The invader has appeared! Would you like to play as them?
jannabid_invasion.1001 = { # only players see this
	type = character_event
	title = jannabid_invasion.1001.t
	desc = {
		desc = jannabid_invasion.1001.desc.1
		first_valid = {
			triggered_desc = { # qarmat converts and enters Fatimid court
				trigger = {
					exists = title:d_shiite.holder
				}
				desc = jannabid_invasion.1001.desc.2.a
			}
			triggered_desc = { # Fatimids haven't spawned, qarmat 'disappears'
				trigger = {
					always = yes
				}
				desc = jannabid_invasion.1001.desc.2.b
			}
		}
		desc = jannabid_invasion.1001.desc.3
	}
	
	theme = hi_theme_desert

	left_portrait = {
		character = scope:invader
		animation = personality_zealous
	}

	immediate = {
		if = {
			limit = { exists = title:d_shiite.holder }
			title:d_shiite.holder = { save_scope_as = fatimid_caliph }
			scope:fatimid_caliph = {
				trigger_event = {
					id = jannabid_invasion.1101
					days = 7
				}
			}
		}
		else = {
			scope:hamadan_qarmat = { death = natural }
		}
	}

	trigger = { hi_show_invader_notification_event_trigger = yes }

	on_trigger_fail = {
		if = {
			limit = { exists = title:d_shiite.holder }
			title:d_shiite.holder = { save_scope_as = fatimid_caliph }
			scope:fatimid_caliph = {
				trigger_event = {
					id = jannabid_invasion.1101
					days = 7
				}
			}
		}
		else = {
			scope:hamadan_qarmat = { death = natural }
		}
	}
	
	option = { name = jannabid_invasion.1001.a }

	option = { # play as the newly spawned invader
		name = jannabid_invasion.1001.b
		trigger = { hi_play_as_invader_event_option_trigger = yes }
		add_internal_flag = dangerous
		scope:invader = {
			set_variable = {
				name = historicinvasions_previous_ruler_var
				value = root
				months = 1
			}
		}
		set_player_character = scope:invader
	}
}

# hamadan qarmat joins Fatimid court
jannabid_invasion.1101 = {
	type = letter_event
	opening = jannabid_invasion.1101.opening
	desc = jannabid_invasion.1101.desc
	sender = {
		character = scope:hamadan_qarmat
		animation = personality_zealous
	}

	immediate = {
		scope:hamadan_qarmat = {
			set_employer = scope:fatimid_caliph
			set_character_faith = scope:fatimid_caliph.faith
			change_first_name = "Hasan"
		}
		set_random_friendship_reason = { TARGET = scope:hamadan_qarmat }
	}

	option = {
		name = jannabid_invasion.1101.a
	}
}

# Mecca is looted
jannabid_invasion.1201 = { # only players see this
	type = character_event
	title = jannabid_invasion.1201.t
	desc = jannabid_invasion.1201.desc
	
	theme = hi_theme_desert

	left_portrait = {
		character = scope:occupant
		animation = personality_greedy
	}

	right_portrait = {
		character = title:c_mecca.holder.top_liege
		animation = grief
	}
	
	option = {
		name = jannabid_invasion.1201.a
		show_as_tooltip = {
			scope:county = { invader_loots_county_effect = yes }
			scope:occupant = { add_character_modifier = qarmatian_looted_mecca_modifier }
		}
	}

	option = {
		name = jannabid_invasion.1201.b
		trigger = { faith = faith:qarmatian }
		exclusive = yes
		show_as_tooltip = {
			scope:county = { invader_loots_county_effect = yes }
			scope:occupant = { add_character_modifier = qarmatian_looted_mecca_modifier }
		}
	}
}