package cz.moneta.test.dsl.ufo.banka.pages.loans.expres;

import cz.moneta.test.dsl.ufo.operations.shared.SharedElements;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.ufo.banka.pages.loans.expres.NEP001_S001_NewExpressPage.CONFIRM_BUTTON;
import static cz.moneta.test.dsl.ufo.banka.pages.loans.expres.NEP001_S001_NewExpressPage.NEP001_S001_PAGE_LABEL_XPATH;

@Wait(value = NEP001_S001_PAGE_LABEL_XPATH, by = Lookup.XPATH)
@Wait(CONFIRM_BUTTON)
@Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE)
public interface NEP001_S001_NewExpressPage extends WebFlow<NEP001_S001_NewExpressPage>, SharedElements, StoreAccessor {

    String NEP001_S001_PAGE_LABEL_XPATH = "//div[text()='NEP001_S001']";
    String CONFIRM_BUTTON = "NEP001_S001_B002";
    String AMOUNT_INPUT = "NEP001_S001_E006";
    String AMOUNT_DIV = "NEP001_S001_L009";
    String NUMBER_OF_INSTALLMENTS_SELECT = "NEP001_S001_C003";
    String TERM_OPTION_XPATH = "//select[@id='NEP001_S001_C003']/option[@value='%s']";
    String CUSTOM_TERM_INPUT = "NEP001_S001_E019";
    String CALCULATE_INSTALLMENT_BUTTON = "NEP001_S001_B004";
    String CONTINUE_BUTTON = "NEP001_S001_B002";
    String FIRST_COAPPLICANT_RELATIONSHIP_SELECT = "NEP001_S001_C005";
    String SECOND_COAPPLICANT_RELATIONSHIP_SELECT = "NEP001_S001_C009";
    String NO_EXTERNAL_PARTNER_CHECKBOX_XPATH = "NEP001_S001_H002_1";
    String HOUSING_LOAN_CHECKBOX = "NEP001_S001_H005";
    String REALTY_TYPE_INPUT = "NEP001_S001_C002";
    String OWNERSHIP_TYPE_INPUT = "NEP001_S001_C004";
    String LOAN_PURPOSE_INPUT = "NEP001_S001_C001";
    String FIXATION_INPUT = "NEP001_S001_C006";
    String FIRST_COAPPLICANT_LABEL = "//label[@for='NEP001_S001_H003_1']";
    String SECOND_COAPPLICANT_LABEL = "//label[@for='NEP001_S001_H004_1']";
    String FILL_FIRST_COAPPLICANT_BUTTON = "NEP001_S001_B005";
    String FILL_SECOND_COAPPLICANT_BUTTON = "NEP001_S001_B006";
    String EXPEL_FIRST_CO_APPLICANT_BUTTON = "NEP001_S001_B007";

    @Select(FIRST_COAPPLICANT_RELATIONSHIP_SELECT)
    NEP001_S001_NewExpressPage selectFirstCoApplicantRelationshipToMainApplicant(String relationship);

    @Select(SECOND_COAPPLICANT_RELATIONSHIP_SELECT)
    NEP001_S001_NewExpressPage selectSecondCoApplicantRelationshipToMainApplicant(String relationship);

    @TypeInto(value = AMOUNT_INPUT, clear = true) //TODO remove clear after fix
    @Click(value = AMOUNT_DIV, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    NEP001_S001_NewExpressPage fillAmount(int amount);

    @CustomAction
    default NEP001_S001_NewExpressPage selectTerm(int term) {
        UfoEndpoint e = getEndpoint(UfoEndpoint.class);
        e.click(() -> NUMBER_OF_INSTALLMENTS_SELECT);
        if (e.isElementVisible(0, String.format(TERM_OPTION_XPATH, term), Lookup.XPATH)) {
            e.click(() -> String.format(TERM_OPTION_XPATH, term), Lookup.XPATH);
            e.waitForElementsToLoad(5, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        } else {
            e.click(() -> String.format(TERM_OPTION_XPATH, 0), Lookup.XPATH);
            e.type(() -> CUSTOM_TERM_INPUT, String.valueOf(term), false);
            e.click(() -> AMOUNT_INPUT);
            e.waitForElementsToLoad(5, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        }
        return null;
    }

    @Click(NUMBER_OF_INSTALLMENTS_SELECT)
    NEP001_S001_NewExpressPage clickNumberOfInstallments();

    @Select(NUMBER_OF_INSTALLMENTS_SELECT)
    @Click(value = NUMBER_OF_INSTALLMENTS_SELECT, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    NEP001_S001_NewExpressPage selectNumberOfInstallments(int number);

    @Click(value = CALCULATE_INSTALLMENT_BUTTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    NEP001_S001_NewExpressPage clickCalculateInstallment();

    @Click(value = CONTINUE_BUTTON, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    NEP001_S001_NewExpressPageTransitions clickContinue();

    @Click(value = NO_EXTERNAL_PARTNER_CHECKBOX_XPATH, jsClick = true)
    NEP001_S001_NewExpressPage clickNoExternalPartnerCheckbox();

    @Click(value = HOUSING_LOAN_CHECKBOX, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    NEP001_S001_NewExpressPage clickHousingLoanCheckbox();

    @Select(value = REALTY_TYPE_INPUT, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    NEP001_S001_NewExpressPage selectRealityType(String realityType);

    @Select(value = OWNERSHIP_TYPE_INPUT, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    NEP001_S001_NewExpressPage selectOwnershipType(String ownershipType);

    @Select(value = FIXATION_INPUT, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    NEP001_S001_NewExpressPage selectFixation(String fixation);

    @Select(LOAN_PURPOSE_INPUT)
    NEP001_S001_NewExpressPage selectLoanPurposeType(String loanPurpose);

    //label because checkbox input has height and width 0px
    @Click(value = FIRST_COAPPLICANT_LABEL, by = Lookup.XPATH)
    NEP001_S001_NewExpressPage clickFirstCoApplicant();

    //label because checkbox input has height and width 0px
    @Click(value = SECOND_COAPPLICANT_LABEL, by = Lookup.XPATH)
    NEP001_S001_NewExpressPage clickSecondCoApplicant();

    @Click(FILL_FIRST_COAPPLICANT_BUTTON)
    HLE001_S001_CoApplicantSearchPage clickFillFirstCoApplicant();

    @Click(FILL_SECOND_COAPPLICANT_BUTTON)
    HLE001_S001_CoApplicantSearchPage clickFillSecondCoApplicant();

    @Click(value = EXPEL_FIRST_CO_APPLICANT_BUTTON, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    NEP001_S001_NewExpressPage clickExpelCoApplicant();

}