package cz.moneta.test.dsl.hypos.pages.hypokom;

import cz.moneta.test.dsl.hypos.pages.HyposPageFlow;
import cz.moneta.test.harness.context.StoreAccessor;
import cz.moneta.test.harness.endpoints.hypos.HyposEndpoint;
import cz.moneta.test.harness.support.web.Click;
import cz.moneta.test.harness.support.web.CustomAction;
import cz.moneta.test.harness.support.web.TypeInto;
import cz.moneta.test.harness.support.web.Wait;
import org.openqa.selenium.TimeoutException;

import static cz.moneta.test.dsl.hypos.pages.hypokom.HypokomContactDetailPage.CONDITIONS_AND_SYMPTOMS_TAB_XPATH;

@Wait(CONDITIONS_AND_SYMPTOMS_TAB_XPATH)
public interface HypokomContactDetailPage extends HyposPageFlow<HypokomContactDetailPage>, StoreAccessor {
    String CONDITIONS_AND_SYMPTOMS_TAB_XPATH = "//a[text()='Stavy a příznaky']";
    String FLAG_DATE_XPATH = "//input[@id='flag-limit']";
    String SET_BUTTON_XPATH = "//input[@id='butt-set-flag']";
    String CLOSE_BUTTON_XPATH = "//a[contains(@class, 'simplemodal-close')]";

    @Click(CONDITIONS_AND_SYMPTOMS_TAB_XPATH)
    HypokomContactDetailPage clickConditionsAndSymptomsTab();

    @TypeInto(FLAG_DATE_XPATH)
    HypokomContactDetailPage fillFlagDate(String date);

    @CustomAction
    default HypokomContactDetailPage clickSetButtonAndDismissAlert() {
        HyposEndpoint endpoint = getEndpoint(HyposEndpoint.class);
        endpoint.click(() -> SET_BUTTON_XPATH);
        //Obe upozorneni se zobrazuji jenom v IE na PC s Outlookem
        try {
            endpoint.dismissAlert(10);
        } catch (TimeoutException e) {
        }
        try {
            endpoint.acceptAlert(10);
        } catch (TimeoutException e) {
        }
        // V headless mode se zde objeví neovladatelné okno, které ovlivňuje zbytek testu
        if(endpoint.isHeadlessModeOn()) {
            endpoint.switchToOtherWindow(10,2);
            endpoint.closeCurrentWindow();
            endpoint.switchToOtherWindow(10,1);
        }
        return null;
    }

    @Click(CLOSE_BUTTON_XPATH)
    HypokomSearchPage clickCloseButton();
}
