package cz.moneta.test.dsl.payment_engine.parts_of_menu.menu_clients;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.payment_engine.home.PaymentEngineHomePage;
import cz.moneta.test.dsl.payment_engine.utils.PaymentEngineUtils;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.payment_engine.PaymentEngineEndpoint;
import cz.moneta.test.harness.support.web.*;


public interface PaymentEngineClientsListOfClientsAccounts extends WebFlow<PaymentEngineClientsListOfClientsAccounts>, StoreAccessor {

    String CLIENTS_ACCOUNT_INPUT = "//input[@name='accIdent']";
    String CLIENTS_ACCOUNT_STATE_ACTIVE = "//input[@name='blockStatus' and @value='00']";
    String CLIENTS_ACCOUNT_STATE_BLOCKED = "//input[@name='blockStatus' and @value='03']";
    String APPLY_BUTTON = "//button[text()='Aplikovat']";
    String DETAIL_BUTTON = "//a[text()='Detail']";
    String AJAX_LOADING = "//div[text()='Načítám...']";
    String BRANCH_CODE_XPATH = "//input[@name='branchCode']";
    String BRANCH_CODE_KEY = "BRANCH_CODE";
    String REFRESH_BUTTON = "//button[@class='x-btn-text x-tbar-loading']";
    String CLOSE_TAB = "//a[@class='x-tab-strip-close']";
    String CLOSE_DETAIL = "//button[text()='Zavřít']";

    @TypeInto(CLIENTS_ACCOUNT_INPUT)
    PaymentEngineClientsListOfClientsAccounts fillClientsAccount(String clientsAccount);

    @Click(value = APPLY_BUTTON, andWait = @Wait(value = AJAX_LOADING, until = Until.GONE))
    PaymentEngineClientsListOfClientsAccounts clickOnApply();

    @Click(DETAIL_BUTTON)
    PaymentEngineClientsListOfClientsAccounts clickOnDetail();

    @Click(value = CLOSE_DETAIL, andWait = @Wait(value = AJAX_LOADING, until = Until.GONE))
    PaymentEngineClientsListOfClientsAccounts closeDetail();

    @Wait(value = CLIENTS_ACCOUNT_STATE_ACTIVE, until = Until.PRESENT_IN_DOM)
    @CheckElementPresent(CLIENTS_ACCOUNT_STATE_ACTIVE)
    PaymentEngineClientsListOfClientsAccounts checkAccountStateActive();

    @Wait(value = CLIENTS_ACCOUNT_STATE_BLOCKED, until = Until.PRESENT_IN_DOM)
    @CheckElementPresent(CLIENTS_ACCOUNT_STATE_BLOCKED)
    PaymentEngineClientsListOfClientsAccounts checkAccountStateBlocked();

    @Wait(value = AJAX_LOADING, until = Until.GONE)
    PaymentEngineClientsListOfClientsAccounts waitForAjax();

    @Wait(explicitWaitSeconds = 5)
    PaymentEngineClientsListOfClientsAccounts wait5sec();

    @Wait(explicitWaitSeconds = 10)
    PaymentEngineClientsListOfClientsAccounts wait10sec();

    @CustomAction
    default PaymentEngineClientsListOfClientsAccounts storeBranchCode(Harness harness) {
        PaymentEngineUtils.storeElementAttribute(harness, BRANCH_CODE_XPATH, BRANCH_CODE_KEY);
        return null;
    }

    @CustomAction
    default PaymentEngineClientsListOfClientsAccounts waitForValue(Harness harness, String value) {
        PaymentEngineUtils.waitForElementPresence(harness, String.format("//*[contains(@class,'x-grid3-row-selected')]//*[contains(text(),'%s')]", value), REFRESH_BUTTON);
        return null;
    }

    @Click(REFRESH_BUTTON)
    PaymentEngineClientsListOfClientsAccounts clickOnRefresh();

    @Click(value = CLOSE_TAB, andWait = @Wait(explicitWaitSeconds = 1))
    PaymentEngineHomePage closeTab();

    @CustomAction
    default PaymentEngineClientsListOfClientsAccounts checkAndUpdateAccountStateIfNotActive() {
        PaymentEngineEndpoint endpoint = getEndpoint(PaymentEngineEndpoint.class);
        if (endpoint.isElementPresent(CLIENTS_ACCOUNT_STATE_ACTIVE, Lookup.XPATH) == false) {
            closeDetail().clickOnRefresh().clickOnDetail().checkAccountStateActive();
        }
        return null;
    }

    @CustomAction
    default PaymentEngineClientsListOfClientsAccounts checkAndUpdateAccountStateIfNotBlocked() {
        PaymentEngineEndpoint endpoint = getEndpoint(PaymentEngineEndpoint.class);
        if (endpoint.isElementPresent(CLIENTS_ACCOUNT_STATE_ACTIVE, Lookup.XPATH) == false) {
            closeDetail().clickOnRefresh().clickOnDetail().checkAccountStateBlocked();
        }
        return null;
    }
}
