package parser;

import obejcts.Ingredient;
import obejcts.Recepie;
import org.imgscalr.Scalr;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import javax.imageio.ImageIO;
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;

public class Arla extends ParserBase {

	public static final String BASE_URL = "https://www.arla.se/recept/";
	public static final String RECEPIE_TITLE_XPATH = ".//a[contains(@class,'c-card__title')]";
	public static final String RECEPIELIST_ITEMS_XPATH = "//div[contains(@class,'c-card--vertical')]";
	public static final String PATH_SEPARATOR = "+";
	public static final String URL_SEARCH_PATTERN = "?search=";
	public static final String COOKIE_CONSENT_BUTTON_XPATH = "//button[@class='save-preference-btn-handler onetrust-close-btn-handler']";
	public static final String IMAGE_XPATH = "//div[@class='c-recipe__image']/picture/img";

	public static final String SHOW_MORE_XPATH = "";

	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);

			String description = driver.findElement(By.xpath("//div[contains(@class,'c-recipe__description')]")).getText();

			String time = "";
			if (checkIfElementExists(driver, "//span[@class='c-label--m u-mt--xxs u-mr--s']")) {
				time = driver.findElement(By.xpath("//span[@class='c-label--m u-mt--xxs u-mr--s']")).getText().replaceAll(
						"[^+0-9]",
						"");
			}
			Recepie newRecepie = new Recepie();
			newRecepie.setDescription(description);
			newRecepie.setTime(time);
			newRecepie.setName(recepieTitle);
			newRecepie.setUrl(driver.getCurrentUrl());

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

			List<WebElement> ingredients = driver.findElements(By.xpath("//div[@class='c-recipe__ingredients-inner']//tr"));
			for (WebElement ingredient : ingredients) {
				String name = ingredient
						.findElement(By.xpath(".//th//span[not (@class)]"))
						.getText().trim();
				String amountMeasureText = ingredient.findElement(By.xpath(".//td")).getText();

				String amount = getAmountFromText(amountMeasureText);
				String measurement = getMeasurement(amountMeasureText);

				database.addIngredientToRecepie(newRecepie.getId(), new Ingredient(fixName(name), amount, measurement));
			}

			int stepCounter = 1;
			List<WebElement> steps = driver
					.findElements(By.xpath("//div[@class='c-recipe__instructions-steps']//span[not(@class)]"));

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

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

		driver.navigate().back();
	}

	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();
		}
	}
}
