-- ----------------------------------------------------------------------------
layoutBedroomRadio.mExits =
{
}
layoutBedroomRadio.mPOIs = 
{
}
layoutBedroomRadio.mHOs = 
{
}
layoutBedroomRadio.mIOs =
{
	[objectBedroomRadioAntennaSocket] = 
	{
		links = {objectShedWirehanger},
	},
	[objectBedroomRadioKnobSocket] = 
	{
		links = {objectBedroomRadiodial},
	},
	[objectBedroomRadioTuningplateSocket] = 
	{
		links = {objectLivingroomRadiofrontplate},
	},
	[objectBedroomRadioWires] = 
	{
		links = {objectLivingroomWires},
	},
	[objectBedroomRadioWiresfixed] = 
	{
		links = {objectShedElectricaltape},
	},
	[objectBedroomRadioKnob] =
	{
	},
	[objectBedroomRadioKnobfm] =
	{
	},
	[objectBedroomRadioFixedcasing] =
	{
		links = {objectShedPliers}
	},
}
-- ----------------------------------------------------------------------------
layoutBedroomRadio.mPuzzle = true
-- ----------------------------------------------------------------------------
function layoutBedroomRadio:onConstruct()

	Layout.onConstruct(self)
	
	objectPaperNewsChanel:ss(kInactive)
	
end
-- ----------------------------------------------------------------------------
function layoutBedroomRadio:onInitialize()

	Layout.onInitialize(self)
	
	objectBedroomRadioKnob:ss(kInactive)
	objectBedroomRadioKnobfm:ss(kInactive)
	objectBedroomRadioWirestape:ss(kInactive)
	objectBedroomRadioWiresfixed:ss(kInactive)
	objectBedroomRadioTuningplate:ss(kInactive)
	objectBedroomRadioTuningplateMask:ss(kInactive)
	objectBedroomRadioAntenna:ss(kInactive)
	objectBedroomRadioPliers:ss(kInactive)
	objectBedroomRadioPliers:setPivotPoint({x=11, y=16})	
	objectBedroomRadioPliersFinished:ss(kInactive)
	objectBedroomRadioFixedcasing:ss(kInactive)
	
	objectBedroomRadioWires:ss(kActive)
	objectBedroomRadioTuningplateSocket:ss(kActive)
	objectBedroomRadioAntennaSocket:ss(kActive)
	objectBedroomRadioKnobSocket:ss(kActive)
	
	objectBedroomRadioPliers:setRotation(self.mMinPliersRotation)
	self.mMinYPosPliers = objectBedroomRadioPliers:globalY(objectBedroomRadioPliers:width(), objectBedroomRadioPliers:height())
	objectBedroomRadioPliers:setRotation(self.mMaxPliersRotation)
	self.mMaxYPosPliers = objectBedroomRadioPliers:globalY(objectBedroomRadioPliers:width(), objectBedroomRadioPliers:height())
	objectBedroomRadioPliers:setRotation(0)
	
	self.mDeltaYPosPliers = self.mMaxYPosPliers - self.mMinYPosPliers
	self.mSliderStepSize = self.mDeltaYPosPliers / self.mPixelDistanceSlide
	self.mSliderStartX = 496 - self:x()
	self.mSliderStopX = 614 - self:x()
	self.mSliderDeltaX = self.mSliderStopX - self.mSliderStartX
	
	--local stepSize = self.mSliderDeltaX - 50 / (self.mActiveFrequencesAmount - 1.0)
	--print("Radio Frequencies X position:")
	--for step = 1, self.mActiveFrequencesAmount do
	--	table.insert(self.mRadioFrequencies, self.mSliderStopX - 50 - stepSize * (step-1))
	--	print(self.mRadioFrequencies[step])
	--end
	self.mRadioFrequencies[2] = 317
	self.mRadioFrequencies[4] = 285
	
	objectBedroomRadioLightOn:setAlpha(0)
	objectBedroomRadioGlowInside:setAlpha(0)
	objectBedroomRadioGlowOutside:setAlpha(0)
	objectBedroomRadioSliderGlow:setAlpha(0)

end
-- ----------------------------------------------------------------------------
function layoutBedroomRadio:onShow()

	Layout.onShow(self)

	if (objectPaperNewsChanel:isIdle()) then
		objectLivingroomRadiostationlistUnlit:ss(kDone)
		objectLivingroomRadiostationlistLit:ss(kDone)
		objectRadiostationsNote:ss(kDone)
	end
	
end
-- ----------------------------------------------------------------------------
function layoutBedroomRadio:onStartAudio()
	if (layoutBedroom ~= nil) then
		layoutBedroom:checkRadioSound()
	end
end

-- ----------------------------------------------------------------------------
function layoutBedroomRadio:onUsedAll(object, props)

	Layout.onUsedAll(self, object, props)
	
	if (object == objectBedroomRadioAntennaSocket) then
		objectBedroomRadioAntennaSocket:ss(kDone)
		objectBedroomRadioAntenna:ss(kIdle)
		
		-- show antenna in livingroom:
		objectBedroomRadioAntennafixed:ss(kIdle)
	end
	
	if (object == objectBedroomRadioKnobSocket) then
		objectBedroomRadioKnob:ss(kActive)
		
	end
	
	if (object == objectBedroomRadioTuningplateSocket) then
		objectBedroomRadioTuningplate:ss(kIdle)
		objectBedroomRadioTuningplateMask:ss(kIdle)
		objectBedroomRadioTuningplateSocket:ss(kDone)
		objectBedroomRadioSlider:ss(kIdle)
		
	end
	
	if (object == objectBedroomRadioWires) then
		objectBedroomRadioWires:ss(kDone)
		objectBedroomRadioWiresfixed:ss(kActive)
		
	end
	
	if (object == objectBedroomRadioWiresfixed) then
		objectBedroomRadioWiresfixed:ss(kDone)
		objectBedroomRadioWirestape:ss(kIdle)
		
	end
	
	if (object == objectBedroomRadioFixedcasing) then
		objectBedroomRadioPliers:ss(kIdle)
		objectBedroomRadioFixedcasing:ss(kIdle)
		self.mTuningStarted = true
		if (not layoutLivingroomRadioStations:isSeen()) then
			think("MESSAGE_RADIO_STATIONS")
		end
		
	end
	
	if (objectBedroomRadioFixedcasing:isInactive()) then
		local readyForPliers = true
		readyForPliers = readyForPliers and objectBedroomRadioAntenna:isIdle()
		readyForPliers = readyForPliers and objectBedroomRadioTuningplate:isIdle()
		readyForPliers = readyForPliers and objectBedroomRadioWirestape:isIdle()
		readyForPliers = readyForPliers and (objectBedroomRadioKnob:isActive() or objectBedroomRadioKnobfm:isIdle())
		if (readyForPliers) then
			objectBedroomRadioFixedcasing:ss(kActive)
		end
		
	end

end
-- ----------------------------------------------------------------------------
function layoutBedroomRadio:onClickIO(object, x, y)

	if (object == objectBedroomRadioKnob) then
		-- make objectBedroomRadioKnobfm idle!
		-- not active!
		-- this is to guarantee the user only clicks it once, so the hint/step
		-- system doesn't fuck up
		objectBedroomRadioKnobfm:ss(kIdle)
		objectBedroomRadioKnob:ss(kInactive)
		self.mStationFound = false
		playAudioTrack("RADIO_SWITCH")
		return true
	end
	
	if (object == objectBedroomRadioKnobfm) then
		objectBedroomRadioKnobfm:ss(kInactive)
		objectBedroomRadioKnob:ss(kActive)	
		self.mStationFound = false
		playAudioTrack("RADIO_SWITCH")
		return true
	end
	
	return Layout.onClickIO(self, object, x, y)

end
-- ----------------------------------------------------------------------------
function layoutBedroomRadio:onMouseDown(x, y)

	if (self:isPuzzling()) then
		if (self.mTuningStarted and self.mStationFound) then
			self:solve()
			return true
		end	
		
		if (objectShedPliers:isSelected() and (not objectBedroomRadioFixedcasing:isActive())) then 
			think("MESSAGE_BEDROOM_RADIO_FIX_FIRST")
		end
		
	end

	return Layout.onMouseDown(self, x, y)

end
-- ----------------------------------------------------------------------------
function layoutBedroomRadio:onTick(time)

	Layout.onTick(self, time)
	
	if (not self:isPuzzling()) then
		return
	end
	
	if (self.mTuningStarted) then
		
		if (self.mStationFound) then
			if (self.mStationFoundTimer > 0) then
				self.mStationFoundTimer = self.mStationFoundTimer - time
				if (self.mStationFoundTimer <= 0) then
					self:solve()
					return
				
				end			
			end		
		end
	
		local targetY = cursor:getY()
		local currentY = objectBedroomRadioPliers:globalY(objectBedroomRadioPliers:width(), objectBedroomRadioPliers:height())
		local currentRotation = objectBedroomRadioPliers:rotation()
		
		local absDistanceToMouse = math.abs(targetY - currentY)
		local absDistanceToStation = math.abs(self.mRadioStationYValue - currentY)
		local fraction = ((currentY - self.mMinYPosPliers) / self.mDeltaYPosPliers)
		local sliderX = self.mSliderStartX + self.mSliderDeltaX * fraction
			
		if (absDistanceToMouse > self.mPliersTargetReachedDistance) then
			if (currentY < targetY) then
				-- mouse cursor (target) y is lower on the screen than the pliers' checking spot, so lower the pliers:
				if (currentRotation < self.mMaxPliersRotation) then
					local rotation = currentRotation + (self.mPliersMaxRotationStep * time)
					if (rotation > self.mMaxPliersRotation) then
						rotation = self.mMaxPliersRotation
					end
					objectBedroomRadioPliers:setRotation(rotation) 
					self:updateSlider(sliderX)
				end
			end
			if (currentY > targetY) then
				-- mouse cursor (target) y is lower on the screen than the pliers' checking spot, so lower the pliers:
				if (currentRotation > self.mMinPliersRotation) then
					local rotation = currentRotation - (self.mPliersMaxRotationStep * time)
					if (rotation < self.mMinPliersRotation) then
						rotation = self.mMinPliersRotation
					end				
					objectBedroomRadioPliers:setRotation(rotation)
					self:updateSlider(sliderX)
				end
			end
		end
			
		self:flickerLight(absDistanceToStation, time)
	
	else
		self:flickerLight(60, time)
	
	end

end
-- ----------------------------------------------------------------------------
layoutBedroomRadio.mPliersTargetReachedDistance = 8
layoutBedroomRadio.mPliersStationReachedDistance = 15
layoutBedroomRadio.mPliersMaxRotationStep = 0.0012
layoutBedroomRadio.mPliersStationTunedTimer = 1500
layoutBedroomRadio.mBedroomRadioFlickerMinimum = 200
layoutBedroomRadio.mStationFound = false
layoutBedroomRadio.mStationFoundTimer = 0
layoutBedroomRadio.mFlickerTimer = 200
layoutBedroomRadio.mMinPliersRotation = -1.2
layoutBedroomRadio.mMaxPliersRotation = 0.041
layoutBedroomRadio.mRadioStationYValue = 272
layoutBedroomRadio.mMaxYPosPliers = 0
layoutBedroomRadio.mMinYPosPliers = 0
layoutBedroomRadio.mPixelDistanceSlide = 91
layoutBedroomRadio.mDeltaYPosPliers = 0
layoutBedroomRadio.mSliderStepSize = 0
layoutBedroomRadio.mSliderStartX = 0
layoutBedroomRadio.mSliderStopX = 0 
layoutBedroomRadio.mSliderDeltaX = 0
layoutBedroomRadio.mActiveFrequencesAmount = 5
layoutBedroomRadio.mRadioFrequencies = {}
layoutBedroomRadio.mRadioAudioMap = {"BEDROOM_RADIO_STATIC", "BEDROOM_RADIO_MUSIC", "BEDROOM_RADIO_STATIC", "BEDROOM_RADIO_NEWS", "BEDROOM_RADIO_STATIC"}
layoutBedroomRadio.mRadioVolumeMap = {0.0, 0.0, 0.0, 0.0, 0.0}
layoutBedroomRadio.mTuningStarted = false
layoutBedroomRadio.mAmTuningTime = 0
layoutBedroomRadio.mTimeBeforeAmHelp = 5000
layoutBedroomRadio.mAmHelpShown = false
-- ----------------------------------------------------------------------------
function layoutBedroomRadio:flickerLight(distance, time)

	local flickerTuner = false
	-- make sure the tuner plate doesn't flicker as jittery as the red light
	self.mFlickerTimer = self.mFlickerTimer - time
	if (self.mFlickerTimer < 0) then
		flickerTuner = true
		self.mFlickerTimer = self.mBedroomRadioFlickerMinimum
	end

	if (objectBedroomRadioKnobfm:isIdle()) then
	
		if (distance < self.mPliersStationReachedDistance) then
			-- slider is on the correct station
			--  light should be on
			objectBedroomRadioLightOn:setAlpha(1)
			objectBedroomRadioGlowInside:setAlpha(1)
			objectBedroomRadioGlowOutside:setAlpha(1)
			objectBedroomRadioSliderGlow:setAlpha(1)
			if (not self.mStationFound) then
				self.mStationFoundTimer = self.mPliersStationTunedTimer
				self.mStationFound = true
			end	
			
		else
			self.mStationFoundTimer = 0
			self.mStationFound = false
			if (math.random(250) < distance) then
				objectBedroomRadioLightOn:setAlpha(0)
				if (flickerTuner) then
					objectBedroomRadioGlowInside:setAlpha(0)
					objectBedroomRadioGlowOutside:setAlpha(0)
					objectBedroomRadioSliderGlow:setAlpha(0)
				end		
			else
				objectBedroomRadioLightOn:setAlpha(1)
				if (flickerTuner) then
					objectBedroomRadioGlowInside:setAlpha(1)
					objectBedroomRadioGlowOutside:setAlpha(1)
					objectBedroomRadioSliderGlow:setAlpha(1)
				end
			end		
		end
	
	else

		local alpha = (math.random(0, 9) < 9) and 1 or 0
		objectBedroomRadioLightOn:setAlpha(alpha)
		objectBedroomRadioGlowInside:setAlpha(alpha)
		objectBedroomRadioGlowOutside:setAlpha(alpha)
		objectBedroomRadioSliderGlow:setAlpha(alpha)
		
	end

end
-- ----------------------------------------------------------------------------
function layoutBedroomRadio:updateSlider(xPos)

	objectBedroomRadioSlider:setX(xPos)
	objectBedroomRadioSliderGlow:setX(xPos)
	
	-- TODO: Sound
	-- Set all frequency maps to volume 0 initially
	for j=1, self.mActiveFrequencesAmount do
		getAudioManager():playTrack({tag=self.mRadioAudioMap[j], definition=self.mRadioAudioMap[j], volume=0.0})
	end
	
	if (objectBedroomRadioKnobfm:isIdle()) then
		if (self.mStationFound) then
			-- TODO: Sound
			-- Set frequency map of the news channel to volume 1
			getAudioManager():playTrack({tag=self.mRadioAudioMap[4], definition=self.mRadioAudioMap[4], volume=1.0})
			
		else
			local step    = 10 --math.abs(self.mSliderDeltaX / (self.mActiveFrequencesAmount - 1.0))
			local freq    = 0
			local audioID = 1
			
			local delta = step / 2
						
			for id, xRadio in pairs(self.mRadioFrequencies) do
				if (xPos > xRadio - delta and xPos < xRadio + delta) then
					freq = xRadio
					audioID = id
					break
				end
			end
			
			local distanceFromFreq = math.abs(freq - xPos)
			local percentageFromFreq = distanceFromFreq / step
			
			--print("distance from left: " .. percentageFromFreq)
			--print("percentage from frequency: " .. percentageFromFreq)
			
			if (audioID > 1) then
				-- TODO: Sound
				-- Set frequency map
				getAudioManager():playTrack({tag=self.mRadioAudioMap[audioID], definition=self.mRadioAudioMap[audioID], volume=(1.0 - percentageFromFreq)})
				getAudioManager():playTrack({tag=self.mRadioAudioMap[1], definition=self.mRadioAudioMap[1], volume=percentageFromFreq})
			else
				getAudioManager():playTrack({tag=self.mRadioAudioMap[1], definition=self.mRadioAudioMap[1], volume=1.0})
			end

		end	
	else
		-- TODO: Sound
		-- Set frequency map of the static channel to volume 1	
		getAudioManager():playTrack({tag=self.mRadioAudioMap[1], definition=self.mRadioAudioMap[1], volume=1.0})
	end
end
-- ----------------------------------------------------------------------------
function layoutBedroomRadio:onSolve()

	Layout.onSolve(self)

	objectBedroomRadioPliers:ss(kDone)
	objectBedroomRadioPliersFinished:ss(kIdle)
	objectBedroomRadioKnob:ss(kDone)
	objectBedroomRadioKnobfm:ss(kIdle)
	layoutBedroom:stopRadioSound()

end
-- ----------------------------------------------------------------------------
function layoutBedroomRadio:onSolveStart()

	Layout.onSolveStart(self)

	self:animate("CloseRadio_blocking",
		{ delay = 2000 },
		{ call = layoutBedroomRadio.hide },
		{ call = function() play(movieSettings[kMovieRadio]) end },
		{ call = layoutBedroomRadio.solveFinish }
	)	

end
-- ----------------------------------------------------------------------------
function layoutBedroomRadio:onQuickPlay()

	Layout.onQuickPlay(self)
	
	objectShedWirehanger:ss(kFound)
	objectBedroomRadiodial:ss(kFound)
	objectLivingroomRadiofrontplate:ss(kFound)
	objectLivingroomRadiofrontplateSmall:ss(kFound)
	objectLivingroomWires:ss(kFound)
	objectShedElectricaltape:ss(kFound)
	objectShedPliers:ss(kFound)

end
-- ----------------------------------------------------------------------------
function layoutBedroomRadio:isSkippable()
	return self.mTuningStarted
end