var TestPreliminary = function(name) {
    this._initialize(name);
    this.superclass = BaseTest;

    this.watchdogTimeout = 0;
    this.completed = false;
    this.properties = new TestParameters()
}

extend(TestPreliminary.prototype, new BaseTest(), {
    start: function() {
        //
        // collect browser deets
        //
        BrowserDetect.init();

        var browser = BrowserDetect.browser;
        var browserVersion = BrowserDetect.version
        var browserString = navigator.userAgent || navigator.vendor || navigator.opera;

        this.properties.merge({browser: browser, browserVersion: browserVersion, browserString: browserString, viewer: (this.useUnity()) ? "unity" : "java" });

        // remove the java detect code from perlimiary test to see if it causes bounces
        this.notifySuccess(null, this.properties)

        /*
        //
        // start PluginDetect Stuff
        //
        this.watchdogTimeout = setTimeout(this.watchdog.bind(this), 15000);
        PluginDetect.onDetectionDone('Java', this.javaDetectCompleted.bind(this), null)
        */
    },

    javaDetectCompleted: function(detector) {
        this.completed = true;

        if(this.watchdogTimeout)
            clearTimeout(this.watchdogTimeout)

        var javaVersion = getJavaVersionSafe();

        this.properties.merge({preliminaryJavaVersion: javaVersion});
        this.setDetectedJavaVersion(javaVersion);

        this.testPreviousFailuresSafe();
        
        this.notifySuccess(null, this.properties)
    },

    watchdog: function() {
        if(! this.completed) {
            this.completed = true

            this.notifyWarning("PRETEST_FAILED", null, this.properties)
            this.notifySuccess(null, this.properties);
        }
    },

    testPreviousFailuresSafe : function() {
        try {
            this.testPreviousFailures();
        } catch(e) {

        }
    },

    /**
     * Looks for a verification_history cookie, and tries to see if we can do something useful with it for the user.
     *
     * for example, if we detected last time
     */
    testPreviousFailures : function() {
        var pastHistory = this.context.getLastVerificationHistory();

        var last_test_name = pastHistory[TestContext.LAST_TEST_KEY];
        var last_success = pastHistory[TestContext.LAST_SUCCESS_KEY];
        var last_code = pastHistory[TestContext.LAST_CODE_KEY];
        
        if(typeof last_test_name == "string") {
            // we found verification history, merge it into current properties set
            this.properties.merge({lastTest: last_test_name, lastSuccess: last_success, lastCode: last_code});

            if(! this.useUnity() ) {
                // only stop the preliminary test for non-unity viewer
                
                if(last_success == null) {
                    // last test didn't complete
                    this.notifyWarning("PREVIOUS_TEST_" + last_test_name + "_INCOMPLETE", null, this.properties);
                } else if(last_success == false) {
                    // last test specifically failed
                    this.notifyWarning("PREVIOUS_TEST_" + last_test_name + "_FAILED", null, this.properties);
                }
            }
        }

    },

    notifyWarning: function(code, msg, params) {
        var newParams = {};
        newParams["WARNING_" + code] = code;

        params.merge(newParams);

        // call super notifyWarning method
        this.superclass.prototype.notifyWarning.apply(this, arguments);
    },

    useUnity : function() {
        return Mixamo.AbFacade.use_unity;
    }
});
