package cz.moneta.test.dsl.monetaportal.components;

import cz.moneta.test.dsl.monetaportal.enums.SearchBranchName;
import cz.moneta.test.dsl.util.DateUtils;
import cz.moneta.test.dsl.util.TestUtils;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.MonetaPortalEndpoint;
import cz.moneta.test.harness.support.web.*;

import java.time.LocalDate;

import static cz.moneta.test.dsl.monetaportal.components.Web2Branch.W2B_SEARCH_BRANCH_INPUT;

@Wait(W2B_SEARCH_BRANCH_INPUT)
public interface Web2Branch extends WebFlow<Web2Branch>, StoreAccessor {

    //W2B
    String W2B_DIV = "//div[@id='W2bModal']";
    String W2B_SEARCH_BRANCH_INPUT = "//input[@name='address']";
    String W2B_SEARCH_RESULT_BRANCH = "//li[@role='option']//div[text()='%s']";
    String W2B_ARRANGE_APPOINTMENT_BUTTON = "//div[@id='branch-detail-info']//button[@type='button']";
    String W2B_FORM_VISIT_DAY_SELECT = "//select[@id='appointmentDate']";
    String W2B_FORM_VISIT_TIME_SELECT = "//select[@id='appointmentTime']";
    String W2B_CONTINUE_BUTTON = "//span[@data-message-id='AppointmentForm.continueButton']/..";
    String W2B_FORM_CLIENT_FULL_NAME_INPUT = "//input[@name='clientFullName']";
    String W2B_FORM_CLIENT_ICO_INPUT = "//input[@name='companyIdentificationNumber']";
    String W2B_FORM_CLIENT_PHONE_NUMBER_INPUT = "//input[@name='clientPhoneNumber']";
    String W2B_FORM_CLIENT_EMAIL_INPUT = "//input[@name='clientEmail']";
    String W2B_FORM_MESSAGE = "//textarea[@name='message']";
    String W2B_SUBMIT_BUTTON = "//button[@type='submit' and .//*[@data-message-id='ContactInfoForm.submitButton']]";
    String W2B_SUCCESS_MESSAGE = "//span[@data-message-id='ResultFormSuccess.appointmentSuccessfulHeader']";
    String W2B_MODAL_CLOSE_BUTTON = W2B_DIV + "//button[@id='modal__header__close-button']";
    String W2B_HAS_IDENTIFICATION_NUMBER = "//label[./input[@name='isSme']]";
    String W2B_DAY_SELECT_INPUT = "//div[@data-name='appointmentDate']";
    String W2B_TIME_SELECT_INPUT = "//div[@data-name='appointmentTime']";
    String W2B_SELECT_FIRST_OPTION = "//li[@role='option'][1]";

    //W2B
    @Click(W2B_SEARCH_BRANCH_INPUT)
    Web2Branch clickW2BSearchBranch();

    @Click(value = W2B_ARRANGE_APPOINTMENT_BUTTON, andWait = @Wait(value = W2B_DAY_SELECT_INPUT, waitSecondsForElement = 5))
    Web2Branch clickW2BArrangeAppointment();

    @Wait(value = {W2B_FORM_VISIT_DAY_SELECT})
    @Click(W2B_FORM_VISIT_DAY_SELECT)
    Web2Branch clickVisitDay();

    @Select(value = W2B_FORM_VISIT_DAY_SELECT, selectByOrder = true)
    Web2Branch selectVisitDay(int valueDay);

    @Click(W2B_FORM_VISIT_TIME_SELECT)
    Web2Branch clickVisitTime();

    @Select(value = W2B_FORM_VISIT_TIME_SELECT, selectByOrder = true)
    Web2Branch selectVisitTime(int valueTime);

    @Click(W2B_CONTINUE_BUTTON)
    Web2Branch clickW2BContinue();

    @Waits({@Wait(value = W2B_SUBMIT_BUTTON, until = Until.CLICKABLE), @Wait(value = W2B_SUBMIT_BUTTON, until = Until.VISIBLE)})
    @Click(value = W2B_SUBMIT_BUTTON, jsClick = true)
    Web2Branch clickW2BSubmit();

    @TypeInto(value = W2B_SEARCH_BRANCH_INPUT, clear = true)
    Web2Branch fillW2BSearchBranch(String searchBranchName);

    @Wait(value = {W2B_FORM_CLIENT_FULL_NAME_INPUT})
    @TypeInto(value = W2B_FORM_CLIENT_FULL_NAME_INPUT, clear = true)
    Web2Branch fillW2BClientFullName(String clientFullName);

    @TypeInto(value = W2B_FORM_CLIENT_ICO_INPUT, clear = true)
    Web2Branch fillW2BClientICO(String clientFullName);

    @TypeInto(value = W2B_FORM_CLIENT_PHONE_NUMBER_INPUT, clear = true)
    Web2Branch fillW2BClientPhoneNumber(String clientPhoneNumber);

    @TypeInto(value = W2B_FORM_CLIENT_EMAIL_INPUT, clear = true)
    Web2Branch fillW2BClientEmail(String clientEmail);

    @Wait(W2B_SUCCESS_MESSAGE)
    Web2Branch checkW2BSuccessMessage();

    @Click(W2B_SEARCH_RESULT_BRANCH)
    Web2Branch clickW2BSearchResultBranches(SearchBranchName searchBranchName);

    @Click(W2B_MODAL_CLOSE_BUTTON)
    Web2Branch clickW2BModalClose();

    @Click(W2B_HAS_IDENTIFICATION_NUMBER)
    Web2Branch clickHasIco();

    @TypeInto(value = W2B_FORM_MESSAGE, clear = true)
    Web2Branch fillW2BMessage(String message);

    @Click(value = W2B_DAY_SELECT_INPUT, jsClick = true, andWait = @Wait(W2B_SELECT_FIRST_OPTION))
    Web2Branch clickAppointmentDate();

    @Click(value = W2B_TIME_SELECT_INPUT, jsClick = true, andWait = @Wait(W2B_SELECT_FIRST_OPTION))
    Web2Branch clickAppointmentTime();

    @Click(value = W2B_SELECT_FIRST_OPTION, jsClick = true, andWait = @Wait(value = W2B_SELECT_FIRST_OPTION,until = Until.GONE))
    Web2Branch clickFirstOption();

    @CustomAction
    default Web2Branch storeAppointmentDateTime(String dateFormat) {
        store("APPOINTMENT_DATE", getAppointmentDate(dateFormat));
        store("APPOINTMENT_TIME", getAppointmentTime());
        return null;
    }

    private String getAppointmentDate(String dateFormat) {
        String dayValue = getElementValue(W2B_DAY_SELECT_INPUT);
        int year = LocalDate.now().getYear();

        // Remove the day name (first word) from the string
        String[] parts = dayValue.split(" ", 2); // Split at the first space
        String datePart = parts.length > 1 ? parts[1] : dayValue; // Take only the date part

        String fullDate = datePart + " " + year; // Append the year

        return DateUtils.getDateWithFormat(TestUtils.dateFromString(fullDate, "dd. MM. yyyy"), dateFormat);
    }

    private String getAppointmentTime() {
        String timeValue = getElementValue(W2B_TIME_SELECT_INPUT);
        return timeValue.substring(0, timeValue.length() - 8);
    }

    private String getElementValue(String path) {
        MonetaPortalEndpoint ep = getEndpoint(MonetaPortalEndpoint.class);
        ep.waitForElementsToLoad(3, path);
        return ep.getText(path);
    }
}