import { element, by, browser, $, ElementFinder, ExpectedConditions as until } from 'protractor';


export class ManageTriggerViewObject {

  public sections: {[name: string]: ElementFinder} = {
    namespace:          $('linear-workflow-section[section-id=namespace]'),
    githostrepo:        $('linear-workflow-section[section-id=repo][section-title="Select Repository"]'),
    customrepo:         $('linear-workflow-section[section-id=repo][section-title="Git Repository"]'),
    triggeroptions:     $('linear-workflow-section[section-id=triggeroptions]'),
    dockerfilelocation: $('linear-workflow-section[section-id=dockerfilelocation]'),
    contextlocation:    $('linear-workflow-section[section-id=contextlocation]'),
    robot:              $('linear-workflow-section[section-id=robot]'),
    verification:       $('linear-workflow-section[section-id=verification]'),
  };

  private customGitRepoInput: ElementFinder = element(by.model('$ctrl.buildSource'));
  private dockerfileLocationInput: ElementFinder = this.sections['dockerfilelocation'].$('input');
  private dockerfileLocationDropdownButton: ElementFinder = this.sections['dockerfilelocation'].$('button[data-toggle=dropdown');
  private dockerContextInput: ElementFinder = this.sections['contextlocation'].$('input');
  private dockerContextDropdownButton: ElementFinder = this.sections['contextlocation'].$('button[data-toggle=dropdown');
  private robotAccountOptions: ElementFinder = this.sections['robot'].element(by.repeater('$ctrl.orderedData.visibleEntries'));

  public continue(): Promise<void> {
    return Promise.resolve(element(by.buttonText('Continue')).click());
  }

  public enterRepositoryURL(url: string): Promise<void> {
    browser.wait(until.presenceOf(this.customGitRepoInput));
    this.customGitRepoInput.clear();

    return Promise.resolve(this.customGitRepoInput.sendKeys(url));
  }

  public enterDockerfileLocation(path: string): Promise<void> {
    browser.wait(until.presenceOf(this.dockerfileLocationInput));
    this.dockerfileLocationInput.clear();

    return Promise.resolve(this.dockerfileLocationInput.sendKeys(path));
  }

  public getDockerfileSuggestions(): Promise<string[]> {
    return Promise.resolve(this.dockerfileLocationDropdownButton.click())
      .then(() => element.all(by.repeater('$ctrl.paths')).map(result => result.getText()));
  }

  public enterDockerContext(path: string): Promise<void> {
    browser.wait(until.presenceOf(this.dockerContextInput));
    this.dockerContextInput.clear();

    return Promise.resolve(this.dockerContextInput.sendKeys(path));
  }

  public getDockerContextSuggestions(): Promise<string[]> {
    return Promise.resolve(this.dockerContextDropdownButton.click())
      .then(() => element.all(by.repeater('$ctrl.contexts')).map(result => result.getText()));
  }

  public selectRobotAccount(index: number): Promise<void> {
    return Promise.resolve(element.all(by.css('input[type=radio]')).get(index).click());
  }
}