define([
    'jQuery',
    'asyncSeries',
    'testUtilities'
], function ($$, asyncSeries, testUtilities) {

    var defineTests = function () {

        module("SCV91|SCV1383- Display message when user with no access to restricted clinic tries to book appointment");
        test('SCV1384 - Validate the message that is displayed when user with no access to restricted clinic tries to book appointment',
            function () {
                myFrame = $$("#myFrame");
                var host = window.location.protocol + "//" + window.location.host;
                var referencePath = host + window.location.pathname;

                stop(4);
                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('clerk0987');
                            $("#password").val('clerk7890$');
                            $('#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 () {
                            ok($('#left-navigation:contains("Open Menu")').text(),
                                "Then I can see the header bar");
                            next();
                        }, 2500);
                    },
                    function () {
                        afterElementLoad($("#calendar-specialty-input"), function () {
                            ok($('.ui-title').is(':visible'),
                                "Header is visible");
                        });

                        //Search and select patient
                        var e = $.Event("keypress");
                        e.which = 13; // # Some key
                        // code value
                        e.keyCode = 13;
                        $("#patient-search-input")
                            .val('666000004')
                            .trigger(e);
                        $('#patient-search-results li:nth-child(2) a')
                            .trigger('tap');

                        // Select Service/Specialty and Clinic to
                        // show calendar
                        $("#calendar-specialty-input").val('Psychiatry')
                            .keyup();
                        $("#calendar-specialty li:nth-child(9) a")
                            .trigger('tap');

                        $('#calendar-clinic-button').trigger('click');

                        // Select Clinic
                        $("#calendar-clinic-menu label").eq(0)
                            .trigger('click');
                        $("#appointment-button").find('a').eq(0).trigger('tap');
                        afterElementLoad($("#calendar"), function () {
                            ok($("#calendar").is(':visible'),
                                'Calendar is visible');
                        });

                        ok($('td').hasClass('available'),
                            'Appointment slot available');

                        //Navigate to the next month
                        $('.fc-button-next').trigger('click');
                        //Click the available slot and validate the book appointment dialog
                        $("#calendar").find('.available').eq(2).trigger('click');
                        //Click on the timeslot and validate the message
                        $("#calendar .fc-view-agendaDay .fc-event:not(.patient-appt):not(.transparent)").eq(2).trigger('click');
                        afterElementLoad($("#message-for-user"), function () {
                            ok($("#message-for-user").is(':visible'),
                                'message is visible');
                        });
                        ok($("#message-for-user").find('h3').eq(0).find('span').eq(1).text(),
                            'Booking for this clinic is restricted.  If you need access, please contact your administrator.');
                        $("#popup-btn-0").trigger('click');

                        myFrame.attr('src', referencePath + '../../AuthorizationServices/logout')
                            .one('load', function () {

                            });

                        myFrame.attr('src', referencePath + '../../PatientViewerServices/oauthlogin?redirect_uri=' + referencePath + '../')
                            .one('load', function () {

                            });

                        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 () {

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

        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);
            }
        }
    };
    return {
        initialize: defineTests
    };
});
