package com.playwrightdemo.test;

import com.playwrightdemo.pages.LeavesPage;
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 java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;

public class LeavesPageTest extends BaseTest{

    LoginPage loginPage;

    LeavesPage leavesPage;
    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();
        leavesPage=new LeavesPage();

    }

    @Test (enabled = true)

    public void verifyLeavesMenu(){

        Assert.assertEquals(leavesPage.getLeaveMenuTxt().trim(),"Leaves");
        logTestMethodName("passed");
    }

    @Test  (enabled = true)

    public void verifyLeavesSlideOutMenu(){


        leavesPage.selectLeavesMenu();
        System.out.println(leavesPage.getHolidayCalendarMenuText());
        Assert.assertTrue(leavesPage.getHolidayCalendarMenuText().contains("Holiday Calender"));
        logTestMethodName("passed");
        System.out.println(leavesPage.getLeaveApplicationMenuText());
        Assert.assertTrue(leavesPage.getLeaveApplicationMenuText().contains("Leave Application"));
        logTestMethodName("passed");
    }

    @Test  (enabled = true)

    public void verifyLeaveApplicationPageNavigation(){

        leavesPage.selectLeavesMenu();
        leavesPage.clickLeaveAppliationLink();
        Assert.assertEquals(leavesPage.getLeaveAplicationPageText().trim(),"Manage Leave Applications");
        logTestMethodName("passed");

    }

    @Test  (enabled = true)

    public void verifyHolidayCalendarPageNavigation(){

        leavesPage.selectLeavesMenu();
        leavesPage.clickHolidayCalendarLink();
        Assert.assertEquals(leavesPage.getHolidayCalendarPageText().trim(),"Holiday");
        Assert.assertEquals(leavesPage.getRHHolidayCalendarPageText().trim(),"Restricted Holiday");
        logTestMethodName("passed");

    }

    @Test (enabled = true)

    public void verifyApplyPL() {

        LocalDate dateToSelect= LocalDate.now().plusDays(17);
        String formattedDateAfter=dateToSelect.format(DateTimeFormatter.ofPattern("dd"));

        leavesPage.selectLeavesMenu();
        leavesPage.clickLeaveAppliationLink();
        leavesPage.selectLeaveTypeFromDropDown("PL");
        leavesPage.selectFullDayLeaveTypeRadioButton();
        leavesPage.selectleaveFromDate(formattedDateAfter);
        leavesPage.selectleaveToDate(formattedDateAfter);
        leavesPage.enterReasonLeaveForm("This is for testing purpose, Please Reject Accordingly");
        leavesPage.clickSubmit();
        Assert.assertEquals(leavesPage.getMessageDiscussionWithRmPopup(),"Have you discussed this leave with your RM?");
        leavesPage.confirmRMDiscussionPopup();
        Assert.assertTrue(leavesPage.getLeaveApplySuccessMsg().contains("You Have Applied For The Leave Successfully!"));


    }



    @Test

    public void verifyApplyCL(){

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

        leavesPage.selectLeavesMenu();
        leavesPage.clickLeaveAppliationLink();
        leavesPage.selectLeaveTypeFromDropDown("CL");
        leavesPage.selectFullDayLeaveTypeRadioButton();
        leavesPage.selectleaveFromDate(formattedDateBefore);
        leavesPage.selectleaveToDate(formattedDateBefore);
        leavesPage.enterReasonLeaveForm("This is for testing purpose, Please Reject Accordingly");
        leavesPage.clickSubmit();
        Assert.assertEquals(leavesPage.getMessageDiscussionWithRmPopup(),"Have you discussed this leave with your RM?");
        leavesPage.cancelRMDiscussionPopup();


    }

    @Test
    public void verifyApplyAL(){

        LocalDate dateToSelect= LocalDate.now().plusDays(18);
        String formattedDateAfter=dateToSelect.format(DateTimeFormatter.ofPattern("dd"));

        leavesPage.selectLeavesMenu();
        leavesPage.clickLeaveAppliationLink();
        leavesPage.selectLeaveTypeFromDropDown("AL");
        leavesPage.selectleaveFromDate(formattedDateAfter);
        leavesPage.selectleaveToDate(formattedDateAfter);
        leavesPage.enterReasonLeaveForm("This is for testing purpose, Please Reject Accordingly");
        leavesPage.clickSubmit();
        Assert.assertEquals(leavesPage.getMessageDiscussionWithRmPopup(),"Have you discussed this leave with your RM?");
        leavesPage.cancelRMDiscussionPopup();

    }

    @Test
    public void verifyApplyRH (){

        LocalDate dateToSelect= LocalDate.now().plusDays(19);
        String formattedDateAfter=dateToSelect.format(DateTimeFormatter.ofPattern("dd"));

        leavesPage.selectLeavesMenu();
        leavesPage.clickLeaveAppliationLink();
        leavesPage.selectLeaveTypeFromDropDown("RH");
        leavesPage.selectleaveFromDate(formattedDateAfter);
        leavesPage.selectleaveToDate(formattedDateAfter);
        leavesPage.enterReasonLeaveForm("This is for testing purpose, Please Reject Accordingly");
        leavesPage.clickSubmit();
        Assert.assertEquals(leavesPage.getMessageDiscussionWithRmPopup(),"Have you discussed this leave with your RM?");
        leavesPage.cancelRMDiscussionPopup();

    }


@Test

public void verifyApplyLWP() {

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

    leavesPage.selectLeavesMenu();
    leavesPage.clickLeaveAppliationLink();
    leavesPage.selectLeaveTypeFromDropDown("LWP");
    leavesPage.selectFullDayLeaveTypeRadioButton();
    leavesPage.selectleaveFromDate(formattedDateBefore);
    leavesPage.selectleaveToDate(formattedDateBefore);
    leavesPage.enterReasonLeaveForm("This is for testing purpose, Please Reject Accordingly");
    leavesPage.clickSubmit();
    Assert.assertEquals(leavesPage.getMessageDiscussionWithRmPopup(),"Have you discussed this leave with your RM?");
    leavesPage.cancelRMDiscussionPopup();

}

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

public void verifyLeaveSearch() {

    LocalDate dateToSelect= LocalDate.now().plusDays(17);
    String formattedDateAfter=dateToSelect.format(DateTimeFormatter.ofPattern("dd"));

    leavesPage.selectLeavesMenu();
    leavesPage.clickLeaveAppliationLink();

    leavesPage.selectFromDateleaveReqList(formattedDateAfter);
    leavesPage.selectToDateleaveReqList(formattedDateAfter);
    leavesPage.selectLeaveStatusFromLeaveReqListDropdown("Pending for Rm Approval");
    leavesPage.searchLeaveReqList();

    Assert.assertTrue(leavesPage.getResultLeaveReqSearch().containsAll(Arrays.asList("PL","Pending for RM Approval","Jul 15th 2024")));


}




}
