package com.playwrightdemo.pages;

import com.microsoft.playwright.Locator;
import com.microsoft.playwright.Page;
import com.playwrightdemo.Base.TestBase;
import com.playwrightdemo.utils.ConfigReader;
import com.playwrightdemo.utils.PlayWrightBrowserFactory;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

public class BasePage extends TestBase {


    BasePage() {
        super();
    }

    public void fillFields(String locator, String key) {

        page.fill(locator, key);

    }


    public void doClickSelector(String selector) {

        page.click(selector);

    }

    public String getTextFromSelector(String selector) {

        return page.textContent(selector);

    }

    public String getInnerTextFromSelector(String selector) {

        return page.innerText(selector);

    }


    public void selectFromDropdown(String selectlocator ,String dropdownValue) {


        page.selectOption(selectlocator,dropdownValue);

    }

    public void SelectFromMultiSelectDropdown(String selectlocator ,String[] dropdownValues) {

        page.selectOption(selectlocator,dropdownValues);

    }





    public void clearField(String selector){

        page.locator(selector).clear();
    }

public void jsAlertAccept() {

        page.onDialog(dialog -> { System.out.println("The popup is displaying :"+dialog.message());
            dialog.accept("Selected Yes over prompt");});


}


    public void jsAlertDismiss() {

        page.onDialog(dialog -> { System.out.println("The popup is displaying :"+dialog.message());
            dialog.accept("Selected No over prompt");});

    }
    public static String selectBeforeDate(long numberOfDaysBefore){


       String beforeDate =LocalDate.now().minusDays(numberOfDaysBefore).format(DateTimeFormatter.ofPattern("dd"));

       return beforeDate;
    }
    public static String selectAfterDate(long numberOfDaysAfter) {


        String afterDate =  LocalDate.now().plusDays(numberOfDaysAfter).format(DateTimeFormatter.ofPattern("dd"));

        return afterDate;

    }


public static void selectHourTimer(int hour, String selector) {

    for (int i = 1; i <= hour; i++) {

        page.click(selector);

    }


}

    public static void selectMinuteTimer(int minutes ,String selector ) {

        for (int i = 1; i <= minutes/2; i++) {

            page.click(selector);

        }

    }



}
