-- Cutscene

Cutscene.root = nil
Cutscene.scene = 0
Cutscene.frame = 0

----------------------------------------------------------------------------------------------------
function Cutscene_INIT()
    Cutscene.root = Cutscene:get_element('cs_root')
    Cutscene.root:setVisible(false)

    Cutscene:get_element('btn_skip_view'):setActive(false)
    Cutscene:get_element('background'):setActive(false)
    Cutscene:set_element_eh('btn_skip_view', function() Cutscene:next() end)
    Cutscene:set_element_eh('background', function() trace('click') end)
    Cutscene:get_element('btn_skip_view'):setActive(false)

    Cutscene.pages = {Cutscene:get_element('comix_frames'), Cutscene:get_element('comix_frames_next')}
    Cutscene.frames = {Cutscene:get_element('frame'), Cutscene:get_element('frame_next')}

    Cutscene.blurFrames = {{Cutscene:get_element('frame_03'), Cutscene:get_element('frame_02'), Cutscene:get_element('frame_01')},
            {Cutscene:get_element('frame_next_03'), Cutscene:get_element('frame_next_02'), Cutscene:get_element('frame_next_01')}}

    Cutscene.currentPage = 1

    Cutscene:set_visible(false)
    Cutscene:get_parent():setActive(false)

    Cutscene:set_element_eh('btn_skip_view', function() Cutscene:play_clip('btn_skip_view', {frame_begin = 1, frame_end = 1}) end, {eh_type = eiRollOver})
    Cutscene:set_element_eh('btn_skip_view', function() Cutscene:play_clip('btn_skip_view', {frame_begin = 0, frame_end = 0}) end, {eh_type = eiRollOut})

    Cutscene.textPages = {}
    for i = 1, 100 do
        for j = 1, 100 do
            local elem = Cutscene:get_element('texts_page_'..i..'_'..j)
            if elem ~= nil then
                if Cutscene.textPages[i] == nil then
                    Cutscene.textPages[i] = {}
                end
                Cutscene.textPages[i][j] = {root = elem, fields = {}}
                for k = 1, 100 do
                    local field = elem:findChild('com_'..i..'_'..j..'_'..k)
                    if field ~= nil then
                        table.insert(Cutscene.textPages[i][j].fields, field)
                    end
                end
            end
        end
    end

    Cutscene:hideAllTexts()
end

----------------------------------------------------------------------------------------------------
function Cutscene:hideAllTexts()
	--for i = 1, #Cutscene.textPages do
	--    for j = 1, #Cutscene.textPages[i] do
	for i = 1, 100 do
        if Cutscene.textPages[i] ~= nil then
            for j = 1, 100 do
                if Cutscene.textPages[i][j] ~= nil then
                    Cutscene.textPages[i][j].root:setVisible(false)
                end
            end
        end
	end
end

----------------------------------------------------------------------------------------------------
function Cutscene:showText(scene, frame)
    if Cutscene.textPages[scene][frame] == nil then
        return
    end

    Cutscene.textPages[scene][frame].root:setVisible(true)
    for i = 1, #Cutscene.textPages[scene][frame].fields do
        local field = Cutscene.textPages[scene][frame].fields[i]
        field:setAlpha(0.0)
        field:updateTextColor()

        local frames = 5
        local current = 0
        timer_mgr:add_timer(
            function()
                current = current + 1
                field:setAlpha(current / frames)
                field:updateTextColor()
            end,
            0.03, frames)
    end
end

----------------------------------------------------------------------------------------------------
function Cutscene_UPDATE()
	return 0
end

----------------------------------------------------------------------------------------------------
function Cutscene:isVisible()
	Cutscene.root:isVisible()
end

----------------------------------------------------------------------------------------------------
function Cutscene:onShow()
    Cutscene:hideAllTexts()
end

----------------------------------------------------------------------------------------------------
function Cutscene:onHide()
end

----------------------------------------------------------------------------------------------------
function Cutscene:isVisible()
    return Cutscene.root:isVisible()
end

function Cutscene:getSecondPage()
    if Cutscene.currentPage == 1 then
        return 2
    end

    return 1
end

----------------------------------------------------------------------------------------------------
function Cutscene:show(aCutsceneIdx, cb)
    if Cutscene.root:isVisible() then
        return
    end

    Cutscene.cb = cb
    --Media:stopBackground( 'level'..HOLevel.current.mLevel )
    --Media:playBackground('Cutscene')

    Cutscene:set_visible(true)
    Cutscene:get_parent():setActive(true)
    Cutscene.root:setVisible(true)

    Cutscene.scene = aCutsceneIdx
    Cutscene.frame = 1

    Cutscene:play_clip('glow_lines', {label_begin = 'begin', label_end = 'end'})
    Cutscene:get_element('btn_skip_view'):setActive(false)

    Cutscene.currentPage = 1
    local page = Cutscene.pages[Cutscene.currentPage]
    local frames = Cutscene.frames[Cutscene.currentPage]
    
    set_frame_listener(page, 'stop', function() Cutscene:onFrameStop() end)
    Cutscene:play_clip(page:getName(), {label_begin = 'begin', label_end = 'stop'})
    play_clip(frames:getCharacter(), {label_begin = aCutsceneIdx..'-1', label_end = aCutsceneIdx..'-1'})

    for i = 1, #Cutscene.blurFrames[1] do
        play_clip(Cutscene.blurFrames[1][i]:getCharacter(), {label_begin = aCutsceneIdx..'-1', label_end = aCutsceneIdx..'-1'})
    end

    Cutscene:play_clip(Cutscene.pages[2]:getName(), {label_begin = 'begin', label_end = 'begin'})
    
    Media:switchBackgroundSet('Cutscene')
--[[Cutscene:get_element('background'):setAlpha(0.0)
    Cutscene:get_element('background'):setActive(false)
    Cutscene:get_element('btn_skip_view'):setActive(false)
    Cutscene:get_element('btn_skip_view'):setAlpha(0)
    local step = 0
    HOLevel.locked = true
    timer_mgr:add_timer(
        function()
            step = step + 1
            Cutscene:get_element('background'):setAlpha(0.8 * step / 30)
            Cutscene:get_element('btn_skip_view'):setAlpha(step / 30)
            if step == 30 then
                Cutscene:get_element('btn_skip_view'):setActive(true)
                Cutscene:get_element('background'):setActive(true)
                Cutscene:play_clip('cs'..aCutsceneIdx, {looped=false})
                Cutscene.current = Cutscene:get_element('cs'..aCutsceneIdx)
            end
        end, 0.01, 30)]]
end

----------------------------------------------------------------------------------------------------
function Cutscene:onFrameStop()
    Cutscene:get_element('btn_skip_view'):setActive(true)
    Cutscene:set_element_eh('btn_skip_view', function() Cutscene:next() end)

    Cutscene:showText(Cutscene.scene, Cutscene.frame)
end

----------------------------------------------------------------------------------------------------
function Cutscene:switchFrame()
    Cutscene:hideAllTexts()
    Cutscene.frame = Cutscene.frame + 1
    Cutscene.currentPage = Cutscene:getSecondPage()
    local page = Cutscene.pages[Cutscene.currentPage]
    local ch = Cutscene.frames[Cutscene.currentPage]:getCharacter()

    local frame = ch:get_labeled_frame(Cutscene.scene..'-'..Cutscene.frame)
    if frame ~= -1 then
        set_frame_listener(page, 'stop', function() Cutscene:onFrameStop() end)
        Cutscene:play_clip(page:getName(), {label_begin = 'begin', label_end = 'stop'})

        play_clip(ch, {frame_begin = frame, frame_end = frame})
        for i = 1, #Cutscene.blurFrames[Cutscene.currentPage] do
            play_clip(Cutscene.blurFrames[Cutscene.currentPage][i]:getCharacter(),
                {frame_begin = frame, frame_end = frame})
        end

    else
        Cutscene:hide()
    end
end

----------------------------------------------------------------------------------------------------
function Cutscene:next()
    local page = Cutscene.pages[Cutscene.currentPage]
    set_frame_listener(page, 'next', function() Cutscene:switchFrame() end)
    Cutscene:play_clip(page:getName(), {label_begin = 'stop', label_end = 'end'})

    local ch = Cutscene.frames[Cutscene.currentPage]:getCharacter()
    local frame = ch:get_labeled_frame(Cutscene.scene..'-'..Cutscene.frame)
    play_clip(ch, {frame_begin = frame, frame_end = frame})
    for i = 1, #Cutscene.blurFrames[Cutscene.currentPage] do
        play_clip(Cutscene.blurFrames[Cutscene.currentPage][i]:getCharacter(),
            {frame_begin = frame, frame_end = frame})
    end
end

----------------------------------------------------------------------------------------------------
function Cutscene:hide()

    --Media:stopBackground('Cutscene')
    
    if not Cutscene.root:isVisible() then
        return
    end

    Cutscene.root:setVisible(false)
    Cutscene:set_visible(false)
    Cutscene:get_parent():setActive(false)

    if Cutscene.cb ~= nil then
        Cutscene.cb()
    end
--[[local step = 0
    Cutscene:get_element('btn_skip_view'):setActive(false)
    timer_mgr:add_timer(
        function()
            step = step + 1
            Cutscene:get_element('background'):setAlpha(0.8 * (1.0 - step / 30))
            Cutscene.current:setAlpha(1.0 - step / 30)
            Cutscene:get_element('btn_skip_view'):setAlpha(1.0 - step / 30)
            if step == 30 then
                Cutscene:get_element('background'):setActive(false)
                Cutscene.root:setVisible(false)
                Cutscene.current:setAlpha(1.0)
                HOLevel.locked = false
            end
        end, 0.003, 30)]]
end
