package com.sudoku.data.manager;

import com.sudoku.data.model.Grid;
import com.sudoku.data.model.PlayedGrid;
import com.sudoku.data.model.User;

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

public final class GridManager {
  private static volatile GridManager instance = null;
  private List<Grid> availableGrids;
  private List<PlayedGrid> playedGrids;

  private GridManager() {
    this.availableGrids = new ArrayList<Grid>();
    this.playedGrids = new ArrayList<PlayedGrid>();
  }

  public final static GridManager getInstance() {
    if (instance == null) {
      synchronized (GridManager.class) {
        if (instance == null) {
          instance = new GridManager();
        }
      }
    }
    return instance;
  }

  public boolean addGrid(Grid grid) {
    return availableGrids.add(grid);
  }

  public boolean addPlayedGrid(Grid grid, User player) {
    return playedGrids.add(new PlayedGrid(grid, player));
  }

  public boolean addPlayedGrid(PlayedGrid pgrid) {
    return playedGrids.add(pgrid);
  }

  public boolean removeGrid(Grid grid) {
    return availableGrids.remove(grid);
  }

  public List<Grid> searchGrid(List<String> keywords) {
    //J'imagine qu'on recherche dans la description et pas dans les tags
    List<Grid> result = new ArrayList<Grid>();
    for (String str : keywords) {
      for (Grid grid : availableGrids) {
        if (grid.getDescription().contains(str))
          result.add(grid);
      }
    }
    return result;
  }


  public boolean updateGridList(List<String> keywords) {
    // BESOIN D'EXPLICATIONS POUR CETTE FONCTION. Je n'ai pas trouv
    // son existence dans le diag de squence
    return false;
  }

  public Grid getLatestPlayedGrid() {
    Grid g = availableGrids.get(0);
    for (Grid grid : availableGrids) {
      if (grid.getUpdateDate().after(g.getUpdateDate())) {
        g = grid;
      }
    }
    return g;
  }

  public List<PlayedGrid> getFinishedGrid() {
    List<PlayedGrid> result = new ArrayList<PlayedGrid>();
    for (PlayedGrid grid : playedGrids) {
      if (grid.isComplete())
        result.add(grid);
    }
    return result;
  }

  public List<PlayedGrid> getIncompleteGrid() {
    List<PlayedGrid> result = new ArrayList<PlayedGrid>();
    for (PlayedGrid grid : playedGrids) {
      if (!grid.isComplete())
        result.add(grid);
    }
    return result;
  }

  public List<Grid> getAvailableGrids() {
    return availableGrids;
  }

}

@@ -415,36 +415,32 @@
 = new ArrayList%3C
-Grid
 %3E();%0A    this.pl
@@ -461,34 +461,24 @@
 w ArrayList%3C
-PlayedGrid
 %3E();%0A  %7D%0A%0A  
@@ -710,24 +710,120 @@
 nstance;%0A  %7D
+%0A  %0A  public void save()%7B // Serialize and save grids%0A      %0A      // TO BE COMPLETED%0A      %0A  %7D
 %0A%0A  public b
@@ -945,26 +945,24 @@
 r player) %7B%0A
-  
   return pla
@@ -1005,21 +1005,22 @@
 ayer));%0A
+
   %7D%0A%0A
+%0A
   public
@@ -1549,22 +1549,21 @@
 result;%0A
+
   %7D%0A%0A
-%0A
   public

Add save