sap.ui.define([
	'sap/ui/test/Opa5',
	'sap/ui/test/matchers/AggregationLengthEquals',
	'sap/ui/test/matchers/I18NText',
	'sap/ui/test/matchers/BindingPath',
	'sap/ui/test/actions/Press',
	'sap/ui/test/actions/EnterText'
],
function (Opa5, AggregationLengthEquals, I18NText, BindingPath, Press, EnterText) {
	"use strict";

	var sViewName = "Worklist",
		sTableId = "table";

	Opa5.createPageObjects({
		onTheWorklistPage: {
			actions: {
				iPressOnMoreData: function () {
					// Press action hits the "more" trigger on a table
					return this.waitFor({
						id: sTableId,
						viewName: sViewName,
						actions: new Press(),
						errorMessage: "The table does not have a trigger"
					});
				},

				iPressOnTheItemWithTheID: function (sId) {
					return this.waitFor({
						controlType: "sap.m.ColumnListItem",
						viewName: sViewName,
						matchers:  new BindingPath({
							path: "/Posts('" + sId + "')"
						}),
						actions: new Press(),
						errorMessage: "No list item with the ID " + sId + " was found."
					});
				},

				iSearchFor: function (sSearchString) {
					return this.waitFor({
						id: "searchField",
						viewName: sViewName,
						actions: new EnterText({
							text: sSearchString
						}),
						errorMessage: "SearchField was not found."
					});
				}
			},
			assertions: {
				theTableHasOneItem: function () {
					return this.waitFor({
						id: sTableId,
						viewName: sViewName,
						matchers: new AggregationLengthEquals({
							name: "items",
							length: 1
						}),
						success: function () {
							Opa5.assert.ok(true, "The table contains one corresponding item.");
						},
						errorMessage: "The table does not contain one item."
					});
				},

				theTableShouldHavePagination: function () {
					return this.waitFor({
						id: sTableId,
						viewName: sViewName,
						matchers: new AggregationLengthEquals({
							name: "items",
							length: 20
						}),
						success: function () {
							Opa5.assert.ok(true, "The table has 20 items on the first page");
						},
						errorMessage: "The table does not contain all items."
					});
				},

				theTableShouldHaveAllEntries: function () {
					return this.waitFor({
						id: sTableId,
						viewName: sViewName,
						matchers: new AggregationLengthEquals({
							name: "items",
							length: 23
						}),
						success: function () {
							Opa5.assert.ok(true, "The table has 23 items");
						},
						errorMessage: "The table does not contain all items."
					});
				},

				theTitleShouldDisplayTheTotalAmountOfItems: function () {
					return this.waitFor({
						id: "tableHeader",
						viewName: sViewName,
						matchers: new I18NText({
							key: "worklistTableTitleCount",
							propertyName: "text",
							parameters: [23]
						}),
						success: function () {
							Opa5.assert.ok(true, "The table header has 23 items");
						},
						errorMessage: "The table header does not contain the number of items: 23"
					});
				},

				iShouldSeeTheTable: function () {
					return this.waitFor({
						id: sTableId,
						viewName: sViewName,
						success: function () {
							Opa5.assert.ok(true, "The table is visible");
						},
						errorMessage: "Was not able to see the table."
					});
				}
			}
		}
	});

});
