package ru.selenium;

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.*;
import org.openqa.selenium.*;

import java.util.List;
import java.util.concurrent.TimeUnit;

import static org.testng.Assert.assertTrue;

public class AddFilm extends TestNgTestBase{

  private boolean acceptNextAlert = true;
  private WebDriverWait wait;
  private int countBeforeAdd;

  @BeforeSuite
  public void login() {
    driver.get(baseUrl + "/php4dvd/");
    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
    wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.presenceOfElementLocated(By.id("username")));
    driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username")).sendKeys("admin");
    driver.findElement(By.name("password")).clear();
    driver.findElement(By.name("password")).sendKeys("admin");
    driver.findElement(By.name("submit")).click();
  }

  @Test //добавить фильм с правильными данными
  public void testAddFilm() throws Exception {
      WebElement results = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("results")));

      driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
      List<WebElement> films = results.findElements(By.className("movie_box")); // посчитать количество фильмов, если они есть
      countBeforeAdd = films.size();
      driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

      WebElement addMovieBtn = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("img[alt=\"Add movie\"]")));
      addMovieBtn.click();

      WebElement imdbSearch = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("imdbsearch")));
      imdbSearch.clear();
      imdbSearch.sendKeys("Everybody Wants Some!!");

      driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
      driver.findElement(By.linkText("Everybody Wants Some!!")).click();

      WebElement saveBtn = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("img[alt=\"Save\"]")));
      saveBtn.click();

      WebElement logoBtn = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("center")));
      logoBtn.click();
      List<WebElement> films1 = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("movie_box")));
      int countAfterAdd = films1.size();

      System.out.println("before:" + countBeforeAdd);
      System.out.println("after:" + countAfterAdd);
      Assert.assertEquals(countAfterAdd , countBeforeAdd + 1);
  }

  @Test // удалить фильм
  public void testDeleteFilm() throws Exception {
      List<WebElement> films = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.className("movie_box"))); // посчитать количество фильмов, если они есть
      countBeforeAdd = films.size();

      WebElement cover = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("movie_cover")));
      cover.click();
      WebElement removeBtn = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("img[alt=\"Remove\"]")));
      removeBtn.click();

      assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to remove this[\\s\\S]$"));

      WebElement results1 = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("results")));
      driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
      List<WebElement> films1 = results1.findElements(By.className("movie_box"));
      int countAfterAdd = films1.size();
      driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);

      Assert.assertEquals(countAfterAdd, countBeforeAdd-1);
  }
      @Test // добавить фильм с плохими данными
      public void testAddFilmWithoutYear() throws Exception {
        WebElement addMovieBtn = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("img[alt=\"Add movie\"]")));
        addMovieBtn.click();

        WebElement imdbSearch = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("imdbsearch")));
        imdbSearch.clear();
        imdbSearch.sendKeys("Everybody Wants Some!!");

        driver.findElement(By.cssSelector("input[type=\"submit\"]")).click();
        driver.findElement(By.cssSelector("td.title")).click();
        driver.findElement(By.name("year")).clear();
        driver.findElement(By.name("year")).sendKeys("");
        driver.findElement(By.cssSelector("img[alt=\"Save\"]")).click();

        WebElement error = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("error")));
        Assert.assertTrue(error.isDisplayed());
        WebElement logoBtn = wait.until(ExpectedConditions.presenceOfElementLocated(By.className("center")));
        logoBtn.click();
  }

    private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

@@ -369,16 +369,18 @@
 tBase%7B%0A%0A
+  
   privat
@@ -413,16 +413,18 @@
 = true;%0A
+  
   privat
@@ -445,16 +445,18 @@
 t wait;%0A
+  
   privat
@@ -461,29 +461,37 @@
 ate 
-int countBeforeAdd
+String FilmName = %22Jaws%22
 ;%0A%0A
+  
   @B
@@ -1430,32 +1430,36 @@
  %D0%BE%D0%BD%D0%B8 %D0%B5%D1%81%D1%82%D1%8C%0A      
+int 
 countBeforeAdd =
@@ -1832,24 +1832,51 @@
 ch.clear();%0A
+//      FilmName = %22Jaws%22;%0A
       imdbSe
@@ -1893,32 +1893,16 @@
 eys(
-%22Everybody Wants Some!!%22
+FilmName
 );%0A%0A
@@ -2018,32 +2018,16 @@
 ext(
-%22Everybody Wants Some!!%22
+FilmName
 )).c
@@ -2479,114 +2479,8 @@
 );%0A%0A
-      System.out.println(%22before:%22 + countBeforeAdd);%0A      System.out.println(%22after:%22 + countAfterAdd);%0A
     
@@ -2792,16 +2792,20 @@
 %D1%8C%0A      
+int 
 countBef
@@ -2799,35 +2799,38 @@
  int countBefore
-Add
+Delete
  = films.size();
@@ -3485,35 +3485,38 @@
   int countAfter
-Add
+Delete
  = films1.size()
@@ -3624,19 +3624,22 @@
 untAfter
-Add
+Delete
 , countB
@@ -3643,19 +3643,22 @@
 ntBefore
-Add
+Delete
 -1);%0A  %7D
@@ -4100,32 +4100,16 @@
 eys(
-%22Everybody Wants Some!!%22
+FilmName
 );%0A%0A

Change film name and count Before and After Delete