package cz.moneta.test.dsl.ufo.operations.ufoOperations;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.pages.fon.UploadUtil;
import cz.moneta.test.dsl.ufo.operations.client.clientDetail.STS701_S002_ClientDetailPage;
import cz.moneta.test.dsl.ufo.operations.shared.SharedElements;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;
import org.junit.jupiter.api.Assertions;

import static cz.moneta.test.dsl.ufo.operations.shared.SharedElements.LOAD_IMG_DIV_CLASS;

public interface PSP701_S004_PrintTemplatePage extends StoreAccessor, WebFlow<PSP701_S004_PrintTemplatePage> {

    String SAVE_TO_DMS_BUTTON = "PSP701_S004_H001_DMS";
    String SAVE_TO_ETS_BUTTON = "PSP701_S004_H001_ETS";
    String SAVE_TO_LOCAL_BUTTON = "PSP701_S004_H001_LOCAL";
    String SAVE_TO_IB_BUTTON = "PSP701_S004_H001_IBSB";
    String CHECKED_SAVE_TO_IB_RADIO = "//input[@type='checkbox' and @id='PSP701_S004_H001_IBSB' and @checked]";
    String SAVE_TO_EMAIL_BUTTON = "PSP701_S004_H001_EMAIL";
    String SEND_BUTTON = "PSP701_S004_B003";
    String CHECKED_SAVE_TO_DMS_RADIO = "//input[@type='checkbox' and @id='PSP701_S004_H001_DMS' and @checked]";
    String CHECKED_SAVE_TO_EMAIL_RADIO = "//input[@type='checkbox' and @id='PSP701_S004_H001_EMAIL' and @checked]";
    String LETTER_COMBOBOX = "PSP701_S004_C003";
    String ATTACHMENT_PATH = "regression/ufo/DP.pdf";
    String FILE_UPLOAD_XPATH = "//input[@type='file' and contains(@class, 'gwt-FileUpload')]";
    String FILE_UPLOAD_BUTTON = "//div[contains(@class, 'ufo-file-upload-button')]";
    String SAVE_BUTTON = "IPP701_S001_B001";
    String FILE_UPLOAD_LABEL = "//div[@class='ufo-file-upload-label ufo-file-upload-label-success']";

    @Wait(value = SAVE_TO_DMS_BUTTON, until = Until.VISIBLE)
    @Click(SAVE_TO_DMS_BUTTON)
    PSP701_S004_PrintTemplatePage clickSaveToDMS();

    @Click(value = SEND_BUTTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    PSP701_S004_PrintTemplatePageTransitions clickSend();

    @CustomAction
    default STS701_S002_ClientDetailPage clickSendAndClosePrintWindow() {

        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.click(() -> SEND_BUTTON);
        endpoint.waitForElementsToLoad(30, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        endpoint.sleepSeconds(5);
        if (endpoint.isHeadlessModeOn()) {
            endpoint.executeScript("window.parent.document.PSP701_S004_D002.done()");
        } else {
            endpoint.executeScript("window.parent.dispatchEvent(new Event('focus'));");
        }
        return null;
    }

    @CustomAction
    default PSP701_S004_PrintTemplatePage clickSaveToEmailIfChecked() {
        UfoEndpoint endpoint = this.getEndpoint(UfoEndpoint.class);

        if (endpoint.isElementPresent(CHECKED_SAVE_TO_EMAIL_RADIO, Lookup.XPATH)) {
            endpoint.click(() -> SAVE_TO_EMAIL_BUTTON);
            endpoint.waitForElementsToLoad(30, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        }

        return null;
    }

    @CustomAction
    default PSP701_S004_PrintTemplatePage clickSaveToDmsIfNotChecked() {
        UfoEndpoint endpoint = this.getEndpoint(UfoEndpoint.class);

        if (!endpoint.isElementPresent(CHECKED_SAVE_TO_DMS_RADIO, Lookup.XPATH)) {
            endpoint.click(() -> SAVE_TO_DMS_BUTTON);
            endpoint.waitForElementsToLoad(30, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        }

        return null;
    }

    @CustomAction
    default PSP701_S004_PrintTemplatePage clickSaveToIbIfNotChecked() {
        UfoEndpoint endpoint = this.getEndpoint(UfoEndpoint.class);

        if (!endpoint.isElementPresent(CHECKED_SAVE_TO_IB_RADIO, Lookup.XPATH)) {
            endpoint.click(() -> SAVE_TO_IB_BUTTON);
            endpoint.waitForElementsToLoad(30, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        }

        return null;
    }

    @Click(value = SAVE_TO_ETS_BUTTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    PSP701_S004_PrintTemplatePage clickSaveToEts();

    @Click(value = SAVE_TO_LOCAL_BUTTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    PSP701_S004_PrintTemplatePage clickSaveToLocal();

    @Select(value = LETTER_COMBOBOX, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    PSP701_S004_PrintTemplatePage selectLetter(String letter);

    @CustomAction
    default PSP701_S004_PrintTemplatePage uploadAttachment(String documentPath) {

        String path = UploadUtil.getResourcePath(documentPath);
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);

        endpoint.type(() -> FILE_UPLOAD_XPATH, path, false, Lookup.XPATH);
        endpoint.waitForElementsToLoad(20, Lookup.XPATH, Until.PRESENT_IN_DOM, FILE_UPLOAD_LABEL);
        return null;
    }

    @Click(value = SAVE_BUTTON, andWait = @Wait(value = SharedElements.LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    PSP701_S004_PrintTemplatePage clickSave();

    @Wait(value = SAVE_TO_IB_BUTTON, until = Until.VISIBLE)
    @CustomAction
    default PSP701_S004_PrintTemplatePage checkIfIBSBIsChecked() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        Assertions.assertEquals("true", endpoint.getAttribute(SAVE_TO_IB_BUTTON, "checked"), "The IB/SB isn't checked.");
        return null;
    }
}
