package cz.moneta.test.dsl.ufo.operations.client.product;

import cz.moneta.test.dsl.ufo.operations.search.OPS001_S001_ClientSearchPageUfoOperations;
import cz.moneta.test.dsl.ufo.operations.client.clientDetail.STS701_S002_ClientDetailPage;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.ufo.UfoEndpoint;
import cz.moneta.test.harness.support.web.*;

import java.util.Arrays;
import java.util.List;

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

@Wait(value = KOK701_S001_MeetingSchedulingPage.KOK701_S001_PAGE_TITLE, waitSecondsForElement = 60)
@Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE, waitSecondsForElement = 80)
public interface KOK701_S001_MeetingSchedulingPage extends StoreAccessor, WebFlow<KOK701_S001_MeetingSchedulingPage> {

    String KOK701_S001_PAGE_TITLE = "KOK701_S001_L999";
    String CALENDAR = "//*[@id='KOK701_S001_K001']/div/table/tbody/tr[2]/td[2]/div/table/tbody/";
    String CALENDAR_NEXT_MONTH = CALENDAR + "tr[1]/td/table/tbody/tr/td[4]/div";
    String CALENDAR_WORK_DAY_1 = CALENDAR + "tr[2]/td/div/table/tbody/tr[3]/td[1]";
    String CALENDAR_WORK_DAY_2 = CALENDAR + "tr[2]/td/div/table/tbody/tr[3]/td[2]";
    String CALENDAR_WORK_DAY_3 = CALENDAR + "tr[2]/td/div/table/tbody/tr[3]/td[3]";
    String CALENDAR_WORK_DAY_4 = CALENDAR + "tr[2]/td/div/table/tbody/tr[3]/td[4]";
    String CLIENT_NAME_FIELD = "KOK701_S001_E006";

    String BACK_DART_BUTTON = "KOK701_S001_P002";
    String MEETING_PLACE = "KOK701_S001_C004";
    String PRODUCT_FIELD = "KOK701_S001_C002";
    String NAME_OF_CLIENT = "KOK701_S001_E006";
    String CLIENT_TELEPHONE_NUMBER = "KOK701_S001_E002";
    String EMAIL_CHECKBOX = "KOK701_S001_H004_2";
    String EMAIL_FIELD = "KOK701_S001_E003";
    String CALENDAR_BUTTON = "KOK701_S001_P001";
    String VALUE_FIELD = "KOK701_S001_E019";
    String TIME_COMBOBOX = "KOK701_S001_C003";
    String NOTE_FOR_MEETING = "KOK701_S001_M001";
    String INTEREST_RATE_FIELD = "KOK701_S001_E027";
    String DOCUMENT_CREDIT_CARD = "KOK701_S001_H001_Kreditní karta";
    String DOCUMENT_ID_CARD = "KOK701_S001_H001_Občanský průkaz";
    String DOCUMENT_PASSPORT = "KOK701_S001_H001_Pas";
    String DOCUMENT_LAST_3_CONFIRM = "KOK701_S001_H001_Poslední 3 potvrzení o zaplacení úvěr. produktů";
    String DOCUMENT_INCOME_STATEMENT = "KOK701_S001_H001_Potvrzení o příjmu";
    String DOCUMENT_BIRTH_CERTIFICATION = "KOK701_S001_H001_Rodný list";
    String DOCUMENT_DRIVING_LICENSE = "KOK701_S001_H001_Řidičský průkaz";
    String DOCUMENT_LOAN_AGREEMENTS = "KOK701_S001_H001_Úvěrové smlouvy";
    String EXTRACT_FROM_OR_DOCUMENT = "KOK701_S001_H001_Výpis z OR";
    String DOCUMENT_TELEPHONE_BILL = "KOK701_S001_H001_Vyúčtování za telefon";
    String DOCUMENT_TRADE_CERTIFICATE = "KOK701_S001_H001_Živnostenský list";
    String DOCUMENT_FIRST_TAX_RETURN = "KOK701_S001_H001_1 daňové přiznání";
    String DOCUMENT_SECOND_TAX_RETURN = "KOK701_S001_H001_2 daňová přiznání";
    String DOCUMENT_THIRD_RETURN_FROM_BU_FROM_LAST_3_MONTHS = "KOK701_S001_H001_3 výpisy z BÚ za poslední 3 m";
    String SEND_MEETING = "KOK701_S001_B001";
    String NEXT_MONTH_ON_CALENDAR_BUTTON = "//div[text()='>' and @class='ufo-calendar-mnext']";
    String REGULAR_DAY_8_IN_CALENDAR = "//*[@id='KOK701_S001_K001']//td[@class='ufo-calendar-regularday'][8]";
    String DATE_FIELD = "KOK701_S001_E016";
    String MEETING_TYPE_COMBOBOX = "KOK701_S001_C018";
    String SENDING_SMS_TAB = "//button[contains(@title, 'KOK701_S001_I003')]";
    String TYPE_SMS_COMBOBOX = "KOK701_S001_C007";
    String TELEPHONE_CLIENT_TEXTBOX = "KOK701_S001_E009";
    String CENTRAL_MMB_CHECKBOX = "KOK701_S001_H014_1";

    String SEND_BUTTON = "KOK701_S001_B005";
    String SELECTED_OM_COMBOBOX = "KOK701_S001_C008";
    String MONDAY_CHECKBOX = "KOK701_S001_H006_1";
    String WEDNESDAY_CHECKBOX = "KOK701_S001_H006_3";
    String TELEPHONE_OM_TEXTBOX = "KOK701_S001_E021";
    String DONT_SENT_NOTIFICATIONS = "KOK701_S001_H007_1";


    @CheckElementPresent(CLIENT_NAME_FIELD)
    KOK701_S001_MeetingSchedulingPage assertClientNameField();

    @Click(BACK_DART_BUTTON)
    STS701_S002_ClientDetailPage clickBackButton();

    @Select(value = MEETING_PLACE, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    KOK701_S001_MeetingSchedulingPage selectOM(String omName);

    @Select(value = MEETING_TYPE_COMBOBOX, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    KOK701_S001_MeetingSchedulingPage setMeetingType(String meetingType);

    @Select(PRODUCT_FIELD)
    KOK701_S001_MeetingSchedulingPage setProduct(String product);

    @Click(CALENDAR_BUTTON)
    @Click(NEXT_MONTH_ON_CALENDAR_BUTTON)
    @Click(value = REGULAR_DAY_8_IN_CALENDAR, by = Lookup.XPATH)
    KOK701_S001_MeetingSchedulingPage setMeetingToFirstWorkingDayOfNextMonth();

    @TypeInto(DATE_FIELD)
    KOK701_S001_MeetingSchedulingPage setMeetingToFirstWorkingDayOfNextMonth(String workingDayOfNextMonth);

    @Select(value = TIME_COMBOBOX, selectByOrder = true)
    KOK701_S001_MeetingSchedulingPage setMeetingTimeSlot(int slot);

    @Click(DONT_SENT_NOTIFICATIONS)
    KOK701_S001_MeetingSchedulingPage setNotificationsToDoNotSend();

    @Click(SEND_MEETING)
    STS701_S002_ClientDetailPage confirmMeeting();

    @TypeInto(value = NAME_OF_CLIENT, clear = true)
    KOK701_S001_MeetingSchedulingPage typeName(String name);

    @TypeInto(value = CLIENT_TELEPHONE_NUMBER, clear = true)
    KOK701_S001_MeetingSchedulingPage typePhoneNumber(String phoneNumber);

    @Click(EMAIL_CHECKBOX)
    KOK701_S001_MeetingSchedulingPage clickSendEmailCheckbox();

    @TypeInto(value = EMAIL_FIELD, clear = true)
    KOK701_S001_MeetingSchedulingPage typeEmail(String email);

    @Click(CALENDAR_BUTTON)
    KOK701_S001_MeetingSchedulingPage clickCalendar();

    @Click(value = CALENDAR_NEXT_MONTH, by = Lookup.XPATH)
    KOK701_S001_MeetingSchedulingPage clickNextMonth();

    @CustomAction
    default KOK701_S001_MeetingSchedulingPage clickAvailableDayInCalendar() {
        UfoEndpoint endpoint = getEndpoint(UfoEndpoint.class);
        List<String> calendarDays = Arrays.asList(
                CALENDAR_WORK_DAY_1,
                CALENDAR_WORK_DAY_2,
                CALENDAR_WORK_DAY_3,
                CALENDAR_WORK_DAY_4
        );

        for (String workDay : calendarDays) {
            endpoint.waitForElementsToLoad(2, Lookup.XPATH, Until.CLICKABLE, workDay);
            endpoint.click(() -> workDay, Lookup.XPATH);
            endpoint.waitForElementsToLoad(2, Lookup.CLASSNAME, Until.GONE, LOAD_IMG_DIV_CLASS);

            if (endpoint.isElementVisible(TIME_COMBOBOX)) {
                return this;
            } else {
                log("Day is full: " + workDay);
                endpoint.click(() -> CALENDAR_BUTTON);
            }
        }
        throw new RuntimeException("No available dates were found on the selected days.");
    }

    @TypeInto(VALUE_FIELD)
    KOK701_S001_MeetingSchedulingPage typeValue(String value);

    @Wait(value = TIME_COMBOBOX, until = Until.VISIBLE)
    @Select(value = TIME_COMBOBOX, selectByOrder = true)
    KOK701_S001_MeetingSchedulingPage selectTime(int order);

    @Click(NOTE_FOR_MEETING)
    KOK701_S001_MeetingSchedulingPage clickNotes();

    @TypeInto(value = NOTE_FOR_MEETING, clear = true)
    KOK701_S001_MeetingSchedulingPage typeIntoNotes(String notes);

    @TypeInto(INTEREST_RATE_FIELD)
    KOK701_S001_MeetingSchedulingPage typeInterestRate(String interestRate);

    @Click(DOCUMENT_CREDIT_CARD)
    KOK701_S001_MeetingSchedulingPage clickCreditCardDocument();

    @Click(DOCUMENT_ID_CARD)
    KOK701_S001_MeetingSchedulingPage clickIDCardDocument();

    @Click(DOCUMENT_PASSPORT)
    KOK701_S001_MeetingSchedulingPage clickPasportDocument();

    @Click(DOCUMENT_LAST_3_CONFIRM)
    KOK701_S001_MeetingSchedulingPage click3PaymentConfirmationDocument();

    @Click(DOCUMENT_INCOME_STATEMENT)
    KOK701_S001_MeetingSchedulingPage clickConfirmationOfIncomeDocument();

    @Click(DOCUMENT_BIRTH_CERTIFICATION)
    KOK701_S001_MeetingSchedulingPage clickBirthCertificateDocument();

    @Click(DOCUMENT_DRIVING_LICENSE)
    KOK701_S001_MeetingSchedulingPage clickDrivingLicenseDocument();

    @Click(DOCUMENT_LOAN_AGREEMENTS)
    KOK701_S001_MeetingSchedulingPage clickLoanAgreementsDocument();

    @Click(EXTRACT_FROM_OR_DOCUMENT)
    KOK701_S001_MeetingSchedulingPage clickExtractFromORDocument();

    @Click(DOCUMENT_TELEPHONE_BILL)
    KOK701_S001_MeetingSchedulingPage clickTelephoneBillingDocument();

    @Click(DOCUMENT_TRADE_CERTIFICATE)
    KOK701_S001_MeetingSchedulingPage clickTradeCertificateDocument();

    @Click(DOCUMENT_FIRST_TAX_RETURN)
    KOK701_S001_MeetingSchedulingPage clickFirstTaxReturnDocument();

    @Click(DOCUMENT_SECOND_TAX_RETURN)
    KOK701_S001_MeetingSchedulingPage clickSecondTaxReturnDocument();

    @Click(DOCUMENT_THIRD_RETURN_FROM_BU_FROM_LAST_3_MONTHS)
    KOK701_S001_MeetingSchedulingPage clickThirdReturnFromBUFromLast3MonthsDocument();

    @Click(SEND_MEETING)
    @AcceptAlert(waitSecondsForAlert = 1, expectedAlertText = "Není vyplněno RČ. Pokud je to správně, klikněte na OK, pokud to správně není, klikněte na Cancel.")
    OPS001_S001_ClientSearchPageUfoOperations clickSendWithoutRCCheck();

    @Click(SEND_MEETING)
    STS701_S002_ClientDetailPage clickSend();

    @Click(value = SENDING_SMS_TAB, by = Lookup.XPATH, andWait = @Wait(value = LOAD_IMG_DIV_CLASS, by = Lookup.CLASSNAME, until = Until.GONE))
    KOK701_S001_MeetingSchedulingPage clickSendingSms();

    @Select(TYPE_SMS_COMBOBOX)
    KOK701_S001_MeetingSchedulingPage selectTypeSms(int value);

    @TypeInto(TELEPHONE_CLIENT_TEXTBOX)
    KOK701_S001_MeetingSchedulingPage typeIntoTelephoneClientTextBox(String telephoneNumber);

    @Wait(value = CENTRAL_MMB_CHECKBOX, until = Until.CLICKABLE)
    @Click(CENTRAL_MMB_CHECKBOX)
    KOK701_S001_MeetingSchedulingPage clickCentralMmbCheckBox();

    @Click(SEND_BUTTON)
    OPS001_S001_ClientSearchPageUfoOperations clickSendButton();

    @Select(SELECTED_OM_COMBOBOX)
    KOK701_S001_MeetingSchedulingPage selectOmFromComboBox(int value);

    @Click(MONDAY_CHECKBOX)
    KOK701_S001_MeetingSchedulingPage clickMondayCheckBox();

    @Click(WEDNESDAY_CHECKBOX)
    KOK701_S001_MeetingSchedulingPage clickWednesdayCheckBox();

    @TypeInto(TELEPHONE_OM_TEXTBOX)
    KOK701_S001_MeetingSchedulingPage typeIntoTelephoneOmTextBox(String telephoneNumber);
}