dofile("Tools.lua")
dofile("GroundUnits/WeaponSystem/Tools/WeaponSystemTools.lua")
dofile("GroundUnits/WeaponSystem/Tools/LauncherTools.lua")
dofile("GroundUnits/WeaponSystem/Tools/TurretTools.lua")

dofile("GroundUnits/WeaponSystem/Tools/AutoGunSounds.lua")
dofile("GroundUnits/WeaponSystem/Tools/MissileSounds.lua")


local host = ED_AudioAPI.createHost(ED_AudioAPI.ContextWorld, "CHARLESDEGAULLE_WS")

-- turret


local CHARLESDEGAULLE_turret = {}

function CHARLESDEGAULLE_turret:calc_pitch_gain(rotationSpeedY)
	local rotSpdY = math.abs(rotationSpeedY)
	local pitch = 0.5 + rotSpdY
	local gain = ((rotSpdY > 1.0) and 1.0 or rotSpdY) + 0.8
	
	return pitch, gain
end

setmetatable(CHARLESDEGAULLE_turret, {__index = class_turret})

function onEvent_CreateTurret(arg1, arg2)
	local ws_type = arg1
	local ws_id = arg2
	
	local sound_file = CHARLESDEGAULLE_turret:select_sound_file(getWsType(ws_type))
	local sound_source = ED_AudioAPI.createSource(host, sound_file)
	
	local turret = CHARLESDEGAULLE_turret:new(getWsType(ws_type), sound_source)
	table.insert(turret_list, turret)
end

function onEvent_StopTurret(arg1)
	if turret_list[arg1] ~= nil then
		turret_list[arg1]:stop_play()
	end
end

-- turret

-- launcher

local CHARLESDEGAULLE_launcher = {}

-- automatic_gun_25mm
-- WS_ID:	1
-- L_ID:	1
-- L_TYPE:	101

-- automatic_gun_25mm
-- WS_ID:	2
-- L_ID:	1
-- L_TYPE:	101

-- automatic_gun_25mm
-- WS_ID:	3
-- L_ID:	1
-- L_TYPE:	101

-- automatic_gun_25mm
-- WS_ID:	4
-- L_ID:	1
-- L_TYPE:	101

-- ship_MK41_SM2
-- WS_ID:	5
-- L_ID:	1
-- L_TYPE:	5

-- ship_MK41_SM2
-- WS_ID:	6
-- L_ID:	1
-- L_TYPE:	5

-- seasparrow
-- WS_ID:	7
-- L_ID:	1
-- L_TYPE:	5

-- seasparrow
-- WS_ID:	8
-- L_ID:	1
-- L_TYPE:	5



function CHARLESDEGAULLE_launcher:select_launcher(ws_id, l_id)
	if ws_id == 1 and l_id == 1 then
		return automatic_gun_25mm
	elseif ws_id == 2 and l_id == 1 then
		return automatic_gun_25mm
	elseif ws_id == 3 and l_id == 1 then
		return automatic_gun_25mm
	elseif ws_id == 4 and l_id == 1 then
		return automatic_gun_25mm
	elseif ws_id == 5 and l_id == 1 then
		return ship_MK41_SM2
	elseif ws_id == 6 and l_id == 1 then
		return ship_MK41_SM2
	elseif ws_id == 7 and l_id == 1 then
		return seasparrow
	elseif ws_id == 8 and l_id == 1 then
		return seasparrow
	end
end

setmetatable(CHARLESDEGAULLE_launcher, {__index = class_launcher})

function onEvent_CreateLauncher(arg1, arg2, arg3)
	local ws_id = arg1
	local l_id = arg2
	local l_type = getWsType(arg3)
	local launcher_param = CHARLESDEGAULLE_launcher:select_launcher(ws_id, l_id)
	
	printLauncherInfo(ws_id, l_id, l_type)
	
	local launcher = class_launcher:new(ws_id, l_id, l_type, launcher_param, host)
	CHARLESDEGAULLE_launcher:table_insert(launcher)
end

function onEvent_SingleShot(arg1, arg2)
	if launcher_list[arg1][arg2] ~= nil then
		CHARLESDEGAULLE_launcher:single_shot(arg1, arg2)
	end
end

function onEvent_StartBurst(arg1, arg2)
	if launcher_list[arg1][arg2] ~= nil then
		CHARLESDEGAULLE_launcher:start_burst(arg1, arg2)
	end
end

function onEvent_EndBurst(arg1, arg2)
	if launcher_list[arg1][arg2] ~= nil then
		CHARLESDEGAULLE_launcher:end_burst(arg1, arg2)
	end
end

function onEvent_ReloadFirst(arg1, arg2)
	if launcher_list[arg1][arg2] ~= nil then
		CHARLESDEGAULLE_launcher:reload_first(arg1, arg2)
	end
end

function onEvent_ReloadSecond(arg1, arg2)
	if launcher_list[arg1][arg2] ~= nil then
		CHARLESDEGAULLE_launcher:reload_second(arg1, arg2)
	end
end

-- launcher

-- maintain

function onUpdate(params)
	updateHost(host, params)
	
	if turret_list[params.WsId] ~= nil then
		turret_list[params.WsId]:update(params.RotationSpeedY)
	end
end

-- maintain

