dofile('data/Scripts/comics/ComicsHelpers.lua')
dofile('data/Scripts/comics/ComicsFrame.lua')

class 'Comics'

function Comics:__init(name, ...)
    self.frameIndex = -1
    self.frames     = {}
    
    self.name                 = name
    self.visible              = false
    self.skipped              = false
    self.skipDelay            = ZOOM_ZONE_APPEARING_TIME
    self.prevPanelMode        = nil
    
    self.clickableScene       = true    -- если true, то можно кликнуть где-угодно для пропуска фразы, если false - то только по панели
    self.ctcCustomTime        = 1     -- задержка появления возможности прокликать фразу. Если значение = nil, то устанавливается значение, равное длине звука липсинга.
    
    self.changeFrameCondition = nil
    self.on_finish            = nil
    
    self.ctcObject = GetCurrentLevel():GetPanelLuaObject().comicsCtcText
    self.ctcAction = nil
    
    do
        local localFrames = {...}
        
        for _, frame in ipairs(localFrames) do
            self:AddFrame(frame)
        end
    end
end
-------------------------------------------------------------------------------

function Comics:AddFrame(frame)
    table.insert(self.frames, frame)
    frame.owner = self
end
-------------------------------------------------------------------------------

function Comics:GetFrame(caption)
    for _, frame in ipairs(self.frames) do
        if frame.caption == caption then
            return frame
        end
    end
    
    return nil
end
-----------------------------------------------------------------------------------

function Comics:GetCurrentFrame()
    return self.frames[self.frameIndex]
end
-----------------------------------------------------------------------------------

function Comics:SetInstantlyFrames(...)
    local captions = {...}
    
    for _, caption in ipairs(captions) do
        self:GetFrame(caption).defaultShowContentTime = 0
    end
end
-----------------------------------------------------------------------------------

function Comics:SetContinuedFrames(...)
    local captions = {...}
    
    for _, caption in ipairs(captions) do
        self:GetFrame(caption):SetAutoContinue(true)
    end
end
-----------------------------------------------------------------------------------

function Comics:ShowNextFrame()
    if self.visible then
        if self.frameIndex < #self.frames then
            local prevFrame = self:GetCurrentFrame()
            self.frameIndex = self.frameIndex + 1
            local nextFrame = self:GetCurrentFrame()
        
            prevFrame:SetVisible(false, nextFrame.objects, nextFrame.defaultShowContentTime)
            nextFrame:SetVisible(true)
        else
            self:Finish()
        end
    end
end
-----------------------------------------------------------------------------------

function Comics:SetChangeFrameCondition(condition)
    self.changeFrameCondition = condition
end
-----------------------------------------------------------------------------------

function Comics:IsChangingFrame()
    return self:GetCurrentFrame().changing
end
-----------------------------------------------------------------------------------

function Comics:Start()
    assert(#self.frames > 0)
    
    if not self.visible then
        local level = GetCurrentLevel()
        local panel = level:GetPanelLuaObject()
        local inv   = level:GetInventoryLuaObject()
        
        HideBlackBar()
        
        if inv:GetTool() then
            inv:DropTool(0)
        end
        
        self.frameIndex = 1
        self.visible    = true
        
        self:GetCurrentFrame():SetVisible(true)
        
        level.comics = self
        level.locker:AddRef()
        
        self.prevPanelMode = panel:GetMode()
        panel:SetMode(EPanelMode.PM_COMICS)
        
        level.timer:Add(function()
                            if self:IsVisible() then
                                panel:SetSkipComicsButtonVisible(true)
                            end
                        end,
                        self.skipDelay)
    end
end
-----------------------------------------------------------------------------------

function Comics:Finish(instantly)
    if self.visible then
        local level = GetCurrentLevel()
        local panel = level:GetPanelLuaObject()
        
        if instantly then
            self:GetCurrentFrame():SetVisible(false, nil, 0, 0)
        else
            self:GetCurrentFrame():SetVisible(false, nil, self.frames[1].defaultShowContentTime)
        end
        
        level.locker:Release()
        level.prev_comics = self
        level.comics      = nil
        
        panel:SetSkipComicsButtonVisible(false)
        panel:SetMode(self.prevPanelMode)
        
        self.prevPanelMode = nil
        self.visible       = false
        
        AddUniqueGameState(COMICS_SHOWN, self.name)
        
        if self.on_finish then
            self.on_finish(self)
        end
    end
end
-----------------------------------------------------------------------------------

function Comics:Skip(instantly)
    self.skipped = true
    self:Finish(instantly)
end
-------------------------------------------------------------------------------

function Comics:IsVisible()
    return self.visible
end
-------------------------------------------------------------------------------

function Comics:IsSkipped()
    return self.skipped
end
-------------------------------------------------------------------------------

function Comics:SetClickableScene(clickableScene)
    self.clickableScene = clickableScene
end
-------------------------------------------------------------------------------

function Comics:IsClickableScene()
    return self.clickableScene
end
-------------------------------------------------------------------------------

function Comics:SetCtcVisible(visible, time)
    if self.ctcAction then
        self.ctcAction:Kill()
        self.ctcAction = nil
    end
    
    self.ctcObject:SetVisible(visible)
    
    if time then
        local level = GetCurrentLevel()
        self.ctcAction = level.timer:Add(function()
                                             self.ctcObject:SetVisible(not visible)
                                             self.ctcAction = nil
                                         end, time)
    end
end
-------------------------------------------------------------------------------

function Comics:IsCtcVisible()
    return self.ctcObject:IsVisible()
end
-------------------------------------------------------------------------------

function Comics:SetCtcCustomTime(ctcCustomTime)
    self.ctcCustomTime = ctcCustomTime
end
-------------------------------------------------------------------------------

function Comics:GetCtcCustomTime()
    return self.ctcCustomTime
end
-------------------------------------------------------------------------------

function Comics:IsSoundPlaying()
    local frame = self:GetCurrentFrame()
    
    return frame and frame.sound and frame.sound:IsPlaying()
end
-------------------------------------------------------------------------------

function Comics:SetZ(z_coord)
	for _, frame in ipairs(self.frames) do
		frame:GetAnchor():SetZ(z_coord)
	end
end
-------------------------------------------------------------------------------