
local LOOP_DELAY = 0.6

local BUTTON_LOCATION = ccp(355, 27)
local FLASH_OFFSET_CONTROLLER = ccp(3, 22)
local FLASH_OFFSET_KEYBOARD = ccp(0, 33)

function createTapActionLoopStartIn()
    local actionLoop = CCArray:create()
    actionLoop:addObject(CCFadeTo:create(0.0, 255))
    actionLoop:addObject(CCDelayTime:create(LOOP_DELAY))
    actionLoop:addObject(CCFadeTo:create(0.0, 0))
    actionLoop:addObject(CCDelayTime:create(LOOP_DELAY))
    
    return actionLoop
end

function createTapActionLoopStartOut()
    local actionLoop = CCArray:create()
    actionLoop:addObject(CCDelayTime:create(LOOP_DELAY))
    actionLoop:addObject(CCFadeTo:create(0.0, 255))
    actionLoop:addObject(CCDelayTime:create(LOOP_DELAY))
    actionLoop:addObject(CCFadeTo:create(0.0, 0))
    
    return actionLoop
end

function engageControllerTutorial(tutorialStepNode, buttonIdlePath, buttonEngagedPath)
    
    engageButtonTutorial(tutorialStepNode, buttonIdlePath, buttonEngagedPath, FLASH_OFFSET_CONTROLLER)
end

function engageKeyboardTutorial(tutorialStepNode, buttonIdlePath, buttonEngagedPath)
    
    engageButtonTutorial(tutorialStepNode, buttonIdlePath, buttonEngagedPath, FLASH_OFFSET_KEYBOARD)
end

function engageButtonTutorial(tutorialStepNode, buttonIdlePath, buttonEngagedPath, flashOffset)
    
    local tapFlash = CCSprite:create("Images/UI/HUD/button_tap_flash.png")
    tapFlash:setScale(0.3)
    tapFlash:setOpacity(0)
    tapFlash:setPosition(ccpAdd(BUTTON_LOCATION, flashOffset))
    tutorialStepNode:addChild(tapFlash)
    
    local buttonIdle = CCSprite:create(buttonIdlePath)
    buttonIdle:setScale(0.4)
    buttonIdle:setOpacity(0)
    buttonIdle:setPosition(BUTTON_LOCATION)
    tutorialStepNode:addChild(buttonIdle)
    
    local buttonEngaged = CCSprite:create(buttonEngagedPath)
    buttonEngaged:setScale(0.4)
    buttonEngaged:setOpacity(0)
    buttonEngaged:setPosition(BUTTON_LOCATION)
    tutorialStepNode:addChild(buttonEngaged)
    
    actionLoopTF = createTapActionLoopStartOut()
    actionLoopJC = createTapActionLoopStartIn()
    actionLoopJT = createTapActionLoopStartOut()
    
    local actionTF = CCRepeatForever:create(CCSequence:create(actionLoopTF))
    tapFlash:runAction(actionTF)
    
    local actionJC = CCRepeatForever:create(CCSequence:create(actionLoopJC))
    buttonIdle:runAction(actionJC)
    
    local actionJT = CCRepeatForever:create(CCSequence:create(actionLoopJT))
    buttonEngaged:runAction(actionJT)
end

function appendTutorialKeyInputType(tutorialKey, inputType)

    if inputType == InputType_Touchscreen then
        return tutorialKey .. "_Touch"
    elseif inputType == InputType_Keyboard then
        return tutorialKey .. "_Keyboard"
    else
        return tutorialKey .. "_Controller"
    end
end

function getInputTypeString(inputType)

    if inputType == InputType_Keyboard then
        return "kb_"
    elseif inputType == InputType_FireTVController then
        return "firetv_"
    elseif inputType == InputType_OuyaController then
        return "ouya_"
    else
        return "ios_"
    end
end

function getButtonSpritePathIdle(inputType, buttonCode)
    
    pathStart = "Images/UI/HUD/button_"
    pathEnd = "_idle.png"
    
    return pathStart .. getInputTypeString(inputType) .. buttonCode .. pathEnd
end

function getButtonSpritePathEngaged(inputType, buttonCode)

    pathStart = "Images/UI/HUD/button_"
    pathEnd = "_engaged.png"
    
    return pathStart .. getInputTypeString(inputType) .. buttonCode .. pathEnd
end
