package Parsers;

import java.time.Duration;
import java.time.LocalDateTime;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import Objects.Product;

public class NetOnNet extends ParserBase {

	public void checkProduct(Product product) {
		ChromeDriver driver = getSeleniumDriver();

		driver.get(product.getUrl());
		WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
		wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("//div[@class='price-big']"), 0));
		//
		WebElement foundElement = driver.findElement(By.xpath("//div[@class='price-big']"));

		String priceValueString = foundElement.getText();
		Float priceValue = formatPrice(priceValueString);
		float storedProductPrice = getStoredProductPrice(product);

		if ((storedProductPrice != -1 && priceValue < storedProductPrice) || storedProductPrice == -1) {
			product.setUpdatedDate(LocalDateTime.now());
			product.setPrice(priceValue);
			insertProduct(product);
		}

		driver.close();
		driver.quit();
	}

}
