###
Neft object
###
@_neft = {}

###
Global object
###
@global = @

###
Console
###
do =>
    getMessage = (type, args) ->
        "[#{type}] " + Array::join.call args, ' '

    console.log = do (_super = console.log) -> ->
        ios.log getMessage('LOG', arguments)
        _super arguments...
    console.info = do (_super = console.info) -> ->
        ios.log getMessage('INFO', arguments)
        _super arguments...
    console.warn = do (_super = console.warn) -> ->
        ios.log getMessage('WARN', arguments)
        _super arguments...
    console.error = do (_super = console.error) -> ->
        ios.log getMessage('ERROR', arguments)
        _super arguments...

###
Timers
###
setTimeout = clearTimeout = setImmediate = null
do ->
    shots = Object.create null

    ios.timerCallback = (id) ->
        if timer = shots[id]
            delete shots[id]
            timer()
        return

    setTimeout = (func, delay, arg1, arg2, arg3) ->
        if typeof func isnt 'function'
            throw new TypeError "'callback' argument must be a function"

        if typeof delay isnt 'number'
            delay = parseInt delay
        if isNaN(delay) or delay < 4
            delay = 4

        argc = arguments.length
        switch argc
            when 1, 2
                callFunc = func
            when 3
                callFunc = ->
                    func arg1
            when 4
                callFunc = ->
                    func arg1, arg2
            when 5
                callFunc = ->
                    func arg1, arg2, arg3
            else
                args = new Array argc - 2
                for i in [2...argc] by 1
                    args[i - 2] = arguments[i]
                callFunc = ->
                    func.apply null, args

        id = ios.timerShot delay
        shots[id] = -> Neft.tryCatch.tryCall callFunc
        id

    setImmediate = (func, arg1, arg2, arg3) ->
        if typeof func isnt 'function'
            throw new TypeError "'callback' argument must be a function"

        argc = arguments.length
        switch argc
            when 1
                callFunc = func
            when 2
                callFunc = ->
                    func arg1
            when 3
                callFunc = ->
                    func arg1, arg2
            when 4
                callFunc = ->
                    func arg1, arg2, arg3
            else
                args = new Array argc - 1
                for i in [1...argc] by 1
                    args[i - 1] = arguments[i]
                callFunc = ->
                    func.apply null, args

        ios.immediate -> Neft.tryCatch.tryCall callFunc

        -1

    clearTimeout = (id) ->
        delete shots[id]
        return

###
Animation frame
###
requestAnimationFrame = null
do ->
    queue = []

    ios.animationFrameCallback = ->
        for i in [0...queue.length]
            Neft.tryCatch.tryCall queue.shift()
        return

    requestAnimationFrame = (func) ->
        if typeof func isnt 'function'
            throw new TypeError "'callback' argument must be a function"

        queue.push func
        return

###
Neft code
###
`{{&neftCode}}`
@Neft = Neft

###
App code
###
`{{&appCode}}`
