import { browser, by, element } from 'protractor';
import { SelectMultipleControlValueAccessor } from '@angular/forms';
import { timeInterval } from 'rxjs/operators';
import { delay } from 'q';
import { elementAttribute } from '@angular/core/src/render3/instructions';

export class AppPage {

  navigateTo() {
    return browser.get('/');
  }

  goBackAPage() {
    return browser.navigate().back();
  }


  getPageTitleText() {
    // browser.sleep(3000); 
    return browser.getTitle();
  }

  sendSearchInput(searchField: string) {
    //send search input
    element(by.className('form-control')).sendKeys(searchField);

    var formFieldName = element(by.className('form-control')).getAttribute('ng-reflect-model');

    var allButtons = element.all(by.className('searchbutton'));

    
    //click search button
    allButtons.get(0).click();

    return formFieldName;
  } 

  getDisplayResultsURL() {
   
    //page goes to display results
    var address = browser.getCurrentUrl();

    return address;
  }

  testSortbyDropdown(sortingBy: number) {
    //wait for angular, then:
    //hover over sort by button
    browser.waitForAngular();

    browser.actions().mouseMove(element(by.className('dropbtn'))).perform();
    
    browser.waitForAngular();
    var sortbyList = element.all(by.className('myClickableHover'));
    
    sortbyList.get(sortingBy).click();

    var address = browser.getCurrentUrl();
    return address;
  }

  getDisplayResultsArticleName() {
    var resultsContainer = element.all(by.className('col-md-11'));
    var firstArticleContainer= resultsContainer.all(by.tagName('a'));

    return firstArticleContainer.get(0).getText();
  }

  clickDisplayResultsArticle() {
    var resultsContainer = element.all(by.className('col-md-11'));
    var firstArticleContainer= resultsContainer.all(by.tagName('a'));
    return firstArticleContainer.get(0).click();
  }

  getArticleInformationArticleName() {
    var secondContainer = element(by.id('info')); 
    var secondArticleContainer = secondContainer.all(by.tagName('h2'));
    
    return secondArticleContainer.get(0).getText();
  }

  getLastPagePosition() {
    var ngxPagination = element.all(by.className('ngx-pagination'));
    var liContainer = ngxPagination.all(by.tagName('li'));

    return liContainer.count();
  }

  goToLastPageInPagination(val) {
    var ngxPagination = element.all(by.className('ngx-pagination'));
    var liContainer = ngxPagination.all(by.tagName('li'));

    return liContainer.get(val - 2).click();
  }
}
