-- ----------------------------------------------------------------------------
layoutLivingroomPhone.mExits =
{
}
layoutLivingroomPhone.mPOIs = 
{
}
layoutLivingroomPhone.mHOs = 
{
}
layoutLivingroomPhone.mIOs =
{
	[objectLivingroomPhone0] = 
	{
		mirrors = {objectLivingroomPhone1, objectLivingroomPhone2, objectLivingroomPhone3, 
				  objectLivingroomPhone4, objectLivingroomPhone5, objectLivingroomPhone6, 
				  objectLivingroomPhone7, objectLivingroomPhone8, objectLivingroomPhone9},
	},
	[objectLivingroomPhoneCordSocket] = 
	{
		links = {objectBedroomHandsetcord}
	},
	[objectLivingroomPhoneDialfaceSocket] = 
	{
		links = {objectBasementDialface}
	},
	[objectLivingroomPhoneEarpiece1Socket] = 
	{
		links = {objectShedEarpiece1}
	},
	[objectLivingroomPhoneEarpiece2Socket] = 
	{
		links = {objectLivingroomEarpiece2}
	},
	[objectLivingroomPhoneResetbutton] = 
	{
		cursor = kCursorOperate,
	},
}

layoutLivingroomPhone.mButtonNumbers = {
    objectLivingroomPhone0, objectLivingroomPhone1, objectLivingroomPhone2,
    objectLivingroomPhone3, objectLivingroomPhone4, objectLivingroomPhone5, 
    objectLivingroomPhone6, objectLivingroomPhone7, objectLivingroomPhone8, 
    objectLivingroomPhone9
}

layoutLivingroomPhone.mButtonNumbersShadow = {
    objectLivingroomPhoneNumbershadow0, objectLivingroomPhoneNumbershadow1, objectLivingroomPhoneNumbershadow2,
    objectLivingroomPhoneNumbershadow3, objectLivingroomPhoneNumbershadow4, objectLivingroomPhoneNumbershadow5,
    objectLivingroomPhoneNumbershadow6, objectLivingroomPhoneNumbershadow7, objectLivingroomPhoneNumbershadow8,
    objectLivingroomPhoneNumbershadow9
}

layoutLivingroomPhone.mButtonNumbersGlow = {
	objectLivingroomPhone0Glow, objectLivingroomPhone1Glow, objectLivingroomPhone2Glow,
	objectLivingroomPhone3Glow, objectLivingroomPhone4Glow, objectLivingroomPhone5Glow,
	objectLivingroomPhone6Glow,	objectLivingroomPhone7Glow,	objectLivingroomPhone8Glow,
	objectLivingroomPhone9Glow
}

layoutLivingroomPhone.mHiddenObjects = {
    objectBedroomHandsetcord,   objectBasementDialface,
    objectShedEarpiece1,        objectLivingroomEarpiece2
}

layoutLivingroomPhone.mSequencesFake = {
    { 9, 1, 1 },
    { 1, 1, 2 },
    { 9, 9, 9 },
    { 5, 5, 3, 7, 8, 0, 0, 8 },
    { 8, 0, 0, 8, 5 },
}

layoutLivingroomPhone.mSequenceCorrect      = { 5, 5, 5, 8, 0, 5, 6 }
layoutLivingroomPhone.mSequenceDialed       = {}
layoutLivingroomPhone.mButtonResetY         = 0
layoutLivingroomPhone.mDialfaceBack         = objectLivingroomPhoneDialfaceBack
layoutLivingroomPhone.mDialfaceFront        = objectLivingroomPhoneDialfaceFront
layoutLivingroomPhone.mDialfaceRotation     = 0
layoutLivingroomPhone.mDialTimer			= 0
layoutLivingroomPhone.mResetHelpTimer		= -1
layoutLivingroomPhone.mIsWorking            = false
layoutLivingroomPhone.mIsDialing            = false
layoutLivingroomPhone.mIgnoreDialing		= false
layoutLivingroomPhone.mPuzzle 				= true

layoutLivingroomPhone.kDialDownSpeedFactor  = 100
layoutLivingroomPhone.kDialUpSpeedFactor    = 200
layoutLivingroomPhone.kDialTimeOut			= 6000
layoutLivingroomPhone.kResetHelpTimeOut		= 6000
-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:onConstruct()

	Layout.onConstruct(self)

	objectBasementFuseboxBackgroundLit:ss(kInactive)
	objectBasementFuseboxHitarea:ss(kActive)
	
	-- Dial face:
	objectLivingroomPhoneHighlight:ss(kInactive)
	objectLivingroomPhoneHighlight2:ss(kInactive)
	objectLivingroomPhoneDialfaceFront:ss(kInactive)
	objectLivingroomPhoneDialfaceBack:ss(kInactive)
	objectLivingroomPhoneNumbershadow0:ss(kInactive)
	objectLivingroomPhoneNumbershadow1:ss(kInactive)
	objectLivingroomPhoneNumbershadow2:ss(kInactive)
	objectLivingroomPhoneNumbershadow3:ss(kInactive)
	objectLivingroomPhoneNumbershadow4:ss(kInactive)
	objectLivingroomPhoneNumbershadow5:ss(kInactive)
	objectLivingroomPhoneNumbershadow6:ss(kInactive)
	objectLivingroomPhoneNumbershadow7:ss(kInactive)
	objectLivingroomPhoneNumbershadow8:ss(kInactive)
	objectLivingroomPhoneNumbershadow9:ss(kInactive)
	objectLivingroomPhone0Glow:ss(kInactive)
	objectLivingroomPhone1Glow:ss(kInactive)
	objectLivingroomPhone2Glow:ss(kInactive)
	objectLivingroomPhone3Glow:ss(kInactive)
	objectLivingroomPhone4Glow:ss(kInactive)
	objectLivingroomPhone5Glow:ss(kInactive)
	objectLivingroomPhone6Glow:ss(kInactive)
	objectLivingroomPhone7Glow:ss(kInactive)
	objectLivingroomPhone8Glow:ss(kInactive)
	objectLivingroomPhone9Glow:ss(kInactive)
	objectLivingroomPhoneDialfaceSocket:ss(kActive)
	
	-- Phone cord:
	objectLivingroomPhoneCord:ss(kInactive)
	objectLivingroomPhoneCordSocket:ss(kActive)
	
	-- Phone earpiece1 (yes, we have a phone with two ear pieces, hahah!!)
	objectLivingroomPhoneEarpiece1:ss(kInactive)
	objectLivingroomPhoneEarpiece1Socket:ss(kActive)
	
	-- Phone earpiece2
	objectLivingroomPhoneEarpiece2:ss(kInactive)
	objectLivingroomPhoneEarpiece2Socket:ss(kActive)
	
	-- Reset button
	objectLivingroomPhoneResetbutton:ss(kActive)

	objectPhonenumber:ss(kInactive)
	
end
-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:onInitialize()

	Layout.onInitialize(self)
	
	self.mIsWorking     = false
	self.mButtonResetY  = objectLivingroomPhoneResetbutton:y()
    
    for i, shadow in ipairs(self.mButtonNumbersShadow) do
        shadow:setPivotPoint("center")
        shadow:becomeChildOf(self.mDialfaceBack)
    end
    
    self.mDialfaceBack:setPivotPoint("center")
    self.mDialfaceFront:becomeChildOf(self.mDialfaceBack)
    
    self.mDialfaceRotation = self.mDialfaceBack:rotation()
    
end
-- ------------------------------------------------------------------------------
function layoutLivingroomPhone:onTick(time)
	
	Layout.onTick(self, time)
	
	if (self.mIsWorking) then
		
		if (not self.mIgnoreDialing) then
			if (self.mDialTimer >= 0 and self.mDialTimer < self.kDialTimeOut) then
			
				self.mDialTimer = self.mDialTimer + time
	
				if (not self.mIsDialing and self.mDialTimer >= self.kDialTimeOut) then
					self:playWrongNumberAudio()
					self.mIgnoreDialing  = true
					self.mResetHelpTimer  = 0
				end
			end
		end
	
		if (self.mResetHelpTimer >= 0 and self.mResetHelpTimer < self.kResetHelpTimeOut) then
			
			self.mResetHelpTimer = self.mResetHelpTimer + time
			
			if (self.mResetHelpTimer >= self.kResetHelpTimeOut) then
			
				-- HELP shopping list
				local helpParams =
				{
					hintIdx = 29,
					x = 612,
					y = 15,
					delayFunction = function()
						return isDialogActive("DIALOG_PUZZLETIPS")
					end
				}
			
				queueHelpDialogExt(helpParams)
				queueHelpArrow(30, 29, 560, 146)
				
			end
			
		end
		
	end
	
end
-- ------------------------------------------------------------------------------
function layoutLivingroomPhone:onStartAudio()

	self:checkDialTone()
    
end

-- ------------------------------------------------------------------------------
function layoutLivingroomPhone:onStopAudio()

    stopAudioGroup("PHONE")
    
end

-- ------------------------------------------------------------------------------
function layoutLivingroomPhone:onShow()
	Layout.onShow(self)
	
	self.mDialTimer     = 0
	self.mResetHelpTimer = -1
end
-- ------------------------------------------------------------------------------
function layoutLivingroomPhone:onHide()

    Layout.onHide(self)
    
    self:resetSequenceDialed()
    
end
-- ------------------------------------------------------------------------------
function layoutLivingroomPhone:onQuickPlay()

	Layout.onQuickPlay(self)
	
	objectBedroomHandsetcord:ss(kFound)
	objectBasementDialface:ss(kFound)
	objectShedEarpiece1:ss(kFound)
	objectLivingroomEarpiece2:ss(kFound)

end
-- ------------------------------------------------------------------------------
function layoutLivingroomPhone:onSolve()

	Layout.onSolve(self)

	for i, object in ipairs(self.mHiddenObjects) do
	    object:ss(kDone)
	end
	
	for object, props in pairs(self.mIOs) do
		if (objectLivingroomPhoneResetbutton ~= object) then
		    object:ss(kDone)
		end
	end
	
	stopAudioGroup("PHONE")
	
end
-- ------------------------------------------------------------------------------
function layoutLivingroomPhone:onSolveStart()

	Layout.onSolveStart(self)
	
	-- if we aren't dialing/cheating call on solve finished manually
    if (not self.mIsDialing) then
        self:solveFinish()
    end
end
-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:onSolveFinish()
    Layout.onSolveFinish(self)
    
    -- play movie
    play(movieSettings[kMoviePhone])
end
-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:onUsedAll(object, props)

	Layout.onUsedAll(self, object, props)
	
	if (object == objectLivingroomPhoneCordSocket) then
		objectLivingroomPhoneCordSocket:ss(kDone)
		objectLivingroomPhoneCord:ss(kIdle)
		
	elseif (object == objectLivingroomPhoneDialfaceSocket) then
		objectLivingroomPhoneHighlight:ss(kIdle)
		objectLivingroomPhoneHighlight2:ss(kIdle)
		objectLivingroomPhoneDialfaceFront:ss(kIdle)
		objectLivingroomPhoneDialfaceBack:ss(kIdle)
		objectLivingroomPhoneNumbershadow0:ss(kIdle)
		objectLivingroomPhoneNumbershadow1:ss(kIdle)
		objectLivingroomPhoneNumbershadow2:ss(kIdle)
		objectLivingroomPhoneNumbershadow3:ss(kIdle)
		objectLivingroomPhoneNumbershadow4:ss(kIdle)
		objectLivingroomPhoneNumbershadow5:ss(kIdle)
		objectLivingroomPhoneNumbershadow6:ss(kIdle)
		objectLivingroomPhoneNumbershadow7:ss(kIdle)
		objectLivingroomPhoneNumbershadow8:ss(kIdle)
		objectLivingroomPhoneNumbershadow9:ss(kIdle)
		
		-- Activate the numbers so we can dial
		objectLivingroomPhone0:ss(kActive)
		objectLivingroomPhone1:ss(kActive)
		objectLivingroomPhone2:ss(kActive)
		objectLivingroomPhone3:ss(kActive)
		objectLivingroomPhone4:ss(kActive)
		objectLivingroomPhone5:ss(kActive)
		objectLivingroomPhone6:ss(kActive)
		objectLivingroomPhone7:ss(kActive)
		objectLivingroomPhone8:ss(kActive)
		objectLivingroomPhone9:ss(kActive)
		
	elseif (object == objectLivingroomPhoneEarpiece1Socket) then
		objectLivingroomPhoneEarpiece1Socket:ss(kDone)
		objectLivingroomPhoneEarpiece1:ss(kIdle)
		
	elseif (object == objectLivingroomPhoneEarpiece2Socket) then
		objectLivingroomPhoneEarpiece2Socket:ss(kDone)
		objectLivingroomPhoneEarpiece2:ss(kIdle)
		
	end
	
	self.mIsWorking = true
	
	for i, object in ipairs(self.mHiddenObjects) do
	    if (not object:isUsed()) then
	        self.mIsWorking = false
	    end
	end
	
	self:checkDialTone()
	
end
-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:onMouseDown(x, y)
    
    local result = Layout.onMouseDown(self, x, y)
    
    if (self:isPuzzling() and not self.mIsDialing) then
        if (objectLivingroomPhoneResetbutton:isInside(x, y)) then
            -- reset button clicked
            self:resetSequenceDialed()
            self:animResetSequenceDialed()
            -- do not show reset help
            if (self.mIsWorking) then
            	self.mResetHelpTimer = self.kResetHelpTimeOut
            end
        else
            for i, button in ipairs(self.mButtonNumbers) do
                if (button:isInside(x, y) and button:isActive()) then
                	-- button number clicked
                	if (objectPhonenumber:isIdle() or self:isQuickPlay()) then
                    	self:dial(i - 1)
                    else
                    	think("MESSAGE_NEED_PHONENUMBER")
                    end
                    
                    break
                end
            end
        end
    end
    
    return result
end
-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:checkSequenceDialed()
    
    if (not self.mIgnoreDialing) then
    	local nrInSequence = #self.mSequenceCorrect
    	local nrDigitsDialled = #self.mSequenceDialed
    	if (nrInSequence == nrDigitsDialled) then
	        local equal = true
        	for i, number in ipairs(self.mSequenceCorrect) do
	            if (i <= #self.mSequenceDialed) then
                	if (number ~= self.mSequenceDialed[i]) then
	                    equal = false
                    	break
                	end            
            	end        
        	end
        
	        if (equal) then
            	self:solve()
            	return
        	end
    	elseif (nrDigitsDialled > nrInSequence) then
			self:playWrongNumberAudio()
			self.mIgnoreDialing  = true
			self.mResetHelpTimer  = 0
    	end
    
    	for i, sequence in ipairs(self.mSequencesFake) do
	        if (#sequence == #self.mSequenceDialed) then
            	local equal = true
	            
            	for j, number in ipairs(sequence) do
	                if (number ~= self.mSequenceDialed[j]) then
                    	equal = false
                    	break
                	end
            	end
	            
            	if (equal) then
	                -- TODO sound breathing
                	self:playBreathingAudio()
                	self.mIgnoreDialing  = true
                	self.mResetHelpTimer  = 0
                	break
            	end
        	end        
	    end
	end
	
end
-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:playBreathingAudio()
	-- make sure the dialing tone is stopped
    getAudioManager():stopTrack({tag="DIAL_TONE"})
	getAudioManager():playTrack({group="PHONE", tag="BREATHING", definition="TELEPHONE_BREATHING"})
end

-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:playWrongNumberAudio()
	-- make sure the dialing tone is stopped
    getAudioManager():stopTrack({tag="DIAL_TONE"})
	getAudioManager():playTrack({group="PHONE", tag="WRONG", definition="TELEPHONE_WRONG"})
end
-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:resetSequenceDialed()
	self.mSequenceDialed = {}
	self.mIgnoreDialing  = false
	self.mDialTimer 	 = 0	
end

-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:animResetSequenceDialed()

	stopAudioGroup("PHONE")
	
	objectLivingroomPhoneResetbutton:animate(
		"animResetSequence_blocking",
		{ command="playTrack group=PHONE track=TELEPHONE_RESET", target=self },
		{ y=self.mButtonResetY + 10,    duration=150 },
		{ y=self.mButtonResetY,         duration=180 }
	)
	      
end

-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:onAnimationFinish(animationName)

	Layout.onAnimationFinish(self, animationName)
	
	if (animationName == "animResetSequence_blocking") then
		if (self.mIsWorking and self:isVisible()) then
			getAudioManager():playTrack({group="PHONE", tag="DIAL_TONE", definition="TELEPHONE_DIAL_TONE"})
		end
	end
end

-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:dial(number)
    
    -- set dialing
    self.mIsDialing = true
    
    -- set init rotation
    self.mDialfaceBack:setRotation(self.mDialfaceRotation)
    
    local idx = number + 1
    
    if (self.mIsWorking and not self.mIgnoreDialing) then
        table.insert(self.mSequenceDialed, number)
        
        self:checkSequenceDialed()
    end
    
    -- make sure the dialing tone is stopped
    getAudioManager():stopTrack({tag="DIAL_TONE"})
    
    -- light up the clicked number
    if (not self.mIgnoreDialing) then
    	self.mButtonNumbersGlow[idx]:ss(kIdle)
    end
    
    -- calc rotation angle
    local fac = number
    
    if (fac == 0) then 
        fac = 10
    end
    
    local angle = 30 * fac
    
    -- calc duration
    local durationDown = fac * self.kDialDownSpeedFactor
    local durationUp   = fac * self.kDialUpSpeedFactor
    
    -- rotate the dial ccw fast cw slow
    self.mDialfaceBack:animate ("DialRotate_blocking",
		{ command="playTrack group=PHONE track=TELEPHONE_DIAL", target=self },
        { drotation=angle,  duration=durationDown   },
        { drotation=-angle, duration=durationUp     },
        { call=onDialFinished                       }
    )
    
    -- rotate the number shadows
    for i, shadow in ipairs(self.mButtonNumbersShadow) do
        shadow:animate (
            { drotation=-angle,     duration=durationDown   },
            { drotation=angle,      duration=durationUp     }
        )
    end
    
end
-- ----------------------------------------------------------------------------
function onDialFinished()
    local self = layoutLivingroomPhone
    
    -- dialing
    self.mIsDialing = false
    
    -- set init rotation
    self.mDialfaceBack:setRotation(self.mDialfaceRotation)
    
    -- kill the glow on the clicked number
    for i, glow in ipairs(self.mButtonNumbersGlow) do
        if (glow:isIdle()) then
            glow:ss(kInactive)
        end
    end
    
    if (self.mIsWorking and not self.mIgnoreDialing) then
        self.mDialTimer = 0	
    end
    
    if (self:isSolving()) then
        self:solveFinish()
    end    
    
end
-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:checkDialTone()
	if (self.mIsWorking) then
		getAudioManager():playTrack({group="PHONE", tag="DIAL_TONE", definition="TELEPHONE_DIAL_TONE"})
	end
end
-- ----------------------------------------------------------------------------
function layoutLivingroomPhone:isSkippable()
	return goalCallPolice:isActive()
end
