package cz.moneta.test.dsl.auto.smartauto.setman;

import cz.moneta.test.dsl.Harness;
import cz.moneta.test.dsl.auto.smartauto.SmartAutoFlow;
import cz.moneta.test.harness.support.web.*;

import static cz.moneta.test.dsl.auto.smartauto.setman.LoginPage.*;

@Wait(value = {LOGO}, waitSecondsForElement = 40)
public interface LoginPage extends SmartAutoFlow<LoginPage> {

    String USERNAME_INPUT = "//input[@type='text']";
    String PASSWORD_INPUT = "//input[@type='password']";
    String LOGIN_BUTTON = "//button[@type='submit']";
    String UNAUTHORIZED_ACCESS = "//div[contains(text(), '403')]";
    String LOGOUT = "//span[contains(@class, 'logout')]";
    String LOGO = "//img[contains(@alt, 'MONETA Auto company logo')]";

    @TypeInto(value=USERNAME_INPUT)
    LoginPage typeUsername(String userName);

    @TypeInto(PASSWORD_INPUT)
    LoginPage typePassword(String password);

    @Click(LOGIN_BUTTON)
    LoginPage clickLoginToFail();

    @Wait(value = UNAUTHORIZED_ACCESS, until = Until.VISIBLE)
    @CheckElementPresent(UNAUTHORIZED_ACCESS)
    LoginPage checkUnauthorizedAccess();

    @Click(LOGIN_BUTTON)
    MainPage clickLogin();

    @Click(LOGOUT)
    LoginPage logout();


    @CustomAction
    default LoginPage logoutIfNecessary(Harness harness) {
        String url = harness.getConfig("selenium.grid.shared.url");
        if (url == null) {
            logout();
        }
        return null;
    }
}