package parser;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.imageio.ImageIO;

import org.imgscalr.Scalr;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

import obejcts.Ingredient;
import obejcts.Recepie;

public class Tasteline extends ParserBase {
	public static final String RECEPIE_TITLE_XPATH = ".//h3";
	public static final String BASE_URL = "https://www.tasteline.com/";
	public static final String PATH_SEPARATOR = "+";
	public static final String RECEPIELIST_ITEMS_XPATH = "//div[contains(@class,'u-shadow-card')]";
	public static final String URL_SEARCH_PATTERN = "sok/";
	public static final String COOKIE_CONSENT_BUTTON_XPATH = "//button[@mode='primary']";
	public static final String IMAGE_XPATH = "//div[@class='Carousel-item']/img";

	public static final String SHOW_MORE_XPATH = "//div[contains(@class,'show-more-button')]";

	public static boolean isFromHereRecepie(String url) {
		return url.contains(BASE_URL);
	}

	@Override protected void parseRecepie(WebElement recepie, String recepieTitle) {
		try {
			Thread.sleep(1000);
			recepie.click();
			Thread.sleep(200);

			wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.xpath("//h1"), 0));

			List<WebElement> spans = driver.findElements(By.xpath("//h1/following-sibling::div/div//span"));

			String time = "";
			String difficulty = "";
			if (spans.size() == 2) {
				time = onlyNumbers(spans.get(0).getText());
				difficulty = spans.get(1).getText();
			} else if (spans.size() == 1) {
				if (onlyNumbers(spans.get(0).getText()).length() > 0) {
					time = onlyNumbers(spans.get(0).getText());
				} else {
					difficulty = spans.get(0).getText();
				}
			}

			Recepie newRecepie = new Recepie();
			newRecepie.setTime(time);
			newRecepie.setUrl(driver.getCurrentUrl());
			newRecepie.setName(recepieTitle);
			newRecepie.setDifficulty(difficulty);

			newRecepie.setDescription(driver.findElement(By.xpath("//h1/following-sibling::div/p")).getText());

			newRecepie.setId(database.insertRecepie(newRecepie));

			List<WebElement> ingredients = driver.findElements(By.xpath("//li[@class='Ingredient u-contents']"));
			String measurement = "";
			String amount = "";
			for (WebElement ingredient : ingredients) {
				if (checkIfElementExists(ingredient, ".//div/abbr")) {
					measurement = ingredient.findElement(By.xpath(".//div/abbr")).getText();
					amount = ingredient.findElement(By.xpath(".//div/span")).getText();
				} else {
					measurement = "";
					amount = "";
				}

				String name = ingredient.findElement(By.xpath(".//span[@class='Ingredient-name ']")).getText().trim();
				database.addIngredientToRecepie(newRecepie.getId(), new Ingredient(fixName(name), amount, measurement));
			}

			List<WebElement> steps = driver
					.findElements(By.xpath("//li[contains(@class, 'Step')]/div[2]"));

			int stepCounter = 1;
			for (WebElement step : steps) {
				database.addRecepieStep(newRecepie.getId(), stepCounter++, step.getText());
			}

			saveImage(newRecepie);

			driver.navigate().back();

		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	void saveImage(Recepie newRecepie) {
		WebElement imageElement = driver.findElement(By.xpath(IMAGE_XPATH));
		String imgSrc = imageElement.getAttribute("currentSrc");

		try {
			if (imgSrc.contains(".webp")) {
				imgSrc = imgSrc.replace(".webp", "");
			}
			URL imageUrl = new URL(imgSrc);
			BufferedImage savedImage = ImageIO.read(imageUrl);

			BufferedImage resizedImage = Scalr.resize(savedImage, 300);

			File file = new File("recepie-image.png");
			ImageIO.write(resizedImage, "png", file);

			database.saveRecepieImage(newRecepie.getId(), file);

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
