namespace rooibos
    ' @ignore
    function init(testSceneName = invalid as string) as void
        if createObject("roAPPInfo").IsDev() <> true then
            rooibos.common.logError(" not running in dev mode! - rooibos tests only support side-loaded builds - aborting")
            return
        end if

        screen = CreateObject("roSGScreen")
        m.port = CreateObject("roMessagePort")
        screen.setMessagePort(m.port)
        if testSceneName = invalid or testSceneName = "" then
            testSceneName = "RooibosScene"
        end if

        rooibos.common.logInfo(`Starting test using test scene with name RooibosScene ${testSceneName}`)
        scene = screen.CreateScene(testSceneName)
        scene.id = "ROOT"
        screen.show()

        m.global = screen.getGlobalNode()
        m.global.addFields({
            "testsScene": scene
            "_rbs_ccn": createObject("roSGNode", "CodeCoverage")
        })

        if scene.hasField("isReadyToStartTests") and scene.isReadyToStartTests = false then
            rooibos.common.logInfo("The scene is not ready yet - waiting for it to set isReadyToStartTests to true")
            scene.observeField("isReadyToStartTests", m.port)

        else
            rooibos.common.logInfo("scene is ready; running tests now")
            print ""
            runner = new rooibos.TestRunner(scene, m)
            runner.Run()

            if runner.config.keepAppOpen = false then
                rooibos.common.logInfo("keepAppOpen is false; exiting Rooibos")
                ' End statement will also exit the caller of this function
                ' leading to an instant exit of the application

                ' Give the io port time to finish sending all the logs
                sleep(400)
                end
            end if
        end if

        while true
            msg = wait(0, m.port)
            msgType = type(msg)
            if msgType = "roSGScreenEvent" then
                if msg.isScreenClosed() then
                    return
                end if
            else if msgType = "roSGNodeEvent" then

                if msg.getField() = "isReadyToStartTests" and msg.getData() = true then
                    rooibos.common.logInfo("scene is ready; running tests now")
                    print ""
                    runner = new rooibos.TestRunner(scene, m)
                    runner.Run()
                end if
            end if
        end while
    end function

    ' @ignore
    function versionCompare(v1 as string, v2 as string) as integer
        v1parts = v1.split(".")
        v2parts = v2.split(".")

        while v1parts.count() < v2parts.count()
            v1parts.push("0")
        end while

        while v2parts.count() < v1parts.count()
            v2parts.push("0")
        end while

        for i = 0 to v1parts.count() - 1
            if v2parts.count() = i then
                return 1
            end if

            if v1parts[i] <> v2parts[i] then
                if v1parts[i] > v2parts[i] then
                    return 1
                else
                    return -1
                end if
            end if
        end for

        if v1parts.count() <> v2parts.count() then
            return -1
        end if

        return 0

    end function
end namespace
