package cz.moneta.test.dsl.brokerportal.contract.shortapplication;

import cz.moneta.test.dsl.brokerportal.enums.Client;
import cz.moneta.test.dsl.brokerportal.enums.Household;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.hypos.BrokerPortalEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.brokerportal.contract.shortapplication.ShortApplicationPage.APPLICATION_CONFIRM_BUTTON;

@Wait(APPLICATION_CONFIRM_BUTTON)
public interface ShortApplicationPage extends WebFlow<ShortApplicationPage>, StoreAccessor {
    String PERSONAL_ID_INPUT = "//input[@name='households[%2$s].persons[%1$s].personalId']";
    String FIRST_IDENTITY_VALUE_INPUT = "//input[@name='households[%2$s].persons[%1$s].identities[0].value']";
    String FIRST_IDENTITY_EXPIRATION_DATE_INPUT = "//input[@name='households[%2$s].persons[%1$s].identities[0].expirationDate']";
    String EMAIL_INPUT = "//input[@name='households[%2$s].persons[%1$s].email']";
    String PHONE_INPUT = "//input[@name='households[%2$s].persons[%1$s].phone']";
    String CHECK_IDENTITY_CHECKBOX = "//label[@id='households[%2$s].persons[%1$s].szrAgreement']";
    String CHECK_IDENTITY_BUTTON = "//button[@id='households[%2$s].persons[%1$s].CallSzrButton']";
    String CHECK_IDENTITY_OK = CHECK_IDENTITY_BUTTON + "[text()='Totožnost ověřena']";
    String APPLICATION_CONFIRM_BUTTON = "//button[@id='applicationConfirm' and not(@disabled)]";

    @CustomAction
    default ShortApplicationPage fillPersonalId(String personalId, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(PERSONAL_ID_INPUT, client.getInHouseholdValue(), household.getValue()), personalId, false);
        return null;
    }

    @CustomAction
    default ShortApplicationPage fillFirstIdentityValue(String identityCardId, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(FIRST_IDENTITY_VALUE_INPUT, client.getInHouseholdValue(), household.getValue()), identityCardId, false);
        return null;
    }

    @CustomAction
    default ShortApplicationPage fillFirstIdentityExpirationDate(String date, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(FIRST_IDENTITY_EXPIRATION_DATE_INPUT, client.getInHouseholdValue(), household.getValue()), date, false);
        return null;
    }

    @CustomAction
    default ShortApplicationPage fillEmail(String email, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(EMAIL_INPUT, client.getInHouseholdValue(), household.getValue()), email, false);
        endpoint.click(() -> String.format(PHONE_INPUT, client.getInHouseholdValue(), household.getValue()));
        endpoint.waitForElementsToLoad(10, APPLICATION_CONFIRM_BUTTON);
        return null;
    }

    @CustomAction
    default ShortApplicationPage fillPhone(String phone, Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.type(() -> String.format(PHONE_INPUT, client.getInHouseholdValue(), household.getValue()), phone, false);
        return null;
    }

    @CustomAction
    default ShortApplicationPage clickCheckIdentityCheckbox(Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.click(() -> String.format(CHECK_IDENTITY_CHECKBOX, client.getInHouseholdValue(), household.getValue()));
        return null;
    }

    @CustomAction
    default ShortApplicationPage clickCheckIdentityButton(Client client, Household household) {
        BrokerPortalEndpoint endpoint = getEndpoint(BrokerPortalEndpoint.class);
        endpoint.click(() -> String.format(CHECK_IDENTITY_BUTTON, client.getInHouseholdValue(), household.getValue()));
        endpoint.waitForElementsToLoad(10, String.format(CHECK_IDENTITY_OK, client.getInHouseholdValue(), household.getValue()));
        return null;
    }

    @Click(APPLICATION_CONFIRM_BUTTON)
    ShortApplicationSummaryPage clickApplicationConfirmButton();
}