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

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

import java.util.List;

@Wait(value = {CreateNewOpportunityPage.CREATE_BUTTON}, until = Until.VISIBLE)
public interface CreateNewOpportunityPage extends WebFlow<CreateNewOpportunityPage>, PageHeader, StoreAccessor {
    String CREATE_BUTTON = "//button[text()='Vytvořit']";
    String NEXT_BUTTON = "//input[@value='Následující']";
    String OPPORTUNITY_INITIATOR_SELECT = "//select[@name='initiated_by']";
    String OPPORTUNITY_CATEGORY_SELECT = "//span[text()='Kategorie příležitosti']/../following-sibling::div/div/select";
    String OPPORTUNITY_SOURCE_SELECT = "//select[contains(@name,'Zdroj_prilezitosti')]";
    String OPPORTUNITY_BRANCH_INPUT = "//label[text()='Obchodní místo']/following-sibling::div/input";
    String OPPORTUNITY_BRANCH_SELECT = "//label[text()='Obchodní místo']/../../../..//lightning-formatted-text";
    String OPPORTUNITY_AMOUNT_INPUT = "//label[text()='Částka']/following-sibling::div/input";
    String OPPORTUNITY_CURRENCY_SELECT = "//span[text()='Měna']/../following-sibling::div/div/select";
    String OPPORTUNITY_SIGNATURE_DATE_INPUT = "//input[contains(@name,'Close_date_input')]";
    String OPPORTUNITY_SIGNATURE_DATE_TODAY_LINK = OPPORTUNITY_SIGNATURE_DATE_INPUT + "//following-sibling::span/a";
    String OPPORTUNITY_PIPELINE_SELECT = "//select[contains(@name,'Stav_v_pipeline_input')]";
    String OPPORTUNITY_PROBABILITY_INPUT = "//label[text()='Pravděpodobnost (%)']/following-sibling::div/input";
    String OPPORTUNITY_DESCRIPTION_TEXTAREA = "//textarea[contains(@name,'Description_input')]";
    String OPPORTUNITY_PRODUCT_POTENTIAL_CHOICE = "//div[@data-value='%s']";

    CreateNewOpportunityPage onNewOpportunityPage();

    @Select(OPPORTUNITY_INITIATOR_SELECT)
    CreateNewOpportunityPage selectOpportunityInitiator(String initiator);

    @Select(OPPORTUNITY_CATEGORY_SELECT)
    CreateNewOpportunityPage selectOpportunityCategory(String category);

    @Select(OPPORTUNITY_SOURCE_SELECT)
    CreateNewOpportunityPage selectOpportunitySource(String source);

    @TypeInto(OPPORTUNITY_BRANCH_INPUT)
    CreateNewOpportunityPage inputOpportunityBranch(String branch);

    @Click(value = OPPORTUNITY_BRANCH_SELECT)
    CreateNewOpportunityPage selectOpportunityBranch();

    @Select(OPPORTUNITY_PIPELINE_SELECT)
    CreateNewOpportunityPage selectOpportunityPipeline(String pipeline);

    @Click(OPPORTUNITY_SIGNATURE_DATE_TODAY_LINK)
    CreateNewOpportunityPage clickOportunityTodaysDate();

    @TypeInto(OPPORTUNITY_SIGNATURE_DATE_INPUT)
    CreateNewOpportunityPage fillOpportunitySignatureDate(String date);

    @TypeInto(OPPORTUNITY_AMOUNT_INPUT)
    CreateNewOpportunityPage fillOpportunityAmount(int amount);

    @Select(OPPORTUNITY_CURRENCY_SELECT)
    CreateNewOpportunityPage selectOpportunityCurrency(String currency);

    @TypeInto(OPPORTUNITY_PROBABILITY_INPUT)
    CreateNewOpportunityPage fillOpportunityProbability(int probability);

    @Click(OPPORTUNITY_DESCRIPTION_TEXTAREA)
    @TypeInto(OPPORTUNITY_DESCRIPTION_TEXTAREA)
    CreateNewOpportunityPage fillOpportunityDescription(String description);

    @CustomAction
    default CreateNewOpportunityPage selectProductPotentials(List<ProductPotential> productPotentialsList) {
        SalesForceEndpoint endpoint = getEndpoint(SalesForceEndpoint.class);
        productPotentialsList.forEach(productPotential -> {
            endpoint.scrollIntoView(String.format(OPPORTUNITY_PRODUCT_POTENTIAL_CHOICE, productPotential.getXpathValue()));
            endpoint.click(() -> String.format(OPPORTUNITY_PRODUCT_POTENTIAL_CHOICE, productPotential.getXpathValue()));
        });
        return null;
    }

    @Click(NEXT_BUTTON)
    NewOpportunityConfirmationPage clickNext();

    @Click(CREATE_BUTTON)
    OpportunityDetailPage clickCreate();

}
