package cz.moneta.test.harness.connectors.web;

import org.openqa.selenium.Cookie;

import java.util.Date;
import java.util.Optional;

public class Cookies {

    private final SeleniumWebConnector connector;

    public Cookies(SeleniumWebConnector connector) {
        this.connector = connector;
    }

    public String getCookie(String name) {
        return Optional.ofNullable(connector.getDriver().manage().getCookieNamed(name))
                .map(Cookie::getValue)
                .orElse(null);
    }

    public void addCookie(String name, String value, String domain, String path, Date expiry, boolean isSecure, boolean isHttpOnly) {
        Cookie cookie = new Cookie(name, value, domain, path, expiry, isSecure, isHttpOnly);
        connector.getDriver().manage().addCookie(cookie);
    }

    public void addCookie(Cookie cookie) {
        connector.getDriver().manage().addCookie(cookie);
    }
}