package com.pojosontheweb.selenium;

import org.openqa.selenium.Keys;

import java.util.HashMap;
import java.util.Map;

import static com.pojosontheweb.selenium.Findrs.*;

public class Google extends AbstractPageObject {

    private final String locale;

    public Google(Findr findr, String locale) {
        super(findr);
        this.locale = locale;
    }

    public static final String DISMISS_SL = "Strinjam se";
    public static final String DISMISS_DE = "Ich stimme zu";
    public static final String DISMISS_EN = "I agree";

    public final static Map<String, String> DISMISS_PER_LOCALE = new HashMap<>();

    static {
        DISMISS_PER_LOCALE.put("en", DISMISS_EN);
        DISMISS_PER_LOCALE.put("de", DISMISS_DE);
        DISMISS_PER_LOCALE.put("sl", DISMISS_SL);
    }

    public Google dismissCookies() {
        String dismissText = DISMISS_PER_LOCALE.get(locale);
        if (dismissText == null) {
            dismissText = DISMISS_EN;
        }

        Findr fDialog = $$("div")
                .where(attrEquals("role", "dialog"))
                .expectOne();
        fDialog
                .where(isDisplayed())
                .$$("button div")
                .where(textEquals(dismissText))
                .expectOne()
                .click();

        fDialog
                .where(not(isDisplayed()))
                .eval();

        return this;
    }

    public Google typeQuery(String query) {
        Findr input = $$("input")
                .where(attrEquals("name", "q"))
                .expectOne();
        input.click();
        input.sendKeys(query, Keys.ENTER);
        return this;
    }

    public Google assertHasResult(String title) {
        $$("div.g h3")
                .where(textEquals(title))
                .expectOne()
                .eval();
        return this;
    }
}

@@ -818,24 +818,26 @@
 Cookies() %7B%0A
+//
         Stri
@@ -885,16 +885,18 @@
 ocale);%0A
+//
         
@@ -918,24 +918,26 @@
  == null) %7B%0A
+//
             
@@ -962,16 +962,18 @@
 ISS_EN;%0A
+//
         
@@ -970,25 +970,29 @@
 //        %7D%0A
-%0A
+//%0A//
         Find
@@ -1009,24 +1009,26 @@
 = $$(%22div%22)%0A
+//
             
@@ -1064,24 +1064,26 @@
  %22dialog%22))%0A
+//
             
@@ -1092,32 +1092,34 @@
   .expectOne();%0A
+//
         fDialog%0A
@@ -1110,32 +1110,34 @@
         fDialog%0A
+//
                 
@@ -1158,16 +1158,18 @@
 ayed())%0A
+//
         
@@ -1194,16 +1194,18 @@
 n div%22)%0A
+//
         
@@ -1244,16 +1244,18 @@
 sText))%0A
+//
         
@@ -1267,32 +1267,34 @@
    .expectOne()%0A
+//
                 
@@ -1299,25 +1299,29 @@
   .click();%0A
-%0A
+//%0A//
         fDia
@@ -1324,16 +1324,18 @@
 fDialog%0A
+//
         
@@ -1369,16 +1369,18 @@
 yed()))%0A
+//
         

disable cookies window