define(
    [ 'jQuery', 'underscore', 'asyncSeries', 'testUtilities', 'scvMockJaxCalls'],
    function ($$, underscore, asyncSeries, scvMockJaxCalls) {
        // ***** FUNCTIONS ******//
        function afterElementLoad($element, callback) {
            if (typeof $element === 'undefined') {
                setTimeout(function () {
                    afterElementLoad($element, callback);
                }, 11000);
            } else {
                callback();
            }
        }

        module("#SCV82/#SCV12 - Details of WL for Patient for a facility ");
        test('#SCV181 - WL for a patient by facility',
            function () {
                // Given I am on the Clerk Request app main page
                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");

                        $('#footer-waitlist-btn').trigger('tap');
                        afterElementLoad(
                            $("#ewl"),
                            function () {
                                ok($("#patient-search").is(
                                        ':visible'),
                                    'Patient Search is displayed in the sub header on the left pane');
                                // ok($("#sub-header
                                // .ui-title").text(),'Wait
                                // ListDC VAMC',
                                // 'Wait List Location is
                                // displayed');
                            });
                    },
                    function () {
                        // Filter section
                        ok(
                            $('#patient-filter .ui-btn-text')
                                .text(),
                            'Filter the Wait List',
                            'Filter the Wait List section displayed');
                        // Validate that the section is collapsed by
                        // default
                        equal(
                                $('#patient-filter .ui-collapsible-heading-collapsed').length > 0,
                            true, 'Filter section is collapsed');
                    },
                    function () {
                        // Validate the Wait List section
                        equal(
                                $("#ewl .ui-btn-text:contains('EWL')").length > 0,
                            true, 'Wait List section exist');
                        // No.of records
                        equal($('#ewl .ctn-count').text(), '8',
                            '8 records exist');
                    }, function () {
                        // Columns in EWL section
                        var $columnHeaders = $("#ewl-table th");
                        ok($columnHeaders.eq(0).attr('aria-label')
                                .split(':')[0], 'Patient',
                            'Column1 - Patient');
                        ok($columnHeaders.eq(1).attr('aria-label')
                                .split(':')[0],
                            'Service Connected',
                            'Column2 - Service Connected');
                        ok($columnHeaders.eq(2).attr('aria-label')
                                .split(':')[0], 'Days on List',
                            'Column3 - Days on List');
                    });
            });

        module("#SCV82/#SCV171 - Default Sort for wait list for patient on facility");
        test(
            '#SCV177 - Validate default sort order',
            function () {
                // Given I am on the Clerk Request app main page
                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");
                        $('#footer-waitlist-btn').trigger('tap');

                        afterElementLoad(
                            $("#sub-header"),
                            function () {
                                ok($("#patient-search").is(
                                        ':visible'),
                                    'Patient Search is displayed in the sub header on the left pane');
                                // ok($("#sub-header
                                // .ui-title").text(),'Wait
                                // ListDC VAMC',
                                // 'Wait List Location is
                                // displayed');

                            });
                    }, function () {
                        ok($("#ewl-table_wrapper th:nth-child(2)")
                                .attr('aria-sort'), 'ascending',
                            'Default sort');

                    });
            });

        module("#SCV82/#SCV171 - Default Sort for wait list for patient on facility");
        test(
            '#SCV177 - Validate default sort order',
            function () {
                var $columnHeaders;
                // Given I am on the Clerk Request app main page
                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");

                        $('#footer-waitlist-btn').trigger('tap');
                        console.log(1);
                        afterElementLoad(
                            $("#ewl"),
                            function () {
                                ok($("#patient-search").is(
                                        ':visible'),
                                    'Patient Search is displayed in the sub header on the left pane');
                                // ok($("#sub-header
                                // .ui-title").text(),'Wait
                                // ListDC VAMC',
                                // 'Wait List Location is
                                // displayed');
                            });
                    },
                    function () {
                        $columnHeaders = $("#ewl-table_wrapper th");
                        // Patient - Ascending
                        $columnHeaders.eq(0)[0].click();
                        equal(
                            $columnHeaders.eq(0)[0].attributes['aria-sort'].value,
                            'ascending', 'Patient-Ascending');
                        // Patient - Descending
                        $columnHeaders.eq(0)[0].click();
                        equal(
                            $columnHeaders.eq(0)[0].attributes['aria-sort'].value,
                            'descending', 'Patient-Descending');

                        // Days on List - Ascending
                        $columnHeaders.eq(2)[0].click();
                        equal(
                            $columnHeaders.eq(2)[0].attributes['aria-sort'].value,
                            'ascending',
                            'Days on List-Ascending');
                        // Days on List - Descending
                        $columnHeaders.eq(2)[0].click();
                        equal(
                            $columnHeaders.eq(2)[0].attributes['aria-sort'].value,
                            'descending',
                            'Days on List-Descending');

                        // Service Connected - Ascending
                        $columnHeaders.eq(1)[0].click();
                        equal(
                            $columnHeaders.eq(1)[0].attributes['aria-sort'].value,
                            'ascending',
                            'Service Connected-Ascending');

                        // Service Connected - Descending
                        $columnHeaders.eq(1)[0].click();
                        equal(
                            $columnHeaders.eq(1)[0].attributes['aria-sort'].value,
                            'descending',
                            'Service Connected-Descending');

                    });
            });

        module("#SCV82/#SCV13 - WL Patient Detail by facility");
        test('#SCV35 - Validate the WL patient detail for facility',
            function () {
                // Given I am on the Clerk Request app main page
                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");

                        $('#footer-waitlist-btn').trigger('tap');
                        afterElementLoad(
                            $("#sub-header"),
                            function () {
                                ok($("#patient-search").is(
                                        ':visible'),
                                    'Patient Search is displayed in the sub header on the left pane');
                                // ok($("#sub-header
                                // .ui-title").text(),'Wait
                                // ListDC VAMC',
                                // 'Wait List Location is
                                // displayed');
                            });
                    },
                    function () {
                        $("#ewl-table tr:nth-child(1)").trigger(
                            'tap');
                        afterElementLoad(
                            $("#inner-content"),
                            function () {

                                var $scrollable = $("#inner-content div:nth-child(2)");
                                // Validate the details

                                ok(
                                        $scrollable
                                            .find("h3:contains('Overview')").length > 0,
                                    'Overview',
                                    'Overview displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Current Status')").length > 0,
                                    'Current Status',
                                    'Current Status displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Full Name')").length > 0,
                                    'Full Name',
                                    'Full Name displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Date of Birth')").length > 0,
                                    'Date of Birth',
                                    'DoB displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('SSN')").length > 0,
                                    'SSN', 'SSN displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Means Test')").length > 0,
                                    'Means Test',
                                    'Means Test displayed');
                                ok(
                                        $scrollable
                                            .find("h3:contains('Request Details')").length > 0,
                                    'Request Details',
                                    'Request Details displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Desired Date of Appointment')").length > 0,
                                    'Desired Date of Appointment',
                                    'Desired Date of Appt displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Date Entered')").length > 0,
                                    'Date Entered',
                                    'Date Entered displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Days on EWL')").length > 0,
                                    'Days on EWL',
                                    'Days on EWL displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Service Connected')").length > 0,
                                    'Service Connected',
                                    'Service Connected displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Priority')").length > 0,
                                    'Priority',
                                    'Priority displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Last Updated')").length > 0,
                                    'Last Updated',
                                    'Last Updated displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Speciality')").length > 0,
                                    'Speciality',
                                    'Speciality displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Clinic')").length > 0,
                                    'Clinic',
                                    'Clinic displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Facility')").length > 0,
                                    'Facility',
                                    'Facility displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Provider')").length > 0,
                                    'Provider',
                                    'Provider displayed');
                                ok(
                                        $scrollable
                                            .find("li:contains('Comments')").length > 0,
                                    'Comments',
                                    'Comments displayed');

                            });
                    });
            });
    });
