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

        function getResultsCount() {
            return $('#patient-list li a').length;
        }

        function getPatientName(pos) {
            return $('#patient-list li:nth-child(' + pos + ') a h3').eq(0)
                .text();
        }

        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('keydown'),
		            $input = $('#primary-search-filter input');

	            e.which = e.keyCode = 13;

	            $input.val(name).trigger(e);
            }
        }

        module("#1134/#1854 - Patient Search");
        test('#2073 - a.Verify that search results are paged at 20 records', function () {
	        ok(true, "The tests are running");

	        $(window).load(function () {
                asyncSeries(function () {
                    afterElementLoad($("#calendar-specialty-input"), function () {
                        // Given I am on the Clerk Request app main page
                        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($("#patient-search").is(':visible'),
		                'Search input is visible');

	                $('#patient-search').trigger('click');

	                return 1500;
                }, function () {
	                patientSelect('zzzretiredone');

	                return 500;
                }, function () {
                    equal(getResultsCount(), 24,
                        'Verify that only 24 rows are displayed');
                });
	        });
        });

        test('#2073 - b.Verify searching by last name', function () {
	        asyncSeries(function () {
		        return 1000;
	        }, function () {
		        afterElementLoad($("#calendar-specialty-input"), function () {
			        // Given I am on the Clerk Request app main page
			        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($("#patient-search").is(':visible'),
			        'Search input is visible');

		        $('#patient-search').trigger('click');

		        return 1500;
	        }, function () {
		        patientSelect('ten');

		        return 500;
	        }, function () {
		        equal(getPatientName(4), 'TEN, PATIENT',
			        'Verify the name of the returned results');
	        });
        });

        test('#2073 - c.Verify searching by SSN', function () {
	        asyncSeries(function () {
		        return 1000;
	        }, function () {
		        afterElementLoad($("#calendar-specialty-input"), function () {
			        // Given I am on the Clerk Request app main page
			        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($("#patient-search").is(':visible'),
			        'Search input is visible');

		        $('#patient-search').trigger('click');

		        return 1500;
	        }, function () {
		        patientSelect('666000004');

		        return 500;
	        }, function () {
		        equal(getResultsCount(), 1,
			        'Verify that only 1 person is displayed');
		        equal(getPatientName(1), 'FOUR, PATIENT',
			        'Verify the name of the returned results');
	        });
        });

        test('#2073 - d.Verify searching by last name,first name', function () {
	        asyncSeries(function () {
		        return 1000;
	        }, function () {
		        afterElementLoad($("#calendar-specialty-input"), function () {
			        // Given I am on the Clerk Request app main page
			        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($("#patient-search").is(':visible'),
			        'Search input is visible');

		        $('#patient-search').trigger('click');

		        return 1500;
	        }, function () {
		        patientSelect('TEN, PATIENT');

		        return 500;
	        }, function () {
		        equal(getPatientName(4), 'TEN, PATIENT',
			        'Verify the name of the returned results');
	        });
        });

        test('#2073 - e.Verify searching by 1st initial of last name + last 4 of SSN', function () {
	        asyncSeries(function () {
		        return 1000;
	        }, function () {
		        afterElementLoad($("#calendar-specialty-input"), function () {
			        // Given I am on the Clerk Request app main page
			        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($("#patient-search").is(':visible'),
			        'Search input is visible');

		        $('#patient-search').trigger('click');

		        return 1500;
	        }, function () {
		        patientSelect('F0004');

		        return 500;
	        }, function () {
		        equal(getResultsCount(), 1,
			        'Verify that only one result is returned');
		        equal(getPatientName(1), 'FOUR, PATIENT',
			        'Verify the name of the returned results');
	        });
        });

        test('#2073 - f.Verify that searching by special characters do not generate errors', function () {
	        asyncSeries(function () {
		        return 1000;
	        }, function () {
		        afterElementLoad($("#calendar-specialty-input"), function () {
			        // Given I am on the Clerk Request app main page
			        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($("#patient-search").is(':visible'),
			        'Search input is visible');

		        $('#patient-search').trigger('click');

		        return 1500;
	        }, function () {
		        patientSelect("\u070f\u17b4\u17b5!@#$%^&*()_+=-~`");

		        return 500;
	        }, function () {
		        equal(getResultsCount(), 0,
			        'Verify that no rows are displayed');
		        equal($('#patient-list li:nth-child(1)')
				        .eq(0).text(), 'No Results Found',
			        'Verify the text when no results are returned');
	        });
        });

        test('#2073 - g.Verify that the system allows searching names with apostrophes', function () {
	        asyncSeries(function () {
		        return 1000;
	        }, function () {
		        afterElementLoad($("#calendar-specialty-input"), function () {
			        // Given I am on the Clerk Request app main page
			        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($("#patient-search").is(':visible'),
			        'Search input is visible');

		        $('#patient-search').trigger('click');

		        return 1500;
	        }, function () {
		        patientSelect("BCMA,TEN-O'PATIENT");

		        return 500;
	        }, function () {
		        equal(getPatientName(1), "BCMA, TEN-O'PATIENT",
			        'Verify the name of the returned results');
	        });
        });

        test('#2073 - h.Verify that the system allows searching names with hyphens', function () {
	        asyncSeries(function () {
		        return 1000;
	        }, function () {
		        afterElementLoad($("#calendar-specialty-input"), function () {
			        // Given I am on the Clerk Request app main page
			        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($("#patient-search").is(':visible'),
			        'Search input is visible');

		        $('#patient-search').trigger('click');

		        return 1500;
	        }, function () {
		        patientSelect("BCMA,ONE-PATIENT");

		        return 500;
	        }, function () {
		        equal(getPatientName(1), "BCMA, ONE-PATIENT",
			        'Verify the name of the returned results');
	        });
        });

        test('#2073 - i.Verify that the system allows searching names with spaces', function () {
	        asyncSeries(function () {
		        return 1000;
	        }, function () {
		        afterElementLoad($("#calendar-specialty-input"), function () {
			        // Given I am on the Clerk Request app main page
			        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($("#patient-search").is(':visible'),
			        'Search input is visible');

		        $('#patient-search').trigger('click');

		        return 1500;
	        }, function () {
		        patientSelect("ten, patient");

		        return 500;
	        }, function () {
		        equal(getPatientName(4), "TEN, PATIENT",
			        'Verify the name of the returned results');
	        });
        });

        test('#2074, SCV-1735 - App Name and Version Number appear in the footer', function () {
	        asyncSeries(function () {
		        return 1000;
	        }, function () {
		        afterElementLoad($("#calendar-specialty-input"), function () {
			        // Given I am on the Clerk Request app main page
			        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($("#footer-appname").is(':visible'),
			        'App Name is visible');
	        }, function () {
		        var patientInfo = $(
			        'h3.ui-title.subHeader-info.float-right')
			        .text().trim(), patientInfoFooter = $(
			        '#footer-appname').text().trim()
			        .replace(/\s+/g, " ").split('.');

		        equal(patientInfoFooter[0].substr(0, patientInfoFooter[0].length - 1),
			        'Scheduling Manager - v',
			        'App name is displayed');
		        ok(patientInfoFooter.length >= 3,
			        'Version number is displayed');
	        });
        });
    });
