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

import cz.moneta.test.dsl.hypos.enums.CardifType;
import cz.moneta.test.dsl.hypos.pages.HyposPageFlow;
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.HypokalkulackaEditLoanPage.SAVE_LOAN_BUTTON;

@Wait(value = SAVE_LOAN_BUTTON, until = Until.VISIBLE)
public interface HypokalkulackaEditLoanPage extends HyposPageFlow<HypokalkulackaEditLoanPage>, StoreAccessor {
    String LOAN_INPUT_DIV = "//div[@id='loan-input']";
    String LOAN_PRODUCT_SELECT = LOAN_INPUT_DIV + "//select[@id='loan-product']";
    String LOAN_ACTION_SELECT = LOAN_INPUT_DIV + "//select[@id='loan-actions']";
    String LOAN_AMOUNT_INPUT = LOAN_INPUT_DIV + "//input[@id='loan-amount']";
    String LOAN_MATURITY_INPUT = LOAN_INPUT_DIV + "//input[@id='loan-maturity-years']";
    String LOAN_OWN_RESOURCES_INPUT = LOAN_INPUT_DIV + "//input[@id='loan-own-resources']";
    String LOAN_OTHER_RESOURCES_INPUT = LOAN_INPUT_DIV + "//input[@id='loan-other-resources']";
    String TOTAL_INVESTMENT_INTENTION_INPUT = LOAN_INPUT_DIV + "//input[@id='total-investment-intention']";
    String LOAN_OBJECT_SELECT = LOAN_INPUT_DIV + "//select[@id='loan-object']";
    String REALTY_PURCHASE_REASON_SELECT = LOAN_INPUT_DIV + "//select[@id='realty-purchase-reason']";
    String LOAN_PURPOSE_TYPE_SELECT = "(" + LOAN_INPUT_DIV + "//table[@id='loan-purposes']//select[contains(@class,'purposes')])[%s]";
    String LOAN_PURPOSE_AMOUNT_INPUT = "(" + LOAN_INPUT_DIV + "//table[@id='loan-purposes']//input[contains(@class,'purpose-amount')])[%s]";
    String LOAN_CARDIF_SELECT = LOAN_INPUT_DIV + "//label[@for='loan-cardif-%s']";
    String LOAN_CARDIF_CLIENT_SELECT = LOAN_INPUT_DIV + "//select[@id='loan-cardif-client']";
    String SAVE_LOAN_BUTTON = LOAN_INPUT_DIV + "//button[@id='calc-loan-store']";

    @TypeInto(value = LOAN_AMOUNT_INPUT, clear = true)
    HypokalkulackaEditLoanPage fillLoanAmount(int loanAmount);

    @TypeInto(value = LOAN_MATURITY_INPUT, clear = true)
    HypokalkulackaEditLoanPage fillLoanMaturity(int loanMaturity);

    @TypeInto(value = LOAN_OWN_RESOURCES_INPUT, clear = true)
    HypokalkulackaEditLoanPage fillOwnResources(int amount);

    @TypeInto(value = LOAN_OTHER_RESOURCES_INPUT, clear = true)
    HypokalkulackaEditLoanPage fillOtherResources(int amount);

    @TypeInto(value = TOTAL_INVESTMENT_INTENTION_INPUT, clear = true)
    HypokalkulackaEditLoanPage fillTotalInvestmentIntention(int loanAmount);

    @Select(LOAN_PRODUCT_SELECT)
    HypokalkulackaEditLoanPage selectLoanProduct(String loanProduct);

    @Select(LOAN_ACTION_SELECT)
    HypokalkulackaEditLoanPage selectLoanAction(String loanAction);

    @Select(LOAN_OBJECT_SELECT)
    HypokalkulackaEditLoanPage selectLoanObject(String loanObject);

    @Select(REALTY_PURCHASE_REASON_SELECT)
    HypokalkulackaEditLoanPage selectRealtyPurchaseReason(String reason);

    @Click(SAVE_LOAN_BUTTON)
    HypokalkulackaPage clickSaveLoanButton();

    @CustomAction
    default HypokalkulackaEditLoanPage addLoanPurposes(HyposContract hyposContract) {
        HyposEndpoint endpoint = getEndpoint(HyposEndpoint.class);
        for (int i = 0; i < hyposContract.getLoanPurposes().size(); i++) {
            String loanPurpose = String.valueOf(i + 1);
            endpoint.selectByValue(String.format(LOAN_PURPOSE_TYPE_SELECT, loanPurpose), hyposContract.getLoanPurposes().get(i).getType());
            endpoint.type(() -> String.format(LOAN_PURPOSE_AMOUNT_INPUT, loanPurpose), hyposContract.getLoanPurposes().get(i).getAmount(), true);
        }
        return null;
    }

    @CustomAction
    default HypokalkulackaEditLoanPage addCardif(HyposContract hyposContract) {
        HyposEndpoint endpoint = getEndpoint(HyposEndpoint.class);
        hyposContract.getClients().stream()
                .filter(cardif -> !cardif.getCardifType().equals(CardifType.WITHOUT_CARDIF.getValue()))
                .forEach(client -> {
                    endpoint.click(() -> String.format(LOAN_CARDIF_SELECT, client.getCardifType()));
                    endpoint.selectByVisibleText(LOAN_CARDIF_CLIENT_SELECT, client.getNonClient().fullName());
                });

        return null;
    }
}