package cz.moneta.test.dsl.salesforce.pages.opportunities;

import cz.moneta.test.dsl.salesforce.components.PageHeader;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.sales_force.SalesForceEndpoint;
import cz.moneta.test.harness.support.web.*;

@Wait(value = SalesListPage.NEW_SALE_BUTTON, until = Until.VISIBLE)
public interface SalesListPage extends WebFlow<SalesListPage>, PageHeader, StoreAccessor {
    String NEW_SALE_BUTTON = "//button[@name='New']";
    String SALE_NEXT_BUTTON = "//div[@aria-label='Vytvořit Obchod']//span[text()='Další']";
    String SALE_PRODUCT_TYPE_SELECT = "//button[@aria-label='Charakter produktu']";
    String SALE_PRODUCT_TYPE_ITEM = SALE_PRODUCT_TYPE_SELECT + "/../..//span[text()='%s']";
    String SALE_LOAN_TYPE_SELECT = "//button[@aria-label='Typ úvěru']";
    String SALE_LOAN_TYPE_ITEM = SALE_LOAN_TYPE_SELECT + "/../..//span[text()='%s']";
    String SALE_AMOUNT_FIELD = "//input[@name='requested_amount__c']";
    String SALE_SAVE_BUTTON = "//button[@name='SaveEdit']";
    String MODAL_WINDOW = "//div[contains(@class,'uiModal')]";

    @Click(NEW_SALE_BUTTON)
    SalesListPage newSale();

    @Click(SALE_NEXT_BUTTON)
    SalesListPage saleNext();

    @CustomAction
    default SalesListPage selectCharacterProduct(String characterProduct) {
        SalesForceEndpoint endpoint = getEndpoint(SalesForceEndpoint.class);
        endpoint.click(() -> SALE_PRODUCT_TYPE_SELECT);
        endpoint.waitForElementsToLoad(4, Lookup.XPATH, Until.VISIBLE, String.format(SALE_PRODUCT_TYPE_ITEM, characterProduct));
        endpoint.click(() -> String.format(SALE_PRODUCT_TYPE_ITEM, characterProduct));
        return null;
    }

    @CustomAction
    default SalesListPage selectLoanType(String loanType) {
        SalesForceEndpoint endpoint = getEndpoint(SalesForceEndpoint.class);
        endpoint.scrollIntoView(SALE_LOAN_TYPE_SELECT, Lookup.XPATH);
        endpoint.click(() -> SALE_LOAN_TYPE_SELECT);
        endpoint.waitForElementsToLoad(4, Lookup.XPATH, Until.VISIBLE, String.format(SALE_LOAN_TYPE_ITEM, loanType));
        endpoint.click(() -> String.format(SALE_LOAN_TYPE_ITEM, loanType));
        return null;
    }

    @TypeInto(SALE_AMOUNT_FIELD)
    SalesListPage typeAmount(int amount);

    @Click(value = SALE_SAVE_BUTTON, andWait = @Wait(value = MODAL_WINDOW, until = Until.GONE))
    SalesListPage saveSale();

}
