package com.example.stepdefinations.pakistan;

import com.example.utils.TextContextSetup;
import io.cucumber.java.After;
import io.cucumber.java.AfterStep;
import io.cucumber.java.Scenario;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

import java.io.File;
import java.io.IOException;

public class Hooks {
    TextContextSetup textContextSetup;

    public Hooks(TextContextSetup textContextSetup) {
        this.textContextSetup = textContextSetup;

    }

    @After
    public void closingDriver() throws IOException {
        textContextSetup.driverManager.WebDriverManager().quit();
    }


    @AfterStep
    public void addScreenShot(Scenario scenario) throws IOException {
        WebDriver driver = textContextSetup.driverManager.WebDriverManager();
        if (scenario.isFailed()) {
            File sourcePath = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            byte[] fileContent = FileUtils.readFileToByteArray(sourcePath);
            scenario.attach(fileContent, "image/png", "Error image");
        }
    }
}
