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

        function patientSelect(name) {
            // This method is used by patient viewer to search and select a
            // patient before proceeding
            if (window.location.href.indexOf('scheduling-manager')) {
                var e = $.Event("keypress"), $input = $("#patient-search-input");
                e.which = e.keyCode = 13;
                $input.val(name).trigger(e);

            }
        }

        module("SCV1337|SCV1405- Display consults for a patient, All columns are sortable, Display consults details");
        test('SCV1406 - Validate consults for a patient', function () {
            ok(true, "Tests are running.");

            $(window).load(function () {
                asyncSeries(slotsGenMed1,
                    fourPatient1,
                    fourPatientinfo,
                    bookedAppts1,
                    fourPatientWl1,
                    facRecall1,
                    facNear1,
                    function () {
                        afterElementLoad($("#calendar-specialty-input"), function () {
                            ok($('#left-navigation').is(':visible'),
                                "Header is visible");
                        });

                        // Then I can see the header info
                        ok($('#left-navigation:contains("Open Menu")').text(),
                            "Then I can see the header bar");
                        return 1500;
                    }, function () {
                        ok($("#inner-content p:contains('Please select a Service/Specialty and Clinic to view availability.')")
                                .text(),
                            'Select Service/Specialty and Clinic to display calendar');
                        // Select Service/Specialty and Clinic to
                        // show calendar
                        $("#calendar-specialty-input").val('General')
                            .keyup();
                        $("#calendar-specialty li:nth-child(4) a")
                            .trigger('tap');

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

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

                    }, function () {
                        // Click View Availability to display the
                        // calendar
                        $("#appointment-button a").trigger('tap');
                        afterElementLoad($("#calendar"), function () {
                            ok($("#calendar")
                                    .is(':visible'),
                                'Calendar is visible');
                        });

                    }, function () {
                        //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');
                        return 1000;
                    }, function () {
                        equal($("#legend div:nth-child(2)").hasClass('legend patient-appt'), true, 'Patient has appt legend displayed');
                        //Go to patient tab
                        $('#patient-button').trigger('tap');
                        return 500;
                    }, function () {
                        afterElementLoad($("#future-apointments-table"), function () {
                            ok($("#future-appointments-table").is(':visible'),
                                'Future Appointments table is visible');
                        });
                        //Go to the consults section
                        $("#consult-orders h3").trigger('click');

                        return 500;
                    }, function () {
                        // Consults table is loaded
                        afterElementLoad($("#consults-filter-table"), function () {
                            ok($("#consults-filter-table").is(':visible'),
                                'Consults table is visible');
                        });

                        //Validate the consults table
                        ok($("#consult-orders-table").find('th').eq(0).text(), 'Service / Specialty', '1st column');
                        ok($("#consult-orders-table").find('th').eq(1).text(), 'Order Date', '2nd column');
                        ok($("#consult-orders-table").find('th').eq(2).text(), 'Days', '3rd column');

                        //Validate that each column is sortable
                        //Days column - default is descending, click to sort ascending
                        equal($("#consult-orders-table").find('th').eq(2).attr('aria-sort'),
                            'descending', 'Days column descending');
                        $("#consult-orders-table").find('th').eq(2).trigger('click');
                        equal($("#consult-orders-table").find('th').eq(2).attr('aria-sort'),
                            'ascending', 'Days column ascending');
                        //Service/Specialty column - ascending
                        $("#consult-orders-table").find('th').eq(0).trigger('click');
                        equal($("#consult-orders-table").find('th').eq(0).attr('aria-sort'),
                            'ascending', 'Service/Specialty ascending');
                        //Service/Specialty column - descending
                        $("#consult-orders-table").find('th').eq(0).trigger('click');
                        equal($("#consult-orders-table").find('th').eq(0).attr('aria-sort'),
                            'descending', 'Service/Specialty descending');
                        //Order Date column - ascending
                        $("#consult-orders-table").find('th').eq(1).trigger('click');
                        equal($("#consult-orders-table").find('th').eq(1).attr('aria-sort'),
                            'ascending', 'Order Date column ascending');
                        //Order Date column - descending
                        $("#consult-orders-table").find('th').eq(1).trigger('click');
                        equal($("#consult-orders-table").find('th').eq(1).attr('aria-sort'),
                            'descending', 'Order Date column descending');

                        //Select a consults list item
                        $("#consult-orders-table").find('tr').eq(1).trigger('tap');
                        return 500;
                    }, function () {
                        //Validate the details are displayed
                        equal($("#inner-content").find('section').eq(1).is(':visible'), true, 'Consult details are displayed');
                        //Print consult button is displayed and enabled
                        equal($("#print-consult").is(':visible'), true, 'Print Consult button displayed');
                        ok($("#print-consult").attr('data-disabled'), 'Print Consult button enabled');


                    });

            });
        });
    });
