package cz.moneta.test.dsl.hypos.pages.contract.approvalchecklist;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.hypos.components.Menu;
import cz.moneta.test.dsl.hypos.enums.ChecklistDocCategory;
import cz.moneta.test.dsl.hypos.enums.ChecklistTab;
import cz.moneta.test.dsl.hypos.enums.Client;
import cz.moneta.test.dsl.hypos.pages.HyposPageFlow;
import cz.moneta.test.harness.endpoints.hypos.HyposEndpoint;
import cz.moneta.test.harness.support.web.*;
import org.openqa.selenium.WebElement;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static cz.moneta.test.dsl.hypos.enums.ChecklistDocCategory.IDENTITY_GENERAL;
import static cz.moneta.test.dsl.hypos.pages.contract.approvalchecklist.ApprovalChecklistPage.PAGE_TITLE;
import static cz.moneta.test.dsl.hypos.pages.contract.approvalchecklist.ChecklistImportDocModalPage.VALIDITY_DATE_INPUT;
import static cz.moneta.test.dsl.hypos.tasks.HyposTasks.defaultDateFormat;

@Wait(PAGE_TITLE)
public interface ApprovalChecklistPage extends HyposPageFlow<ApprovalChecklistPage>, Menu {
    String PAGE_TITLE = "//h1[text()='Checklist dokumentů']";
    String CHECKLIST_TAB_LOAN = "//span[text()='Úvěr']";
    String CHECKLIST_TAB_ALLOCATED_COLLATERAL = "//span[text()='Zajištění']";
    String CHECKLIST_TAB_CLIENT = "//table//div//li[contains(@aria-controls,'section-%1$s')]%2$s";
    String ADD_OTHER_RQ_DOC = "//tbody[@id='o_ostatni_uver']//a[text()='Přidat požadovaný dokument']";
    String UPLOAD_UW_EXCEPTION_LINK = "//tbody[@id='ostatni_uver-general']//a[@class='upload-file float-right']";
    String DOCS_IN_CATEGORY = "//div[contains(@id,'section-%1$s')]%2$s//tbody[contains(@id,'%3$s')]//tr[not(contains(@class,'hidden'))]//a[text()='nahrát']";
    String DOCS_IN_CATEGORY_UPLOAD_BUTTON = "(" + DOCS_IN_CATEGORY + ")[%4$s]";

    @Click(CHECKLIST_TAB_LOAN)
    ApprovalChecklistPage clickChecklistLoan();

    @Click(CHECKLIST_TAB_ALLOCATED_COLLATERAL)
    ApprovalChecklistPage clickChecklistAllocatedCollateral();

    @Click(value = DOCS_IN_CATEGORY, isStringDynamicXpath = true)
    ApprovalChecklistPage clickChecklistClient(String client);

    @Click(ADD_OTHER_RQ_DOC)
    ChecklistAddDocTypeModalPage clickAddOtherRqDoc();

    @Click(UPLOAD_UW_EXCEPTION_LINK)
    ChecklistImportDocModalPage clickUploadUwException();

    @CustomAction
    default ApprovalChecklistPage addChecklistDocs(ChecklistTab checklistTab, Client client, ChecklistDocCategory category, Harness harness) {
        HyposEndpoint endpoint = getEndpoint(HyposEndpoint.class);
        String clientOrder = checklistTab.equals(ChecklistTab.CLIENT) ? "[" + client.getOrderValue() + "]" : "";

        List<WebElement> docs = endpoint.findAndGetListOfElements(String.format(DOCS_IN_CATEGORY, checklistTab.getValue(), clientOrder, category.getValue()), Lookup.XPATH);

        if (!docs.isEmpty()) {
            endpoint.click(() -> String.format(CHECKLIST_TAB_CLIENT, checklistTab.getValue(), clientOrder));
            for (int i = 1; i <= docs.size(); i++) {
                String doc = String.format(DOCS_IN_CATEGORY_UPLOAD_BUTTON, checklistTab.getValue(), clientOrder, category.getValue(), i);
                endpoint.click(() -> doc);
                endpoint.waitForElementsToLoad(20, Until.GONE, LOADER);
                ChecklistImportDocModalPage checklistImportDocModalPage = Builders.newWebFlowBuilder(ChecklistImportDocModalPage.class, endpoint, harness);
                if (endpoint.isElementVisible(1, VALIDITY_DATE_INPUT, Lookup.XPATH)) {
                    checklistImportDocModalPage.fillValidityDate(LocalDate.now().plusYears(category == IDENTITY_GENERAL ? 5 : 0).format(DateTimeFormatter.ofPattern(defaultDateFormat)))
                            .checkValidityDate();
                }
                checklistImportDocModalPage.uploadFileChecklist(harness.tasks().common().getFakePdfPath())
                        .saveFileChecklist();
            }
        }
        return null;
    }
}