package com.playwrightdemo.test;

import com.playwrightdemo.pages.AttendancePage;
import com.playwrightdemo.pages.BasePage;
import com.playwrightdemo.pages.DashboardPage;
import com.playwrightdemo.pages.LoginPage;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;

public class AttendancePageTest extends BaseTest {

    AttendancePage attendancePage;

    LoginPage loginPage;
    DashboardPage dashboardPage;

    Logger logger = LogManager.getLogger(this.getClass());

    private void logTestMethodName(String status) {
        StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
        String methodName = stackTraceElements[2].getMethodName();
        logger.info("Test method '" + methodName + "' " + status);
    }


    @BeforeSuite

    public void setUP() throws IOException {

        intialization();

        loginPage = new LoginPage();
        loginPage.doLogin();
        attendancePage = new AttendancePage();


    }

    @Test(enabled = true)

    public void verifyAttendanceSlideOutMenu() throws InterruptedException {

        attendancePage.selectAttendanceMenu();

        Assert.assertTrue(attendancePage.getApplyAttendanceMenuName().contains("Apply Attendance"));
        Assert.assertTrue(attendancePage.getAttendanceCalendarMenuName().contains("Attendance Calendar"));

        logTestMethodName("passed");


    }

    @Test(enabled = true)


    public void verifyApplyAttendanceLandingPage() throws InterruptedException {

        attendancePage.selectAttendanceMenu();
        attendancePage.clickApplyAttendanceLink();
        Assert.assertEquals(attendancePage.getApplyAttendancePageText().trim(), "Apply Your Attendance");
        logTestMethodName("passed");

    }

    @Test(enabled = true)


    public void verifyAttendanceCalendarLandingPage() throws InterruptedException {

        attendancePage.selectAttendanceMenu();
        attendancePage.clickAttendanceCalLink();
        Assert.assertEquals(attendancePage.getAttendanceCalPageText().trim(), "Attendance Calendar");
        logTestMethodName("passed");

    }


    @Test(enabled = true)

    public void verifyWorkFromHomeApply() throws InterruptedException {


        attendancePage.selectAttendanceMenu();
        attendancePage.clickApplyAttendanceLink();

        attendancePage.getDataFromCalendar("19");

        Thread.sleep(5000);

        attendancePage.selectValueFromReasonDropdown("Work From Home");

        attendancePage.setPunchInTime(10, 30);

        attendancePage.setPunchOutTime(20, 30);

        attendancePage.enterValidReason("This is for testing purpose, please don't approve");

        attendancePage.submitAttendanceForm();

        attendancePage.acceptPopup();

        Assert.assertTrue(attendancePage.getAttendanceFormSubmissionSuccess().contains("You Have Applied Successfully"));

        logTestMethodName("passed");
    }

    @Test(enabled = true)

    public void verifyinvalidWorkFromHomeApply() throws InterruptedException {


        attendancePage.selectAttendanceMenu();
        attendancePage.clickApplyAttendanceLink();

        attendancePage.getDataFromCalendar(BasePage.selectBeforeDate(2));

        Thread.sleep(5000);

        attendancePage.selectValueFromReasonDropdown("Work From Home");

        attendancePage.setPunchInTime(10, 30);

        attendancePage.setPunchOutTime(20, 30);

        attendancePage.enterValidReason("This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence test test test test test test ");

        attendancePage.submitAttendanceForm();

        attendancePage.acceptPopup();

        Assert.assertTrue(attendancePage.getAttendanceFormSubmissionError().contains("Whoops! There were some problems with your input."));

        logTestMethodName("passed");
    }


    @Test(enabled = true)

    public void verifyMissedInPunchApply() throws InterruptedException {


        attendancePage.selectAttendanceMenu();

        attendancePage.clickApplyAttendanceLink();

        attendancePage.getDataFromCalendar("16");

        attendancePage.selectValueFromReasonDropdown("Missed In Punch");

        attendancePage.setPunchInTime(11, 30);

        attendancePage.enterValidReason("This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence test test test test test test ");

        attendancePage.submitAttendanceForm();

        attendancePage.acceptPopup();

        Assert.assertTrue(attendancePage.getAttendanceFormSubmissionError().contains("Whoops! There were some problems with your input."));

        logTestMethodName("passed");

    }

    @Test(enabled = true)

    public void verifyMissedOutPunchApply() throws InterruptedException {


        attendancePage.selectAttendanceMenu();
        attendancePage.clickApplyAttendanceLink();

        attendancePage.getDataFromCalendar("17");

        attendancePage.selectValueFromReasonDropdown("Missed Out Punch");

        attendancePage.setPunchOutTime(22, 30);

        attendancePage.enterValidReason("This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence test test test test test test ");

        attendancePage.submitAttendanceForm();

        attendancePage.acceptPopup();

        Assert.assertTrue(attendancePage.getAttendanceFormSubmissionError().contains("Whoops! There were some problems with your input."));

        logTestMethodName("passed");

    }


    @Test(enabled = true)

    public void verifyOutForMeetingApply() throws InterruptedException {


        attendancePage.selectAttendanceMenu();
        attendancePage.clickApplyAttendanceLink();

        attendancePage.getDataFromCalendar("17");

        attendancePage.selectValueFromReasonDropdown("Out For Meeting");

        attendancePage.setPunchInTime(12, 30);

        attendancePage.setPunchOutTime(22, 30);

        attendancePage.enterValidReason("This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence test test test test test test ");

        attendancePage.submitAttendanceForm();

        attendancePage.acceptPopup();

        Assert.assertTrue(attendancePage.getAttendanceFormSubmissionError().contains("Whoops! There were some problems with your input."));


    }


    @Test(enabled = true)

    public void verifyBioMetricIssueApply() throws InterruptedException {


        attendancePage.selectAttendanceMenu();
        attendancePage.clickApplyAttendanceLink();

        attendancePage.getDataFromCalendar("14");

        attendancePage.selectValueFromReasonDropdown("Bio Metric Issue");

        attendancePage.setPunchInTime(12, 30);

        attendancePage.setPunchOutTime(22, 30);

        attendancePage.enterValidReason("This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence This is a test check for excellence test test test test test test ");

        attendancePage.submitAttendanceForm();

        attendancePage.acceptPopup();

        Assert.assertTrue(attendancePage.getAttendanceFormSubmissionError().contains("Whoops! There were some problems with your input."));


    }


    @Test(enabled = true)

    public void verifyAttendanceListSection() {

        attendancePage.selectAttendanceMenu();
        attendancePage.clickApplyAttendanceLink();

        Assert.assertEquals(attendancePage.getAttendanceListext(), "Attendance Request List");
    }


    @Test(enabled = true)

    public void verifyAttendanceListSearch() throws InterruptedException {

        attendancePage.selectAttendanceMenu();
        attendancePage.clickApplyAttendanceLink();

        attendancePage.getDataFromCalendarAttndlist("13");
        attendancePage.getDataToCalendarAttndlist("13");
        attendancePage.clickSearchButton();

        Thread.sleep(8000);
        Assert.assertTrue(attendancePage.getResultTableData().get(0).toString().contains("13"), "Same Date is shown");
        Assert.assertEquals(attendancePage.getResultTableData().get(3).toString(), "Work tt From Home");

    }

    @Test(enabled = true)

    public void verifyAttendanceCalendarSearch() throws InterruptedException {
        attendancePage.selectAttendanceMenu();
        attendancePage.clickAttendanceCalLink();

        attendancePage.selectEmpDropdown("Ankit Srivastava");
        attendancePage.selectfromDateAttndCalendar("3");
        attendancePage.selectToDateAttndCalendar("3");

        attendancePage.selectEmpModeDropdown("Employee");
        attendancePage.clickSearchAttendanceCalendar();

        Assert.assertTrue(attendancePage.getAverageTimeSpent().contains(attendancePage.getAttendanceCalendarTableData().get(6).toString()));

    }
}