package com.playwrightdemo.test;

import com.playwrightdemo.pages.AttendancePage;
import com.playwrightdemo.pages.LoginPage;
import com.playwrightdemo.pages.WorklogPage;
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.Test;
import com.playwrightdemo.pages.LoginPage;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;

public class WorklogPageTest extends BaseTest {


    LoginPage loginPage;

    WorklogPage worklogPage;
    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();
        worklogPage=new WorklogPage();

    }

    @Test(enabled = true)

    public void verifyWorkLogMenu() {

        Assert.assertEquals(worklogPage.getWorklogMenuText(),"Worklog");

        logTestMethodName("passed");

    }

    @Test(enabled = true)

    public void verifyWorkLogSlideOutMenu() {
        worklogPage.selectWorklogMenu();

        Assert.assertTrue(worklogPage.getFillWorklogMenuText().contains("Fill Worklog"));

        logTestMethodName("passed");

        Assert.assertTrue(worklogPage.getWorklogReportMenuText().contains("Worklog Report"));
        logTestMethodName("passed");

    }

    @Test(enabled = true)

    public void verifyFillWorkLogPageNavigation() {

        worklogPage.selectWorklogMenu();
        worklogPage.clickFillWorklogLink();
        Assert.assertEquals(worklogPage.getWorklogReportPageText(),"Worklog");
        logTestMethodName("passed");
    }


    @Test(enabled = true)

    public void verifyWorkLogReportPageNavigation() {

        worklogPage.selectWorklogMenu();
        worklogPage.clickWorklogReportLink();
        Assert.assertEquals(worklogPage.getWorklogReportPageText(),"Worklog Report");
        logTestMethodName("passed");

    }

    @Test(enabled = true)

    public void verifyFillWorkLogButton() {

        worklogPage.selectWorklogMenu();
        worklogPage.clickFillWorklogLink();
        Assert.assertEquals(worklogPage.getFillWorklogButtonText(),"Fill Worklog");
        logTestMethodName("passed");

    }

    @Test(enabled = true)

    public void verifyFillWorkLogFormTitle() throws InterruptedException {

        worklogPage.selectWorklogMenu();
        Thread.sleep(5000);
        worklogPage.clickFillWorklogLink();
        worklogPage.clickFillWorkLogButon();
        Thread.sleep(5000);
        Assert.assertEquals(worklogPage.getWorkLogFormTitle().trim(),"add your worklog");
        logTestMethodName("passed");

    }

    @Test(enabled = true, dependsOnMethods ={"verifyFillWorkLogFormTitle"})

    public void verifyFillWorkLogForm() throws InterruptedException {

        worklogPage.selectWorklogMenu();
        worklogPage.clickFillWorklogLink();
        worklogPage.clickFillWorkLogButon();
        worklogPage.selectProjDropdownWorkLogForm();
        //worklogPage.enterTimeSpentWorkLogForm();
        worklogPage.enterTaskDescription();
        worklogPage.clickSaveButtonWorkLogForm();
        Assert.assertTrue(worklogPage.getWorkLogFormSubmitSuccessMessage().contains("Worklog submitted successfully"));


    }


    @Test(enabled = true,dependsOnMethods ={"verifyFillWorkLogForm"})

    public void verifyDeleteWorkLog() throws InterruptedException {

        Thread.sleep(5000);
        worklogPage.selectWorklogMenu();
        worklogPage.clickFillWorklogLink();
        worklogPage.deleteWorkogEntry();
        Assert.assertEquals(worklogPage.getDeleteConfrmationpPopupText(),"Are you sure you want to delete this worklog ?");
        worklogPage.confirmDeletePopUp();
        Assert.assertTrue(worklogPage.getWorklogEntyDeleteSuccessMessage().contains("Successfully deleted the worklog!"));


    }


    @Test(enabled = true)

    public void verifySearchWorkLogReport() throws InterruptedException {

        LocalDate dateToSelect= LocalDate.now().minusDays(2);
      String formattedDateBefore=dateToSelect.format(DateTimeFormatter.ofPattern("dd"));

        worklogPage.selectWorklogMenu();
        worklogPage.clickWorklogReportLink();
        worklogPage.selectUnitDropdown(new String[]{"ISHIR"});
        // worklogPage.selectSupervisorDropdown();
        Thread.sleep(20000);
        worklogPage.selectResourcesDropdown(new String[]{"Nitin Paliwal","Saravanan Namburajan"});
        Thread.sleep(20000);
        worklogPage.selectFromDateWorklogReport(formattedDateBefore);
        worklogPage.selectToDateWorklogReport(formattedDateBefore);
        worklogPage.clickSearchButton();
        Thread.sleep(20000);

        Assert.assertTrue(worklogPage.getWorkLogTableData().containsAll(Arrays.asList("Nitin Paliwal","Saravanan Namburajan")));

        Thread.sleep(20000);
        logTestMethodName("passed");

    }







}
