package pl.dataconsulting.APEX_TAF.framework.service;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;

import java.io.File;
import java.io.IOException;
import java.nio.file.Path;

@Lazy
@Service
public class ScreenShotService {

    @Autowired
    ApplicationContext ctx;

    @Value("${screenshot.path}/")
    private Path path;

    public void takeScreenShot(final String imageName) throws IOException {
        File sourceFile = ((TakesScreenshot) this.ctx.getBean(WebDriver.class)).getScreenshotAs(OutputType.FILE);
        String tmp = this.path.resolve(imageName).toString();

        FileCopyUtils.copy(sourceFile, this.path.resolve(imageName).toFile());
    }

    public byte[] getScreenshot(){
        return this.ctx.getBean(TakesScreenshot.class).getScreenshotAs(OutputType.BYTES);
    }



}
