package cz.moneta.test.dsl.hypos.pages.hypokalkulacka;

import cz.moneta.test.dsl.hypos.enums.YesNo;
import cz.moneta.test.dsl.hypos.pages.HyposPageFlow;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.dsl.util.data.hypos.HyposContract;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.hypos.HyposEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.hypos.pages.hypokalkulacka.HypokalkulackaLoanApplicationPage.SAVE_APPLICATION_FORM_BUTTON;
import static cz.moneta.test.dsl.hypos.tasks.HyposTasks.defaultDateFormat;

@Wait(value = SAVE_APPLICATION_FORM_BUTTON, until = Until.VISIBLE)
public interface HypokalkulackaLoanApplicationPage extends HyposPageFlow<HypokalkulackaLoanApplicationPage>, StoreAccessor {
    String APPLICATION_FORM_DIV = "//div[@id='application-form']";
    String BRANCH_SELECT = APPLICATION_FORM_DIV + "//select[@id='appl-branch-la']";
    String BIRTH_PLACE_INPUT = APPLICATION_FORM_DIV + "//input[@id='appl-natal-place-%s']";
    String CLIENT_ID_PAPERS_LINK = APPLICATION_FORM_DIV + "//a[@id='appl-identity%1$s-link-%2$s']";
    String PEP_CHECKBOX = APPLICATION_FORM_DIV + "//input[@name='is_polliticaly_exposed-%1$s' and @value=%2$s]";
    String PEP_REASON = APPLICATION_FORM_DIV + "//textarea[@id='appl-pol-exp-reason-%1$s']";
    String SAVE_APPLICATION_FORM_BUTTON = APPLICATION_FORM_DIV + "//button[text()='Uložit']";

    String IDENTITY_INPUT_DIV = "//div[@id='identity-form']";
    String IDENTITY_TYPE_SELECT = IDENTITY_INPUT_DIV + "//select[@id='ident-type']";
    String IDENTITY_NUMBER_INPUT = IDENTITY_INPUT_DIV + "//input[@id='ident-value']";
    String IDENTITY_EXPIRY_DATE_INPUT = IDENTITY_INPUT_DIV + "//input[@id='ident-expiry']";
    String IDENTITY_SAVE_BUTTON = IDENTITY_INPUT_DIV + "//button[text()='Uložit']";
    String IDENTITY_LOADER_DIV = "//div[@class='loading-overlay']";

    @Select(BRANCH_SELECT)
    HypokalkulackaLoanApplicationPage selectContactBranch(String branch);

    @Click(SAVE_APPLICATION_FORM_BUTTON)
    HypokalkulackaPage clickSaveApplicationButton();

    @CustomAction
    default HypokalkulackaLoanApplicationPage addBirthPlace(HyposContract hyposContract) {
        HyposEndpoint endpoint = getEndpoint(HyposEndpoint.class);
        for (int i = 0; i < hyposContract.getClients().size(); i++) {
            String client = String.valueOf(i + 1);
            endpoint.type(() -> String.format(BIRTH_PLACE_INPUT, client), hyposContract.getClients().get(i).getNonClient().placeOfBirth(), false);
        }
        return null;
    }

    @CustomAction
    default HypokalkulackaLoanApplicationPage selectPep(HyposContract hyposContract) {
        HyposEndpoint endpoint = getEndpoint(HyposEndpoint.class);
        for (int i = 0; i < hyposContract.getClients().size(); i++) {
            String client = String.valueOf(i + 1);
            String pep = hyposContract.getClients().get(i).getPep();
            endpoint.click(() -> String.format(PEP_CHECKBOX, client, pep));
            if (pep.equals(YesNo.YES.getValue())) {
                endpoint.type(() -> String.format(PEP_REASON, client), "Test PEP", false);
            }
        }
        return null;
    }

    @CustomAction
    default HypokalkulackaLoanApplicationPage addFirstIdentity(HyposContract hyposContract) {
        HyposEndpoint endpoint = getEndpoint(HyposEndpoint.class);
        for (int i = 0; i < hyposContract.getClients().size(); i++) {
            String client = String.valueOf(i + 1);
            endpoint.click(() -> String.format(CLIENT_ID_PAPERS_LINK, 1, client));
            endpoint.selectByVisibleText(IDENTITY_TYPE_SELECT, hyposContract.getClients().get(i).getFirstIdentityType());
            endpoint.waitForElementsToLoad(20, Until.GONE, IDENTITY_LOADER_DIV);
            endpoint.type(() -> IDENTITY_NUMBER_INPUT, hyposContract.getClients().get(i).getNonClient().idCardNumber(), false);
            endpoint.waitForElementsToLoad(20, Until.GONE, IDENTITY_LOADER_DIV);
            endpoint.type(() -> IDENTITY_EXPIRY_DATE_INPUT, DateUtils.getDateWithFormat(hyposContract.getClients().get(i).getNonClient().idCardExpiredRaw(), defaultDateFormat), false);
            endpoint.waitForElementsToLoad(20, Until.GONE, IDENTITY_LOADER_DIV);
            endpoint.waitForElementsToLoad(10, Until.VISIBLE, IDENTITY_SAVE_BUTTON);
            endpoint.click(() -> IDENTITY_SAVE_BUTTON);
        }
        return null;
    }

    @CustomAction
    default HypokalkulackaLoanApplicationPage addSecondIdentity(HyposContract hyposContract) {
        HyposEndpoint endpoint = getEndpoint(HyposEndpoint.class);
        for (int i = 0; i < hyposContract.getClients().size(); i++) {
            String client = String.valueOf(i + 1);
            endpoint.click(() -> String.format(CLIENT_ID_PAPERS_LINK, 2, client));
            endpoint.selectByVisibleText(IDENTITY_TYPE_SELECT, hyposContract.getClients().get(i).getSecondIdentityType());
            endpoint.waitForElementsToLoad(20, Until.GONE, IDENTITY_LOADER_DIV);
            endpoint.type(() -> IDENTITY_NUMBER_INPUT, hyposContract.getClients().get(i).getNonClient().idCardNumber(), false);
            endpoint.waitForElementsToLoad(20, Until.GONE, IDENTITY_LOADER_DIV);
            endpoint.type(() -> IDENTITY_EXPIRY_DATE_INPUT, DateUtils.getDateWithFormat(hyposContract.getClients().get(i).getNonClient().idCardExpiredRaw(), defaultDateFormat), false);
            endpoint.waitForElementsToLoad(20, Until.GONE, IDENTITY_LOADER_DIV);
            endpoint.waitForElementsToLoad(10, Until.VISIBLE, IDENTITY_SAVE_BUTTON);
            endpoint.click(() -> IDENTITY_SAVE_BUTTON);
        }
        return null;
    }
}