local TurtleController = require("controller.turtle_controller")
local AudioController = require("controller.audio_controller")

---@class AudioTestingController
---@field controller TurtleController
---@field audio AudioController
local AudioTestingController = {}
AudioTestingController.__index = AudioTestingController

---@return AudioTestingController
function AudioTestingController:Create()
	local t = {}
	setmetatable(t, AudioTestingController)

	-----------------------------------------------------------------------------------------------
	-- Fields
	-----------------------------------------------------------------------------------------------
	t.controller = TurtleController:Create()
	t.audio = AudioController:Create()

	return t
end

-----------------------------------------------------------------------------------------------
-- Behavior Methods
-----------------------------------------------------------------------------------------------

function AudioTestingController:TestAudioWithoutMovement()
	local function dancing()
		for _ = 1, 256 do
			self.controller:TurnRelative(1)
		end
		self.audio:StopPlaying()
	end

	parallel.waitForAll(dancing, self.audio:PlayAudioFactory("bangarang"))
end

function AudioTestingController:TestAudioWithMovement()
	local function movement()
		for _ = 1, 10 do
			self.controller:MoveForward(10)
			self.controller:MoveVertical(3)
			self.controller:TurnRelative(2)
			self.controller:MoveForward(10)
			self.controller:MoveVertical(-3)
			self.controller:TurnRelative(2)
		end
		self.audio:StopPlaying()
	end

	parallel.waitForAll(movement, self.audio:PlayAudioFactory("bangarang"))
end

-----------------------------------------------------------------------------------------------
-- Main Method
-----------------------------------------------------------------------------------------------

function AudioTestingController:Run()
	self.controller:Configure()
	self.controller:DisableMiningForward()
	self.controller:DisableMiningAbove()
	self.controller:DisableMiningBelow()

	print("There are multiple tests available:")
	print("1: Audio without movement")
	print("2: Audio with movement")

	local choice = 0
	while choice < 1 or choice > 2 do
		print("Choose a test by entering its number:")
		choice = tonumber(io.read()) or 0
	end

	if choice == 1 then
		self:TestAudioWithoutMovement()
	elseif choice == 2 then
		self:TestAudioWithMovement()
	end
end

return AudioTestingController
