package Candidate_Assesment;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.PageFactory;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import RevalERP_HRMS.basesetup.BaseSetup;
import RevalERP_HRMS.elements.CandidateAssessmentElements;
import RevalERP_HRMS.excels.Excels;
import RevalERP_HRMS.utils.CommonUtils;
import jxl.Sheet;

public class Candidate_Assessment_Script extends BaseSetup {
	public CandidateAssessmentElements Elements;
	public Sheet sheet1;

	//Before Executing the Script make Sure that Remark, CV source and Technology are created in master module

	public String Department_Creation = "CandidateAssessmentScript";

	@BeforeClass
	public void setUp() {
		driver = getDriver();
		CommonUtils.ModuleName = "Candidate_Assessment_Script";
		driver.manage().window().maximize();
		// Check the URL in BaseSetup Class
		driver.get(baseUrl);
		CommonUtils.Reports("Candidate_Assessment_Script.html", "Candidate_Assessment_Script");
		Elements = PageFactory.initElements(driver, CandidateAssessmentElements.class);
		// Check the User Credentials in Common utils class
		CommonUtils.login();
		sheet1 = CommonUtils.readExcelSheet(Excels.filepath2 + "Cancdidate_Assesement_Test_Data.xls", 0);
		driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
	}

	@Test(priority = 1)
	public void TC_001() throws IOException, AWTException {

		CommonUtils.HRMsMenu();
		CommonUtils.scrollTOElement(Elements.CondidateAssessmentModule, driver);
		CommonUtils.CaptureScreenshot_for_IsDisplayed(driver, Elements.CondidateAssessmentModule, "TC-02", "CondidateAssessment Module is displaying ", "CondidateAssessment Module is not  displaying");
		try {
			CommonUtils.Thread_Sleep(3);
			CommonUtils.JS_Click(Elements.CondidateAssessmentModule);	
		} catch (Exception E) {
			CommonUtils.Thread_Sleep(3);
			Elements.CondidateAssessmentModule.click();
		}
	}

	@Test(priority = 2)
	public void TC_003() throws IOException {
		CommonUtils.Thread_Sleep(3);
		Elements.AddCanndidateAssessmentButton.click();
		CommonUtils.Thread_Sleep(3);
		Elements.RecruiterNameDropdown.click();
		CommonUtils.Thread_Sleep(2);
		Elements.RecruiterNameDropdownOption.click();
		CommonUtils.Thread_Sleep(3);
		Elements.DateOfContactDatePicker.click();
		CommonUtils.Thread_Sleep(3);
		try {
			String Current_Date = CommonUtils.getCurrentDay();
			String date_1 = CommonUtils.getCurrentDatePlus(1);
			int Selectiondate = Integer.parseInt(date_1);
			int currentdate = Integer.parseInt(Current_Date);

			System.out.println("date:"+date_1);
			if(currentdate>10) {
				if(Selectiondate<10) {
					driver.findElement(By.xpath("(//*[@class='xdsoft_next'])[1]")).click();
					CommonUtils.Thread_Sleep(3);
					driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-2]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
				}else {
					List<WebElement> dates = driver.findElements(By.xpath("((//*[@class='xdsoft_calendar'])[last()-2]/table/tbody/tr/td/div[text()='"+date_1+"'])"));
					String date=dates.get(0).getText();
					int d=Integer.parseInt(date);
					if(dates.size()==2) {
						driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-2]/table/tbody/tr/td/div[text()='"+date_1+"'])[2]")).click();

					}else {
						driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-2]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
					}
				}
			}else {
				driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-2]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
			}
		}catch (Exception e) {
		}
		CommonUtils.Thread_Sleep(3);
		Elements.FollowUpDate_DatePicker.click();
		CommonUtils.Thread_Sleep(3);
		try {
			String Current_Date = CommonUtils.getCurrentDay();
			String date_1 = CommonUtils.getCurrentDatePlus(1);
			int Selectiondate = Integer.parseInt(date_1);
			int currentdate = Integer.parseInt(Current_Date);

			System.out.println("date:"+date_1);
			if(currentdate>10) {
				if(Selectiondate<10) {
					driver.findElement(By.xpath("(//*[@class='xdsoft_next'])[3]")).click();
					CommonUtils.Thread_Sleep(3);
					driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-1]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
				}else {
					List<WebElement> dates = driver.findElements(By.xpath("((//*[@class='xdsoft_calendar'])[last()-1]/table/tbody/tr/td/div[text()='"+date_1+"'])"));
					String date=dates.get(0).getText();
					int d=Integer.parseInt(date);
					if(dates.size()==2) {
						driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-1]/table/tbody/tr/td/div[text()='"+date_1+"'])[2]")).click();

					}else {
						driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-1]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
					}
				}
			}else {
				driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-1]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
			}
		}catch (Exception e) {
		}
		CommonUtils.Thread_Sleep(3);
		Elements.CandidateName.click();
		Elements.CandidateName.sendKeys(CommonUtils.getExcelData(2, 1, sheet1));
		Elements.MobileField.sendKeys(CommonUtils.getExcelData(3, 1, sheet1));
		Elements.EmailField.click();
		Elements.EmailField.sendKeys(CommonUtils.getExcelData(4, 1, sheet1));
		Elements.DateOfBirth_DatePicker.click();
		CommonUtils.Thread_Sleep(2);
		Actions actions= new Actions(driver);
		CommonUtils.Thread_Sleep(2);
		driver.findElement(By.xpath("(//*[@class='xdsoft_label xdsoft_year'])[3]/span")).click();
		CommonUtils.Thread_Sleep(3);
		WebElement scrollbar = driver.findElement(By.xpath("(//*[@class='xdsoft_scrollbar'])[8]/div"));
		CommonUtils.Thread_Sleep(3);

		actions.clickAndHold(scrollbar).moveByOffset(0, -30).release().perform();

		WebElement year = driver.findElement(By.xpath("(//*[@class='xdsoft_select xdsoft_yearselect xdsoft_scroller_box'])[3]/div[1]/div[text()='2006']"));
		//CommonUtils.scrollTOElement(year, driver);
		CommonUtils.Thread_Sleep(3);
		year.click();
		CommonUtils.Thread_Sleep(3);
		List<WebElement> allMonthss_1 = driver.findElements(By.xpath("(//*[@class='xdsoft_calendar'])[last()]/table/tbody/tr/td"));
		CommonUtils.Thread_Sleep(3);
		CommonUtils.clickGivenDay(allMonthss_1, CommonUtils.getCurrentDay());
		CommonUtils.Thread_Sleep(3);
		Elements.AddressField.sendKeys(CommonUtils.getExcelData(5, 1, sheet1));
		CommonUtils.Thread_Sleep(3);
		Elements.GeneralTAB_TechnologyDropdown.click();
		CommonUtils.Thread_Sleep(3);
		Elements.GeneralTAB_TechnoloagyDropdownOption.click();
		CommonUtils.Thread_Sleep(2);
		CommonUtils.scrollTOElement(Elements.FresherRadioButton, driver);
		CommonUtils.Thread_Sleep(2);
		Elements.FresherRadioButton.click();
		Elements.CVUpload.sendKeys(PDFFile);
		Elements.RecruiterRemarksField.sendKeys("Testing");
		CommonUtils.Thread_Sleep(2);
		Elements.RemarkDropdown.click();
		CommonUtils.Thread_Sleep(2);
		Elements.RemarksDropdownOption.click();
		CommonUtils.Thread_Sleep(2);
		Elements.CVSourcesDropdown.click();
		CommonUtils.Thread_Sleep(2);
		Elements.CVSourcesDropdownOption.click();
		Elements.saveButton.click();
		CommonUtils.Thread_Sleep(2);
		CommonUtils.CaptureScreenshot_for_IsDisplayed(driver, Elements.SuccessMSG, "TC-03", "Success message is display", "success message is not display");
		CommonUtils.Thread_Sleep(3);
		CommonUtils.scroll_up(driver);
		CommonUtils.Thread_Sleep(2);
		Elements.InterviewStatusTAB.click();
		CommonUtils.Thread_Sleep(2);
		Elements.SystemTaskScheduleDate.click();
		CommonUtils.Thread_Sleep(3);
		try {
			String Current_Date = CommonUtils.getCurrentDay();
			String date_1 = CommonUtils.getCurrentDatePlus(1);
			int Selectiondate = Integer.parseInt(date_1);
			int currentdate = Integer.parseInt(Current_Date);

			System.out.println("date:"+date_1);
			if(currentdate>10) {
				if(Selectiondate<10) {
					driver.findElement(By.xpath("(//*[@class='xdsoft_next'])[1]")).click();
					CommonUtils.Thread_Sleep(3);
					driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-3]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
				}else {
					List<WebElement> dates = driver.findElements(By.xpath("((//*[@class='xdsoft_calendar'])[last()-3]/table/tbody/tr/td/div[text()='"+date_1+"'])"));
					String date=dates.get(0).getText();
					int d=Integer.parseInt(date);
					if(dates.size()==2) {
						driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-3]/table/tbody/tr/td/div[text()='"+date_1+"'])[2]")).click();

					}else {
						driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-3]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
					}
				}
			}else {
				driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-3]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
			}
		}catch (Exception e) {
		}
		Elements.SystemTaskDoneByTextField.sendKeys(CommonUtils.getExcelData(12, 1, sheet1));
		Elements.SystemTaskStatusDropdown.click();
		CommonUtils.Thread_Sleep(2);
		Elements.SystemTaskStatusDropdownOption.click();
		Elements.InterviewStatus_CommentsField.sendKeys(CommonUtils.getExcelData(17, 1, sheet1));
		Elements.TechnicalRoundScheduleDate.click();
		CommonUtils.Thread_Sleep(3);
		try {
			String Current_Date = CommonUtils.getCurrentDay();
			String date_1 = CommonUtils.getCurrentDatePlus(1);
			int Selectiondate = Integer.parseInt(date_1);
			int currentdate = Integer.parseInt(Current_Date);

			System.out.println("date:"+date_1);
			if(currentdate>10) {
				if(Selectiondate<10) {
					driver.findElement(By.xpath("(//*[@class='xdsoft_next'])[3]")).click();
					CommonUtils.Thread_Sleep(3);
					driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-2]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
				}else {
					List<WebElement> dates = driver.findElements(By.xpath("((//*[@class='xdsoft_calendar'])[last()-2]/table/tbody/tr/td/div[text()='"+date_1+"'])"));
					String date=dates.get(0).getText();
					int d=Integer.parseInt(date);
					if(dates.size()==2) {
						driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-2]/table/tbody/tr/td/div[text()='"+date_1+"'])[2]")).click();

					}else {
						driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-2]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
					}
				}
			}else {
				driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-2]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
			}
		}catch (Exception e) {
		}
		Elements.TechnicalRoundDoneByTextField.sendKeys(CommonUtils.getExcelData(16, 1, sheet1));
		Elements.TechnicalRoundstatusDropdown.click();
		CommonUtils.Thread_Sleep(2);
		Elements.TechnicalRoundstatusDropdownOption.click();
		Elements.Technicalround_CommentsTextField.sendKeys(CommonUtils.getExcelData(18, 1, sheet1));
		CommonUtils.Thread_Sleep(2);
		CommonUtils.scrollTOElement(Elements.Technicalround_CommentsTextField, driver);
		CommonUtils.Thread_Sleep(2);
		Elements.ManagerRoundScheduleDate.click();
		CommonUtils.Thread_Sleep(2);
		try {
			String Current_Date = CommonUtils.getCurrentDay();
			String date_1 = CommonUtils.getCurrentDatePlus(1);
			int Selectiondate = Integer.parseInt(date_1);
			int currentdate = Integer.parseInt(Current_Date);

			System.out.println("date:"+date_1);
			if(currentdate>10) {
				if(Selectiondate<10) {
					driver.findElement(By.xpath("(//*[@class='xdsoft_next'])[5]")).click();
					CommonUtils.Thread_Sleep(3);
					driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-1]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
				}else {
					List<WebElement> dates = driver.findElements(By.xpath("((//*[@class='xdsoft_calendar'])[last()-1]/table/tbody/tr/td/div[text()='"+date_1+"'])"));
					String date=dates.get(0).getText();
					int d=Integer.parseInt(date);
					if(dates.size()==2) {
						driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-1]/table/tbody/tr/td/div[text()='"+date_1+"'])[2]")).click();

					}else {
						driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-1]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
					}
				}
			}else {
				driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()-1]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
			}
		}catch (Exception e) {
		}
		Elements.ManagerRoundDonebyTextField.sendKeys(CommonUtils.getExcelData(19, 1, sheet1));
		Elements.ManagerRoundStatusDropdown.click();
		CommonUtils.Thread_Sleep(2);
		Elements.ManagerRoundStatusDropdownOption.click();
		Elements.ManagerRound_Comment.sendKeys(CommonUtils.getExcelData(21, 1, sheet1));
		Elements.HRRoundScheduleDate.click();
		CommonUtils.Thread_Sleep(3);
		try {
			String Current_Date = CommonUtils.getCurrentDay();
			String date_1 = CommonUtils.getCurrentDatePlus(1);
			int Selectiondate = Integer.parseInt(date_1);
			int currentdate = Integer.parseInt(Current_Date);

			System.out.println("date:"+date_1);
			if(currentdate>10) {
				if(Selectiondate<10) {
					driver.findElement(By.xpath("(//*[@class='xdsoft_next'])[7]")).click();
					CommonUtils.Thread_Sleep(3);
					driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
				}else {
					List<WebElement> dates = driver.findElements(By.xpath("((//*[@class='xdsoft_calendar'])[last()]/table/tbody/tr/td/div[text()='"+date_1+"'])"));
					String date=dates.get(0).getText();
					int d=Integer.parseInt(date);
					if(dates.size()==2) {
						driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()]/table/tbody/tr/td/div[text()='"+date_1+"'])[2]")).click();

					}else {
						driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
					}
				}
			}else {
				driver.findElement(By.xpath("((//*[@class='xdsoft_calendar'])[last()]/table/tbody/tr/td/div[text()='"+date_1+"'])[1]")).click();
			}
		}catch (Exception e) {
		}

		Elements.HrRoundDoneByTextField.sendKeys(CommonUtils.getExcelData(22, 1, sheet1));
		Elements.HRRoundStatusDropdown.click();
		CommonUtils.Thread_Sleep(2);
		Elements.HRRoundStatusDropdownOption.click();
		Elements.HRRound_Comment.sendKeys(CommonUtils.getExcelData(24, 1, sheet1));
		CommonUtils.scroll_down(driver);;
		CommonUtils.Thread_Sleep(2);
		Elements.FinalstatusDropdown.click();
		CommonUtils.Thread_Sleep(2);
		Elements.FinalstatusDropdownOption.click();
		CommonUtils.Thread_Sleep(3);
		Elements.saveButton.click();
		CommonUtils.CaptureScreenshot_for_IsDisplayed(driver, Elements.SuccessMSG, "TC-04", "Success message is display", "success message is not display");
		CommonUtils.Thread_Sleep(2);
		CommonUtils.scroll_up(driver);
		CommonUtils.Thread_Sleep(3);
		Elements.CandidateDocumentTAB.click();
		CommonUtils.Thread_Sleep(2);
		Elements.DocumentNameTextField.click();
		Elements.DocumentNameTextField.sendKeys(PDFFile);
		Elements.DocumentCategoryDropdown.click();
		CommonUtils.Thread_Sleep(3);
		Elements.DocumentCategoryDropdownOption.click();
		CommonUtils.Thread_Sleep(1);
		Elements.AttachementChoosFile.sendKeys(PDFFile);
		CommonUtils.Thread_Sleep(2);
		Elements.saveButton.click();
		CommonUtils.Thread_Sleep(2);
		CommonUtils.CaptureScreenshot_for_IsDisplayed(driver, Elements.SuccessMSG, "TC-05", "Success message is display", "success message is not display");
	}

	//@AfterClass
	public void closeTC() {
		driver.quit();
	}


}
