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

import cz.moneta.test.dsl.ufo.operations.client.clientDetail.STS701_S002_ClientDetailPage;
import cz.moneta.test.dsl.ufo.operations.shared.topmenu.XXX702_SharedTopMenu;
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 java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

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

@Wait(value = ETS701_S001_ClientDocuments.OUTGOING_CORRESPONDENCE_BUTTON_XPATH, by = Lookup.XPATH)
@Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE)
public interface ETS701_S001_ClientDocuments extends WebFlow<ETS701_S001_ClientDocuments>, XXX702_SharedTopMenu<ETS701_S001_ClientDocuments>,  StoreAccessor {

    String PRINT_NAME_LABEL = "ETS701_S001_L033-1";
    String SECOND_PRINT_NAME_LABEL = "ETS701_S001_L033-2";
    String DATE_TIME_LABEL = "ETS701_S001_L014-1";
    String SECOND_DATE_TIME_LABEL = "ETS701_S001_L014-2";
    String CLIENT_DOCUMENTS_BUTTON_XPATH = "//button[contains(@title, '[ETS701_S001_I002]')]";
    String CHATS_BUTTON_XPATH = "//button[contains(@title, '[ETS701_S001_I005]')]";
    String MANUAL_SEND_BUTTON_XPATH = "//button[contains(@title, '[ETS701_S001_I004]')]";
    String OUTGOING_CORRESPONDENCE_BUTTON_XPATH = "//button[contains(@title, '[ETS701_S001_I001]')]";
    String CORRESPONDENCE_DETAIL_BUTTON = "ETS701_S001_P001-1";

    String SEARCH_BUTTON = "ETS701_S001_B006";
    String DOCUMENT_DETAIL_BUTTON = "ETS701_S001_P002-1";
    String CORRESPONDENCE_DATE = "ETS701_S001_L014-1";
    String RC_INPUT_IN_OUTGOING_CORRESPONDENCES_TAB = "ETS701_S001_E001";
    String FIRST_DOCUMENT_RADIO = "ufo-et-ETS701_S001_F001_row_1_sel";
    String COMPANY_COMBOBOX = "ETS701_S001_C004";

    String CONTINUE_BUTTON = "ETS701_S001_B013";
    String CITY_LABEL = "//input[@name ='ETS701_S001_E040' and @value='Praha']";
    String SENDING_CHANNEL_DIV = "ETS701_S001_L042-1";
    String SEARCH_DOCUMENTS = "ETS701_S001_B002";

    @Click(value = CLIENT_DOCUMENTS_BUTTON_XPATH, by = Lookup.XPATH)
    ETS701_S001_ClientDocuments clickClientDocument();

    @Wait(value = DOCUMENT_DETAIL_BUTTON, until = Until.VISIBLE)
    @Click(value = CHATS_BUTTON_XPATH, by = Lookup.XPATH)
    ETS701_S001_ClientDocuments clickChats();

    @Click(value = OUTGOING_CORRESPONDENCE_BUTTON_XPATH, by = Lookup.XPATH)
    ETS701_S001_ClientDocuments clickOutgoingCorrespondence();

    @Click(value = MANUAL_SEND_BUTTON_XPATH, by = Lookup.XPATH, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    ETS701_S001_ClientDocuments clickManualSend();

    @Click(CORRESPONDENCE_DETAIL_BUTTON)
    ETS701_S001_ClientDocuments clickCorrespondenceDetail();

    @Wait(explicitWaitSeconds = 10)
    @Click(value = SEARCH_BUTTON, andWait = @Wait(explicitWaitSeconds = 5))
    ETS701_S001_ClientDocuments clickSearch();

    @Wait(value = DOCUMENT_DETAIL_BUTTON, until = Until.VISIBLE)
    @Click(DOCUMENT_DETAIL_BUTTON)
    ETS701_S001_ClientDocuments clickDocumentDetail();

    @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE)
    @Wait(value = FIRST_DOCUMENT_RADIO, until = Until.VISIBLE)
    @CheckElementContent(PRINT_NAME_LABEL)
    ETS701_S001_ClientDocuments assertPrintName(String printName);

    @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE)
    @Wait(value = FIRST_DOCUMENT_RADIO, until = Until.VISIBLE)
    @CheckElementContent(SECOND_PRINT_NAME_LABEL)
    ETS701_S001_ClientDocuments assertSecondPrintName(String printName);

    @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE)
    @CheckElementContent(DATE_TIME_LABEL)
    ETS701_S001_ClientDocuments assertDate(String date);

    @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE)
    @CheckElementContent(SECOND_DATE_TIME_LABEL)
    ETS701_S001_ClientDocuments assertSecondDate(String date);

    @CustomAction
    default ETS701_S001_ClientDocuments checkDistributionAndGetTime() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("d.M.yyyy H:mm:ss");
        String requirementDate = menuClickClientRequirements().clickRefreshRequirements().getFirstRequirementSubmitDate();
        endpoint.waitForElementsToLoad(10, CORRESPONDENCE_DATE);
        String correspondenceDate = endpoint.getText(CORRESPONDENCE_DATE);
        LocalDateTime reqDate = LocalDateTime.parse(requirementDate, formatter).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime corDate = LocalDateTime.parse(correspondenceDate, formatter).withMinute(0).withSecond(0).withNano(0);
        Assertions.assertEquals(reqDate, corDate, "Distribution times are not the same");
        return null;
    }

    @Select(COMPANY_COMBOBOX)
    ETS701_S001_ClientDocuments selectCompany(String company);

    @Wait(value = CITY_LABEL, by = Lookup.XPATH)
    @Click(CONTINUE_BUTTON)
    PSP701_S004_PrintTemplatePage clickContinue();

    @CustomAction
    default ETS701_S001_ClientDocuments checkRcInOutgoingCorrespondency(String rcOfExistingClient) {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        String value = endpoint.getAttribute(RC_INPUT_IN_OUTGOING_CORRESPONDENCES_TAB, "value");
        Assertions.assertEquals(rcOfExistingClient, value);
        return null;
    }

    @TypeInto(value = RC_INPUT_IN_OUTGOING_CORRESPONDENCES_TAB, clear = true)
    ETS701_S001_ClientDocuments fillRc(String rcOfExistingClient);

    @CustomAction
    default ETS701_S001_ClientDocuments clickDocumentDetailAndCheckOpening() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.waitForElementsToLoad(20, Until.CLICKABLE, DOCUMENT_DETAIL_BUTTON);
        endpoint.click(() -> DOCUMENT_DETAIL_BUTTON);
        endpoint.switchToOtherWindow(10, 3);
        return null;
    }

    @CheckElementContent(SENDING_CHANNEL_DIV)
    ETS701_S001_ClientDocuments checkSendingChannel(String channel);

    @CustomAction
    default STS701_S002_ClientDetailPage closeCorrespondency() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        endpoint.closeCurrentWindow();
        endpoint.switchToOtherWindow(3, 1);
        return null;
    }

    @Click(value = SEARCH_DOCUMENTS)
    ETS701_S001_ClientDocuments clickSearchDocuments();
}