package cz.moneta.test.dsl.ib.loans.expressloan;

import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ib.IbEndpoint;
import cz.moneta.test.harness.support.web.Builder;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.Lookup;
import cz.moneta.test.harness.support.web.Until;
import cz.moneta.test.harness.support.web.Wait;
import cz.moneta.test.harness.support.web.Waits;
import org.junit.jupiter.api.Assertions;

import static cz.moneta.test.dsl.ib.IbPageFlow.BUSY_LOADER;
import static cz.moneta.test.dsl.ib.loans.expressloan.IbYourExpressLoanInformationComponent.HEADER_MENU;
import static cz.moneta.test.dsl.ib.loans.expressloan.IbYourExpressLoanInformationComponent.HEADER_TEXT;
import static cz.moneta.test.dsl.ib.loans.expressloan.IbYourExpressLoanInformationComponent.RE_COUNTING_BUTTON;

@Waits(value = {
        @Wait(value = HEADER_MENU, by = Lookup.ID),
        @Wait(value = HEADER_TEXT),
        @Wait(value = RE_COUNTING_BUTTON)})
public interface IbYourExpressLoanInformationComponent<T> extends Builder, StoreAccessor {
    String HEADER_MENU = "menu1div";
    String HEADER_TEXT = "//div[text() = 'Vaše půjčka']";

    String LOAN_AMOUNT_VALUE = "loanAmountTotal";
    String LOAN_TERMS_VALUE = "loanTerms";
    String INSTALLMENT_AMOUNT = "installmentAmount";
    String INTEREST_RATE = "interestRate";

    String RE_COUNTING_BUTTON = "//a[@class = 'smallPrimaryButton']";

    @Wait(value = {LOAN_AMOUNT_VALUE, LOAN_TERMS_VALUE, INSTALLMENT_AMOUNT, INTEREST_RATE}, by = Lookup.ID)
    @CustomAction
    default T verifyLoanInformation(String expectedLoanAmount, String expectedLoanTerms, String expectedInstallmentAmount, String expectedInterestRate) {
        IbEndpoint endpoint = getEndpoint(IbEndpoint.class);
        endpoint.waitForElementsToLoad(20, Until.GONE, BUSY_LOADER);
        String actualLoanAmount = endpoint.getText(LOAN_AMOUNT_VALUE, Lookup.ID).replaceAll(" ", "");
        String actualLoanTerms = endpoint.getText(LOAN_TERMS_VALUE, Lookup.ID);
        String actualInstallmentAmount = endpoint.getText(INSTALLMENT_AMOUNT, Lookup.ID);
        String actualInterestRate = endpoint.getText(INTEREST_RATE, Lookup.ID);

        Assertions.assertTrue(actualLoanAmount.contains(expectedLoanAmount), "Expected loan amount " + expectedLoanAmount + " was not equal to actual " + actualLoanAmount);
        Assertions.assertTrue(actualLoanTerms.contains(expectedLoanTerms), "Expected loan term " + expectedLoanTerms + " was not equal to actual " + actualLoanTerms);
        Assertions.assertTrue(actualInstallmentAmount.contains(expectedInstallmentAmount), "Expected installment total amount " + expectedInstallmentAmount + " was not equal to actual " + actualInstallmentAmount);
        Assertions.assertTrue(actualInterestRate.contains(expectedInterestRate), "Expected interest rate " + expectedInterestRate + " was not equal to actual " + actualInterestRate);
        return null;
    }
}
