-------------------------------------------------------------------------------
class 'PhotoGallery' (Node)
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
local PAGE_NAME         = 'Layer_'
local WAY_NAME 	        = 'anchor_ways_'

local LOC_START_SCALE   = 0.7
local LOC_FINAL_SCALE   = 0.85

local PREVIEW_SIZE      = {
                            width   = 220,
                            height  = 120
                          }

local RES_PATH          = 'Data/Textures/FlyPhotos/'
local RES_PREW_PATH     = 'Data/Textures/PhoneGallery/'
local VIDEO_PATH        = 'Data/Video/Gallery/'
local SOUND_PATH        = 'Data/Sounds/Gallery/'
local DEFAULT_INDEX     = 1

local SCALE_PREVIEW     = 0.9
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:__init(screen)   
    Node.__init(self, screen:getMainEntity(), 'PhotoGallery', 0)  
    
    self.main_entity:attachTo( screen:getEntityNamed('anchor_content') )
    self.main_entity:setXY(10, -772)

    self.screen     = screen 
    self.gallery_pages  = {}

    self.pageCount = 0;

    self:loadData()
    self:init()    
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:init()
    self:initGalleryPages()

    self.c_index = self:getCurrentPage()

    self:createPage(self.gallery_pages[self.c_index]) 

    self.prev_bttn  = self:getEntityNamed('bttn_gallery_prev')   
    self.next_bttn  = self:getEntityNamed('bttn_gallery_next')  

    self.prev_bttn.onPress:add( bind(self.onClickBttnPrev, self) )
    self.next_bttn.onPress:add( bind(self.onClickBttnNext, self) )

    self:checkBttnsStates()
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:onClickBttnPrev()
    local prev_index    = self.c_index
    self.c_index        = self.c_index - 1
    self:checkBttnsStates()
    
    local entity_page = self.gallery_pages[prev_index]   
    
    self:showPage(self.gallery_pages[self.c_index], entity_page, 'prev') 
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:onClickBttnNext()
    local prev_index    = self.c_index
    self.c_index        = self.c_index + 1
    self:checkBttnsStates()

    local entity_page = self.gallery_pages[prev_index]
    self:showPage(self.gallery_pages[self.c_index], entity_page, 'next') 
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:checkBttnsStates()
    SetGameState('CurrentPhotoGalleryPage', tostring(self.c_index))

    self:getEntityNamed('gallery_page'):setText(tostring(self.c_index))   
    if self.pageCount - 1  > 0 then
        if self.c_index > 1 and self.c_index < self.pageCount then
            self.prev_bttn:show()
            self.next_bttn:show()
        elseif self.c_index > 1 then
            self.prev_bttn:show()
            self.next_bttn:hide()
        else
            self.prev_bttn:hide()
            self.next_bttn:show()
        end
    else
        self.prev_bttn:hide()
        self.next_bttn:hide()
    end  
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:initGalleryPages()
    if HasGameState('PhotoGallerySlot') then
        local size = GetGameState('PhotoGallerySlot'):getSize()

        self.pageCount = 0
     
        for index = 1, size do    
            local id            = GetGameState('PhotoGallerySlot'):get(index - 1)
            local galleryType   = GetGameState('GalleryType_' .. id):get(0)

            local name = PAGE_NAME .. id 

            local page = math.floor((index - 1)/ 9)

            local current_index = ((index - 1) - (9 * page)) 
            local column = current_index % 3
            local row    = math.floor(current_index/ 3)

            local x = (PREVIEW_SIZE.width * SCALE_PREVIEW + 15) * column - 300
            local y = (PREVIEW_SIZE.height * SCALE_PREVIEW + 15) * row - 185

            local anchor = nil
            if not self:hasEntityNamed('anchor_preview_page_' .. page) then
                anchor = CreateSpriteEntity('')
                anchor:attachTo(self:getEntityNamed('anchor_preview'))
                anchor:setName('anchor_preview_page_' .. page)
                anchor:setRespondable(true)                
                anchor:setOrder(10) 
                anchor:setXY(320, 190) 
                self.pageCount = self.pageCount + 1
                self.gallery_pages[self.pageCount] = anchor
            else
                anchor = self:getEntityNamed('anchor_preview_page_' .. page)
            end

            local preview = CreateSpriteEntity(RES_PREW_PATH .. name .. '_preview.jpg')
            preview:attachTo(anchor)
            preview:setName(name .. '_preview')
            preview:setXY(x + 110, y + 60)
            preview:setRespondable(true)
            preview:setScale(SCALE_PREVIEW)
            preview:setOrder(10)    

            local icon = nil
            if (galleryType == 'video') then
                icon = CreateSpriteEntity(RES_PREW_PATH .. 'video_camera_icon.png') 
                icon:setXY(0, 0)
            else
                icon = CreateSpriteEntity(RES_PREW_PATH .. 'camera_icon.png') 
                icon:setXY(97, 52)
            end
            icon:attachTo(preview)
            icon:setName(name .. '_icon')
            
            icon:setRespondable(false)
            icon:setOrder(2)    

            preview.onPress:add( function(entity) self:pressOnPreview(entity, galleryType) end)
            preview.onFocusIn:add(function(entity) self:zoomIn(entity) end)
            preview.onFocusOut:add(function(entity) self:zoomOut(entity) end)

            anchor:hide()
        end  

        return self.pageCount    
    end
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:zoomIn(entity)
    self.zoom_in_effector = entity:ChangeScale(SCALE_PREVIEW, SCALE_PREVIEW*1.05, 0.2)
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:zoomOut(entity)
    if self.zoom_in_effector then
        entity:destroyEffectors()
        self.zoom_in_effector = nil
    end
    entity:ChangeScale(entity:getScale().x, SCALE_PREVIEW, 0.2)
end
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
function PhotoGallery:pressOnPreview(entity, galleryType)
    if entity then
        local texture_name = string.gsub(entity:getName(), '_preview', '')

        self.current_view = nil
        if (galleryType == 'video') then
            self.current_view = CreateVideoEntity(VIDEO_PATH .. texture_name .. '.webm', false)
            self.current_view:setSoundTrack(SOUND_PATH .. texture_name .. '.ogg')
            self.current_view:setHideOnStop(false)
        else
            self.current_view = CreateSpriteEntity(RES_PATH .. texture_name .. '.jpg')
        end
        
        self.current_view:attachTo(self.screen:getEntityNamed('anchor_content'))
        self.current_view:setName(texture_name .. '_preview')
        self.current_view:setScrXY(entity:getScrX(), entity:getScrY())
        self.current_view:setRespondable(true)
        self.current_view:setOrder(1000)  

        local start_pos = { x = self.current_view:getX(), y = self.current_view:getY() }

        local effector_show = self:createShowEffector(self.current_view, start_pos)
        
        playCommonSound('GalleryShowPreview')

        local function on_end_show()
            local function on_press_close()
                local effector_hide = self:createHideEffector(self.current_view, start_pos)

                playCommonSound('GalleryShowPreview')

                local function on_end_hide()
                    self.current_view:hide()
                    entity:setRespondable(true)                    
                    self.current_view:destroy()
                end 

                entity:ChangeOpacity(0, 1, 0.1)  
                effector_hide.onFinish:add(on_end_hide)
                effector_hide:play()
            end
            local bttn_close = CreateButtonEntity(RES_PREW_PATH .. 'bttn_close.png')
            bttn_close:attachTo(self.current_view)
            bttn_close:setName(self.current_view:getName() .. '_close')
            bttn_close:setXY(390, -185)
            bttn_close:setOrder(10)
            bttn_close:setRespondable(true)

            bttn_close.onPress:add(on_press_close)

            if (galleryType == 'video') then
                self:createVideoPlay()
            end
        end

        entity:setRespondable(false)
        entity:ChangeOpacity(1, 0, 0.1)        

        effector_show.onFinish:add(on_end_show)
        effector_show:play()
    end
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:createVideoPlay()
    local bttn_play = CreateButtonEntity(RES_PREW_PATH .. 'bttn_play.png')
    bttn_play:attachTo(self.current_view)
    bttn_play:setName(self.current_view:getName() .. '_play')
    bttn_play:setXY(0, 185)
    bttn_play:setOrder(10)
    bttn_play:setRespondable(true)

    local bttn_pause = CreateButtonEntity(RES_PREW_PATH .. 'bttn_pause.png')
    bttn_pause:attachTo(self.current_view)
    bttn_pause:setName(self.current_view:getName() .. '_pause')
    bttn_pause:setXY(0, 185)
    bttn_pause:setOrder(10)
    bttn_pause:hide()

    local function on_press_play()        
        self.current_view:play()  
        bttn_pause:show()   
        bttn_play:hide()   
    end

     local function on_press_pause()       
        self.current_view:pause()
        bttn_pause:hide()   
        bttn_play:show()         
    end

    local function on_end_video()
        self.current_view:stop()
        self.current_view:play()
        self.current_view:pause()
    end

    bttn_play.onPress:add(on_press_play)     
    bttn_pause.onPress:add(on_press_pause)     
    self.current_view.onFinish:add(on_end_video) 
end 
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:createShowEffector(entity, start_pos)
    local targetPos = { x = 512, y = -386}
    local time = 0.2

    local effector_show  = CreateAnimationEffector()

    effector_show:attachTo(entity)
    effector_show:setDestroysOnEnd(true)       
    
    effector_show:setKey(_TRACK_X, 0, start_pos.x)
    effector_show:setKey(_TRACK_Y, 0, start_pos.y)
    effector_show:setKey(_TRACK_S, 0, 0.3)
    effector_show:setKey(_TRACK_X, time, targetPos.x)
    effector_show:setKey(_TRACK_Y, time, targetPos.y)
    effector_show:setKey(_TRACK_S, time, 1.0)

    return effector_show
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:createHideEffector(entity, endPos)
    local targetPos = { x = 512, y = -386}
    local time = 0.2

    local effector_hide = CreateAnimationEffector()

    effector_hide:attachTo(entity)
    effector_hide:setDestroysOnEnd(true)       
    
    effector_hide:setKey(_TRACK_X, 0, targetPos.x)
    effector_hide:setKey(_TRACK_Y, 0, targetPos.y)
    effector_hide:setKey(_TRACK_S, 0, 1.0)
    effector_hide:setKey(_TRACK_X, time, endPos.x)
    effector_hide:setKey(_TRACK_Y, time, endPos.y)
    effector_hide:setKey(_TRACK_S, time, 0.3)

    return effector_hide
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:getCurrentPage()
    if HasGameState('CurrentPhotoGalleryPage') then
        return tonumber(GetGameState('CurrentPhotoGalleryPage'):get(0))
    end

    return DEFAULT_INDEX
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:showPage(page, prev_page, direction)
    page:show() 

    local x_prev = 0
    local time = 0.4
    local scale_page = nil

    if (direction == 'prev') then  
        x_prev = prev_page:getX()     
        page:setX(-850)
        page:MoveTo(x_prev, page:getY(), time)  

        scale_page = prev_page
        prev_page:setOrder(9)
        prev_page:ChangeScale(1, 0.7, time)
        prev_page:ChangeOpacity(1, 0, time)
    else
        x_next = -850
        prev_page:MoveTo(x_next, page:getY(), time)  

        scale_page = page
        page:setOrder(9)
        page:ChangeScale(0.7, 1, time)
        page:ChangeOpacity(0, 1, time)
    end 

    local screen = self.screen
    screen.mouseBlocker:push()
    screen.timer:add( function()
                        screen.mouseBlocker:pop()
                        prev_page:hide()
                        scale_page:setOrder(10)
                      end, time)
    
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:createPage(page) 
    if HasGameState('PhotoGallerySlot') then
        page:show()       
    end
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:loadData()
    if self.CreateLoaderTable then
        local table_resource = self:CreateLoaderTable()
        LoadScene(self.main_entity, table_resource)
    end 
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:showScreen()
	self.main_entity:show()
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function PhotoGallery:hideScreen()
	self.main_entity:hide()
end
-------------------------------------------------------------------------------

--------------------------------------------------------------------------------
--                      GENERATED CONTENT. DO NOT EDIT.                       --
--------------------------------------------------------------------------------

function PhotoGallery:CreateLoaderTable()
	local objects =
	{
		{ name = 'gallery_bg',  entityType = 'SpriteEntity',  x = 508,  y = 384,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/gallery_bg.png',  width = 848,  height = 446,  },
		{ name = 'anchor_control',  entityType = 'SpriteEntity',  x = 840,  y = 552,  order = 60,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  width = 2,  height = 2,  },
		{ name = 'gallery_page',  entityType = 'TextEntity',  parentName = 'anchor_control',  x = -280,  y = 30,  order = 4,  editingLocked = false,  treeItemIsExpanded = true,  text = '12',  height = 24,  },
		{ name = 'bttn_gallery_prev',  entityType = 'ButtonEntity',  parentName = 'anchor_control',  x = -616,  y = -168,  order = 2,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/scroll_bttn.png',  text = '',  activeRectX = 75,  activeRectY = 442,  },
		{ name = 'bttn_gallery_next',  entityType = 'ButtonEntity',  parentName = 'anchor_control',  x = 67,  y = -168,  order = 2,  scaleX = -1,  scaleY = 1,  editingLocked = false,  treeItemIsExpanded = true,  texturePath = 'Data/Textures/Screens/Phone/scroll_bttn.png',  text = '',  activeRectX = 75,  activeRectY = 442,  },
		{ name = 'anchor_preview',  entityType = 'SpriteEntity',  x = 223,  y = 185,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = true,  width = 2,  height = 2,  },
	}
	return objects
end
