# Advance-phase effects, branching on nc_training_phase.
#
# Pacing: each advance starts a 30-day cooldown (nc_phase_cooldown_days).
# The Advance button is gated on cooldown <= 0 in the scripted_gui.
# Phase 3 is also a 30-day "final training period" - mastery does NOT fire
# on click 3. Instead, the on_daily action fires the mastery once the phase 3
# cooldown reaches 0 (see nc_grant_mastery_for_active_training_effect below
# and nc_on_actions.txt). The Conclude button only opens after that.
#
# Phase 0 -> 1: pay phase-1 (convoys consumed), set phase = 1, light tier-1 reward, start cooldown
# Phase 1 -> 2: pay phase-2 (convoys consumed), set phase = 2, light tier-2 reward, start cooldown
# Phase 2 -> 3: pay phase-3 (convoys consumed), set phase = 3, light tier-3 reward, start FINAL cooldown
#               (the mastery idea is granted by the on_daily handler when this cooldown ends)
#
# Cost model: each phase consumes convoys (sunk in exercise). The relevant
# ship type for the training is REQUIRED to be present (gated by
# nc_can_pay_current_phase / has_navy_size) but is NOT consumed - the navy
# practices with its own ships and they return to port intact.
#
# Training id mapping:
#   1 = Submarine Studies   (track: submarines,    multiplier 3.0)
#   2 = Screen Studies      (track: screens,       multiplier 1.0)
#   3 = Carrier Studies     (track: carriers,      multiplier 5.0)
#   4 = Battleship Studies  (track: capital_ships, multiplier 2.0)

@nc_phase_cooldown_default = 30

# ============================================================
# Per-training advance effects
# ============================================================

# Submarine Studies (id = 1) - 5/10/15 convoys per phase (30 total)
nc_advance_t1_submarine_studies_effect = {
	if = {
		limit = { check_variable = { nc_training_phase = 0 } }
		add_equipment_to_stockpile = { type = convoy_1 amount = -5 }
		set_variable = { nc_training_phase = 1 }
		set_variable = { nc_reward_t1_frame = 2 }
		set_variable = { nc_phase_cooldown_days = @nc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { nc_training_phase = 1 } }
		add_equipment_to_stockpile = { type = convoy_1 amount = -10 }
		set_variable = { nc_training_phase = 2 }
		set_variable = { nc_reward_t2_frame = 2 }
		set_variable = { nc_phase_cooldown_days = @nc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { nc_training_phase = 2 } }
		add_equipment_to_stockpile = { type = convoy_1 amount = -15 }
		set_variable = { nc_training_phase = 3 }
		set_variable = { nc_reward_t3_frame = 2 }
		set_variable = { nc_phase_cooldown_days = @nc_phase_cooldown_default }
	}
}

# Screen Studies (id = 2) - 5/10/15 convoys per phase (30 total)
nc_advance_t1_screen_studies_effect = {
	if = {
		limit = { check_variable = { nc_training_phase = 0 } }
		add_equipment_to_stockpile = { type = convoy_1 amount = -5 }
		set_variable = { nc_training_phase = 1 }
		set_variable = { nc_reward_t1_frame = 2 }
		set_variable = { nc_phase_cooldown_days = @nc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { nc_training_phase = 1 } }
		add_equipment_to_stockpile = { type = convoy_1 amount = -10 }
		set_variable = { nc_training_phase = 2 }
		set_variable = { nc_reward_t2_frame = 2 }
		set_variable = { nc_phase_cooldown_days = @nc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { nc_training_phase = 2 } }
		add_equipment_to_stockpile = { type = convoy_1 amount = -15 }
		set_variable = { nc_training_phase = 3 }
		set_variable = { nc_reward_t3_frame = 2 }
		set_variable = { nc_phase_cooldown_days = @nc_phase_cooldown_default }
	}
}

# Carrier Studies (id = 3) - 5/10/15 convoys per phase (30 total)
nc_advance_t1_carrier_studies_effect = {
	if = {
		limit = { check_variable = { nc_training_phase = 0 } }
		add_equipment_to_stockpile = { type = convoy_1 amount = -5 }
		set_variable = { nc_training_phase = 1 }
		set_variable = { nc_reward_t1_frame = 2 }
		set_variable = { nc_phase_cooldown_days = @nc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { nc_training_phase = 1 } }
		add_equipment_to_stockpile = { type = convoy_1 amount = -10 }
		set_variable = { nc_training_phase = 2 }
		set_variable = { nc_reward_t2_frame = 2 }
		set_variable = { nc_phase_cooldown_days = @nc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { nc_training_phase = 2 } }
		add_equipment_to_stockpile = { type = convoy_1 amount = -15 }
		set_variable = { nc_training_phase = 3 }
		set_variable = { nc_reward_t3_frame = 2 }
		set_variable = { nc_phase_cooldown_days = @nc_phase_cooldown_default }
	}
}

# Battleship Studies (id = 4) - 5/10/15 convoys per phase (30 total)
nc_advance_t1_battleship_studies_effect = {
	if = {
		limit = { check_variable = { nc_training_phase = 0 } }
		add_equipment_to_stockpile = { type = convoy_1 amount = -5 }
		set_variable = { nc_training_phase = 1 }
		set_variable = { nc_reward_t1_frame = 2 }
		set_variable = { nc_phase_cooldown_days = @nc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { nc_training_phase = 1 } }
		add_equipment_to_stockpile = { type = convoy_1 amount = -10 }
		set_variable = { nc_training_phase = 2 }
		set_variable = { nc_reward_t2_frame = 2 }
		set_variable = { nc_phase_cooldown_days = @nc_phase_cooldown_default }
	}
	else_if = {
		limit = { check_variable = { nc_training_phase = 2 } }
		add_equipment_to_stockpile = { type = convoy_1 amount = -15 }
		set_variable = { nc_training_phase = 3 }
		set_variable = { nc_reward_t3_frame = 2 }
		set_variable = { nc_phase_cooldown_days = @nc_phase_cooldown_default }
	}
}

# ============================================================
# Dispatchers
# ============================================================

# Advance dispatcher: maps nc_training_id to the right per-training advance effect.
# The outer guard makes this a no-op while the phase cooldown is still running
# OR the training has already reached phase 3 (the engine waits on cooldown
# elapsing and the on_daily mastery grant before the player can Conclude).
# This is belt-and-suspenders alongside the scripted_gui _click_enabled trigger.
nc_advance_active_training_effect = {
	if = {
		limit = {
			check_variable = { nc_training_active = 1 }
			check_variable = { nc_phase_cooldown_days < 1 }
			check_variable = { nc_training_phase < 3 }
			nc_can_pay_current_phase = yes
		}
		if = {
			limit = { check_variable = { nc_training_id = 1 } }
			nc_advance_t1_submarine_studies_effect = yes
		}
		else_if = {
			limit = { check_variable = { nc_training_id = 2 } }
			nc_advance_t1_screen_studies_effect = yes
		}
		else_if = {
			limit = { check_variable = { nc_training_id = 3 } }
			nc_advance_t1_carrier_studies_effect = yes
		}
		else_if = {
			limit = { check_variable = { nc_training_id = 4 } }
			nc_advance_t1_battleship_studies_effect = yes
		}
	}
}

# Mastery-grant dispatcher: fires the timed add_daily_mastery for the active
# training. Called by the on_daily handler the moment phase 3's cooldown
# transitions to 0. Each branch maps a training id to its naval doctrine
# track and rate.
#
# Per-track raw rates input (per sea_doctrine_tracks.txt multipliers):
#   submarines    multiplier 3.0
#   screens       multiplier 1.0
#   carriers      multiplier 5.0
#   capital_ships multiplier 2.0
#
# Mastery scales with the academy tier upgrade for the matching track.
# Scaling: T1 = base, T2 = ~2x base, T3 = ~2.7x base. Per-tier amounts are
# listed inline below so retuning is just a number swap. The tier vars live
# on the country and are bumped by nc_complete_upgrade_effect.
nc_grant_mastery_for_active_training_effect = {
	if = {
		limit = { check_variable = { nc_training_id = 1 } }
		if = {
			limit = { check_variable = { nc_submarine_tier = 3 } }
			add_daily_mastery = { amount = 0.24 days = 360 name = nc_t1_submarine_studies_complete track = submarines }
		}
		else_if = {
			limit = { check_variable = { nc_submarine_tier = 2 } }
			add_daily_mastery = { amount = 0.18 days = 210 name = nc_t1_submarine_studies_complete track = submarines }
		}
		else = {
			add_daily_mastery = { amount = 0.09 days = 180 name = nc_t1_submarine_studies_complete track = submarines }
		}
	}
	else_if = {
		limit = { check_variable = { nc_training_id = 2 } }
		if = {
			limit = { check_variable = { nc_screen_tier = 3 } }
			add_daily_mastery = { amount = 0.256 days = 360 name = nc_t1_screen_studies_complete track = screens }
		}
		else_if = {
			limit = { check_variable = { nc_screen_tier = 2 } }
			add_daily_mastery = { amount = 0.192 days = 210 name = nc_t1_screen_studies_complete track = screens }
		}
		else = {
			add_daily_mastery = { amount = 0.094 days = 180 name = nc_t1_screen_studies_complete track = screens }
		}
	}
	else_if = {
		limit = { check_variable = { nc_training_id = 3 } }
		if = {
			limit = { check_variable = { nc_carrier_tier = 3 } }
			add_daily_mastery = { amount = 0.24 days = 360 name = nc_t1_carrier_studies_complete track = carriers }
		}
		else_if = {
			limit = { check_variable = { nc_carrier_tier = 2 } }
			add_daily_mastery = { amount = 0.18 days = 210 name = nc_t1_carrier_studies_complete track = carriers }
		}
		else = {
			add_daily_mastery = { amount = 0.09 days = 180 name = nc_t1_carrier_studies_complete track = carriers }
		}
	}
	else_if = {
		limit = { check_variable = { nc_training_id = 4 } }
		if = {
			limit = { check_variable = { nc_battleship_tier = 3 } }
			add_daily_mastery = { amount = 0.256 days = 360 name = nc_t1_battleship_studies_complete track = capital_ships }
		}
		else_if = {
			limit = { check_variable = { nc_battleship_tier = 2 } }
			add_daily_mastery = { amount = 0.192 days = 210 name = nc_t1_battleship_studies_complete track = capital_ships }
		}
		else = {
			add_daily_mastery = { amount = 0.094 days = 180 name = nc_t1_battleship_studies_complete track = capital_ships }
		}
	}

	# Training is fully complete - notify the player. Mastery is active,
	# the slot will free once they click Conclude in the naval college.
	country_event = { id = nc_academy.3 hours = 0 }
}

# Conclude: clears the slot once the player acknowledges the completion screen.
# The timed mastery idea fired by nc_grant_mastery_for_active_training_effect
# is independent of this - it runs to completion regardless of whether the
# slot is occupied.
nc_conclude_active_training_effect = {
	set_variable = { nc_training_active = 0 }
	set_variable = { nc_training_id = 0 }
	set_variable = { nc_training_phase = 0 }
	set_variable = { nc_phase_cooldown_days = 0 }
	set_variable = { nc_reward_t1_frame = 1 }
	set_variable = { nc_reward_t2_frame = 1 }
	set_variable = { nc_reward_t3_frame = 1 }
}
