package PAN;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.concurrent.TimeUnit;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.interactions.Actions;

import io.github.bonigarcia.wdm.WebDriverManager;

public class pan  {

	public static void main(String[] args) throws Exception {
		System.setProperty("webdriver.chrome.driver", "./chromedriver.exe");


		WebDriver driver = new ChromeDriver();


		// ChromeOptions ops = new ChromeOptions();
		//ops.addArguments("--remote-allow-origins=*");
		//  WebDriver driver = new EdgeDriver();
		driver.manage().window().maximize();

		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
		Thread.sleep(3000);

		driver.get("https://www.tdscpc.gov.in/");

		// driver.manage().window().maximize();
		// new Actions(driver);
		String src = "./panverify (1).xlsx";
		FileInputStream fis = new FileInputStream(src);
		XSSFWorkbook wb = new XSSFWorkbook(fis);
		XSSFSheet s1 = wb.getSheetAt(0);
		XSSFSheet s2 = wb.getSheetAt(0);
		XSSFRow rw = s1.getRow(0);
		XSSFRow rw1 = s1.getRow(1);
		XSSFCell col = rw.getCell(0);
		XSSFCell col2 = rw.getCell(1);
		int rowcount = s1.getPhysicalNumberOfRows();
		System.out.println(rowcount);

		Thread.sleep(3000L);

		try {
			driver.findElement(By.xpath("//input[@value='Continue >>']")).click();

			Thread.sleep(3000);
			driver.findElement(By.xpath("//a[text()='Login']")).click();
			Thread.sleep(3000);
		} catch (Exception e) {
			// TODO: handle exception
			driver.findElement(By.xpath("//a[text()='Login']")).click();
			Thread.sleep(3000);
		}


		//driver.findElement(By.xpath("/html/body/div/div[5]/div[2]/div/div/input")).click();
		// driver.findElement(By.xpath("//a[text()='Login']")).click();
		//Thread.sleep(3000L);
		// driver.findElement(By.xpath("/html/body/div/div[2]/div/div[1]/div[1]/div[2]/a")).click();
		Thread.sleep(3000L);
		WebElement userid = driver.findElement(By.id("userId"));
		userid.click();
		userid.sendKeys(new CharSequence[]{"CSPL175"});

		Thread.sleep(3000L);
		WebElement password = driver.findElement(By.id("psw"));
		password.click();
		password.sendKeys(new CharSequence[]{"PNEC06987B"});
		Thread.sleep(3000L);
		WebElement tanno = driver.findElement(By.id("tanpan"));
		tanno.click();
		tanno.sendKeys(new CharSequence[]{"PNEC06987B"});
		//	      Thread.sleep(3000L);
		//	      String captchaVal = JOptionPane.showInputDialog("Please enter the captcha value:");
		//	      driver.findElement(By.id("captcha")).sendKeys(new CharSequence[]{captchaVal});
		Thread.sleep(20000);

		//login btn
		//driver.findElement(By.xpath("/html/body/div[1]/form/div[2]/div[2]/div[2]/div[2]/div[12]/p[2]/input")).click();

		driver.findElement(By.xpath("//*[@id='clickLogin']")).click();
		Thread.sleep(3000L);

		//Statement/Payment click
		//driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[4]/ul/li[2]/div/a/span[2]")).click();
		driver.findElement(By.xpath("//span[text()='Statements / Payments']")).click();	      
		Thread.sleep(3000L);

		//click on pan verification.
		//driver.findElement(By.xpath("/html/body/div[1]/div[1]/div[4]/ul/li[2]/ul/li[5]/a")).click();
		driver.findElement(By.xpath("//span[text()='Statements / Payments']/parent::a/parent::div/following-sibling::ul/li/a[text()='PAN Verification']")).click();

		Thread.sleep(3000L);
		WebElement formtype = driver.findElement(By.id("frmType1"));
		formtype.click();
		formtype.sendKeys(new CharSequence[]{Keys.ARROW_DOWN});
		Thread.sleep(1000L);


		for(int i = 1; i < rowcount; ++i) {
			System.out.println("__________________________________");
			WebElement panno = driver.findElement(By.id("pannumber"));
			panno.clear();
			Thread.sleep(2000L);
			String pan = s1.getRow(i).getCell(0).getStringCellValue();
			panno.sendKeys(new CharSequence[]{pan});
			Thread.sleep(2000L);
			driver.findElement(By.xpath("/html/body/div[1]/div[4]/form[1]/div/div/table/tbody/tr[5]/td/table/tbody/tr/td[5]/input")).click();
			Thread.sleep(1000L);
			String status = "Invalid";
			String name = "Invalid";

			try {
				status = driver.findElement(By.xpath("/html/body/div[1]/div[4]/div[4]/table/tbody/tr/td[1]")).getText();
				name = driver.findElement(By.xpath("/html/body/div[1]/div[4]/div[4]/table/tbody/tr/td[2]")).getText();
			} catch (Exception var24) {
				System.out.println("invalid PAN Card");
			}

			System.out.println("PAN Status : " + pan);
			System.out.println("PAN Status : " + status);
			System.out.println("Name : " + name);
			s2.getRow(i).createCell(1).setCellValue(name);
			s2.getRow(i).createCell(2).setCellValue(status);
			FileOutputStream focut = new FileOutputStream(src);
			wb.write(focut);
		}

		wb.close();
		driver.close();

	}

}
