package com.playwrightdemo.utils;

import com.microsoft.playwright.Page;
import com.playwrightdemo.Base.TestBase;
import org.apache.commons.mail.DefaultAuthenticator;
import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;

public class TestUtils extends TestBase {

    private static String screenShotPath = "Screenshot";
    private static String videoRecordingPath = "src/main/videoRecording";


    public static String getScreenshotOnError() {

        String ScreenshotName = "Screenshot_" + new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()) + ".png";

        byte[] buffer = page.screenshot(new Page.ScreenshotOptions().setPath(Paths.get(screenShotPath + "/" + ScreenshotName)).setFullPage(true));

        String base64Path = Base64.getEncoder().encodeToString(buffer);

        return base64Path;

    }

    public static void getVideoOnError() {

        String VideoRecordName = "VideoRecord_" + new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date()) + ".webm";

        page.video().saveAs(Paths.get(videoRecordingPath + "/" + VideoRecordName));


    }


    public static void onErrorEmail() throws EmailException {

        Email email = new SimpleEmail();
        email.setHostName(ConfigReader.getHostName());
        email.setSmtpPort(ConfigReader.getPortNumber());
        email.setAuthenticator(new DefaultAuthenticator(ConfigReader.getEmailUserName(), ConfigReader.getEmailPassword()));
        email.setSSLOnConnect(true);
        email.setFrom(ConfigReader.getEmailUserName());
        email.setSubject("This is test email");
        email.setMsg("Email Sending Utility");
        email.addTo("asrivastava@ishir.com");
        email.send();

    }

}
