package guittests;

import static mealplaner.model.MealBuilder.from;
import static mealplaner.model.enums.CookingTime.SHORT;
import static org.assertj.swing.data.TableCell.row;
import static testcommons.CommonFunctions.getMeal1;
import static testcommons.CommonFunctions.getMeal2;
import static testcommons.CommonFunctions.getMeal3;
import static testcommons.CommonFunctions.getRecipe2;

import java.util.ArrayList;
import java.util.List;

import org.assertj.swing.fixture.JTableFixture;
import org.junit.Test;

import guittests.helpers.AssertJMealplanerTestCase;
import mealplaner.model.Meal;
import mealplaner.recipes.model.Recipe;

public class DatabaseEditTest extends AssertJMealplanerTestCase {

  @Test
  public void addMealAddsMealsInCorrectOrder() {
    List<Meal> meals = new ArrayList<>();
    Meal meal1 = getMeal1();
    Meal meal2 = getMeal2();
    meals.add(meal1);
    meals.add(meal2);
    windowHelpers.addMealFromDatabase(meal2);
    windowHelpers.addMealFromDatabase(meal1);
    windowHelpers.compareDatabaseInTable(meals);
    window.button("ButtonPanelDatabaseEdit2").isEnabled();
    window.button("ButtonPanelDatabaseEdit3").isEnabled();
    window.close();
  }

  @Test
  public void removeMealsWorksWithMultipleMeals() {
    List<Meal> meals = new ArrayList<>();
    Meal meal3 = getMeal3();
    meals.add(meal3);
    windowHelpers.addMealFromDatabase(getMeal1());
    windowHelpers.addMealFromDatabase(getMeal2());
    windowHelpers.addMealFromDatabase(meal3);
    window.table().selectRows(0, 1);
    window.button("ButtonPanelDatabaseEdit1").click();
    windowHelpers.compareDatabaseInTable(meals);
    window.button("ButtonPanelDatabaseEdit2").isEnabled();
    window.button("ButtonPanelDatabaseEdit3").isEnabled();
    window.close();
  }

  @Test
  public void canChangeAllAspectsOfAMeal() {
    Meal meal1 = getMeal1();
    Recipe recipe = getRecipe2();
    windowHelpers.addMealFromDatabase(meal1);
    JTableFixture table = window.table();
    table.cell(row(0).column(1)).enterValue(SHORT.toString());
    table.cell(row(0).column(6)).enterValue("New comment");
    table.cell(row(0).column(7)).click();
    windowHelpers.enterRecipe(recipe, window.dialog());
    Meal newMeal = from(meal1)
        .cookingTime(SHORT)
        .comment("New comment")
        .recipe(recipe)
        .create();
    List<Meal> meals = new ArrayList<>();
    meals.add(newMeal);
    windowHelpers.compareDatabaseInTable(meals);
    window.close();
  }

  @Test
  public void changingSavingNotSavingOfFieldsWorksCorrectl() {
    // implement me.
  }
}

@@ -1075,34 +1075,39 @@
 DatabaseEdit2%22).
-is
+require
 Enabled();%0A    w
@@ -1139,34 +1139,39 @@
 DatabaseEdit3%22).
-is
+require
 Enabled();%0A    w
@@ -1483,32 +1483,215 @@
 atabase(meal3);%0A
+    window.button(%22ButtonPanelDatabaseEdit3%22).requireEnabled();%0A    window.button(%22ButtonPanelDatabaseEdit2%22).requireEnabled();%0A    window.button(%22ButtonPanelDatabaseEdit2%22).click();%0A
     window.table
@@ -1861,18 +1861,23 @@
 Edit2%22).
-is
+require
 Enabled(
@@ -1925,18 +1925,23 @@
 Edit3%22).
-is
+require
 Enabled(
@@ -2746,24 +2746,827 @@
     
-// implement me.
+windowHelpers.addMealFromDatabase(getMeal1());%0A    window.button(%22ButtonPanelDatabaseEdit2%22).click();%0A    window.button(%22ButtonPanelDatabaseEdit3%22).requireDisabled();%0A    window.button(%22ButtonPanelDatabaseEdit2%22).requireDisabled();%0A    JTableFixture table = window.table();%0A    table.cell(row(0).column(1)).enterValue(SHORT.toString());%0A    checkDisabilityAndAbort();%0A    table.cell(row(0).column(6)).enterValue(%22New comment%22);%0A    checkDisabilityAndAbort();%0A  %7D%0A%0A  private void checkDisabilityAndAbort() %7B%0A    window.button(%22ButtonPanelDatabaseEdit3%22).requireEnabled();%0A    window.button(%22ButtonPanelDatabaseEdit2%22).requireEnabled();%0A    window.button(%22ButtonPanelDatabaseEdit3%22).click();%0A    window.button(%22ButtonPanelDatabaseEdit3%22).requireDisabled();%0A    window.button(%22ButtonPanelDatabaseEdit2%22).requireDisabled();
 %0A  %7D

Add Test to test saving and cancelling in Database