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.util.data.payment.PaymentData;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.payment_engine.PaymentEngineEndpoint;
import cz.moneta.test.harness.support.auth.Credentials;
import cz.moneta.test.harness.support.web.*;

public interface PaymentEngineClientsAuthorizationsForDirectDebits extends WebFlow<PaymentEngineClientsAuthorizationsForDirectDebits>, StoreAccessor {
    String PREFIX = "//div[@id='Klienti / CZ Svolení k inkasu']";
    String CREATE_AUTHORIZATION_FOR_DIRECT_DEBITS = PREFIX + "//a[text()='Založit']";
    String APPROVE_AUTHORIZATION_FOR_DIRECT_DEBITS = PREFIX + "//a[text()='Schválit']";
    String EDIT_AUTHORIZATION_FOR_DIRECT_DEBITS = PREFIX + "//a[text()='Editovat']";
    String DELETE_AUTHORIZATION_FOR_DIRECT_DEBITS = "//a[text() = 'Vymazat']";
    String REJECT_AUTHORIZATION_FOR_DIRECT_DEBITS = "//a[text() = 'Zamítnout']";
    String DEBTOR_ACCOUNT = PREFIX + "//input[@type='text' and @name='dbtrAccIdent']";
    String CREDITOR_ACCOUNT = PREFIX + "//input[@type='text' and @name='cdtrAccIdent']";
    String STATUS = PREFIX + "//input[@type='hidden' and @name='status']/following-sibling::input";
    String ID = "//input[@name = 'id']";
    String APPLY = PREFIX + "//button[text() = 'Aplikovat']";
    String OK_BUTTON = PREFIX + "//button[text() = 'OK']";
    String SELECT_STATUS = "//input[@name = 'status']//following-sibling::img";
    String STATUS_NULL = "//div[text()='Archivovaný']/preceding-sibling::div";
    String STATUS_FOR_VERIFICATION = "//div[text()='Ke schválení']";
    String STATUS_VALID = "//div[text()='Platný']";
    String CHECK_STATUS = "//div[@class='x-grid3-cell-inner x-grid3-col-10']";
    String PERIOD_MAX_AMOUNT = "//input[@name = 'periodMaxAmount']";
    String CLOSE_TAB = "//a[@class = 'x-tab-strip-close']";
    String AUTH_ROOT_ID_XPATH = PREFIX + "//div[@class='x-grid3-cell-inner x-grid3-col-1']";
    String AUTH_ROOT_ID_KEY = "AUTH_ROOT_ID_KEY";
    String AJAX_LOADING = "//div[text()='Načítám...']";
    String FIRST_ROW = "//div[contains(@class,'x-grid3-row-selected')]//div[@class='x-grid3-cell-inner x-grid3-col-2']";

    @Click(CREATE_AUTHORIZATION_FOR_DIRECT_DEBITS)
    PaymentEngineCreateAuthorizationForDirectDebit clickOnCreateAuthorizationForDirectDebit();

    @Click(value = EDIT_AUTHORIZATION_FOR_DIRECT_DEBITS, andWait = @Wait(value = AJAX_LOADING, until = Until.GONE))
    PaymentEngineClientsAuthorizationsForDirectDebits clickOnEditAuthorizationForDirectDebit();

    @Click(value = DELETE_AUTHORIZATION_FOR_DIRECT_DEBITS, andWait = @Wait(value = AJAX_LOADING, until = Until.GONE))
    PaymentEngineClientsAuthorizationsForDirectDebits clickOnDeleteAuthorizationForDirectDebit();

    @Click(value = REJECT_AUTHORIZATION_FOR_DIRECT_DEBITS, andWait = @Wait(value = OK_BUTTON, until = Until.VISIBLE))
    PaymentEngineClientsAuthorizationsForDirectDebits clickOnRejectAuthorizationForDirectDebit();

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

    @Click(value = APPROVE_AUTHORIZATION_FOR_DIRECT_DEBITS, andWait = @Wait(value = OK_BUTTON, until = Until.VISIBLE))
    PaymentEngineClientsAuthorizationsForDirectDebits clickOnApprove();

    @TypeIntos({@TypeInto(CREDITOR_ACCOUNT), @TypeInto(DEBTOR_ACCOUNT)})
    PaymentEngineClientsAuthorizationsForDirectDebits fillManualDataForSearch(String creditorAcc, String debtorAcc);

    @Click(value = OK_BUTTON, andWait = @Wait(value = OK_BUTTON, until = Until.GONE))
    PaymentEngineClientsAuthorizationsForDirectDebits clickOk();

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

    @StoreElementContent(storeKey = AUTH_ROOT_ID_KEY, value = AUTH_ROOT_ID_XPATH, by = Lookup.XPATH)
    PaymentEngineClientsAuthorizationsForDirectDebits storeRootId();

    @TypeInto(ID)
    PaymentEngineClientsAuthorizationsForDirectDebits fillId(String rootId);

    @Click(SELECT_STATUS)
    PaymentEngineClientsAuthorizationsForDirectDebits clickOnStatus();

    @Wait(STATUS_NULL)
    @Click(value = STATUS_NULL)
    PaymentEngineClientsAuthorizationsForDirectDebits selectStatusNull();

    @Wait(STATUS_FOR_VERIFICATION)
    @Click(value = STATUS_FOR_VERIFICATION)
    PaymentEngineClientsAuthorizationsForDirectDebits selectStatusForVerification();

    @Wait(STATUS_VALID)
    @Click(value = STATUS_VALID)
    PaymentEngineClientsAuthorizationsForDirectDebits selectStatusValid();

    @CheckElementPresent(value = CHECK_STATUS, isStringDynamicXpath = true)
    @Wait(explicitWaitSeconds = 2)
    PaymentEngineClientsAuthorizationsForDirectDebits checkStatus(String status);

    @Wait(value = AJAX_LOADING, until = Until.GONE)
    @TypeInto(PERIOD_MAX_AMOUNT)
    PaymentEngineClientsAuthorizationsForDirectDebits editAmount(String periodMaxAmount);

    @Click(CLOSE_TAB)
    PaymentEngineHomePage closeTab();

    @CustomAction
    default PaymentEngineClientsAuthorizationsForDirectDebits checkAuthorizationForDirectDebitNotPresent(Harness harness, PaymentData data, Credentials user1, Credentials user2) {
        PaymentEngineEndpoint endpoint = getEndpoint(PaymentEngineEndpoint.class);
        if (endpoint.isElementPresent(FIRST_ROW, Lookup.XPATH)) {
            harness.tasks().paymentEngineTasks().removeAuthorizationForDirectDebit(data, user1, user2);
        }
        return null;
    }

    @CustomAction
    default PaymentEngineClientsAuthorizationsForDirectDebits checkAuthorizationForDirectDebitForVerificationNotPresent() {
        PaymentEngineEndpoint endpoint = getEndpoint(PaymentEngineEndpoint.class);
        if (endpoint.isElementPresent(FIRST_ROW, Lookup.XPATH)) {
            clickOnRejectAuthorizationForDirectDebit().clickOk();
        }
        return null;
    }
}
