package cz.moneta.test.dsl.ufo.banka.pages.fon;

import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.ufo.banka.pages.fon.DMS070_S001_ScanDocumentsPage.*;
import static cz.moneta.test.dsl.ufo.operations.shared.SharedElements.LOAD_IMG_DIV_CLASS;

@Wait(value = DMS070_S001_PAGE_LABEL_XPATH, by = Lookup.XPATH, waitSecondsForElement = 60)
@Wait(value = DISABLED_CONTINUE_BUTTON, by = Lookup.ID, until = Until.VISIBLE)
@Wait(value = LOAD_IMG_DIV_CLASS, until = Until.GONE, by = Lookup.CLASSNAME)
public interface DMS070_S001_ScanDocumentsPage extends WebFlow<DMS070_S001_ScanDocumentsPage>, StoreAccessor {
    String DMS070_S001_PAGE_LABEL_XPATH = "//div[text()='DMS070_S001']";
    String DISABLED_CONTINUE_BUTTON = "DMS070_S001_X140";
    String FAKE_ID_PATH = "regression/ufo/ID-card_CZ_2012.jpg";
    String FAKE_ZL = "regression/ufo/ZL.jpg";
    String FAKE_DP = "regression/ufo/DP.pdf";
    String FAKE_REGISTER_ENTRY = "regression/ufo/vypis_RES.pdf";
    String DOCUMENT_TYPE_OP = "Občanský průkaz";
    String DOCUMENT_TYPE_ZL = "Dokument prokazující oprávnění k podnikání";
    String DOCUMENT_TYPE_DP = "Daňové přiznání";
    String DOCUMENT_TYPE_BALANCE_SHEET = "Rozvaha";
    String DOCUMENT_TYPE_INCOME_MODULE = "Výkaz zisku a ztráty";
    String DOCUMENT_TYPE_REGISTRY_ENTRY = "Elektronický výpis z ŽR";
    String FILE_UPLOAD_INPUT = "gwt-FileUpload";

    String CONTINUE_BUTTON = "DMS070_S001_B002";
    String SAVE_DOCUMENT_BUTTON = "DMS070_S001_B009";
    String ASSIGN_DOCUMENT_BUTTON = "DMS070_S001_B003";
    String UPLOAD_DOCUMENT_BUTTON = "DMS070_S001_B010";
    String DOCUMENT_TYPE_XPATH = "//table[@id='DMS070_S001_G003_UfoTable']//td[text()='%s']";
    String DOCUMENT_TYPE = "DMS070_S001_C002";
    String FIRST_UPLOADED_DOCUMENT_RADIO = "DMS070_S001_G001-0";
    String UPLOADED_DOCUMENT_XPATH = "//table[@id='DMS070_S001_G001_UfoTable']//td[contains(text(),'Scan')]";
    String TEST = "//*[@id=\"DMS070_S001_G001_UfoTable\"]/tbody/tr[2]/td[text()=\"Elektronický výpis z ŽR\"]";

    private UfoEndpoint uploadDocument(String documentPath) {
        String path = UploadUtil.getResourcePath(documentPath);
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        log("Path to fake ID: {}", path);
        endpoint.click(() -> UPLOAD_DOCUMENT_BUTTON);
        endpoint.type(() -> FILE_UPLOAD_INPUT, path, false, Lookup.CLASSNAME);
        endpoint.waitForElementsToLoad(80, Lookup.XPATH, UPLOADED_DOCUMENT_XPATH);
        return endpoint;
    }

    private void uploadAndAssignDocumentWithSelect(String documentPath, String documentType) {
        UfoEndpoint endpoint = uploadDocument(documentPath);
        endpoint.selectByVisibleText(DOCUMENT_TYPE, documentType);
        endpoint.sendKeysAsChord(Key.TAB);
    }

    private void uploadAndAssignDocumentWithRadioButton(String documentPath, String documentType) {
        UfoEndpoint endpoint = uploadDocument(documentPath);
        endpoint.click(() -> String.format(DOCUMENT_TYPE_XPATH, documentType), Lookup.XPATH);
        endpoint.click(() -> ASSIGN_DOCUMENT_BUTTON);
        endpoint.waitForElementsToLoad(10, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);
        endpoint.waitForElementsToLoad(10, Lookup.XPATH, Until.GONE, UPLOADED_DOCUMENT_XPATH);
    }

    @CustomAction
    default DMS070_S001_ScanDocumentsPage reloadPage() {
        UfoEndpoint endpoint = this.getEndpoint(UfoEndpoint.class);
        endpoint.refeshPage();
        return null;
    }

    @CustomAction
    default DMS070_S001_ScanDocumentsPage uploadAndAssignFakeId() {
        uploadAndAssignDocumentWithSelect(FAKE_ID_PATH, DOCUMENT_TYPE_OP);
        return null;
    }

    @CustomAction
    default DMS070_S001_ScanDocumentsPage uploadAndAssignFakeDP() {
        uploadAndAssignDocumentWithRadioButton(FAKE_DP, DOCUMENT_TYPE_DP);
        return null;
    }

    @CustomAction
    default DMS070_S001_ScanDocumentsPage uploadAndAssignFakeBalanceSheet() {
        uploadAndAssignDocumentWithRadioButton(FAKE_ZL, DOCUMENT_TYPE_BALANCE_SHEET);
        return null;
    }

    @CustomAction
    default DMS070_S001_ScanDocumentsPage uploadAndAssignFakeIncomeModule() {
        uploadAndAssignDocumentWithRadioButton(FAKE_REGISTER_ENTRY, DOCUMENT_TYPE_INCOME_MODULE);
        return null;
    }

    @CustomAction
    default DMS070_S001_ScanDocumentsPage uploadAndAssignFakeZL() {
        uploadAndAssignDocumentWithSelect(FAKE_ZL, DOCUMENT_TYPE_ZL);
        return null;
    }

    @CustomAction
    default DMS070_S001_ScanDocumentsPage uploadAndAssignFakeRegisterRecord() {
        uploadAndAssignDocumentWithSelect(FAKE_REGISTER_ENTRY, DOCUMENT_TYPE_REGISTRY_ENTRY);
        return null;
    }

    @Click(value = SAVE_DOCUMENT_BUTTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 60))
    DMS070_S001_ScanDocumentsPage saveUploadedDocument();

    @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 60)
    @Click(CONTINUE_BUTTON)
    DMS070_S001_ScanDocumentsPageTransitions clickContinue();
}
