namespace = apb_greatwork

# The Great Work (ap_great_work)
#  .1   hidden, country: starts the situation one day after the perk is taken
#  .2   hidden, situation, monthly: keeps the "Call the faithful" capital modifier in step
#  .10  country: choose a wing (fired when stages 2, 3 and 4 are first entered)
#  .20  situation, random: strike
#  .21  situation, random: collapse
#  .22  situation, random: benefactor
#  .23  situation, random: saboteur
#  .30  situation: the Work is finished (reward, then destroy_situation in after)

# Start the situation
country_event = {
	id = apb_greatwork.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		has_ascension_perk = ap_great_work
		NOT = { has_country_flag = apb_great_work_complete }
		NOT = {
			any_situation = {
				is_situation_type = apb_situation_great_work
			}
		}
	}

	immediate = {
		start_situation = {
			type = apb_situation_great_work
			target = this
		}
	}
}

# Monthly: the capital, and only the capital, has the stability bonus while
# "Call the faithful" is the active approach. Covers a moved capital and the
# automatic fall back to the default approach.
situation_event = {
	id = apb_greatwork.2
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		NOT = { has_situation_flag = apb_gw_ending }
	}

	immediate = {
		if = {
			limit = { current_situation_approach = apb_gw_faithful }
			owner = { apb_gw_faithful_on = yes }
		}
		else = {
			owner = { apb_gw_faithful_off = yes }
		}
	}
}

# Choose a wing. A country event, so it still works if it is left open until
# after the situation has ended.
country_event = {
	id = apb_greatwork.10
	title = apb_greatwork.10.name
	desc = apb_greatwork.10.desc
	picture = GFX_evt_metropolis
	show_sound = event_default
	is_triggered_only = yes

	option = {
		name = apb_greatwork.10.a
		if = {
			limit = { NOT = { is_variable_set = apb_gw_wing_archive } }
			custom_tooltip = apb_gw_wing_archive_first_tt
		}
		else = {
			custom_tooltip = apb_gw_wing_archive_repeat_tt
		}
		hidden_effect = {
			apb_gw_build_wing = { WING = archive MOD = archive }
		}
	}
	option = {
		name = apb_greatwork.10.b
		if = {
			limit = { NOT = { is_variable_set = apb_gw_wing_forum } }
			custom_tooltip = apb_gw_wing_forum_first_tt
		}
		else = {
			custom_tooltip = apb_gw_wing_forum_repeat_tt
		}
		hidden_effect = {
			apb_gw_build_wing = { WING = forum MOD = forum }
		}
	}
	option = {
		name = apb_greatwork.10.c
		if = {
			limit = { NOT = { is_variable_set = apb_gw_wing_foundry } }
			custom_tooltip = apb_gw_wing_foundry_first_tt
		}
		else = {
			custom_tooltip = apb_gw_wing_foundry_repeat_tt
		}
		hidden_effect = {
			apb_gw_build_wing = { WING = foundry MOD = foundry }
		}
	}
	option = {
		name = apb_greatwork.10.d
		if = {
			limit = { NOT = { is_variable_set = apb_gw_wing_garden } }
			custom_tooltip = apb_gw_wing_garden_first_tt
		}
		else = {
			custom_tooltip = apb_gw_wing_garden_repeat_tt
		}
		hidden_effect = {
			apb_gw_build_garden = yes
		}
	}
}

# Strike (not for gestalts)
situation_event = {
	id = apb_greatwork.20
	title = apb_greatwork.20.name
	desc = apb_greatwork.20.desc
	picture = GFX_evt_alien_city
	show_sound = event_default
	situation = this
	is_triggered_only = yes

	trigger = {
		NOT = { has_situation_flag = apb_gw_ending }
		owner = {
			is_gestalt = no
			NOT = { has_country_flag = apb_gw_event_cd }
		}
	}

	immediate = {
		owner = { set_timed_country_flag = { flag = apb_gw_event_cd days = 1080 } }
	}

	option = {
		name = apb_greatwork.20.a
		allow = {
			owner = { has_resource = { type = energy amount >= 500 } }
		}
		owner = { add_resource = { energy = -500 } }
		ai_chance = { factor = 2 }
	}
	option = {
		name = apb_greatwork.20.b
		custom_tooltip = apb_gw_halt_180_tt
		hidden_effect = {
			owner = { set_timed_country_flag = { flag = apb_gw_work_halted days = 180 } }
		}
		ai_chance = { factor = 1 }
	}
}

# Collapse
situation_event = {
	id = apb_greatwork.21
	title = apb_greatwork.21.name
	desc = apb_greatwork.21.desc
	picture = GFX_evt_city_ruins
	show_sound = event_bad_omen
	situation = this
	is_triggered_only = yes

	trigger = {
		NOT = { has_situation_flag = apb_gw_ending }
		owner = { NOT = { has_country_flag = apb_gw_event_cd } }
	}

	immediate = {
		owner = { set_timed_country_flag = { flag = apb_gw_event_cd days = 1080 } }
	}

	option = {
		name = apb_greatwork.21.a
		allow = {
			owner = { has_resource = { type = minerals amount >= 800 } }
		}
		owner = { add_resource = { minerals = -800 } }
		ai_chance = { factor = 2 }
	}
	option = {
		name = apb_greatwork.21.b
		owner = {
			capital_scope? = {
				add_modifier = { modifier = apb_great_work_rubble days = 720 }
			}
		}
		ai_chance = { factor = 1 }
	}
}

# Benefactor
situation_event = {
	id = apb_greatwork.22
	title = apb_greatwork.22.name
	desc = apb_greatwork.22.desc
	picture = GFX_evt_throne_room
	show_sound = event_default
	situation = this
	is_triggered_only = yes

	trigger = {
		NOT = { has_situation_flag = apb_gw_ending }
		owner = { NOT = { has_country_flag = apb_gw_event_cd } }
	}

	immediate = {
		owner = { set_timed_country_flag = { flag = apb_gw_event_cd days = 1080 } }
	}

	option = {
		name = apb_greatwork.22.a
		add_situation_progress = 4
		ai_chance = { factor = 1 }
	}
	option = {
		name = apb_greatwork.22.b
		owner = { add_resource = { energy = 1500 } }
		ai_chance = { factor = 1 }
	}
}

# Saboteur. The stoppage is applied in immediate so it cannot be dodged by
# leaving the event open; both options show it as a tooltip.
situation_event = {
	id = apb_greatwork.23
	title = apb_greatwork.23.name
	desc = apb_greatwork.23.desc
	picture = GFX_evt_spymaster
	show_sound = event_bad_omen
	situation = this
	is_triggered_only = yes

	trigger = {
		NOT = { has_situation_flag = apb_gw_ending }
		owner = { NOT = { has_country_flag = apb_gw_event_cd } }
	}

	immediate = {
		owner = {
			set_timed_country_flag = { flag = apb_gw_event_cd days = 1080 }
			set_timed_country_flag = { flag = apb_gw_work_halted days = 90 }
		}
	}

	option = {
		name = apb_greatwork.23.a
		custom_tooltip = apb_gw_halt_90_tt
		owner = { add_resource = { unity = 300 } }
		ai_chance = { factor = 1 }
	}
	option = {
		name = apb_greatwork.23.b
		custom_tooltip = apb_gw_halt_90_tt
		owner = { add_resource = { influence = 30 } }
		ai_chance = { factor = 1 }
	}
}

# The Work is finished (fired once from the situation's on_progress_complete).
# Reward in immediate, destroy_situation in after.
situation_event = {
	id = apb_greatwork.30
	title = apb_greatwork.30.name
	desc = apb_greatwork.30.desc
	picture = GFX_evt_huge_monument
	show_sound = event_default
	location = owner.capital_scope
	situation = this
	is_triggered_only = yes

	immediate = {
		owner = {
			set_country_flag = apb_great_work_complete
			apb_gw_faithful_off = yes
			capital_scope? = {
				add_modifier = { modifier = apb_great_work_seat days = -1 }
			}
		}
	}

	option = {
		name = apb_greatwork.30.a
		custom_tooltip = apb_gw_complete_tt
	}

	after = {
		destroy_situation = this
	}
}
