package com.qunzhixinxi.hnqz.admin.util;

import lombok.extern.slf4j.Slf4j;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import ru.yandex.qatools.ashot.AShot;
import ru.yandex.qatools.ashot.Screenshot;
import ru.yandex.qatools.ashot.coordinates.WebDriverCoordsProvider;

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

//import sun.misc.BASE64Decoder;

@Slf4j
public class Html2ImageByJsWrapper {

//	public static String SERVER_URL="192.168.43.131";
//	public static String API_URL="https://beijingapi.yaoyi.net";

//	private static ChromeDriver getChromeDriver() {
//		System.setProperty("webdriver.chrome.driver","/usr/bin/chromedriver");
//		ChromeOptions options=new ChromeOptions();
//		//设置 chrome 的无头模式
//		options.addArguments("--headless");
//		options.addArguments("--disable-gpu");
//		options.addArguments("--no-sandbox");
//		options.addArguments("--disable-dev-shm-usage");
//		options.addArguments("--start-maximized");
//		options.addArguments("blink-settings=imagesEnabled=false");
//		return new ChromeDriver(options);
//	}


	private static RemoteWebDriver getRemoteWebDriver() {
		//设置 chrome 的无头模式
//		options.addArguments("--headless");
//		options.addArguments("--disable-gpu");
//		options.addArguments("--no-sandbox");
//		options.addArguments("--disable-dev-shm-usage");
//		options.addArguments("--start-maximized");
//		options.addArguments("blink-settings=imagesEnabled=false");
		return null;
	}


	public static BufferedImage renderHtml2Image(String url,String serverUrl) throws IOException {
		log.info("url: {}", url);

		DesiredCapabilities dc = DesiredCapabilities.chrome();
		try {
			RemoteWebDriver driver = new RemoteWebDriver(new URL(serverUrl), dc);
			driver.get(url);
			Thread.sleep(4000);

		//等待页面加载完成
		new WebDriverWait(driver, 300).until(driver1 -> ((JavascriptExecutor) driver)
				.executeScript("return document.readyState").equals("complete"));
		JavascriptExecutor jexec = (JavascriptExecutor) driver;
		Long width = (Long) jexec.executeScript("return document.body.scrollWidth");
		Long height = (Long) jexec.executeScript("return document.body.scrollHeight");
		//设置浏览窗口大小
		driver.manage().window().setSize(new Dimension(width.intValue(), height.intValue()));
		Screenshot screenshot = new AShot().coordsProvider(new WebDriverCoordsProvider()).takeScreenshot(driver);

		driver.quit();
		return  screenshot.getImage();

		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return null;
	}

//	public static  void main(String [] args) throws IOException {
//		BufferedImage img = null;
//		String url = "https://api.yaoyi.net/h5/#/pages/task/task-detail/index?id=1087";
//		long start = System.currentTimeMillis();
//		img = Html2ImageByJsWrapper.renderHtml2Image(url);
//		long end = System.currentTimeMillis();
//		System.out.println("cost:  " + (end - start));
//
//		System.out.println(Base64Util.encode(img, "png"));
//	}

}
