define(
    [ 'jQuery', 'asyncSeries', 'testUtilities' ],
    function ($$, asyncSeries) {
        function afterElementLoad($element, callback) {
            if (typeof $element === 'undefined') {
                setTimeout(function () {
                    afterElementLoad($element, callback);
                }, 1000);
            } else {
                callback();
            }
        }

        module("SCV-51|SCV-184 - Display Consults for Selected Patient");
        test(
            'SCV-230 - Validate consult order and its details',
            function () {
                // Given I am on the Scheduling app
                asyncSeries(
                    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 () {
                // Validate wait list section for a single
                // patient
                asyncSeries(
                    function () {
                        // 1.Once u login the Booking
                        // tab on the footer is
                        // highlighted.
                        ok(
                                $("#footer .ui-block-a .ui-btn-active").length > 0,
                            'Booking tab is highlighted');
                        // 2. Search and select a
                        // patient
                        searchPatient("vamobilepatient,five");

                    },
                    function () {
                        $(
                            '#patient-search-results li:nth-child(2) a')
                            .trigger('tap');
                        afterElementLoad($("#footer"),
                            function () {

                            });

                        // Being still on the booking
                        // tab the next page is
                        // displayed with WL for the
                        // selected patient
                    },
                    function () {
                        ok(
                                $("#footer .ui-block-a .ui-btn-active").length > 0,
                            true,
                            'Booking tab is highlighted');

                        // Validate the contents of the
                        // left pane Header
                    },
                    function () {
                        ok(
                            $("#header .ui-title")
                                .text(),
                            'VA Scheduling Manager',
                            'VA Scheduling Manager is displayed in the header');

                        // Second header
                    },
                    function () {
                        // ok($("#sub-header-right
                        // h3:nth-child(2)")
                        // .text().trim(),
                        // 'VAMOBILEPATIENT
                        // (9999)','Patient last name
                        // (last 4 of ssn) is
                        // displayed');

                        ok(
                            $(
                                "#sub-header-right .ui-btn-text")
                                .text().trim(),
                            'Info',
                            'Info button displayed');

                        // Book Appointment button
                        // currently disabled

                    },
                    function () {
                        var patientInfo = $(
                            'h3.ui-title.subHeader-info.float-right')
                            .text().trim(), patientInfoFooter = $(
                            '#footer-patientname')
                            .text().trim().replace(
                            /\s+/g, " ")
                            .split(' - ');

                        // equal(patientInfo, 'patient66
                        // (9966)', 'Patient info shows
                        // Lastname + last 4 digits of
                        // ssn');
                        equal(
                            patientInfoFooter[0],
                            'VAMOBILEPATIENT, FIVE',
                            'Full patient name is displayed');
                        equal(patientInfoFooter[1],
                            'FEMALE',
                            'Patient gender is displayed');
                        equal(patientInfoFooter[2],
                            '999-99-9999',
                            'Patient SSN is displayed');
                        equal(patientInfoFooter[3],
                            '01/01/1900',
                            'Patient DOB is displayed');
                        equal(patientInfoFooter[4],
                            '113',
                            'Patient age is displayed');

                    },
                    function () {
                        // Validate the Wait List
                        // section
                        ok(
                            $(
                                "#consult-orders .ui-btn-text:contains('Consult Orders')")
                                .is(':visible'),
                            'Consult Orders section exist');
                        // Consult order table
                        $("#consult-orders .ui-icon")
                            .trigger('click');
                    },
                    function () {
                        var $columnHeaders = $('#consult-orders-table_wrapper th');
                        ok($columnHeaders.eq(1).text()
                                .trim(),
                            'Service/Specialty',
                            'Column1 - Service/Specialty');
                        ok($columnHeaders.eq(2).text()
                                .trim(), 'Order Date',
                            'Column2 - Order Date');
                        ok($columnHeaders.eq(3).text()
                                .trim(),
                            'Days on List',
                            'Column3 - Days on List');
                        // Consult order details

                    },
                    function () {
                        $(
                            '#consult-orders-table tr:nth-child(1)')
                            .trigger('tap');
                        afterElementLoad(
                            $("#content-primary"),
                            function () {
                                ok(
                                    $(
                                        "#content-primary div:nth-child(2) pre:contains('Order Information')")
                                        .is(
                                        ":visible"),
                                    'Details displayed');
                            });
                        // Print button
                        ok($('#print-consult').is(
                                ":visible"),
                            'Print button displayed');
                        ok(
                            $(
                                "#content-primary div:nth-child(2):contains('Print Consult')")
                                .is(":visible"),
                            'Print button text');
                    });
            });

        module("SCV-51|SCV170 - Default sort order is Days on List");
        test(
            'SCV232 - Validate default sort order on consult orders for patient',
            function () {
                asyncSeries(
                    function () {
                        // 1.Once u login the Booking
                        // tab on the
                        // footer is highlighted.
                        equal(
                                $("#footer .ui-block-a .ui-btn-active").length > 0,
                            true,
                            'Booking tab is highlighted');
                        // 2. Search and select a
                        // patient
                        searchPatient("vamobilepatient,five");
                    },
                    function () {
                        $(
                            '#patient-search-results li:nth-child(2) a')
                            .trigger('tap');

                        afterElementLoad(
                            $("#consult-orders"),
                            function () {

                            });
                        // Validate the default sort is
                        // by 'Days on List' column
                        // (descending)
                    },
                    function () {
                        $(
                            '#consult-orders .ui-icon-arrow-r')
                            .trigger('click');
                        ok(
                            $(
                                "#consult-orders-table_wrapper th:nth-child(3)")
                                .attr(
                                'aria-sort'),
                            'descending',
                            'Default sort');

                    });
            });

        module("SCV-51|SCV169 - User can sort by any column in the table");
        test(
            'SCV-231 - Validate that each column is sortable',
            function () {
                var $columnHeaders;
                asyncSeries(
                    function () {
                        // 1.Once u login the Booking
                        // tab on the
                        // footer is
                        // highlighted.
                        equal(
                                $("#footer .ui-block-a .ui-btn-active").length > 0,
                            true,
                            'Booking tab is highlighted');
                        // 2. Search and select a
                        // patient
                        searchPatient("vamobilepatient,five");

                    },
                    function (next) {
                        $(
                            '#patient-search-results li:nth-child(2) a')
                            .trigger('tap');

                        afterElementLoad(
                            $("#consult-orders"),
                            function () {

                            });
                        // Validate the default sort is
                        // by 'Days on List' column
                        // (descending)
                    },
                    function () {
                        $(
                            '#consult-orders .ui-icon-arrow-r')
                            .trigger('click');
                        // Service/Specialty - Ascending
                        $(
                            '#consult-orders-table_wrapper th:nth-child(1)')
                            .click();
                        ok(
                            $(
                                '#consult-orders-table_wrapper th:nth-child(1)')
                                .attr(
                                'aria-sort'),
                            'ascending',
                            'Service/Specialty - Ascending');
                        // Service/Specialty -
                        // Descending
                        $(
                            '#consult-orders-table_wrapper th:nth-child(1)')
                            .click();
                        ok(
                            $(
                                '#consult-orders-table_wrapper th:nth-child(1)')
                                .attr(
                                'aria-sort'),
                            'descending',
                            'Service/Specialty-Descending');

                        // Order Date - Ascending
                        $(
                            '#consult-orders-table_wrapper th:nth-child(2)')
                            .click();
                        ok(
                            $(
                                '#consult-orders-table_wrapper th:nth-child(2)')
                                .attr(
                                'aria-sort'),
                            'ascending',
                            'Order Date-Ascending');
                        // Order Date - Descending
                        $(
                            '#consult-orders-table_wrapper th:nth-child(2)')
                            .click();
                        ok(
                            $(
                                '#consult-orders-table_wrapper th:nth-child(2)')
                                .attr(
                                'aria-sort'),
                            'descending',
                            'Order Date-Descending');

                        // Days on list - Descending
                        // var $columnHeaders =
                        // $('#consult-orders-table_wrapper);
                        $(
                            '#consult-orders-table_wrapper th:nth-child(3)')
                            .click();
                        ok(
                            $(
                                '#consult-orders-table_wrapper th:nth-child(3)')
                                .attr(
                                'aria-sort'),
                            'descending',
                            'Days on List-Descending');
                        // Days on list - Ascending
                        $(
                            '#consult-orders-table_wrapper th:nth-child(3)')
                            .click();
                        ok(
                            $(
                                '#consult-orders-table_wrapper th:nth-child(3)')
                                .attr(
                                'aria-sort'),
                            'ascending',
                            'Days on List-Ascending');

                        // Alert - Ascending
                        $(
                            '#consult-orders-table_wrapper th:nth-child(4)')
                            .click();
                        ok(
                            $(
                                '#consult-orders-table_wrapper th:nth-child(4)')
                                .attr(
                                'aria-sort'),
                            'ascending',
                            'Alert-Ascending');
                        // Alert - Descending
                        $(
                            '#consult-orders-table_wrapper th:nth-child(4)')
                            .click();
                        ok(
                            $(
                                '#consult-orders-table_wrapper th:nth-child(4)')
                                .attr(
                                'aria-sort'),
                            'descending',
                            'Alert-Descending');

                    });

            });
    });
})
;
