dofile ('Data/Scripts/Common/Screens/ZoomZoneBase.lua')

class 'ZoomZone' (ZoomZoneBase)
-------------------------------------------------------------------------------
function ZoomZone:__init(name, mode, playfield)
    ZoomZoneBase.__init(self, name, mode, playfield)
    
    self.animated_close_open = true  -- анимированое открытие/закрытие зума
    self.is_closable = true  -- анимированое открытие/закрытие зума

    self.current_blockers = 0
end
-------------------------------------------------------------------------------

function ZoomZone:getModeSizeTable()
	local modeSizeTable =
	{
		[ ZOOM_TYPE_FULLSCREEN		] = { w = WWORK_W, h = WWORK_H },
		[ ZOOM_TYPE_CIRCLE_BIG		] = { w = 300,	  h = 300	 },
		[ ZOOM_TYPE_RECT_VERTICAL	] = { w = 384,	  h = 512	 },
		[ ZOOM_TYPE_RECT_HORIZONTAL	] = { w = 640,	  h = 480	 },
		[ ZOOM_TYPE_NO_BORDER		] = { w = 0,	  h = 0		 },
		[ ZOOM_TYPE_DISABLED		] = { w = 8,	  h = 8		 },
		[ ZOOM_TYPE_VIDEO_CALL      ] = { w = 1036,   h = 568    },
		[ ZOOM_TYPE_MATCH3          ] = { w = 1366,   h = 568    },
	}
	return modeSizeTable
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function ZoomZone:open(time)
    if self:isVisible() or self:isDisabled() then
        return
    end

    self.playfield:hideInfromScreen()

    time = (time == nil) and ZOOM_ZONE_APPEARING_TIME or time
    local activeZone = self.playfield:getActiveZones():GetTop()
    
    if activeZone then
        local order = activeZone:getMainEntity():getOrder() + 10
        self:getMainEntity():setOrder(order)
    end
    self:show(time)
    
    if self.node and self.node.state ~= NodeState.TRIPPED then
        self.node:trigger()
    end

    self.playfield:getActiveZones():Push(self)
        
    self:onOpen()
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function ZoomZone:setMode(mode)
    local widthRTT
    local heightRTT
    local offsetX
    local offsetY

    if mode == ZOOM_TYPE_FULLSCREEN then
        -- РЎРѕР·РґР°РЅРёРµ СЃРїСЂР°Р№С‚Р° РґР»СЏ Р·Р°РєСЂС‹РІР°РЅРёСЏ Р·РѕРЅС‹
	    do
	        local size = Vector2f(WORK_W, 140)
	    
	        local closeFullScreenZone = CreateSpriteEntity('')
	        closeFullScreenZone:setName('closeFullScreenZone')
            closeFullScreenZone:attachTo(self:getMainEntity())
            closeFullScreenZone:setXY(0,  WORK_H/2 - size.y/2)
            closeFullScreenZone:setSize(size.x, size.y)
            closeFullScreenZone:setHotSpot(size.x/2, size.y/2)
            closeFullScreenZone:setColor( Colorf(1, 1, 1, 0.01) )
            closeFullScreenZone:setOrder(1500)

            local function close(entity, state)
                if state then
                    local active_zone = self.playfield:getActiveZones():GetTop()
                    if active_zone then
                        active_zone:close()
                    end
                end
            end

            closeFullScreenZone:setCursorOverName('CursorDown')
            closeFullScreenZone.onClickBegin:add(close)
	    end
    elseif mode == ZOOM_TYPE_CIRCLE_BIG then
        local zoom_border = CreateSpriteEntity('Data/Textures/GUI/Zoom/zoom300.png')
        zoom_border:attachTo(self:getMainEntity())
        zoom_border:setName('ZoomZoneBorder'..self.name)
        zoom_border:setXY(1, -2)
        zoom_border:setOrder(3)
        zoom_border:setRespondable(false)
        self.object_border = zoom_border
        
        local bttn_close = CreateButtonEntity('Data/Textures/GUI/Zoom/close.png')
        bttn_close:attachTo(self:getMainEntity())
        bttn_close:setName('ZoomZoneClose'..self.name)
	    bttn_close:setXY(99, -117)
	    bttn_close:setOrder(60)
	    bttn_close:setRespondable(false)
        bttn_close:setActiveRectSize(40, 30)
	    self.close_button = bttn_close
	    
	    widthRTT    = 300
        heightRTT   = 300
        offsetX     = 0
        offsetY     = 0        
    elseif mode == ZOOM_TYPE_RECT_HORIZONTAL then
        local zoom_border = CreateSpriteEntity('Data/Textures/GUI/Zoom/zoom640.png')
        zoom_border:attachTo(self:getMainEntity())
        zoom_border:setName('ZoomZoneBorder'..self.name)
        zoom_border:setXY(12,-8)
        zoom_border:setOrder(3)
        zoom_border:setRespondable(false)
        self.object_border = zoom_border
        
        local bttn_close = CreateButtonEntity('Data/Textures/GUI/Zoom/close.png')
        bttn_close:attachTo(self:getMainEntity())
        bttn_close:setName('ZoomZoneClose'..self.name)
	    bttn_close:setXY(321, -236)
	    bttn_close:setOrder(60)
	    bttn_close:setRespondable(false)
        bttn_close:setActiveRectSize(40, 30)
	    self.close_button = bttn_close
	    
	    widthRTT    = 640
        heightRTT   = 480
        offsetX     = 0
        offsetY     = 0
    elseif mode == ZOOM_TYPE_RECT_VERTICAL then
        local zoom_border = CreateSpriteEntity('Data/Textures/GUI/Zoom/zoom384.png')
        zoom_border:attachTo(self:getMainEntity())
        zoom_border:setName('ZoomZoneBorder'..self.name)
        zoom_border:setXY(12, -10)
        zoom_border:setOrder(3)
        zoom_border:setRespondable(false)
        self.object_border = zoom_border
        
        local bttn_close = CreateButtonEntity('Data/Textures/GUI/Zoom/close.png')
        bttn_close:attachTo(self:getMainEntity())
        bttn_close:setName('ZoomZoneClose'..self.name)
	    bttn_close:setXY(193, -253)
	    bttn_close:setOrder(4)
	    bttn_close:setRespondable(false)
        bttn_close:setActiveRectSize(40, 30)
	    self.close_button = bttn_close
	    
	    widthRTT    = 384
        heightRTT   = 512
        offsetX     = 0
        offsetY     = 0  
    elseif mode == ZOOM_TYPE_VIDEO_CALL then
        local zoom_under = CreateSpriteEntity('Data/Textures/GUI/Zoom/zoom_video_call_under.png')
        zoom_under:attachTo(self:getMainEntity())
        zoom_under:setName('ZoomZoneBorder'..self.name)
        zoom_under:setXY(0, 0)
        zoom_under:setOrder(0)
        zoom_under:setRespondable(false)
    
        local zoom_border = CreateSpriteEntity('Data/Textures/Screens/Phone/phone_cover.png')
        zoom_border:attachTo(self:getMainEntity())
        zoom_border:setName('ZoomZoneBorder'..self.name)
        zoom_border:setXY(0, 0)
        zoom_border:setOrder(4)
        zoom_border:setRespondable(false)
        self.object_border = zoom_border    
	    
	    widthRTT    = 1036
        heightRTT   = 568
        offsetX     = 0
        offsetY     = 0
    end
    
    -- create RTTEntity
    if widthRTT and heightRTT then
        --entity = CreateRTTEntity(widthRTT, heightRTT)
        entity = CreateRenderedObject()
        entity:attachTo(self:getMainEntity())
        entity:setName('ZoomZoneRTT'..self.name)
        entity:setOrder(1)
        self.rtt_entity = entity
        
        self:getMainEntity():setXY(self:getMainEntity():getX() + offsetX, self:getMainEntity():getY() + offsetY)
        
        local x = self:getMainEntity():getX() - offsetX
        local y = self:getMainEntity():getY() - offsetY
        self.entity_anchor:setScissorBox(x - widthRTT/2, y - heightRTT/2, x + widthRTT/2, y + heightRTT/2)
    end
    
    self.baseX = self:getMainEntity():getX()
    self.baseY = self:getMainEntity():getY()
end
-------------------------------------------------------------------------------

function ZoomZone:show(time)
    if self:isVisible() == true then
        return
    end
    
    self:loading_resource()
    -- Console:pushText('ZoomZone:show '..tostring(time))

    self:getMainEntity():destroyEffectors()
    self:getMainEntity():show()
    
    if self.playfield:getActiveZones():GetTop() == nil then --if first opening zoom
        self.playfield:getZoomFade():destroyEffectors()
        self.playfield:getZoomFade():show()
        if time and time > 0 then
            self.playfield:getZoomFade():ChangeOpacity(0, 1, time):setDestroysOnEnd(false)
        else
            self.playfield:getZoomFade():setOpacity(1)
        end
    end
    self.playfield:getZoomFade():setOrder( self:getMainEntity():getOrder() - 1 )
    self:getMainEntity():setOpacity(1)
    self.is_visible = true

    if self.animated_close_open and self.mode ~= ZOOM_TYPE_FULLSCREEN then
        if self.close_button then
            self.close_button:setRespondable(false)
        end
        self.mouseBlocker:push()
        self.current_blockers = self.current_blockers + 1

        local function on_oscillation_ended()
            if self.close_button then
                self.close_button:setRespondable(true)
            end

            for i = 1, self.current_blockers do
                self.mouseBlocker:pop()
            end
            self.current_blockers = 0

            self:onOpenFinished()
        end

        playCommonSound('ZoomOpen')        
        self.playfield:oscillateZoomIn(time,   -- open time
                                0.005,     -- amplitude
                                10,     -- frequency
                                0.1,    -- damping time                                
                                10,     -- number of iterations
                                self.trigger_object,
                                self:getMainEntity()
                            ).onFinish:add(on_oscillation_ended)
    else
        self:onOpenFinished()
    end

    self.playfield:hideBlackBar()
    
    if self.on_open then
        --on_open is deprecated and could be removed in future. Please use onOpen method instead!!!
        self:on_open()
    end
    
    --hack
    if self.mode == ZOOM_TYPE_VIDEO_CALL then
        self.playfield:getPanel():getControlsPanel():setPhoneButtonBlocked(true)
    end
    
    self:setZoomZoneVisited()
end
-------------------------------------------------------------------------------
function ZoomZone:close(time)
    if not self:isVisible() then
        return
    end

     if time ~= 0 and not self.is_closable then
        return
    end

    local active_zone = self.playfield:getActiveZones():GetTop()
    if active_zone == nil then
        WriteWarning("trying to close zoom "..self.name.." while zoom stack already empty")
        return
    elseif active_zone and active_zone.name ~= self.name then
        WriteWarning("trying to close zoom "..self.name.." while it is not on top if zoom stack")
        return
    end
    self.playfield:getActiveZones():Pop()

    time = (time == nil) and ZOOM_ZONE_APPEARING_TIME or time
    self:hide(time)
    
    self:onClose()
end
-------------------------------------------------------------------------------
function ZoomZone:hide(time)
    if self:isVisible() == false then
        return
    end   
    
    local active_zone = self.playfield:getActiveZones():GetTop()
    if not active_zone then
        self.playfield:getZoomFade():destroyEffectors()
        self.playfield:getZoomFade():setRespondable(false)
        self.playfield:getZoomFade():ChangeOpacity(nil, 0, time):setDestroysOnEnd(false)
    else
        self.playfield:getZoomFade():setOrder( active_zone:getMainEntity():getOrder() - 1)
    end
        
    self:getMainEntity():destroyEffectors()
    if time and time > 0 then        
        self.playfield:getTimer():add(  function()
                                            self.is_visible = false
                                            local active_zone = self.playfield:getActiveZones():GetTop()
                                            if active_zone then
                                                self.playfield:getZoomFade():setRespondable(true)
                                            end
                                        end, time)
    else
        self:getMainEntity():setOpacity(0)
        self:getMainEntity():hide()
        if not active_zone then
            self.playfield:getZoomFade():hide()
        end

        self.is_visible = false
    end

    if self.close_button then
        self.close_button:setRespondable(false)
    end

    local function do_on_close_finished()
        self:onCloseFinished()
            
        --hack
        if self.mode == ZOOM_TYPE_VIDEO_CALL then
            self.playfield:getPanel():getControlsPanel():setPhoneButtonBlocked(false)
        end
    end

    if self.animated_close_open and self.mode ~= ZOOM_TYPE_FULLSCREEN then
        self.mouseBlocker:push()
        self.current_blockers = self.current_blockers + 1

        local function on_oscillation_ended()
            self:getMainEntity():setOpacity(0)
            self:getMainEntity():hide()
      
            for i = 1, self.current_blockers do
                self.mouseBlocker:pop()
            end
            self.current_blockers = 0

            do_on_close_finished()
        end

        playCommonSound('ZoomClose')   
        self.playfield:oscillateZoomOut(1.1, -- max scale
                            0.1, -- min scale
                            time/2, -- scale up time
                            time/2,  -- scale down time
                            self.trigger_object,
                            self:getMainEntity()
                        ).onFinish:add(on_oscillation_ended)
    else
        self:getMainEntity():setOpacity(0)
        self:getMainEntity():hide()
        
        do_on_close_finished()
    end    

    self.playfield:hideBlackBar()

    if self.on_close then
        --on_close is deprecated, called in init by mistake, and could be removed in future. Please use onClose method instead!!!
        self:on_close()
    end
end

-------------------------------------------------------------------------------
function ZoomZone:onOpenFinished()
end

-------------------------------------------------------------------------------
function ZoomZone:onCloseFinished()
end