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

    var defineTests = function () {

        module("SCV55|SCV1392, SCV1393- Overbooking and booking of unavailable slot by user with SDOB key and SDMOB key");
        test('SCV1424 - Validate Overbooking and booking of unavailable slot by user with SDOB key and SDMOB key',
            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('clerk6767');
                            $("#password").val('clerk7676$');
                            $('#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();
                        }, 2000);
                    },
                    function () {
                        afterElementLoad($("#calendar-specialty-input"), function () {
                            ok($('.ui-title').is(':visible'),
                                "Header is visible");
                        });

                        //***** Has SDOB Key *****//
                        // show calendar
                        $("#calendar-specialty-input").val('Medicine')
                            .keyup();
                        $("#calendar-specialty li:nth-child(6) a")
                            .trigger('tap');

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

                        // Select Clinic
                        $("#calendar-clinic-menu label").eq(3)
                            .trigger('click');

                        // Click View Availability to display the
                        // calendar
                        $("#appointment-button a").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(6).trigger('click');
                        //Validate that there is a slot that can be overbooked
                        equal($("#calendar").find('button').eq(18).hasClass('transparent'),
                            false, 'Can click on the slot to overbook');
                        equal($("#calendar .fc-view-agendaDay .fc-event:not(.patient-appt):not(.transparent)").eq(12).attr('data-status'), 'A', '1 overbook');

                        //Click to validate that the book appointment dialog is displayed
                        //Search and select patient
                        var e = $.Event("keypress");
                        e.which = 13; // # Some key
                        // code value
                        e.keyCode = 13;
                        $("#patient-search-input").val(
                            '666000001')
                            .trigger(e);
                        $('#patient-search-results li:nth-child(2) a')
                            .trigger('tap');

                        $("#calendar .fc-view-agendaDay .fc-event:not(.patient-appt):not(.transparent)").eq(12).trigger('click');
                        //Book Appointment dialog is displayed
                        ok($("#book-appointment h3").text(), 'Book Appointment', 'Book appointment dialog');
                        //Click Cancel Appt button to return back to the calendar (day view)
                        $("#cancel-btn").trigger('click');

                        //Validate user does has SDMOB key and can superbook the slot
                        equal($("#calendar").find('button').eq(22).hasClass('transparent'),
                            false, 'Can click on the slot to superbook');
                        equal($("#calendar").find('button').eq(22).attr('data-status'), 'No Availability', 'Can super book');

                        //Click on the slot and validate that the book appointment dialog is displayed
                        $("#calendar .fc-view-agendaDay .fc-event:not(.patient-appt):not(.transparent)").eq(20).trigger('click');
                        ok($("#book-appointment h3").text(), 'Book Appointment', 'Book appointment dialog');
                        //Click Cancel Appt button to return back to the calendar (day view)
                        $("#cancel-btn").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
    };
});
