package com.intland.codebeamer.functionaltest.test;

import com.codeborne.selenide.testng.ScreenShooter;
import com.intland.codebeamer.functionaltest.helper.Helper;
import com.intland.codebeamer.functionaltest.page.CreateUserPage;
import com.intland.codebeamer.functionaltest.page.LoginPage;
import com.intland.codebeamer.functionaltest.page.LostPasswordPage;
import com.intland.codebeamer.functionaltest.page.mystart.EditAccountPage;
import com.intland.codebeamer.functionaltest.page.mystart.MyStartPage;
import com.intland.codebeamer.functionaltest.page.SentPasswordPage;
import com.intland.codebeamer.functionaltest.page.systemadmin.SystemAdminPage;
import com.intland.codebeamer.functionaltest.page.systemadmin.UsersPage;
import org.apache.commons.lang3.RandomStringUtils;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import static com.codeborne.selenide.Condition.text;
import static com.codeborne.selenide.Condition.visible;
import static com.codeborne.selenide.Selenide.close;
import static com.codeborne.selenide.Selenide.open;
import static com.codeborne.selenide.WebDriverRunner.getWebDriver;
import static org.testng.Assert.assertTrue;

@Listeners(ScreenShooter.class)
public class AuthenticationTests extends BaseTest {

    private String username;

    @Test
    public void testInvalidLogin() {
        getWebDriver().manage().window().maximize();
        LoginPage loginPage = open("/login.spr", LoginPage.class);
        loginPage.setUsername("bond");
        loginPage.setPassword("0070");
        loginPage.clickOnLogin();
        loginPage.getInfoMessages().shouldHave(text("The entered username is invalid!"));
    }

    @Test
    public void testValidLogin() {
        getWebDriver().manage().window().maximize();
        LoginPage loginPage = open("/login.spr", LoginPage.class);
        loginPage.setUsername("bond");
        loginPage.setPassword("007");
        MyStartPage myStartPage = loginPage.clickOnLogin();
        myStartPage.getMyStartMenu().shouldBe(visible);
        loginPage = myStartPage.clickOnLogout();
        loginPage.getLoginMenu().shouldBe(visible);
    }

    @Test
    public void testLostPassword() {
        getWebDriver().manage().window().maximize();
        LoginPage loginPage = open("/login.spr", LoginPage.class);
        LostPasswordPage lostPasswordPage = loginPage.clickOnForgotIt();
        lostPasswordPage.setAccountName("test-user-sel-1109411");
        SentPasswordPage sentPasswordPage = lostPasswordPage.clickOnSenMyPasswordInEmail();
        sentPasswordPage.getMessage().shouldHave(text("The password reset URL was sent you by email, Please check your mailbox."));
    }

    @Test
    public void testNotAMemberYet() {
        getWebDriver().manage().window().maximize();
        LoginPage loginPage = open("/login.spr", LoginPage.class);
        CreateUserPage createUserPage = loginPage.clickOnNotAMemberYetRegisterNow();
        username = RandomStringUtils.randomAlphanumeric(10);
        createUserPage.setUserName(username);
        createUserPage.setPassword(username);
        createUserPage.setConfirmPassword(username);
        createUserPage.setFirstName("Elek");
        createUserPage.setLastName("Test");
        createUserPage.setEmail(username + "@mail.ru");
        MyStartPage myStartPage = createUserPage.clickOnSave();
        assertTrue(myStartPage.isPageLoaded());
        //after
        myStartPage = Helper.loginWithSystemAdmin();
        SystemAdminPage systemAdminPage = myStartPage.clickOnSystemAdmin();
        UsersPage usersPage = systemAdminPage.clickOnUserAccounts();
        usersPage.fillFilter(username);
        usersPage.clickOnGo();
        EditAccountPage editAccountPage = usersPage.clickOnEditInContextMenu(username);
        editAccountPage.selectStatus(CreateUserPage.Status.DISABLED);
        editAccountPage.clickOnSave();
    }

    @AfterMethod
    public void afterMethod() {
        close();
    }
}