-- Closeup - базовый класс клозапа

----------------------------------------------------------------------------------------------------
Closeup = {}
Closeup.active = false

----------------------------------------------------------------------------------------------------
function Closeup:new(aName, aScene)
	local obj = {}
	setmetatable(obj, self)
	self.__index = self

	obj.mName = aName
    obj.mScene = aScene

    obj.mDisplay = level_mgr:load_level(aName)

    obj.mDisplay:get_element('cu_parent'):setVisible(false)
    obj.mDisplay:set_visible(false)
    obj.mIsVisible = false
    
    obj:loadAmbients()

    obj.mHids = {}
    obj:processChildren(obj.mDisplay, '', 'hid_'..obj.mScene..'_',
        function(elem)
            obj.mHids[elem:getName()] = true
        end)

	return obj
end

function Closeup:setCloseCB(aFunc)
    self.mCloseCB = aFunc
end

----------------------------------------------------------------------------------------------------
function Closeup:loadAmbients()
    self.mAmbients = {}

    local func = function(elem)
            local name = string.gsub(elem:getName(), "amb_", "")
            self.mAmbients[name] = {}
            self.mAmbients[name].elem = elem
        end

    self:processChildren(self.mDisplay, "", "amb_", func)
end

----------------------------------------------------------------------------------------------------
function Closeup:processChildren(aDisplay, aElemName, aPrefix, aFunc)
    scene = aDisplay:get_element(aElemName)

    local size = aDisplay:get_element_count()
    for i = 0, size - 1 do
        elem = aDisplay:get_element_at(i)
        if scene == nil or scene:isParentOf(elem) then
            elemName = elem:getName()
            if string.sub(elemName, 0, aPrefix:len()) == aPrefix then
                aFunc(elem, parent_name)
            end
        end
    end
    
end

----------------------------------------------------------------------------------------------------
function Closeup:show()

    Closeup.active = true

    if self.mIsVisible then
        return
    end

    self.mDisplay:set_visible(true)
    self.mDisplay:set_active(true)
    self.mDisplay:get_element('cu_parent'):setVisible(true)
    self.mDisplay:get_element('cu_parent'):setAlpha(0.0)
    local step = 0
    timer_mgr:add_timer(
        function()
            step = step + 1
            self.mDisplay:get_element('cu_parent'):setAlpha(step / 15)
        end, 0.003, 15)

    table.foreach(self.mAmbients,
        function(name)
            play_clip(self.mAmbients[name].elem:getCharacter(), {looped = true})
        end)

    self.mDisplay:set_element_eh('close_cu',
            function()
                self:hide()
            end)
--[[
    self.mDisplay:set_element_eh('back_click', function() end)
    self.mDisplay:set_element_eh('back_click', function() end, {eh_type = eiRollOver})
    self.mDisplay:set_element_eh('back_click', function() end, {eh_type = eiRollOut})
]]
    self.mIsVisible = true
end

----------------------------------------------------------------------------------------------------
function Closeup:hide()

    Closeup.active = false

    if not self.mIsVisible then
        return
    end

    local step = 0
    timer_mgr:add_timer(
        function()
            step = step + 1
            self.mDisplay:get_element('cu_parent'):setAlpha(1 - step / 15)
            if step == 15 then
                self.mDisplay:get_element('cu_parent'):setVisible(false)
                self.mDisplay:set_visible(false)
            end
        end, 0.003, 15)

    self.mIsVisible = false
    
    if self.mCloseCB ~= nil then
        self.mCloseCB()
    end
end

----------------------------------------------------------------------------------------------------
function Closeup:getHidList(aIsBase)
    if aIsBase == nil then
        aIsBase = false
    end

    local tList = {}
    for name, state in pairs(self.mHids) do
        if aIsBase then
            local x = string.gsub(name, "hid_"..self.mScene.."_", "")
            table.insert(tList, x)
        else
            table.insert(tList, name)
        end
    end
    return tList
end

----------------------------------------------------------------------------------------------------
function Closeup:getHidCount()
    local tHidCount = 0
    table.foreach(self.mHids,
        function(name)
            if self.mHids[name] then
                tHidCount = tHidCount + 1
            end
        end)

    return tHidCount
end

----------------------------------------------------------------------------------------------------
function Closeup:isVisible()
    return self.mIsVisible
end

----------------------------------------------------------------------------------------------------
function Closeup:setFound(full_name)
    self.mHids[full_name] = false
end

----------------------------------------------------------------------------------------------------
function Closeup:setAllFound()
    table.foreach(self.mHids,
        function(name)
            self.mHids[name] = false
        end)
end
----------------------------------------------------------------------------------------------------
function Closeup:setNotFound(full_name)
    self.mHids[full_name] = true
end
