class 'l_12_grannies_room_z_clock' (ZoomEventHandler)

local HOUR_NAND = 'hour_hand'
local MINUTE_NAND = 'minute_hand'

local PLUMMET = 'plummet_'
local LIGHT = 'light_'

local TIME = 1

local PUSH_DISTANCE = 40

local SAVE_STATE = '12_grannies_room_z_clock'

function l_12_grannies_room_z_clock:__init(name)
    ZoomEventHandler.__init(self, name)
    
    ObjectVisible('area_hint_no_code', false)
    
    self:hideLight()
    self:hideDigits()
    self:setCursors()
	
	self.time = { hour = 0, minute = 0 }
	self.finish = { hour = 7, minute = 15 }
    self:setHand( self.time.hour, self.time.minute )
    
    local x, y, w, h = self.level.zoom_clock:GetClippingInfo()
	Object( PLUMMET .. 1 ):SetClippingRect( x, y, w, h )
	Object( PLUMMET .. 2 ):SetClippingRect( x, y, w, h )

    self.soundEntity = {}
    self.soundEntity.apply = MakeSoundEntity( '12_grannies_room_z_clock_apply_plummet' )
    self.soundEntity.pushPlummet = MakeSoundEntity( '12_grannies_room_z_clock_push_plummet' )
    self.soundEntity.rightTime = MakeSoundEntity( '12_grannies_room_z_clock_right_time' )

	self:loadGame()

    -------------------------
    -- tool apply
    do
        ObjectOpacity('plummet_1', 0)
        ObjectOpacity('plummet_2', 0)
        ----
        
        self.weightsAppliedCount = 0
        ----
        
        local function on_apply_weight(e)
	        local id = (string.sub(e.toolName, -1) == '1') and '2' or '1'
	        
	        self.weightsAppliedCount = self.weightsAppliedCount + 1
	        
	        if self.weightsAppliedCount < 2 then
	            self.level.custom_cursors[e.target] = 'CursorActive'
	        else
	            ObjectVisible(e.target, false)
	        end
	        
	        if e.save then
	            ObjectOpacity('plummet_'..id, 1)
	            
	            if self.weightsAppliedCount == 2 then
	                if HasActionCompletedState('clock_code_found') then
                        self:startMMG()
                    else
                        ObjectVisible('area_hint_no_code', true)
                    end
                end
	        else
	            local time = 0.5
	            ObjectOpacity('plummet_'..id, 1, time)
	            ObjectOpacity('chain_'..id, 0, time)
	            self.soundEntity.apply:PlaySound( false )
	            if self.weightsAppliedCount == 2 then
                    self.level.timer:Add(function()
                                            if HasActionCompletedState('clock_code_found') then
                                                self:startMMG()
                                            else
                                                ObjectVisible('area_hint_no_code', true)
                                            end
                                         end,
                                         time)
                end
	        end
	    end
	    ----
	    
	    local function on_click_apply_weights_area(e)
	        return 'BBT_12_NEED_APPLY_WEIGHTS'
	    end
	    ----
	    
	    RegisterToolApplyInfo('10_weight_1', 'area_apply_zc_weights', on_apply_weight, on_click_apply_weights_area)
	    RegisterToolApplyInfo('10_weight_2', 'area_apply_zc_weights', on_apply_weight, on_click_apply_weights_area)
    end
    
    RegisterMonologObject('area_hint_no_code', 'BBT_12_NO_CODE')
end

function l_12_grannies_room_z_clock:startMMG()
    ObjectVisible('area_hint_no_code', false)
    
    if not HasActionCompletedState('mmg_started') then
        AddActionCompletedState('mmg_started')
    end
    
    if self.zone:IsVisible() then
        self.level:GetPanelLuaObject():SetMode( EPanelMode.PM_MINIGAME )
    end
end

function l_12_grannies_room_z_clock:SkipMiniGame()

	self.zone:Disable()

	Object( HOUR_NAND ):DestroyEffectors()
	Object( MINUTE_NAND ):DestroyEffectors()

	self.level:GetPanelLuaObject():SetMode( EPanelMode.PM_QUEST )
	self:setHand( self.finish.hour, self.finish.minute )
	ObjectOpacity( { 'seven', 'trhee' }, TIME  )

	self:blockMouseOnPeriod( TIME * 3 + 0.01 )
	
	self.soundEntity.rightTime:StopSound(  )
	self.soundEntity.rightTime:PlaySound( false )
    
    self.time.hour   = self.finish.hour
    self.time.minute = self.finish.minute
    
    self:saveGame()
    
	self.timer:Add( function ()
		self.timer:Add( function()
			self:OnComplete(false)
		end, TIME * 2 )
	end, TIME + 0.01 )

end

function l_12_grannies_room_z_clock:saveGame()

	Helpers.SaveObjectToGameState( SAVE_STATE, self.time )

end

function l_12_grannies_room_z_clock:loadGame()

	local save = Helpers.RestoreObjectFromGameState( SAVE_STATE )
	if( save ~= nil ) then
		self.time = save
		self:setHand( self.time.hour, self.time.minute )
		
		if self:Win() then
            --self.soundEntity.rightTime:StopSound(  )
            --self.soundEntity.rightTime:PlaySound( false )
            
            ObjectOpacity( { 'seven', 'trhee' }, 0  )
            self:OnComplete(true)
		end
	end
end

function l_12_grannies_room_z_clock:OnComplete(save)
    ObjectOpacity('z_floor_clock_open',  1)
    ObjectOpacity('z_floor_clock_close', 0)
    
    ObjectVisible('area_hint_zf_scratch', false)
    ObjectVisible('area_click_zf_board',  true)
    
    if save then
        self.zone:Disable()
        ObjectOpacity('layer_clock_open',  1)
        ObjectOpacity('layer_clock_close', 0)
        
        ObjectVisible('area_zoom_safe', true)
    else
        self.level.mouseBlocker:Push()
        self.zone:CloseAndDisable()
        
        self.level.timer:Add(   function()
                                    local time = 0.5
                                    ObjectOpacity('layer_clock_open',  1, time)
                                    ObjectOpacity('layer_clock_close', 0, time)
                                    MakeSoundEntity('12_open_clock'):PlaySound(false)
                                    
                                    self.level.timer:Add(   function()
                                                                self.level.mouseBlocker:Pop()
                                                                ObjectVisible('area_zoom_safe', true)
                                                            end,
                                                            time)
                                end,
                                ZOOM_ZONE_APPEARING_TIME)
    end
end

function l_12_grannies_room_z_clock:OnScreenObjectMouseDown( object, key )
    if not HasActionCompletedState('mmg_started') then
        return
    end
    
	if key == HGEK_LBUTTON then
		local objectName = object:GetName()

		if( string.find( objectName, PLUMMET ) ) then
		
			self.soundEntity.pushPlummet:StopSound( )
			self.soundEntity.pushPlummet:PlaySound( false )
			
			self:pushPlummet( objectName )
			local num = tonumber( string.sub( objectName, -1 ) )
			self:pushHand( num )
			self:hideLight()
			
		elseif( string.find( objectName, LIGHT ) ) then
		
			self.soundEntity.pushPlummet:StopSound( )
			self.soundEntity.pushPlummet:PlaySound( false )
			
			local num = tonumber( string.sub( objectName, -1 ) )
			local hand = PLUMMET .. num

			self:pushPlummet( hand )
			self:pushHand( num )
			self:hideLight()
		end
		
		if self:Win() then
			self.timer:Add( function ()
								self.soundEntity.rightTime:StopSound(  )
								self.soundEntity.rightTime:PlaySound( false )
                                ObjectOpacity( { 'seven', 'trhee' }, TIME  )
                                self:blockMouseOnPeriod( TIME * 2 )
                                self.timer:Add( function()
                                    self:OnComplete(false)
                                end, TIME * 2 )
                            end,
                            TIME + 0.01 )
		end
	end
end

function l_12_grannies_room_z_clock:OnDefaultOpen()
    if HasActionCompletedState('mmg_started') then
	    self.level:GetPanelLuaObject():SetMode( EPanelMode.PM_MINIGAME )
	end
end

function l_12_grannies_room_z_clock:OnDefaultClose()
	self.level:GetPanelLuaObject():SetMode( EPanelMode.PM_QUEST )
end

function l_12_grannies_room_z_clock:Win()

	if( self.time.hour == self.finish.hour and self.time.minute == self.finish.minute ) then
		return true
	end
	return false

end

function l_12_grannies_room_z_clock:pushHand( plummetNumber )

	if( plummetNumber == 1 ) then

		self.time.minute = self.time.minute - 15
		self.time.hour = self.time.hour
		if( self.time.minute < 0 ) then
			self.time.minute = 45
			self.time.hour = self.time.hour - 1
			self:changeClockHand( -1, -15 )
			if( self.time.hour < 0 ) then
				self.time.hour = 11
			end
		else
			self:changeClockHand( 0, -15 )
		end
		self:saveGame()
		--Console:Log( 'hour = ' .. self.time.hour .. ' minute = ' .. self.time.minute )
		
	else

		self:changeClockHand( 1, 30 )

		self.time.minute = self.time.minute + 30
		self.time.hour = self.time.hour + 1
		if( self.time.minute >= 60 ) then
			self.time.minute = self.time.minute - 60
		end
		if( self.time.hour > 12 ) then
			self.time.hour = 1
		end
		self:saveGame()
		--Console:Log( 'hour = ' .. self.time.hour .. ' minute = ' .. self.time.minute )
		
	end

end

function l_12_grannies_room_z_clock:changeClockHand( hour, minute )

	Object( HOUR_NAND ):Rotate( math.rad( hour * 30 ), TIME )
	Object( MINUTE_NAND ):Rotate( math.rad( minute * 6 ), TIME )

end


function l_12_grannies_room_z_clock:pushPlummet( handName )

	local x = Object( handName ):GetX()
	local y = Object( handName ):GetY()
	Object( handName ):MoveTo( x, y + PUSH_DISTANCE, TIME / 2 )
	self.timer:Add( function()
		Object( handName ):MoveTo( x, y, TIME / 2 )
	end, TIME / 2)

	self:blockMouseOnPeriod( TIME )

end

function l_12_grannies_room_z_clock:blockMouseOnPeriod( period )
	
	self.mouseBlocker:Push()
	self:ignoreHands( true )

	self.timer:Add( function ()
		self.mouseBlocker:Pop()
		self:ignoreHands( false )
	end, period )

end

function l_12_grannies_room_z_clock:ignoreHands( ignore )

	if( ignore ) then
		play_field:IgnoreOnTestPoint( PLUMMET .. 1 )
		play_field:IgnoreOnTestPoint( PLUMMET .. 2 )
	else
		play_field:DoNotIgnoreOnTestPoint( PLUMMET .. 1 )
		play_field:DoNotIgnoreOnTestPoint( PLUMMET .. 2 )
	end
	
end

function l_12_grannies_room_z_clock:setHand( hour, minute )

	assert( hour >= 0 and hour < 13 )
	assert( minute >= 0 and minute < 61 )

	--// minute hand 180
	--// hour hand 0
	Object( HOUR_NAND ):SetAngle( math.rad( hour * 30 ) )
	Object( MINUTE_NAND ):SetAngle( math.rad( minute * 6 + 180 ) )

end

function l_12_grannies_room_z_clock:hideLight( )

	ObjectOpacity( { 'light_1', 'light_2' }, 0 )

end

function l_12_grannies_room_z_clock:hideDigits( )

	ObjectOpacity( { 'seven', 'trhee' }, 0  )

end

function l_12_grannies_room_z_clock:setCursors()

	self.level.custom_cursors[ 'light_1' ] = "CursorActive"
	self.level.custom_cursors[ 'light_2' ] = "CursorActive"
	self.level.custom_cursors[ 'plummet_1' ] = "CursorActive"
	self.level.custom_cursors[ 'plummet_2' ] = "CursorActive"

end

function l_12_grannies_room_z_clock:OnObjectMouseEnter( object )

	local name = object:GetName()
	if( string.find( name, PLUMMET ) ) then
		local num = tonumber( string.sub( name, -1 ) )
		ObjectOpacity( LIGHT .. num, 1 )
	elseif( string.find( name, LIGHT ) ) then
		ObjectOpacity( name, 1 )
	end

end

function l_12_grannies_room_z_clock:OnObjectMouseLeave( object )

	local name = object:GetName()
	if( string.find( name, PLUMMET ) ) then
		local num = tonumber( string.sub( name, -1 ) )
		ObjectOpacity( LIGHT .. num, 0 )
	elseif( string.find( name, LIGHT ) ) then
		ObjectOpacity( name, 0 )
	end

end

--------------------------------------------------------------------------------
-- CLEAR_ON_SAVE_BEGIN
-- Do not edit code listed below (generated by the editor on each save)
function l_12_grannies_room_z_clock:LoadData(screen)
	local anim
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/z_clock/z_clock_bg.jpg',0,0,640,480,1,0)
	anim:SetHotSpot(320,240)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('z_clock_bg',anim,512,384,100,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/z_clock/center.png',0,0,26,26,1,0)
	anim:SetHotSpot(13,13)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('center',anim,521,323,107,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/z_clock/hour_hand.png',0,0,54,88,1,0)
	anim:SetHotSpot(29,84)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('hour_hand',anim,521,323,106,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/z_clock/light_1.png',0,0,88,108,1,0)
	anim:SetHotSpot(44,54)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('light_1',anim,535,553,104,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/z_clock/light_2.png',0,0,88,128,1,0)
	anim:SetHotSpot(44,64)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('light_2',anim,485,564,105,-2,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/z_clock/minute_hand.png',0,0,28,124,1,0)
	anim:SetHotSpot(14,9)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('minute_hand',anim,521,323,105,180,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/z_clock/onlay.png',0,0,250,98,1,0)
	anim:SetHotSpot(125,49)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('onlay',anim,513,442,104,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/z_clock/plummet_1.png',0,0,76,214,1,0)
	anim:SetHotSpot(38,107)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('plummet_1',anim,545,495,102,-2,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/z_clock/plummet_2.png',0,0,58,238,1,0)
	anim:SetHotSpot(29,119)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('plummet_2',anim,493,505,103,-2,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/z_clock/seven.png',0,0,84,88,1,0)
	anim:SetHotSpot(42,44)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('seven',anim,474,403,105,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/z_clock/trhee.png',0,0,84,96,1,0)
	anim:SetHotSpot(42,48)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('trhee',anim,613,319,104,0,1,1)
	
	anim = CreateAnimation('data/Texture/Common/area.png',0,0,8,8,1,0)
	anim:SetHotSpot(4,7)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_apply_zc_weights',anim,517,570,200,0,50,55)
	
	anim = CreateAnimation('data/Texture/Common/area_round.png',0,0,128,128,1,0)
	anim:SetHotSpot(64,64)
	anim:SetBlendMode(2)
	anim:SetColor('0x01FFFFFF')
	screen:CreateObject('area_hint_no_code',anim,514,399,201,0,3,3.5)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/z_clock/chain_1.png',0,0,32,122,1,0)
	anim:SetHotSpot(16,61)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('chain_1',anim,545,514,101,0,1,1)
	
	anim = CreateAnimation('data/Texture/Scenes/12_grannies_room/main/z_clock/chain_2.png',0,0,32,140,1,0)
	anim:SetHotSpot(16,70)
	anim:SetBlendMode(2)
	anim:SetColor('0xFFFFFFFF')
	screen:CreateObject('chain_2',anim,492,515,101,0,1,1)
	
end
-- CLEAR_ON_SAVE_END
