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

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.Lookup;
import cz.moneta.test.harness.support.web.Until;

import java.io.File;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.Objects;

import static cz.moneta.test.dsl.ufo.banka.pages.fon.DMS001_S001_ScanDocumentsPage.*;

public final class UploadUtil {

    private UploadUtil() {
        //hide constructor
    }

    private static UfoEndpoint uploadDocument(String documentPath, String documentType, StoreAccessor harness) {
        String path = getResourcePath(documentPath);
        UfoEndpoint endpoint = harness.getEndpoint(UfoEndpoint.class);
        harness.log("Path to fake ID: {}", path);
        endpoint.type(() -> FILE_UPLOAD_INPUT, path, false, Lookup.CLASSNAME);
        endpoint.waitForElementsToLoad(80, FIRST_UPLOADED_DOCUMENT_RADIO);
        return endpoint;
    }

    public static void uploadAndAssignDocument(String documentPath, String documentType, StoreAccessor harness) {
        UfoEndpoint endpoint = uploadDocument(documentPath, documentType, harness);
        endpoint.selectByVisibleText(DOCUMENT_TYPE, documentType);
        endpoint.click(() -> ASSIGN_DOCUMENT_BUTTON);
        endpoint.waitForElementsToLoad(10, Lookup.CLASSNAME, Until.GONE, SharedElements.LOAD_IMG_DIV_CLASS);
    }

    public static void uploadAndAssignDocumentByRadioButton(String documentPath, String documentType, StoreAccessor harness) {
        UfoEndpoint endpoint = uploadDocument(documentPath, documentType, harness);
        endpoint.click(() -> String.format(DOCUMENTS_TO_ASSIGN_TABLE_XPATH, documentType), Lookup.XPATH);
        endpoint.click(() -> ASSIGN_LOADED_DOCUMENT_BUTTON);
        endpoint.waitForElementsToLoad(10, Lookup.CLASSNAME, Until.GONE, SharedElements.LOAD_IMG_DIV_CLASS);
    }

    public static void uploadDocument(String documentPath, StoreAccessor harness) {
        String path = getResourcePath(documentPath);

        UfoEndpoint endpoint = harness.getEndpoint(UfoEndpoint.class);
        endpoint.type(() -> FILE_UPLOAD_INPUT, path, false, Lookup.CLASSNAME);
        endpoint.waitForElementsToLoad(80, FIRST_UPLOADED_DOCUMENT_RADIO);
    }

    public static String getResourcePath(String documentPath) {
        String path;
        try {
            File currentJar = new File(UploadUtil.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath());
            if (currentJar.isFile()) {
                path = new File(documentPath).getAbsolutePath();
            } else {
                path = Paths.get(Objects.requireNonNull(UploadUtil.class.getClassLoader().getResource(documentPath)).toURI()).toFile().getAbsolutePath();
            }
        } catch (URISyntaxException | NullPointerException e) {
            throw new IllegalStateException("Could not correctly load file " + documentPath);
        }
        return path;
    }
}
