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

import cz.moneta.test.dsl.ufo.banka.pages.main.PSP004_S001_PrintPage;
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 org.openqa.selenium.WebElement;

import java.util.List;
import java.util.Objects;

import static cz.moneta.test.dsl.ufo.banka.pages.loans.NEP017_S014_SelectApprovedVariantsPage.PAGE_ID;

@Wait(PAGE_ID)
public interface NEP017_S014_SelectApprovedVariantsPage extends WebFlow<NEP017_S014_SelectApprovedVariantsPage>, SharedElements, StoreAccessor {

    String PAGE_ID = "NEP017_S014_X001";
    String FIRST_VARIANT_RADIO = "NEP017_S014_G001-0";
    String SECOND_VARIANT_RADIO = "NEP017_S014_G001-1";
    String SELECT_BUTTON = "NEP017_S014_B001";
    String AMOUNT_INPUT = "NEP017_S014_E002";
    String CALCULATE_INSTALMENTS_BUTON = "NEP017_S014_B005";
    String TERM_RADIO_XPATH = "//table[@id='NEP017_S014_G002_UfoTable']//td[contains(@class, 'ufo-Table-BodyCell') and text() = '%s']";
    String FIXED_VARIANT_RADIO_XPATH = "//table[@id='NEP017_S014_G001_UfoTable']//td[2][not(text()=' ')]";
    String CONTINUE_BUTTON = "NEP017_S014_B001";
    String CREDIT_TR = "//table[@id='NEP017_S014_G001_UfoTable']/tbody//tr";
    String CHOSEN_LOAN_OPTION_STORE_KEY = "CHOSEN_LOAN_OPTION";

    @Click(SELECT_BUTTON)
    NEP017_S014_SelectApprovedVariantsPageTransitions clickSelect();

    @Click(value = FIRST_VARIANT_RADIO, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    NEP017_S014_SelectApprovedVariantsPage clickFirstVariant();

    @Click(value = SECOND_VARIANT_RADIO, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    NEP017_S014_SelectApprovedVariantsPage clickSecondVariant();

    @TypeInto(AMOUNT_INPUT)
    NEP017_S014_SelectApprovedVariantsPage fillAmount(int amount);

    @CustomAction
    default NEP017_S014_SelectApprovedVariantsPage fillAmountIfVisibleAndChooseInstallments(int amount) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        if (endpoint.isElementVisible(AMOUNT_INPUT)) {
            endpoint.type(() -> AMOUNT_INPUT, String.valueOf(amount), true);
            endpoint.click(() -> CALCULATE_INSTALMENTS_BUTON);
            endpoint.waitForElementsToLoad(20, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        }
        return null;
    }

    @Click(value = CALCULATE_INSTALMENTS_BUTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    NEP017_S014_SelectApprovedVariantsPage clickCalculateInstalments();

    @CustomAction
    default NEP017_S014_SelectApprovedVariantsPage clickTermRadio(int term) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String xpath = String.format(TERM_RADIO_XPATH, term);
        endpoint.click(() -> xpath, Lookup.XPATH);
        endpoint.waitForElementsToLoad(150, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        return null;
    }

    @CustomAction
    default NEP017_S014_SelectApprovedVariantsPage clickFixedVariant() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.jsClick(() -> String.format(FIXED_VARIANT_RADIO_XPATH), Lookup.XPATH);
        endpoint.waitForElementsToLoad(150, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        return null;
    }

    @Click(CONTINUE_BUTTON)
    PSP004_S001_PrintPage clickContinue();

    @CustomAction
    default NEP017_S014_SelectApprovedVariantsPage selectRequiredOrFirstVariation(int clientRequiredAmount, int clientRequiredTerm) {
        UfoEndpoint e = getEndpoint(UfoEndpoint.class);
        List<WebElement> credits = e.findAndGetListOfElements(CREDIT_TR, Lookup.XPATH);
        for (int i = credits.size(); i > 0; i--) {
            String CREDIT_TR = "//table[@id='NEP017_S014_G001_UfoTable']/tbody//tr[%s]/td[%s]";

            String amount = e.getText(String.format(CREDIT_TR, i, 2), Lookup.XPATH).replaceAll("[\\s\\u00A0]+", "");
            String term = e.getText(String.format(CREDIT_TR, i, 5), Lookup.XPATH);

            boolean sameAsRequiredVariation = Objects.equals(amount, String.valueOf(clientRequiredAmount)) && Objects.equals(term, String.valueOf(clientRequiredTerm));
            if (sameAsRequiredVariation || i == 1) {
                credits.get(i - 1).click();
                e.waitForElementsToLoad(10, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
                store(CHOSEN_LOAN_OPTION_STORE_KEY, String.format("Chosen option: amount: %s, term: %s", amount, term));
                break;
            }
        }
        return null;
    }
}