define([
    'jQuery',
    'async',
    'common',
    'noSdobKey',
    'noSdmobKey',
    'hasSdobKeyNoSdmobKey',
    'hasSdobKeyHasSdmobKey',
    'restrictedClinic',
    'hasRestrictedClinic'
  
], function (jQuery, async, commonTests,
	   noSdobKey,
	   noSdmobKey,
	   hasSdobKeyNoSdmobKey,
	   hasSdobKeyHasSdmobKey,
	   restrictedClinic,
	   hasRestrictedClinic
		) {

    var $$ = jQuery.noConflict(true),
        myFrame = $$("#myFrame");

    var setup = function(){
		var host = window.location.protocol + "//" + window.location.host;
		var referencePath = host + window.location.pathname;

		defineTests();
		//require(['common']);
		require(['1511']);
		require(['1134']);
		require(['scv49']);
		require(['scv94']);
		//require(['scv70']); Test deleted as we removed the filter functionality from the Wait List page
		require(['scv47']);
		//require(['scv82']); //Wait List detail for patient
		//require(['scv51']); replaced by 1337
		require(['facilityNearRecall']); //Validates the Near and Recall entries and details for Facility
		require(['scv67']); //Validate future appointments list and details
		require(['patientNearRecall']); //Validates the Near and Recall entries and details for patient
		require(['bookAppt']); //Validate book appointment
		require(['scv727']); //Default view
		require(['cancelAppt']); //Cancel Appointment
		require(['scv948']);//Clear button in Clinic menu
		require(['scv86']);//VAAC requests displayed in SCV
		require(['vaacInScv']); //VAAC integration validation
		require(['scv74']); //Consults for service/specialty
		require(['scv1337']); //Consults for Patient
		//require(['scv55']); //Validate booking nomenclature on the calendar day view
		require(['scv222']); //Validate pagination

		async.series([
            function(next){
                $$(document).ready(function(){
                    next();
                });
            },
            function(next){
	     /*       var deleteTokenUrl = referencePath + '../../AuthorizationServices/rest/token';

	            if (sessionStorage.token && sessionStorage.token !== "null") {
		            $$.ajax({
			            url: deleteTokenUrl,
			            async: false,
			            type: "DELETE",
			            success: function () {
				            sessionStorage.clear();
				            myFrame.attr('src', referencePath + '../../AuthorizationServices/logout')
					            .one('load', function () {
						            next();
					            });
			            }
		            });
	            } else {*/
		            myFrame.attr('src', referencePath + '../../AuthorizationServices/logout')
			            .one('load', function () {
				            next();
			            });
	       //     }
            },
            function(next){
                myFrame.attr('src', referencePath + '../../PatientViewerServices/oauthlogin?redirect_uri=' + referencePath + '../')
                    .one('load', function(){
                        next();
                    });
            },
            function(next){
                afterJqueryLoad(function(){
                    //grab jQuery from inside the document
                    $ = jQuery = myFrame[0].contentWindow.jQuery;

                    $("#name-c").val('cprs1234');
                    $("#password").val('cprs4321$');
                    $('#facilityName').val('Test VAMC 3');
                    $('#autocomplete-container input').val('Test VAMC 3');
                    $('#facilityCode').val('777');

                    $("#logonForm").submit();
                    myFrame.one('load', function(){
                        afterJqueryLoad(function(){
                            afterOauthTokenInSessionStorage(function(){
                                next();
                            });
                        });
                    });
                });
            },
            function(next){
                setTimeout(function(){
                    if($('h4:contains("DISCLAIMER")').length === 1){
                        $('#accept-btn').trigger('tap');
                    }
                    next();

                }, 500);
            }
        ], function(){
            start();
        });

        QUnit.testStart(function(){
            stop();
            myFrame.off("load");
            myFrame.attr('src', referencePath + '../')
                .on("load", function(){
                    afterJqueryLoad(function(){
                        var referencePath = window.location.protocol + "//" + window.location.host + window.location.pathname;

                        //grab jQuery from inside the document
                        $ = jQuery = myFrame[0].contentWindow.jQuery;

                        //turn off async so tests will wait for ajax results
                        $.ajaxSetup({ async: false });

                        $.getScript(referencePath + "../libs/js/jquery.simulate.js");
						$.getScript(referencePath + "../libs/js/jquery.mockjax.js");

                        //turn off animations so they do not break tests
                        $.support.cssTransitions = false;
                        $.fx.off = true;

                        var started = false;
                        var iFrameDocument = myFrame[0].contentDocument ? myFrame[0].contentDocument : (myFrame[0].contentWindow.document || myFrame[0].document);

                        // workaround for iframe's load event not working properly
                        $(iFrameDocument).ready(function() {
                            setTimeout(function() {
                                if (!started) {
                                    started = true;
                                    QUnit.start();
                                }
                            }, 1000);
                        });

                        // if workaround fails (jquery ready fired before we could register handler), wait 5 seconds and run
                        setTimeout(function() {
                            if (!started) {
                                started = true;
                                QUnit.start();
                            }
                        }, 0000);
                    });
                });
        });

    };

    function afterJqueryLoad(callback){
        if(typeof myFrame[0].contentWindow.jQuery === 'undefined'){
            setTimeout(function(){
                afterJqueryLoad(callback);
            }, 50);
        }
        else{
            callback();
        }
    }

    function afterOauthTokenInSessionStorage(callback){
        var token = myFrame[0].contentWindow.sessionStorage.token;
        if(typeof token !== 'undefined' && token && token !== 'null'){
            callback();
        }
        else{
            setTimeout(function(){
                afterOauthTokenInSessionStorage(callback);
            }, 200);
        }
    }

    function defineTests(){
		/*commonTests.initialize();
	    noSdobKey.initialize();
	    noSdmobKey.initialize();
		hasSdobKeyNoSdmobKey.initialize();
		hasSdobKeyHasSdmobKey.initialize();
		hasRestrictedClinic.initialize();
	    restrictedClinic.initialize();*/
	}

	return {
		initialize: setup
	};
});
