package com.playwrightdemo.pages;

import java.time.LocalDate;
import java.util.List;

public class LeavesPage extends BasePage {

    String txtLeavesMenu = "li[title='Leave Management']";
    String txtMenuHolidayCalendarLink = "a:has-text('Holiday Calender')";
    String txtMenuLeaveApplicationLink = "a:has-text('Leave Application')";
    String txtTitleHolidayCal = "div.card-body h5[class*='red_border']";
    String txtTitleRestrictedHolidayCal = "div.card-body h5[class*='blue_border']";
    String txtTitleManageLeaves = "h5:has-text('Manage Leave Applications')";
    String txtTitleBalanceLeave = "h5:has-text('Balance Leave')";
    String txtTitleLeaveReqList = "h5:has-text('Leave Request List')";

    String dropdownLeaveType = "select#leave_type";
    String btnRadioFullDay = "input[type='radio'][id='from_date_fullday']";
    String btnRadioHalfDay = "input[type='radio'][id='from_date_half']";
    String getBtnRadioShortLeave = "input[type='radio'][id='from_date_quarter']";

    String txtAreaReasonLeaveForm = "textarea#reason";

    String btnSubmitleaveForm = "button#submit-leave";

    String leaveCalStartDate = "div#planned_start_date";
    String leaveCalEndDate = "div#planned_end_date";

    String dayPicker = "//table[@class='table-condensed']//td[@class='day'] [text()='%s']";

    String txtRMDiscussionPopupMsg = "div.modal-dialog div.bootstrap-dialog-message";
    String btnOKRMDiscussion = "div.modal-footer button.btn.btn-primary";

    String btnCancelRMDiscussion = "button.btn.btn-default";

    String txtLeaveApplySucess="div:has-text('Success. You Have Applied For The Leave Successfully! ')";

    String leaveListEndDate="input#dateto";
    String leaveListStartDate="input#datefrom";

    String btnSearchLeaveReqList="button#btnsearch";

    String dropdownLeaveStatusLeaveReq="select#status_id";

    String  resultsLeaveReqSearchTable="table#dataTables-leave tbody";

    public String getLeaveMenuTxt() {

        return getTextFromSelector(txtLeavesMenu);
    }


    public void selectLeavesMenu() {

        doClickSelector(txtLeavesMenu);

    }

    public String getHolidayCalendarMenuText() {

        return getTextFromSelector(txtMenuHolidayCalendarLink);

    }

    public String getLeaveApplicationMenuText() {

        return getTextFromSelector(txtMenuLeaveApplicationLink);
    }

    public void clickHolidayCalendarLink() {

        doClickSelector(txtMenuHolidayCalendarLink);
    }

    public void clickLeaveAppliationLink() {

        doClickSelector(txtMenuLeaveApplicationLink);
    }

    public String getHolidayCalendarPageText() {

        return getTextFromSelector(txtTitleHolidayCal);
    }

    public String getRHHolidayCalendarPageText() {

        return getTextFromSelector(txtTitleRestrictedHolidayCal);
    }


    public String getLeaveAplicationPageText() {

        return getTextFromSelector(txtTitleManageLeaves);
    }

    public void selectLeaveTypeFromDropDown(String leaveType) {

        selectFromDropdown(dropdownLeaveType, leaveType);

    }

    public void selectFullDayLeaveTypeRadioButton() {

        doClickSelector(btnRadioFullDay);
    }

    public void selectHalfDayLeaveTypeRadioButton() {

        doClickSelector(btnRadioHalfDay);

    }

    public void selectShortLeaveTypeRadioButton() {

        doClickSelector(getBtnRadioShortLeave);
    }


    public void selectleaveFromDate(String startDateValue) {


        doClickSelector(leaveCalStartDate);
        doClickSelector(String.format(dayPicker, startDateValue));

    }

    public void selectleaveToDate(String endDateValue) {

        doClickSelector(leaveCalEndDate);
        doClickSelector(String.format(dayPicker, endDateValue));

    }

    public void enterReasonLeaveForm(String reasonForLeave) {

        fillFields(txtAreaReasonLeaveForm, reasonForLeave);

    }

    public void clickSubmit() {

        doClickSelector(btnSubmitleaveForm);

    }

    public void getBalanceLeaveSectionTitle() {

        getTextFromSelector(txtTitleBalanceLeave);
    }

    public String getMessageDiscussionWithRmPopup() {

        return getTextFromSelector(txtRMDiscussionPopupMsg);


    }

    public void confirmRMDiscussionPopup() {

        doClickSelector(btnOKRMDiscussion);
    }

    public void cancelRMDiscussionPopup() {

        doClickSelector(btnCancelRMDiscussion);
    }

    public String getLeaveApplySuccessMsg() {

        return getTextFromSelector(txtLeaveApplySucess);
    }

    public void selectFromDateleaveReqList(String startDateValue) {



        doClickSelector(leaveListStartDate);
        doClickSelector(String.format(dayPicker, startDateValue));

    }

    public void selectToDateleaveReqList(String endDateValue) {



        doClickSelector(leaveListEndDate);
        doClickSelector(String.format(dayPicker, endDateValue));

    }

    public void selectLeaveStatusFromLeaveReqListDropdown (String leaveStatus) {
        selectFromDropdown(dropdownLeaveStatusLeaveReq,leaveStatus);


    }


public void searchLeaveReqList() {

        doClickSelector(btnSearchLeaveReqList);
}

public List<String> getResultLeaveReqSearch() {

    String rowsLocator=resultsLeaveReqSearchTable+" "+"tr";
    String rowDataLocator=rowsLocator+" "+"td";
    List<String> rowdata=  page.locator(rowDataLocator).allInnerTexts();
    return rowdata;


}


}
