/*
 * https://webfolder.io/license.html
 */
package io.webfolder.cdp.sample;

import static io.webfolder.cdp.JsonLibrary.Jackson;
import static io.webfolder.cdp.SelectorEngine.Playwright;
import static io.webfolder.cdp.logger.CdpLoggerType.Console;
import static io.webfolder.cdp.session.WaitUntil.NetworkIdle;

import io.webfolder.cdp.Example;
import io.webfolder.cdp.Launcher;
import io.webfolder.cdp.Options;
import io.webfolder.cdp.session.Session;
import io.webfolder.cdp.session.SessionFactory;

@Example
public class AutomationExample {

    public static void main(String[] args) {
        Options options = Options
                                .builder()
                                .jsonLibrary(Jackson)
                                .createNewUserDataDir(true)
                                .loggerType(Console)
                                .selectorEngine(Playwright)
                            .build();

        Launcher launcher = new Launcher(options);

        try (SessionFactory factory = launcher.launch();
                            Session session = factory.create()) {

            session.enableNetworkLog();

            session.navigate("https://danube-webshop.herokuapp.com/");
            session.waitDocumentReady();

            // focus to search form
            session.focus(":left-of(button:has-text('%s'))", "SEARCH");
            // type the product name
            session.sendKeys("Parry Hotter");
            // click to search button
            session.click("button:has-text('%s')", "SEARCH");

            session.waitUntil(NetworkIdle);

            session.click("li:has-text('%s')", "Parry Hotter");

            // wait until product page load
            boolean productPageLoaded = session.waitUntil(s -> s.matches("button:has-text('%s')", "ADD TO CART"));
            assert productPageLoaded;
            session.click("button:has-text('%s')", "ADD TO CART");

            // wait until shopping cart load
            boolean addToCartPageLoaded = session.waitUntil(s -> s.matches("button:has-text('%s')", "CHECKOUT"));
            assert addToCartPageLoaded;
            session.click("button:has-text('%s')", "CHECKOUT");

            // wait until checkout page load
            boolean checkoutPageLoaded = session.waitUntil(s -> s.matches("h1:has-text('%s')", "Checkout"));
            assert checkoutPageLoaded;

            // fill the form
            session.focus("[placeholder='%s']", "Name")   ; session.sendKeys("Foo");
            session.focus("[placeholder='%s']", "Surname"); session.sendKeys("Bar");
            session.focus("[placeholder='%s']", "Address"); session.sendKeys("My Address");
            session.focus("[placeholder='%s']", "Zipcode"); session.sendKeys("11111");
            session.focus("[placeholder='%s']", "City")   ; session.sendKeys("My City");
            session.sendTab();
            session.sendKeys("My Company");

            // click to radio button
            session.click(":left-of(:has-text('%s'))", "as soon as possible");

            // focus & click to buy button
            session.focus("button:has-text('%s')", "BUY");
            session.sendEnter();

            boolean loadedDonePage = session.waitUntil(s -> s.matches("button:has-text('%s')", "SHOP MORE"));
            assert loadedDonePage;

            // we are done, test executed successfully
        } finally {
            launcher.kill();
        }
    }
}