package cz.moneta.test.dsl.ufo.banka.pages.products.sme;

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

import java.util.Random;

import static cz.moneta.test.dsl.ufo.operations.shared.SharedElements.LOAD_IMG_DIV_CLASS;

@Wait(value = KUL043_S001_ChangeSelectedParametersPage.KUL043_S001_PAGE_LABEL_XPATH, by = Lookup.XPATH, waitSecondsForElement = 60)
public interface KUL043_S001_ChangeSelectedParametersPage extends WebFlow<KUL043_S001_ChangeSelectedParametersPage>, StoreAccessor {

    String KUL043_S001_PAGE_LABEL_XPATH = "//div[text()='KUL043_S001']";
    String INTEREST_RATE_KEY = "INTEREST_RATE";
    String PAYMENTS_COUNT_KEY = "PAYMENTS_COUNT";
    String LIMITS_CREDIT_KEY = "LIMITS_CREDIT";
    String ANNUITY_PAYMENT_KEY = "ANNUITY_PAYMENT";
    String PAYMENT_TYPE_ANUITY_RADIO = "KUL043_S001_R001_2";
    String PAYMENT_TYPE_LINEAR_RADIO = "KUL043_S001_R001_1";
    String INTEREST_RATE_TYPE_SELECT = "KUL043_S001_C001";
    String MONTHLY_CHARGES_SELECT = "KUL043_S001_C003";
    String INSURANCE_YES_RADIO = "KUL043_S001_R002_1";
    String INSURANCE_NO_RADIO = "KUL043_S001_R002_0";
    String MAXIMAL_INTEREST_RATE_INPUT = "KUL043_S001_E004";
    String MINIMAL_INTEREST_RATE_INPUT = "KUL043_S001_E002";
    String SUGGESTED_SUMMARY_INTEREST_RATE_INPUT = "KUL043_S001_E001";
    String BENEFIT_FROM_CAMPAIGN_SELECT = "KUL043_S001_C002";
    String EARLY_REPAYMENT_CHARGE_SELECT = "KUL043_S001_C006";
    String EARLY_REPAYMENT_CHARGE_FIRST_VALID_OPTION_XPATH = "//select[@id='KUL043_S001_C006']/option[not(text()='(není)')][1]";
    String REPAY_INSURANCE_CHARGE_NO_RADIO = "KUL043_S001_R002_0";
    String RECALCULATE_BUTTON = "KUL043_S001_B001";
    String PAYMENTS_CALCULATOR_BUTTON = "KUL043_S001_B005";
    String PRINT_OFFER_BUTTON = "KUL043_S001_B006";
    String CONTINUE_BUTTON = "KUL043_S001_B003";
    String LOADER_DIV_XPATH = "//div[@class=\"gwt-PopupPanel\"]";
    String LIMITS_CREDIT_XPATH = "//table[@id='KUL043_S001_G002_UfoTable']//td[1]";
    String PAYMENT_COUNT_XPATH = "//table[@id='KUL043_S001_G002_UfoTable']//td[2]";
    String ANNUITY_PAYMENT_XPATH = "//table[@id='KUL043_S001_G002_UfoTable']//td[7]";

    @StoreElementContent(storeKey = LIMITS_CREDIT_KEY, value = LIMITS_CREDIT_XPATH, by = Lookup.XPATH)
    KUL043_S001_ChangeSelectedParametersPage storeLimitsCredit();

    @StoreElementContent(storeKey = PAYMENTS_COUNT_KEY, value = PAYMENT_COUNT_XPATH, by = Lookup.XPATH)
    KUL043_S001_ChangeSelectedParametersPage storePaymentsCount();

    @StoreElementContent(storeKey = ANNUITY_PAYMENT_KEY, value = ANNUITY_PAYMENT_XPATH, by = Lookup.XPATH)
    KUL043_S001_ChangeSelectedParametersPage storeAnnuityPayment();

    @Click(value = PAYMENT_TYPE_ANUITY_RADIO, jsClick = true)
    KUL043_S001_ChangeSelectedParametersPage clickPaymentTypeAnuity();

    @Click(value = PAYMENT_TYPE_LINEAR_RADIO, jsClick = true)
    KUL043_S001_ChangeSelectedParametersPage clickPaymentTypeLinear();

    @Select(INTEREST_RATE_TYPE_SELECT)
    KUL043_S001_ChangeSelectedParametersPage selectInterestRateType(String interestRateType);

    @Select(MONTHLY_CHARGES_SELECT)
    KUL043_S001_ChangeSelectedParametersPage selectMonthlyCharges(String monthlyCharges);

    @Click(INSURANCE_YES_RADIO)
    KUL043_S001_ChangeSelectedParametersPage clickInsuranceYes();

    @Click(INSURANCE_NO_RADIO)
    KUL043_S001_ChangeSelectedParametersPage clickInsuranceNo();

    @CustomAction
    default KUL043_S001_ChangeSelectedParametersPage fillSuggestedSummaryInterestRate(Harness harness) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        float maxInterestRateF = Float.parseFloat(endpoint.getAttribute(MAXIMAL_INTEREST_RATE_INPUT, Lookup.DEFAULT, "value").replace(',', '.'));
        float minInterestRateF = Float.parseFloat(endpoint.getAttribute(MINIMAL_INTEREST_RATE_INPUT, Lookup.DEFAULT, "value").replace(',', '.'));
        Random r = new Random();
        float randomInterestRateF = minInterestRateF + r.nextFloat() * (maxInterestRateF - minInterestRateF);
        String randomInterestRate = String.format("%.2f", randomInterestRateF).replace('.', ',');
        harness.store(INTEREST_RATE_KEY, randomInterestRate);
        endpoint.type(() -> SUGGESTED_SUMMARY_INTEREST_RATE_INPUT, randomInterestRate, true);
        return null;
    }

    @Select(BENEFIT_FROM_CAMPAIGN_SELECT)
    KUL043_S001_ChangeSelectedParametersPage selectBenefitFromCampaign(String benefitFromCampaign);

    @Select(EARLY_REPAYMENT_CHARGE_SELECT)
    KUL043_S001_ChangeSelectedParametersPage selectEarlyRepaymentCharge(String earlyRepaymentCharge);

    @CustomAction
    default KUL043_S001_ChangeSelectedParametersPage selectFirstValidRepaymentCharge() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String firstValidValue = endpoint.getText(EARLY_REPAYMENT_CHARGE_FIRST_VALID_OPTION_XPATH, Lookup.XPATH);
        endpoint.selectByVisibleText(EARLY_REPAYMENT_CHARGE_SELECT, firstValidValue);
        return null;
    }

    @Click(value = REPAY_INSURANCE_CHARGE_NO_RADIO, jsClick = true, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    KUL043_S001_ChangeSelectedParametersPage clickRepayInsuranceChargeNo();

    @Click(value = RECALCULATE_BUTTON, andWait = @Wait(value = LOADER_DIV_XPATH, by = Lookup.XPATH, until = Until.GONE))
    KUL043_S001_ChangeSelectedParametersPage clickRecalculate();

    @Click(PAYMENTS_CALCULATOR_BUTTON)
    KUL043_S001_ChangeSelectedParametersPage clickPaymentsCalculator();

    @Click(PRINT_OFFER_BUTTON)
    KUL043_S001_ChangeSelectedParametersPage clickPrintOffer();

    @Click(CONTINUE_BUTTON)
    SMA002_S001_SMEAutomatsApplicationOverview clickContinue();
}
