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

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.ufo.banka.tasks.products.NewSmeAutomatSubTasks;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.WebEndpoint;
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.DMS001_S001_ScanDocumentsPage.DMS001_S001_PAGE_LABEL_XPATH;
import static cz.moneta.test.dsl.ufo.banka.pages.fon.DMS001_S001_ScanDocumentsPage.SKIP_SCAN_BUTTON;
import static cz.moneta.test.dsl.ufo.operations.shared.SharedElements.LOAD_IMG_DIV_CLASS;

@Wait(value = DMS001_S001_PAGE_LABEL_XPATH, by = Lookup.XPATH, waitSecondsForElement = 80)
@Wait(value = SKIP_SCAN_BUTTON, waitSecondsForElement = 80)
public interface DMS001_S001_ScanDocumentsPage extends StoreAccessor, WebFlow<DMS001_S001_ScanDocumentsPage> {
    String DMS001_S001_PAGE_LABEL_XPATH = "//div[text()='DMS001_S001']";

    String FAKE_ID_PATH = "regression/ufo/ID-card_CZ_2012.jpg";
    String FAKE_ZL = "regression/ufo/ZL.jpg";
    String FAKE_REGISTER_ENTRY = "regression/ufo/vypis_RES.pdf";
    String FAKE_DP = "regression/ufo/DP.pdf";
    String FIRST_UPLOADED_DOCUMENT_RADIO = "DMS001_S001_G001-0";
    String FIRST_CLIENTS_DOCUMENT_TO_ASSIGN_RADIO = "DMS001_S001_G002-0";
    String DOCUMENTS_TO_ASSIGN_TABLE_XPATH = "//table[@id='DMS001_S001_G004_UfoTable']//td[text()='%s']";
    String FIRST_DOCUMENT_TO_ASSIGN_RADIO = "DMS001_S001_G004-0";
    String SECOND_DOCUMENT_TO_ASSIGN_RADIO = "DMS001_S001_G004-1";
    String THIRD_DOCUMENT_TO_ASSIGN_RADIO = "DMS001_S001_G004-2";
    String DOCUMENT_TO_ASSIGN_RADIO = "DMS001_S001_G004-%s";
    String DOCUMETNS_TO_ASSIGN_TABLE_XPATH = "//table[@id='DMS001_S001_G004_UfoTable']//tbody/tr//input[contains(@id, 'DMS001_S001_G004')]";
    String FILE_UPLOAD_INPUT = "gwt-FileUpload";
    String ASSIGN_DOCUMENT_BUTTON = "DMS001_S001_B001";
    String CONTINUE_BUTTON = "DMS001_S001_B002";
    String ASSIGN_LOADED_DOCUMENT_BUTTON = "DMS001_S001_B003";
    String SKIP_SCAN_BUTTON = "DMS001_S001_B007";
    String SCANNED_DOCUMENTS_BUTTON = "DMS001_S001_B008";
    String ASSIGN_CLIENTS_LOADED_DOCUMENT_BUTTON = "DMS001_S001_B009";
    String POPUP_BUTTON = "DMS001_S001_B010";
    String FIRST_ASSIGNED_DOCUMENT_CELL = "//table[@id='DMS001_S001_G003_UfoTable']/tbody/tr";
    String ASSIGNED_DOCUMENT_CELL = "//table[@id='DMS001_S001_G003_UfoTable']/tbody/tr/td[text()='%s']";
    String DOCUMENT_TYPE = "DMS001_S001_C001";
    String DOCUMENT_TYPE_2 = "DMS001_S001_C002";
    String POPUP_DESCRIPTION_LABEL_XPATH = "//*[@id='DMS001_S001_L017' and contains(text(),'Naskenujte dokumenty')]";
    String DOCUMENT_TYPE_OP = "Občanský průkaz";
    String DOCUMENT_TYPE_ZL = "Dokument prokazující oprávnění k podnikání";
    String DOCUMENT_TYPE_REGISTRY_ENTRY = "Elektronický výpis z ŽR";
    String DOCUMENT_TYPE_EXTRACT_ZL = "Živnostenský list / výpis z ŽR";
    String DOCUMENT_TYPE_INCOME_CONFIRMATION = "Potvrzení o příjmu";
    String DOCUMENT_TYPE_DP = "Daňové přiznání";
    String DOCUMENT_TYPE_FU = "Potvrzení o bezdlužnosti od FÚ";
    String DOCUMENT_TYPE_CSSZ = "Potvrzení bezdlužnosti ČSSZ";

    default void loadPage(WebEndpoint endpoint) {
        if (endpoint.isElementVisible(2, POPUP_BUTTON, Lookup.ID)) {
            endpoint.click(() -> POPUP_BUTTON);
        }
    }

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

    @CustomAction
    default DMS001_S001_ScanDocumentsPage uploadAndAssignFakeId() {
        UploadUtil.uploadAndAssignDocument(FAKE_ID_PATH, DOCUMENT_TYPE_OP, this);
        return null;
    }

    @CustomAction
    default DMS001_S001_ScanDocumentsPage uploadAndAssignFakeIdAndContinueToMainPage() {
        UploadUtil.uploadAndAssignDocument(FAKE_ID_PATH, DOCUMENT_TYPE_OP, this);
        return null;
    }

    @CustomAction
    default DMS001_S001_ScanDocumentsPage uploadAndAssignFakeZL() {
        UploadUtil.uploadAndAssignDocument(FAKE_ZL, DOCUMENT_TYPE_ZL, this);
        return null;
    }

    @CustomAction
    default DMS001_S001_ScanDocumentsPage uploadAndAssignFakeRegisterRecord() {
        UploadUtil.uploadAndAssignDocument(FAKE_REGISTER_ENTRY, DOCUMENT_TYPE_REGISTRY_ENTRY, this);
        return null;
    }

    @CustomAction
    default DMS001_S001_ScanDocumentsPage uploadAndAssignFakeDebtFreeConfirmationFU() {
        UploadUtil.uploadAndAssignDocumentByRadioButton(FAKE_REGISTER_ENTRY, DOCUMENT_TYPE_FU, this);
        return null;
    }

    @CustomAction
    default DMS001_S001_ScanDocumentsPage uploadAndAssignFakeDebtFreeConfirmationCSSZ() {
        UploadUtil.uploadAndAssignDocumentByRadioButton(FAKE_REGISTER_ENTRY, DOCUMENT_TYPE_CSSZ, this);
        return null;
    }

    @CustomAction
    default DMS001_S001_ScanDocumentsPage uploadAndAssignFakeIncomeConfirmation() {
        UploadUtil.uploadAndAssignDocument(FAKE_REGISTER_ENTRY, DOCUMENT_TYPE_INCOME_CONFIRMATION, this);
        return null;
    }

    @CustomAction
    default DMS001_S001_ScanDocumentsPage uploadAndAssignFakeDP() {
        UploadUtil.uploadDocument(FAKE_DP, this);
        return null;
    }

    @CustomAction
    default DMS001_S001_ScanDocumentsPage uploadBusinessRegister() {
        UploadUtil.uploadDocument(FAKE_REGISTER_ENTRY, this);
        return null;
    }

    @CustomAction
    default DMS001_S001_ScanDocumentsPage uploadFakeExtractZL() {
        UploadUtil.uploadDocument(FAKE_ZL, this);
        return null;
    }

    @Select(DOCUMENT_TYPE_2)
    DMS001_S001_ScanDocumentsPage selectDocumentType(String documentType);

    @Click(FIRST_UPLOADED_DOCUMENT_RADIO)
    DMS001_S001_ScanDocumentsPage selectFirstUploadedDocument();

    @Click(value = FIRST_DOCUMENT_TO_ASSIGN_RADIO, jsClick = true, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 60))
    DMS001_S001_ScanDocumentsPage selectFirstDocumentToAssign();

    @Click(value = FIRST_CLIENTS_DOCUMENT_TO_ASSIGN_RADIO, jsClick = true, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 60))
    DMS001_S001_ScanDocumentsPage selectFirstClientsDocumentToAssign();

    @Click(value = SECOND_DOCUMENT_TO_ASSIGN_RADIO, jsClick = true, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 60))
    DMS001_S001_ScanDocumentsPage selectSecondDocumentToAssign();

    @Click(value = THIRD_DOCUMENT_TO_ASSIGN_RADIO, jsClick = true, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 60))
    DMS001_S001_ScanDocumentsPage selectThirdDocumentToAssign();

    @Click(value = DOCUMENT_TO_ASSIGN_RADIO, jsClick = true, isStringDynamicXpath = true, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 60))
    DMS001_S001_ScanDocumentsPage selectDocumentToAssign(String index);

    @Click(SCANNED_DOCUMENTS_BUTTON)
    DMS001_S001_ScanDocumentsPageTransitions clickScannedDocuments();

    @Click(value = ASSIGN_DOCUMENT_BUTTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 60))
    DMS001_S001_ScanDocumentsPage assignTaxReturn();

    @Click(value = ASSIGN_LOADED_DOCUMENT_BUTTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 60))
    DMS001_S001_ScanDocumentsPage assignLoadedDocument();

    @Click(value = ASSIGN_CLIENTS_LOADED_DOCUMENT_BUTTON, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 60))
    DMS001_S001_ScanDocumentsPage assignClientsLoadedDocument();

    @Click(CONTINUE_BUTTON)
    DMS001_S001_ScanDocumentsPageTransitions clickContinue();

    @Click(value = SKIP_SCAN_BUTTON, jsClick = true)
    DMS001_S001_ScanDocumentsPageTransitions clickSkipButton();

    @Click(value = SKIP_SCAN_BUTTON, andWait = @Wait(value = POPUP_DESCRIPTION_LABEL_XPATH, until = Until.VISIBLE, waitSecondsForElement = 30, by = Lookup.XPATH))
    DMS001_S001_ScanDocumentsPage clickSkipButtonAndStayOnPage();

    @CustomAction
    default DMS001_S001_ScanDocumentsPage assignAllDocuments(Harness harness) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        int documentsToAssignCount = endpoint.findAndGetListOfElements(DOCUMETNS_TO_ASSIGN_TABLE_XPATH, Lookup.XPATH).size();

        NewSmeAutomatSubTasks tasks = harness.tasks().ufo().banka().newSmeAutomatSubTasks();

        then(tasks.assignFirstDocument());

        for (int i = 1; i < documentsToAssignCount; ++i) {
            String index = String.valueOf(i);
            if (endpoint.isElementVisible(2, String.format(DOCUMENT_TO_ASSIGN_RADIO, index), Lookup.ID)) {
                then(tasks.assignDocumentByIndex(index));
            }
        }
        return null;
    }
}