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.Arrays;
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.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 CandidateAssessmentSearchValidation 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

	@BeforeClass
	public void setUp() {
		driver = getDriver();
		CommonUtils.ModuleName = "CandidateAssessmentSearchValidation";
		driver.manage().window().maximize();
		// Check the URL in BaseSetup Class
		driver.get(baseUrl);
		CommonUtils.Reports("CandidateAssessmentSearchValidation.html", "CandidateAssessmentSearchValidation");
		Elements = PageFactory.initElements(driver, CandidateAssessmentElements.class);
		// Check the User Credentials in Common utils class
		CommonUtils.login();
		sheet1 = CommonUtils.readExcelSheet(Excels.filepath2 + "EmployeeTestData.xls", 0);
		// 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.Thread_Sleep(3);
		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 {
		List<WebElement> View_Page = Arrays.asList(Elements.CondidateAssessmentTitle,Elements.Searchfield,Elements.FromDate,Elements.ToDate,Elements.TechnologyDropdown,Elements.SubmitButton,Elements.AddCanndidateAssessmentButton,Elements.CandidateNameGride,Elements.DateOfContactGride,Elements.FinalStatusGride,Elements.TechnologyGride);
		try {
			for(int i=0;i<View_Page.size();i++) {
				CommonUtils.CaptureScreenshot_for_IsDisplayed(driver,View_Page.get(i), "TC_004", "Candidate Assessment list page below mentioned fields are display", "Candidate Assessment list page below mentioned fields are not display");
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	@Test(priority = 3)
	public void SearchFieldValidationTC_128() throws IOException {
		CommonUtils.Thread_Sleep(3);
		Elements.Searchfield.click();

		Elements.Searchfield.sendKeys(CommonUtils.getExcelData(0, 1, sheet1));

		try {
			if (Elements.Searchfield.isDisplayed()) {
				CommonUtils.captureScreenshot_pass(driver, "TC-128", " Accept Alphabates");
				System.out.println("pass-Candidate Assessment TC-128");
			}
		} catch (Exception e) {
			CommonUtils.captureScreenshot_fail(driver, "TC-128", "not Accept Alphabates");
			System.out.println("fail-Candidate Assessment TC-128");
		}

		Elements.Searchfield.clear();
		Elements.Searchfield.sendKeys(CommonUtils.getExcelData(0, 2, sheet1));
		try {
			if (Elements.Searchfield.isDisplayed()) {
				CommonUtils.captureScreenshot_pass(driver, "TC_121", " Accept Alphabates with Spaces");
				System.out.println("pass-Candidate Assessment TC-TC_121");
			}
		} catch (Exception e) {
			CommonUtils.captureScreenshot_fail(driver, "TC_121", "Not Accept Alphabates with Spaces");
			System.out.println("fail-Candidate Assessment TC-121");
		}


		Elements.Searchfield.clear();
		Elements.Searchfield.sendKeys(CommonUtils.getExcelData(0, 3, sheet1));


		try {
			if (Elements.Searchfield.isDisplayed()) {
				CommonUtils.captureScreenshot_pass(driver, "TC-122", " Accept Alphanumeric Value");
				System.out.println("pass-Candidate Assessment TC-122");
			}
		} catch (Exception e) {
			CommonUtils.captureScreenshot_fail(driver, "TC-122", "Not Accept Alphanumeric Value");

			System.out.println("fail-Candidate Assessment TC-122");
		}

		Elements.Searchfield.clear();
		Elements.Searchfield.sendKeys(CommonUtils.getExcelData(0, 4, sheet1));

		try {
			if (Elements.Searchfield.isDisplayed()) {
				CommonUtils.captureScreenshot_pass(driver, "TC_123", " Accept Numbers");
				System.out.println("pass-Candidate Assessment TC_123");
			}
		} catch (Exception e) {
			CommonUtils.captureScreenshot_fail(driver, "TC_123", "Not Accept Numbers");

			System.out.println("fail-Candidate Assessment TC_123");
		}
		Elements.Searchfield.clear();
		Elements.Searchfield.sendKeys(CommonUtils.getExcelData(0, 5, sheet1));
		try {
			if (Elements.Searchfield.isDisplayed()) {
				CommonUtils.captureScreenshot_pass(driver, "TC123.1", " Accept Special Character");
				System.out.println("Pass-Candidate Assessment TC-123.1");
			}
		} catch (Exception e) {
			CommonUtils.captureScreenshot_fail(driver, "TC-123.1", "Not Accept Special Character");
			System.out.println("Fail-Candidate Assessment TC-123.1");
		}


		Elements.Searchfield.clear();

		Elements.Searchfield.sendKeys(CommonUtils.getExcelData(0, 17, sheet1));
		try {
			if (Elements.InvalidSearch.isDisplayed()) {
				CommonUtils.captureScreenshot_pass(driver, "TC123.2", "not  Accept Special Character <> ");
				System.out.println("Pass-Candidate Assessment TC-123.2");
			}
		} catch (Exception e) {
			CommonUtils.captureScreenshot_fail(driver, "TC-123.2", " Accept Special Character <> ");
			System.out.println("Fail-Candidate Assessment TC-123.2");
		}
		Elements.Searchfield.clear();
		CommonUtils.Thread_Sleep(2);

		Elements.Searchfield.click();
		Elements.Searchfield.sendKeys(CommonUtils.getExcelData(0, 8, sheet1));


		try {
			if (Elements.Searchfield.isDisplayed()) {
				CommonUtils.captureScreenshot_pass(driver, "TC_124", "should accept max 64");
				System.out.println("Pass-Candidate Assessment TC-124");
			}
		} catch (Exception e) {
			CommonUtils.captureScreenshot_fail(driver, "TC_124", "shouldn't accept max 64");
			System.out.println("Fail-Candidate Assessment TC_124");
		}


		Elements.Searchfield.clear();
	}
	@Test(priority = 4)
	public void TechnalogyFieldValidationTC_112() throws IOException {
		CommonUtils.scrollTOElement(Elements.TechnologyDropdown, driver);
		CommonUtils.Thread_Sleep(2);
		try {
			if (Elements.TechnologyDropdown.isDisplayed()) {
				CommonUtils.captureScreenshot_pass(driver, "TC_74", "Field should be  mandatory");
				System.out.println("Pass-CanndidateAssessment TC_74");
			}}catch(Exception E){
				CommonUtils.captureScreenshot_fail(driver, "TC_74", "Field shouldn't be  mandatory");
				System.out.println("Fail-CanndidateAssessment TC_74");
			}
		CommonUtils.Thread_Sleep(2);
		try {
			CommonUtils.JS_Click( Elements.TechnologyDropdown);
		}catch(Exception E) {
			Elements.TechnologyDropdown.click();
		}

		CommonUtils.Thread_Sleep(3);
		CommonUtils.Thread_Sleep(2);
		List<WebElement> tabledata = driver.findElements(By.xpath("//*[@class='ng-dropdown-panel ng-select-bottom']/div/div[2]/div"));
		for (int i = 1; i <= tabledata.size(); i++) {
			String TechnicalRoundstatusDropdown = driver
					.findElement(By.xpath("//*[@class='ng-dropdown-panel ng-select-bottom']/div/div[2]/div[" + i + "]"))
					.getText();
			System.out.println("pass:-TC_75");
		}
		try {
			CommonUtils.Thread_Sleep(2);
			CommonUtils.JS_Click( Elements.TechnologyDropdown);
			CommonUtils.Thread_Sleep(2);
			Elements.TechnologyDropdownOption.click();
			System.out.println(" pass_TC77:-user selected TechnologyDropdownOption options are display");
		}catch(Exception e)
		{
			System.out.println("Fail:-TC77 user selected  TechnologyDropdownOption not  display/ dropdown records are unaviable ");
		}

	}

	@AfterClass
	public void closeTC() {
		driver.quit();
	}

}