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

import cz.moneta.test.dsl.salesforce.components.PageHeader;
import cz.moneta.test.dsl.salesforce.pages.MainPage;
import cz.moneta.test.dsl.salesforce.pages.opportunities.CreateNewOpportunityPage;
import cz.moneta.test.dsl.salesforce.pages.opportunities.OpportunitiesListPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.sales_force.SalesForceEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.salesforce.pages.accounts.AccountDetailPage.ACCOUNT_LOCK_BUTTON;
import static cz.moneta.test.dsl.salesforce.pages.accounts.AccountDetailPage.ACCOUNT_UNLOCK_BUTTON;

@Wait(value = {ACCOUNT_LOCK_BUTTON, ACCOUNT_UNLOCK_BUTTON}, until = Until.VISIBLE)
public interface AccountDetailPage extends WebFlow<AccountDetailPage>, StoreAccessor, PageHeader {
    String ACCOUNT_LOCK_BUTTON = "//button[@name='Account.Zamknout_odemcene']";
    String ACCOUNT_UNLOCK_BUTTON = "//button[@name='Account.Unlock']";
    String NEW_OPPORTUNITY_BUTTON = "//button[text()='Nová SME přiležitost']";
    String OPPORTUNITIES_BUTTON = "//div[contains(@class,'active')]//button[contains(@title,'Příležitosti')]/../../a";
    String PRICING_REVIEW_BUTTON = "//runtime_platform_actions-action-renderer[@apiname='pricing_review_button']//button";
    String PRICING_ESS_REVIEW_BUTTON = "//runtime_platform_actions-action-renderer[@apiname='Karta_ESS']//button";
    String MENU_EXPAND_BUTTON = "//runtime_platform_actions-actions-ribbon//button[@class='slds-button slds-button_icon-border-filled']";
    String NOTES_SUMMARY = "//a[text()='Souhrn poznámek']";
    String NOTES_SUMMARY_FRAME = "//*[@id='tab-5']/slot/flexipage-component2/slot/flexipage-aura-wrapper/div/article/div[2]/div/force-aloha-page/div/iframe"; // TODO: change
    String NOTES_SUMMARY_ACCOUNT_NAME = "//span[text()='%s']";

    String ACCOUNT_ID = "//flexipage-tab2//div[./span[text()='IČ']]//following::lightning-formatted-text[text()='%s']";
    String ACCOUNT_LOCKED = "//flexipage-tab2//div[./span[text()='Zamknuto']]//following::lightning-formatted-text[1]";
    String ACCOUNT_OWNER = "//flexipage-tab2//div[./span[text()='Vlastník']]//following::lightning-formatted-text[1]";
    String SELECTION_DIALOG_DIV = "//div[@data-aura-class='forceChatterPublisherPresentationPanel']";
    String OWNER_SELECT = SELECTION_DIALOG_DIV + "//select";
    String OWNER_SELECT_SAVE_BUTTON = SELECTION_DIALOG_DIV + "//button[text()='Save']";
    String OWNER_SELECT_CANCEL_BUTTON = SELECTION_DIALOG_DIV + "//button[text()='Cancel']";
    String CONFIRMATION_DIALOG_TEXT = "//div[@data-aura-class='forceVisualMessageQueue']//span[text()='Úspěšně dokončeno']";
    String ACCOUNT_NAME = "//flexipage-tab2//div[./span[text()='Account Name']]/following-sibling::div//lightning-formatted-text";
    String REMEDIAL = "//div[@data-aura-class='cLockSubjekt' and contains(text(),'remedial')]";

    AccountDetailPage onAccountDetailPage();

    @Click(ACCOUNT_LOCK_BUTTON)
    AccountDetailPage clickLockAccount();

    @Select(OWNER_SELECT)
    @Click(value = OWNER_SELECT_SAVE_BUTTON, andWait = @Wait(value = CONFIRMATION_DIALOG_TEXT, until = Until.GONE))
    AccountDetailPage selectAndSaveAccountOwner(String owner);

    @Click(value = ACCOUNT_UNLOCK_BUTTON, andWait = @Wait(value = CONFIRMATION_DIALOG_TEXT, until = Until.GONE))
    AccountDetailPage clickUnlockAccount();

    @Click(NEW_OPPORTUNITY_BUTTON)
    CreateNewOpportunityPage clickNewOpportunityButton();

    @Click(OPPORTUNITIES_BUTTON)
    OpportunitiesListPage clickOpportunities();

    @Click(OWNER_SELECT_CANCEL_BUTTON)
    AccountDetailPage cancelDialogWindow();

    @Click(value = NOTES_SUMMARY, andWait = @Wait(value = NOTES_SUMMARY_FRAME, by = Lookup.XPATH))
    AccountDetailPage clickNotesSummary();

    @SwitchToFrame(value = NOTES_SUMMARY_FRAME, by = Lookup.XPATH)
    AccountDetailPage switchToNoteSummaryFrame();

    @CheckElementPresent(value = NOTES_SUMMARY_ACCOUNT_NAME, isStringDynamicXpath = true)
    AccountDetailPage checkNotesSummaryAccountName(String account);

    @CheckElementPresent(value = REMEDIAL)
    AccountDetailPage checkIfSubjectInRemedial();

    @SwitchToDefaultContent()
    AccountDetailPage switchToMainFrame();

    @CheckElementPresent(value = ACCOUNT_ID, isStringDynamicXpath = true)
    AccountDetailPage checkAccountId(String account);

    @Wait(value = ACCOUNT_LOCKED, until = Until.PRESENT_IN_DOM)
    @GetElementContent(ACCOUNT_LOCKED)
    String getAccountLock();

    @Wait(value = ACCOUNT_OWNER, until = Until.PRESENT_IN_DOM)
    @GetElementContent(ACCOUNT_OWNER)
    String getAccountOwner();

    @Wait(value = ACCOUNT_NAME, until = Until.PRESENT_IN_DOM)
    @GetElementContent(ACCOUNT_NAME)
    String getAccountName();

    @CustomAction
    default AccountDetailPage refreshPage() {
        SalesForceEndpoint endpoint = getEndpoint(SalesForceEndpoint.class);
        endpoint.refeshPage();
        return null;
    }

    @CustomAction
    default MainPage clickPricingReviewAndWaitForDownload() {
        SalesForceEndpoint endpoint = getEndpoint(SalesForceEndpoint.class);
        String expectedFileName = "KartaKlienta_" + getAccountName().replace(" ", "+") + ".xls";
        log("Starting download of file: {}", expectedFileName);
        endpoint.click(() -> PRICING_REVIEW_BUTTON, Lookup.XPATH);
        endpoint.checkIfFileExists(expectedFileName, 60);
        return null;
    }

    @CustomAction
    default MainPage clickPricingEssReviewAndWaitForDownload() {
        SalesForceEndpoint endpoint = getEndpoint(SalesForceEndpoint.class);
//        TODO: check what is the expected filename
        String expectedFileName = "KartaKlienta_" + getAccountName().replace(" ", "+") + ".xls";
        log("Starting download of file: {}", expectedFileName);
        endpoint.click(() -> PRICING_ESS_REVIEW_BUTTON, Lookup.XPATH);
        endpoint.checkIfFileExists(expectedFileName, 60);
        return null;
    }

}
