Tutorial = {}

Tutorial.mChapters = {}

------------------------------------------------------------------------------------------
function Tutorial:init()

    Tutorial.mTips = table.copy(Profile:getUserData("Tutorial.tips"))
    Tutorial.mChapters = table.copy(Profile:getUserData("Tutorial.chapters"))
    
    if not Tutorial.mChapters then Tutorial.mChapters = {} end   
    if Tutorial.mTips then return end

    Tutorial.mTips =
    {
        ['map.scenes'] = {text = 'ids_tutorial_map',
                          x = 440, y = 280, w = 200, h = 120,
                          type = Bubble.TYPE_RIGHT_DOWN},

        ['ho.challenge'] = {text = 'ids_tutorial_challenge',
                            x = 840, y = 640, w = 220, h = 90,
                            type = Bubble.TYPE_UP_LEFT},

        ['ho.hids'] = {text = 'ids_tutorial_hids',
                       x = 295, y = 690, w = 400, h = 120,
                       type = Bubble.TYPE_UP_RIGHT},

        ['ho.hint'] = {text = 'ids_tutorial_hint',
                       x = 70, y = 700, w = 230, h = 80,
                       type = Bubble.TYPE_UP_RIGHT},

        ['ho.keys'] = {text = 'ids_tutorial_keys',
                       x = 720, y = 670, w = 220, h = 110,
                       type = Bubble.TYPE_UP_LEFT},

        ['ho.closeup'] = {text = 'ids_tutorial_closeup',
                          x = 350, y = 355, w = 450, h = 90,
                          type = Bubble.TYPE_RIGHT_DOWN},

        ['collapse.chips'] = {text = 'ids_collapse_hint1',
                              x = 246, y = 563, w = 200, h = 100,
                              type = Bubble.TYPE_LEFT_DOWN},

        ['collapse.pull'] = {text = 'ids_collapse_hint2',
                              x = 246, y = 659, w = 200, h = 100,
                              type = Bubble.TYPE_LEFT_UP},

        ['collapse.lines'] = {text = 'ids_collapse_hint3',
                              x = 210, y = 292, w = 230, h = 100,
                              type = Bubble.TYPE_RIGHT_UP},

        ['collapse.score'] = {text = 'ids_collapse_hint4',
                              x = 65, y = 240, w = 230, h = 80,
                              type = Bubble.TYPE_DOWN_RIGHT},

        ['match2.rules'] = {text = 'ids_match2_hint',
                          x = 500, y = 300, w = 200, h = 150,
                          type = Bubble.TYPE_NORMAL},

        ['ttwist.base'] = {text = 'ids_texttwist_hint1',
                          x = 210, y = 490, w = 300, h = 80,
                          type = Bubble.TYPE_UP_RIGHT},

        ['ttwist.word'] = {text = 'ids_texttwist_hint2',
                          x = 240, y = 360, w = 300, h = 80,
                          type = Bubble.TYPE_UP_RIGHT},

        ['ttwist.board'] = {text = 'ids_texttwist_hint3',
                          x = 280, y = 150, w = 400, h = 70,
                          type = Bubble.TYPE_NORMAL},

        ['ttwist.enter'] = {text = 'ids_texttwist_hint4',
                          x = 630, y = 650, w = 300, h = 75,
                          type = Bubble.TYPE_UP_RIGHT},

        ['ttwist.twist'] = {text = 'ids_texttwist_hint5',
                          x = 400, y = 650, w = 380, h = 75,
                          type = Bubble.TYPE_UP_LEFT},

        ['ttwist.bar'] = {text = 'ids_texttwist_hint6',
                          x = 100, y = 80, w = 400, h = 75,
                          type = Bubble.TYPE_DOWN_RIGHT}
    }

    Tutorial:addChapter('TextTwist')

    Tutorial:addChapter('Map')
    Tutorial:addTip('Map', 'map.scenes', 'Map.open', 'level.selected')

    Tutorial:addChapter('HO')
    Tutorial:addTip('HO', 'ho.hids', 'remark.closed', 'hid.found')
    Tutorial:addTip('HO', 'ho.challenge', 'ho.hids.end', 'challenge.complete')
    Tutorial:addTip('HO', 'ho.hint', 'ho.challenge.end', 'hint.pressed')
    Tutorial:addTip('HO', 'ho.keys', 'ho.hint.end', 'key.found')
    Tutorial:addTip('HO', 'ho.closeup', 'closeup.show', 'closeup.click')

    Tutorial:addChapter('Collapse')
    Tutorial:addTip('Collapse', 'collapse.chips', 'Collapse.open', 'collapse.destroyChips')
    Tutorial:addTip('Collapse', 'collapse.pull', 'collapse.chips.end', 'collapse.pull')
    Tutorial:addTip('Collapse', 'collapse.lines', 'collapse.pull.end')
    Tutorial:addTip('Collapse', 'collapse.score', 'collapse.lines.end', 'Collapse.close')

    Tutorial:addChapter('Match2')
    Tutorial:addTip('Match2', 'match2.rules', 'Match2.open', 'match2.match')

    Tutorial:addChapter('TextTwist')
    Tutorial:addTip('TextTwist', 'ttwist.base', 'TextTwist.open', 'ttwist.chipClick')
    Tutorial:addTip('TextTwist', 'ttwist.word', 'ttwist.base.end', 'ttwist.chipClick')
    Tutorial:addTip('TextTwist', 'ttwist.board', 'ttwist.word.end')
    Tutorial:addTip('TextTwist', 'ttwist.enter', 'ttwist.board.end', 'ttwist.enter')
    Tutorial:addTip('TextTwist', 'ttwist.twist', 'ttwist.enter.end', 'ttwist.twist')
    Tutorial:addTip('TextTwist', 'ttwist.bar', 'ttwist.twist.end', 'TextTwist.close')
    
    Profile:setUserData("Tutorial.tips", table.copy(Tutorial.mTips))
    Profile:setUserData("Tutorial.chapters", table.copy(Tutorial.mChapters))
end

------------------------------------------------------------------------------------------
function Tutorial:addChapter(chapter)
    Tutorial.mChapters[chapter] = {}
end

------------------------------------------------------------------------------------------
function Tutorial:addTip(chapter, tip, activateMsg, closeMsg)
    assert(Tutorial.mChapters[chapter] ~= nil)

    if Tutorial.mChapters[chapter].tips == nil then
       Tutorial.mChapters[chapter].tips = {}
    end

    Tutorial.mChapters[chapter].tips[activateMsg] = {tip = tip, close = closeMsg}
end

------------------------------------------------------------------------------------------
function Tutorial:enterChapter(chapterName)
    if Tutorial.currentChapter == nil then
        Tutorial:leaveChapter()
    end

    Tutorial.currentChapter = chapterName

    local chapter = Tutorial.mChapters[Tutorial.currentChapter]
    if chapter and chapter.currentMessage ~= nil then
        Tutorial:showTip(chapter.tips[chapter.currentMessage].tip)
    end

    Tutorial:sendMessage(chapterName..'.open')
end

------------------------------------------------------------------------------------------
function Tutorial:leaveChapter()
    if Tutorial.currentChapter ~= nil then
        Tutorial:sendMessage(Tutorial.currentChapter..'.close')
        Tutorial.currentChapter = nil
        Bubble:hide(false)
    end
end

------------------------------------------------------------------------------------------
function Tutorial:sendMessage(message)
    if Tutorial.currentChapter == nil then
        return
    end

    local chapter = Tutorial.mChapters[Tutorial.currentChapter]
    assert(chapter ~= nil)
    
    if chapter.tips == nil then
        return
    end

    if chapter.currentMessage ~= nil and chapter.tips[chapter.currentMessage].close == message then
        Tutorial.closeTip()
        return
    end

    if chapter.tips[message] == nil or chapter.currentMessage == message then
        return
    end

    Tutorial.closeTip()
    chapter.currentMessage = message
    Tutorial:showTip(chapter.tips[message].tip)
end

------------------------------------------------------------------------------------------
function Tutorial:closeTip()
    local chapter = Tutorial.mChapters[Tutorial.currentChapter]

    if chapter.currentMessage ~= nil then
        chapter.tips[chapter.currentMessage] = nil
        chapter.currentMessage = nil
        Bubble:hide(false)
        Profile:setUserData("Tutorial.tips", table.copy(Tutorial.mTips))
        Profile:setUserData("Tutorial.chapters", table.copy(Tutorial.mChapters))
    end
end

------------------------------------------------------------------------------------------
function Tutorial:showTip(tip)
    tipRec = Tutorial.mTips[tip]
    assert(tipRec ~= nil)
    local chapter = Tutorial.mChapters[Tutorial.currentChapter]
    local closeMsg = tip..'.end'

    Bubble:show(tipRec.text, tipRec.x, tipRec.y, tipRec.w, tipRec.h, tipRec.type, true,
        function() Tutorial:sendMessage(closeMsg) end)
end

------------------------------------------------------------------------------------------
function Tutorial:onBubbleClose()
    if not Tutorial.currentChapter then return end
    local chapter = Tutorial.mChapters[Tutorial.currentChapter]
    if chapter.currentMessage ~= nil then
        chapter.tips[chapter.currentMessage] = nil
        chapter.currentMessage = nil
        Profile:setUserData("Tutorial.tips", table.copy(Tutorial.mTips))
        Profile:setUserData("Tutorial.chapters", table.copy(Tutorial.mChapters))
    end
end
