# Source: PxDataTable.js

    /**
     * Created by 212556710 on 6/8/17.
     */

    (function () {
        "use strict";

        /**
         * Creates new PxDataTable
         * @class PxDataTable
         */
        var PxDataTable = function () {
            var helper = require("./TestHelper");
            var TestHelperPO = require("./TestHelperPO");
            var logger = require("./Logger.js");
            var section, tableContainer;
            var EC = protractor.ExpectedConditions;
            return {
                /**
                 * Sets parent element of table
                 * @method initialize
                 * @param {Object} sectionElement
                 */
                //tested
                initialize: function (sectionElement) {
                    section = sectionElement;
                    tableContainer = section.element(by.css("div.flex.px-data-table"));
                },

                /**
                 * Sets the value of table container
                 * @method setContainer
                 * @param {Object} containerElement
                 */
                //tested
                setContainer: function (containerElement) {
                    tableContainer = containerElement;
                    logger.info("container set " + containerElement)
                },

                /**
                 * Retrieves the value of table container
                 * @method getContainer
                 * @return {String}
                 */
                //tested
                getContainer: function () {
                    return tableContainer
                },

                /**
                 * Retrieves the number of columns present in table
                 * @method getColumnCount
                 * @return {Integer}
                 */
                //tested
                getColumnCount: function () {
                    var deferred = protractor.promise.defer();
                    var columnCount = 0;
                    var tableheaders = tableContainer.all(by.css("div.flex.header--container"));
                    return tableheaders.then(function (columns, callback) {
                        columns.forEach(function (option, index) {
                            columnCount++;
                        });
                        deferred.fulfill(columnCount);
                        return deferred.promise;
                    });

                },

                /**
                 * Retrieves the number of rows present in table
                 * @method getRowCount
                 * @return {Integer}
                 */
                //tested
                getRowCount: function () {
                    var deferred = protractor.promise.defer();
                    var rowCount = 0;
                    var rows = tableContainer.all(by.css("div.tr.rows.aha-table"));
                    return rows.then(function (tablerows, callback) {
                        tablerows.forEach(function (option, index) {
                            rowCount++;
                        });
                        deferred.fulfill(rowCount);
                        return deferred.promise;
                    });
                },

                /**
                 * Retrieves the value of the current page number
                 * @method getCurrentPage
                 * @return {Integer}
                 */
                //tested
                getCurrentPage: function () {
                    var deferred = protractor.promise.defer();
                    var currentPageNumber;
                    var pageNumbers = tableContainer.all(by.css("px-pagination#pagination span.pager span.btn--pagination--number"));
                    return pageNumbers.then(function (pages, callback) {
                        pages.forEach(function (page, index) {
                            page.getText().then(function (pageText) {
                                currentPageNumber=pageText;
                                deferred.fulfill(currentPageNumber);
                            });
                        });
                        return deferred.promise;
                    });

                },

                /**
                 * Navigates to specified page number
                 * @method goToPage
                 * @param {Integer} pageNumber
                 * @return {Integer}
                 */
                //tested
                goToPage: function (pageNumber) {
                    var deferred = protractor.promise.defer();
                    var currentPageNumbers=[];
                    var pageNumbers = tableContainer.all(by.css("px-pagination#pagination span.pager span"));
                    return pageNumbers.then(function (pagesElements, callback) {
                        pagesElements.forEach(function (pages, index) {
                            currentPageNumbers.push(pages);

                        });
                        return currentPageNumbers[pageNumber-1].click().then(function (resolve, callback) {
                            deferred.fulfill(resolve);
                            return deferred.promise;
                        });

                    });
                },

                /**
                 * Navigates to next page
                 * @method goToNextPage
                 */
                //tested
                goToNextPage: function () {
                    var deferred = protractor.promise.defer();
                    var currentPageNumbers=[];
                    var pageNumbers = tableContainer.all(by.css("px-pagination#pagination span.paging button i.fa-angle-right"));
                    return pageNumbers.then(function (pagesElements, callback) {
                        pagesElements.forEach(function (pages, index) {
                            currentPageNumbers.push(pages);

                        });
                        return currentPageNumbers[0].click().then(function (resolve, callback) {
                            deferred.fulfill(resolve);
                            return deferred.promise;
                        });
                    });
                },

                /**
                 * Navigates to previous page
                 * @method goToPreviousPage
                 */
                //tested
                goToPreviousPage: function () {
                    var deferred = protractor.promise.defer();
                    var currentPageNumbers=[];
                    var pageNumbers = tableContainer.all(by.css("px-pagination#pagination span.paging button i.fa-angle-left"));
                    return pageNumbers.then(function (pagesElements, callback) {
                        pagesElements.forEach(function (pages, index) {
                            currentPageNumbers.push(pages);
                        });
                        return currentPageNumbers[0].click().then(function (resolve, callback) {
                            deferred.fulfill(resolve);
                            return deferred.promise;
                        });
                    });

                },

                /**
                 * Retrieves current value of row per page field
                 * @method getRowPerPageValue
                 * @return {Integer}
                 */
                getRowPerPageValue: function () {
                    var deferred = protractor.promise.defer();
                    var selectedOption;
                    var rowsPerPage = tableContainer.all(by.css("select#pageSizeSelect option[selected]"));
                    return rowsPerPage.getText().then(function (text) {
                        selectedOption = text;
                        deferred.fulfill(selectedOption);
                        return deferred.promise;
                    });
                },

                /**
                 * Sets value of row per page field
                 * @method setRowPerPageValue
                 * @param {Integer} selectValue
                 */
                setRowPerPageValue: function (selectValue) {
                    // var pagination = tableContainer.all(by.css("select#pageSizeSelect"));
                    /* pagination.click().then(function (text) {*/
                    var options=  tableContainer.element(by.css("select#pageSizeSelect option[value='"+selectValue+"']"));
                    return options.click();
                },

                /**
                 * Applies filter in precise column
                 * @method setFilter
                 * @param {String} columName
                 * @param {Integer} filterValue
                 */
                setFilter: function (columName,filterValue) {
                    return this.getFilter(columName).then(function (columnfilters) {
                        return columnfilters.sendKeys(filterValue);
                    });
                },

                /**
                 * Clears filter applied in precised column
                 * @method clearFilter
                 * @param {String} columnName
                 */
                clearFilter: function (columnName) {
                    return this.getFilter(columnName).then(function (columnfilters) {
                        return columnfilters.clear();
                    });
                },

                /**
                 * Retrieves filter value of precise column
                 * @method getFilter
                 * @param {String} columnName
                 * @return {String}
                 */
                //tested
                getFilter: function (columnName) {
                    var deferred = protractor.promise.defer();
                    var filtersList = [];
                    var index;
                    return this.getColumnindex(columnName).then(function (colIndex) {
                        index = colIndex;
                        var tablefilters = tableContainer.all(by.css("span.td.style-scope:not([style]) input"));
                        return tablefilters.then(function (filters, callback) {
                            filters.forEach(function (option, index) {
                                filtersList.push(option);
                            });
                            deferred.fulfill(filtersList[index]);
                            return deferred.promise;
                        });
                    });
                },

                /**
                 * Applies sorting in specified column
                 * @method sortColumn
                 * @param {String} columnName
                 */
                sortColumn: function (columnName) {
                    var columnSortElements = [];
                    var columnElement;
                    var self=this;
                    return this.getColumnHeaderElements().then(function (columnElement) {
                        columnSortElements = columnElement;
                        console.log("in sorting getColumnHeaderElements columnSortElements  " +columnSortElements );
                        return self.getColumnindex(columnName).then(function(columnIndex){
                            columnElement= columnSortElements[columnIndex]
                            return columnElement.element(by.css("span.column-head.u-m--")).click();
                        });
                    });
                },

                /**
                 * Retrieves index value of column
                 * @method getColumnindex
                 * @param columnName
                 * @return {Integer}
                 */
                //tested
                getColumnindex: function (columnName) {
                    var deferred = protractor.promise.defer();
                    var columnIndex;
                    var columnns = [];

                    return this.getColumnNames().then(function (colNames) {
                        colNames.forEach(function (Names, index) {
                            columnns.push(Names);
                        });
                        columnIndex = columnns.indexOf(columnName);
                        deferred.fulfill(columnIndex);
                        return deferred.promise;
                    });
                },

                /**
                 * Retrieves column names
                 * @method getColumnNames
                 * @return {String}
                 */
                //tested
                getColumnNames: function () {
                    var deferred = protractor.promise.defer();
                    var columnNames = [];
                    var tableheaders = tableContainer.all(by.css("span.th.style-scope:not([style]) div.flex.header--container"));
                    return tableheaders.then(function (columns, callback) {
                        columns.forEach(function (columnElement, index) {
                            columnElement.getText().then(function (elementText) {
                                columnNames.push(elementText);
                            });
                        });
                        deferred.fulfill(columnNames);
                        return deferred.promise;
                    });
                },

                /**
                 * Retrieves an array of column header elements
                 * @method getColumnHeaderElements
                 * @return {Array}
                 */
                getColumnHeaderElements: function () {
                    var deferred = protractor.promise.defer();
                    var columnElements = [];
                    var tableheaders = tableContainer.all(by.css("span.th.style-scope:not([style]) div.flex.header--container"));
                    return tableheaders.then(function (columns, callback) {
                        columns.forEach(function (columnElement, index) {
                            columnElements.push(columnElement);
                        });
                        deferred.fulfill(columnElements);
                        return deferred.promise;
                    });

                },

                /**
                 * Retrieves text of precise cell by using row and column number or index value as input
                 * @method getCellText
                 * @param {Integer} row
                 * @param {Integer} column
                 * @return {String}
                 */
                //tested
                getCellText: function (row, column) {
                    return this.getCellElement(row, column).then(function (cellElement) {
                        return cellElement.element(by.css("span.cell--value")).getText();
                    });

                },

                /**
                 * Retrieves text for specified by using cell element value
                 * @method getCellElementText
                 * @param cellElement
                 * @return {String}
                 */
                getCellElementText: function (cellElement) {
                    return this.getCellElement(row, column).then(function (cellElement) {
                        return cellElement.element(by.css("span.cell--value")).getText();
                    });
                },

                /**
                 * Retrieves html data for precise cell by using row and column number or index value as input
                 * @method getCellHtml
                 * @param {Integer} row
                 * @param {Integer} column
                 * @return {String}
                 */
                getCellHtml: function (row,column) {
                    var deferred = protractor.promise.defer();
                    return this.getCellElement(row, column).then(function (cellElement) {
                        var cellHtml= cellElement.element(by.css("aha-html-echo.px-data-table-cell"));
                        deferred.fulfill(cellHtml);
                        return deferred.promise;
                    });
                },

                /**
                 * Retrieves all the text value for precise column
                 * @method getColumnTextValues
                 * @param {String} columnName
                 * @return {String}
                 */
                //tested
                getColumnTextValues: function (columnName) {
                    var deferred = protractor.promise.defer();
                    var columnValue = [];
                    //var allElements=[];
                    var self=this;
                    return this.getColumnindex(columnName).then(function (ColumnIndex) {
                        return self.getRowCount().then(function (rowCount) {
                            //return self.getCellElements().then(function (allTableElements) {
                            //allElements = allTableElements;
                            for (var i = 0; i < rowCount; i++) {
                                self.getCellText(i, ColumnIndex).then(function (cellText) {
                                    columnValue.push(cellText);
                                });
                            }
                            deferred.fulfill(columnValue);
                            return deferred.promise;
                            //});
                        });
                    });
                },

                /**
                 * Retrieves all html values for precise column
                 * @method getColumnHtmlValues
                 * @param {String} columnName
                 * @return {String}
                 */
                getColumnHtmlValues: function (columnName) {
                    var deferred = protractor.promise.defer();
                    var allColumnHtml = [];
                    //var allElements=[];
                    var self=this;
                    return this.getColumnindex(columnName).then(function (ColumnIndex) {
                        return self.getRowCount().then(function (rowCount) {
                            //return self.getCellElements().then(function (allTableElements) {
                            //allElements = allTableElements;
                            for (var i = 0; i < rowCount; i++) {
                                self.getCellHtml(i, ColumnIndex).then(function (cellHtml) {
                                    allColumnHtml.push(cellHtml);
                                });
                            }
                            deferred.fulfill(allColumnHtml);
                            return deferred.promise;
                            //});
                        });
                    });
                },

                /**
                 * Retrieves all records of specified column
                 * @method getColumnElementValues
                 * @param {String} columnName
                 * @return {String}
                 */
                getColumnElementValues: function (columnName) {
                    var deferred = protractor.promise.defer();
                    var allColumnElement = [];
                    //var allElements=[];
                    var self=this;
                    return this.getColumnindex(columnName).then(function (colIndex) {
                        return self.getRowCount().then(function (rowCount) {
                            //return self.getCellElements().then(function (allTableElements) {
                            //allElements = allTableElements;
                            for (var i = 0; i < rowCount; i++) {
                                self.getCellElement(i, colIndex).then(function (cellElement) {
                                    allColumnElement.push(cellElement);
                                });
                            }
                            deferred.fulfill(allColumnElement);
                            return deferred.promise;
                            //});
                        });
                    });

                },

                /**
                 * Retrieves all text value of specified row
                 * @method getRowTextValues
                 * @param {Integer} rowIndex
                 * @return {String}
                 */
                getRowTextValues: function (rowIndex) {
                    var deferred = protractor.promise.defer();
                    var allRowText = [];
                    //var allElements=[];
                    var self=this;
                    return this.getColumnCount().then(function (colCount) {
                        //return self.getCellElements().then(function (allTableElements) {
                        //allElements = allTableElements;
                        for (var j = 0; j < colCount; j++) {
                            self.getCellText(rowIndex, j).then(function (cellText) {
                                allRowText.push(cellText);
                            });
                        }
                        deferred.fulfill(allRowText);
                        return deferred.promise;
                        //});
                    });
                },

                /**
                 * Retrieves all html values of specified row
                 * @method getRowHtmlValues
                 * @param {Integer} rowIndex
                 * @return {String}
                 */
                getRowHtmlValues: function (rowIndex) {
                    var deferred = protractor.promise.defer();
                    var allRowHtml = [];
                    //var allElements=[];
                    var self=this;
                    this.getColumnCount().then(function (colCount) {
                        //return self.getCellElements().then(function (allTableElements) {
                        //allElements = allTableElements;
                        for (var j = 0; j < colCount; j++) {
                            self.getCellHtml(rowIndex, j).then(function (cellHtml) {
                                allRowHtml.push(cellHtml);
                            });
                        }
                        deferred.fulfill(allRowHtml);
                        //});
                    });
                    return deferred.promise;
                },

                /**
                 * Retrieves all records of element value of specified row
                 * @method getRowElementValues
                 * @param {Integer} rowIndex
                 * @return {String}
                 */
                getRowElementValues: function (rowIndex) {
                    var deferred = protractor.promise.defer();
                    var allRowElement = [];
                    var allElements=[];
                    var self=this;
                    return this.getColumnCount().then(function (colCount) {
                        //return self.getCellElements().then(function (allTableElements) {
                        //allElements = allTableElements;
                        for (var j = 0; j < colCount; j++) {
                            self.getCellElement(rowIndex, j).then(function (cellHtml) {
                                allRowElement.push(cellHtml);
                            });
                        }
                        deferred.fulfill(allRowElement);
                        return deferred.promise;
                        //});
                    });
                },

                /**
                 * Retrieves all cell elements of all table records
                 * @method getCellElements
                 * @return {String}
                 */
                //tested
                getCellElements: function () {
                    var deferred = protractor.promise.defer();
                    var elems = [];
                    var tableElements = [];

                    return this.getColumnCount().then(function (columnCount) {
                        return this.getRowCount().then(function (RowCount) {
                            var elementsGrid = tableContainer.all(by.css("div.tr.rows.aha-table px-data-table-cell"));
                            return elementsGrid.then(function (elements, callback) {
                                elements.forEach(function (TabElems, index) {
                                    elems.push(TabElems);
                                });
                                var counter = 0;
                                for (var i = 0; i < RowCount; i++) {
                                    var columnArray = [];
                                    for (var j = 0; j < columnCount; j++) {
                                        columnArray.push(elems[counter]);
                                        counter++;
                                    }
                                    tableElements.push(columnArray);
                                }
                                deferred.fulfill(tableElements);
                                return deferred.promise;
                            });
                        });
                    });
                },

                /**
                 * Retrieves cell element value for specified row and column
                 * @method getCellElement
                 * @param {Integer} rowNumber
                 * @param {Integer} columnNumber
                 * @return {String}
                 */
                //tested
                getCellElement: function (rowNumber, columnNumber) {
                    var deferred = protractor.promise.defer();
                    //var allElements=[];
                    this.getCellElements().then(function (allTableElements) {
                        //allElements = allTableElements;
                        deferred.fulfill(allTableElements[rowNumber][columnNumber]);
                        return deferred.promise;
                    });
                },
            }
        };
        module.exports = new PxDataTable
    })();