<%#
 Copyright 2013-2022 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import { element, by<% if (!readOnly) { %>, ElementFinder<% } %> } from 'protractor';

<%_
let elementGetter = `getText()`;
if (enableTranslation) {
    elementGetter = `getAttribute('${jhiPrefix}Translate')`;
}
_%>
export class <%= entityClass %>ComponentsPage {
    <%_ if (!readOnly) { _%>
    createButton = element(by.id('jh-create-entity'));
    deleteButtons = element.all(by.css('<%= jhiPrefixDashed %>-<%= entityFileName %> div table .btn-danger'));
    <%_ } _%>
    title = element.all(by.css('<%= jhiPrefixDashed %>-<%= entityFileName %> div h2#page-heading span')).first();
    noResult = element(by.id('no-result'));
    entities = element(by.id('entities'));

    <%_ if (!readOnly) { _%>
    async clickOnCreateButton(): Promise<void> {
        await this.createButton.click();
    }

    async clickOnLastDeleteButton(): Promise<void> {
        await this.deleteButtons.last().click();
    }

    async countDeleteButtons(): Promise<number> {
        return this.deleteButtons.count();
    }
    <%_ } _%>

    async getTitle(): Promise<string> {
        return this.title.<%- elementGetter %>;
    }
}

<%_ if (!readOnly) { _%>

export class <%= entityClass %>UpdatePage {
    pageTitle = element(by.id('<%= jhiPrefixDashed %>-<%= entityFileName %>-heading'));
    saveButton = element(by.id('save-entity'));
    cancelButton = element(by.id('cancel-save'));

<%_ fields.forEach((field) => {
    const fieldName = field.fieldName;
    const fieldIsEnum = field.fieldIsEnum;
_%>
    <%_ if (fieldIsEnum) { _%>
    <%= fieldName %>Select = element(by.id('field_<%= fieldName %>'));
    <%_ } else if (field.fieldTypeBinary && field.blobContentTypeText) { _%>
    <%= fieldName %>Input = element(by.id('field_<%= fieldName %>'));
    <%_ } else if (field.fieldTypeBinary) { _%>
    <%= fieldName %>Input = element(by.id('file_<%= fieldName %>'));
    <%_ } else { _%>
    <%= fieldName %>Input = element(by.id('field_<%= fieldName %>'));
    <%_ } _%>
<%_ }); _%>

<%_ relationships.forEach((relationship) => {
    const ownerSide = relationship.ownerSide;
    const relationshipName = relationship.relationshipName;
_%>
    <%_ if (ownerSide) { _%>
    <%= relationshipName %>Select = element(by.id('field_<%= relationshipName %>'));
    <%_ } _%>
<%_ }); _%>

    async getPageTitle(): Promise<string> {
        return this.pageTitle.<%- elementGetter %>;
    }

<%_ fields.forEach((field) => {
    const fieldName = field.fieldName;
    const fieldNameCapitalized = field.fieldNameCapitalized;
    const fieldIsEnum = field.fieldIsEnum;
_%>
    <%_ if (field.fieldTypeBoolean) { _%>
    get<%= fieldNameCapitalized %>Input(): ElementFinder {
        return this.<%= fieldName %>Input;
    }

    <%_ } else if (fieldIsEnum) { _%>
    async set<%= fieldNameCapitalized %>Select(<%= fieldName %>: string): Promise<void> {
        await this.<%= fieldName %>Select.sendKeys(<%= fieldName %>);
    }

    async get<%= fieldNameCapitalized %>Select(): Promise<string> {
        return await this.<%= fieldName %>Select.element(by.css('option:checked')).getText();
    }

    async <%= fieldName %>SelectLastOption(): Promise<void> {
        await this.<%= fieldName %>Select.all(by.tagName('option')).last().click();
    }

    <%_ } else if (field.fieldTypeBinary && field.blobContentTypeText) { _%>
    async set<%= fieldNameCapitalized %>Input(<%= fieldName %>: string): Promise<void> {
        await this.<%= fieldName %>Input.sendKeys(<%= fieldName %>);
    }

    async get<%= fieldNameCapitalized %>Input(): Promise<string> {
        return await this.<%= fieldName %>Input.getAttribute('value');
    }

    <%_ } else { _%>
    async set<%= fieldNameCapitalized %>Input(<%= fieldName %>: string): Promise<void> {
        await this.<%= fieldName %>Input.sendKeys(<%= fieldName %>);
    }

    async get<%= fieldNameCapitalized %>Input(): Promise<string> {
        return await this.<%= fieldName %>Input.getAttribute('value');
    }

    <%_ } _%>
<%_ }); _%>

<%_ relationships.forEach((relationship) => {
    const ownerSide = relationship.ownerSide;
    const relationshipName = relationship.relationshipName;
    const relationshipNameCapitalized = relationship.relationshipNameCapitalized;
_%>
    <%_ if (ownerSide) { _%>
    async <%= relationshipName %>SelectLastOption(): Promise<void> {
        await this.<%= relationshipName %>Select.all(by.tagName('option')).last().click();
    }

    async <%= relationshipName %>SelectOption(option: string): Promise<void> {
        await this.<%= relationshipName %>Select.sendKeys(option);
    }

    get<%= relationshipNameCapitalized %>Select(): ElementFinder {
        return this.<%= relationshipName %>Select;
    }

    async get<%= relationshipNameCapitalized %>SelectedOption(): Promise<string> {
        return await this.<%= relationshipName %>Select.element(by.css('option:checked')).getText();
    }

    <%_ } _%>
<%_ }); _%>

    async save(): Promise<void> {
        await this.saveButton.click();
    }

    async cancel(): Promise<void> {
        await this.cancelButton.click();
    }

    getSaveButton(): ElementFinder {
        return this.saveButton;
    }
}

export class <%= entityClass %>DeleteDialog {
    private dialogTitle = element(by.id('<%= jhiPrefixDashed %>-delete-<%= entityInstance %>-heading'));
    private confirmButton = element(by.id('<%= jhiPrefixDashed %>-confirm-delete-<%= entityInstance %>'));

    async getDialogTitle(): Promise<string> {
        return this.dialogTitle.<%- elementGetter %>;
    }

    async clickOnConfirmButton(): Promise<void> {
        await this.confirmButton.click();
    }
}

<%_ } _%>
