package cz.moneta.test.dsl.ufo.banka.pages.cards.credit;

import cz.moneta.test.dsl.ufo.banka.pages.cards.debit.OBR003_S001_ClientContractSignPage;
import cz.moneta.test.dsl.ufo.banka.pages.common.pages.VER012_OBR001_008_ContractSignedPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;
import org.openqa.selenium.WebElement;

import java.util.List;

import static cz.moneta.test.dsl.ufo.banka.pages.cards.credit.OPN008_S001_SignContractPage.FIRST_SIGNER_CHECKBOX;
import static cz.moneta.test.dsl.ufo.banka.pages.cards.credit.OPN008_S001_SignContractPage.OPN008_S001_PAGE_LABEL_XPATH;

@Wait(value = OPN008_S001_PAGE_LABEL_XPATH, by = Lookup.XPATH, waitSecondsForElement = 60)
@Wait(value = FIRST_SIGNER_CHECKBOX, waitSecondsForElement = 60)
public interface OPN008_S001_SignContractPage extends WebFlow<OPN008_S001_SignContractPage>, StoreAccessor {

    String OPN008_S001_PAGE_LABEL_XPATH = "//div[text()='OPN008_S001']";
    String ALL_APPLICANT_CHECK_BOXES = "//input[contains(@id,'OPN008_S001_G001')]";
    String FIRST_SIGNER_CHECKBOX = "OPN008_S001_G001-0";
    String YES_BUTTON = "OPN008_S001_B009";
    String YES_CLIENT_BANKER_BUTTON = "OPN008_S001_B001";
    String CONTROL_BUTTON = "OPN008_S001_B005";
    String CONFIRM_BUTTON = "OPN008_S001_B006";
    String CONFIRM_BUTTON_XPATH = "//button[@id='" + CONFIRM_BUTTON + "' and not(@disabled)]";
    String APPLICATION_ID_LABEL = "OPN008_S001_L013";
    String APPLICATION_ID_KEY = "APPLICATION_ID";

    @Click(FIRST_SIGNER_CHECKBOX)
    OPN008_S001_SignContractPage clickFirstSignerCheckbox();

    @Click(YES_BUTTON)
    VER012_OBR001_008_ContractSignedPage clickYesButton();

    @Click(YES_BUTTON)
    OBR003_S001_ClientContractSignPage clickYesButtonToClientContractSignPage();

    @CustomAction
    default OPN008_S001_SignContractPage dismissPopupWindow() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.click(() -> YES_CLIENT_BANKER_BUTTON);
        endpoint.acceptAlert(25);
        return null;
    }

    @Click(value = CONTROL_BUTTON,
            andWait = @Wait(value = {CONFIRM_BUTTON}, until = Until.VISIBLE))
    OPN008_S001_SignContractPage clickControlButton();

    @Click(value = CONFIRM_BUTTON_XPATH, by = Lookup.XPATH)
    VER012_OBR001_008_ContractSignedPage clickConfirmButton();

    @StoreElementContent(storeKey = APPLICATION_ID_KEY, value = APPLICATION_ID_LABEL)
    OPN008_S001_SignContractPage storeApplicationID();

    @Click(value = CONFIRM_BUTTON_XPATH, by = Lookup.XPATH)
    OBR003_S001_ClientContractSignPage clickConfirmButtonToSignConfirmation();

    @CustomAction
    default OPN008_S001_SignContractPage clickAllSignatureCheckboxes() {
        UfoEndpoint e = getEndpoint(UfoEndpoint.class);
        List<WebElement> applicantCheckboxes = e.findAndGetListOfElements(ALL_APPLICANT_CHECK_BOXES, Lookup.XPATH);
        for (WebElement applicantCheckbox : applicantCheckboxes){
            applicantCheckbox.click();
        }
        return null;
    }
}
