package de.fau.cs.mad.kwikshop.android.model;

import com.j256.ormlite.stmt.DeleteBuilder;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import de.fau.cs.mad.kwikshop.android.common.Item;
import de.fau.cs.mad.kwikshop.android.common.ShoppingList;
import de.fau.cs.mad.kwikshop.android.model.interfaces.ListStorage;

public class LocalListStorage implements ListStorage<ShoppingList> {

    @Override
    public int createList() {
        ShoppingList newList = new ShoppingList();
        try {
            ListStorageFragment.getDatabaseHelper().getShoppingListDao().create(newList);
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
        return newList.getId();
    }

    @Override
    public List<ShoppingList> getAllLists() {
        try {
            ArrayList<ShoppingList> lists = new ArrayList<>();
            for(ShoppingList list : ListStorageFragment.getDatabaseHelper().getShoppingListDao()) {
                lists.add(list);

                if(list.getCalendarEventDate() != null) {
                    ListStorageFragment.getDatabaseHelper().getCalendarDao().refresh(list.getCalendarEventDate());
                }

                for (Item i : list.getItems()) {

                    if (i.getUnit() != null) {
                        ListStorageFragment.getDatabaseHelper().getUnitDao().refresh(i.getUnit());
                    }

                    if (i.getGroup() != null) {
                        ListStorageFragment.getDatabaseHelper().getGroupDao().refresh(i.getGroup());
                    }
                }
            }
            return Collections.unmodifiableList(lists);

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public ShoppingList loadList(int listID) {
        ShoppingList loadedList;
        try {
            loadedList = ListStorageFragment.getDatabaseHelper().getShoppingListDao().queryForId(listID);

            if(loadedList.getCalendarEventDate() != null) {
                ListStorageFragment.getDatabaseHelper().getCalendarDao().refresh(loadedList.getCalendarEventDate());
            }

            for (Item i : loadedList.getItems()) {

                if (i.getUnit() != null) {
                    ListStorageFragment.getDatabaseHelper().getUnitDao().refresh(i.getUnit());
                }

                if (i.getGroup() != null) {
                    ListStorageFragment.getDatabaseHelper().getGroupDao().refresh(i.getGroup());
                }
            }

        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
        return loadedList;
    }

    @Override
    public boolean saveList(ShoppingList list) {
        try {
            // Update all Items first
            for (Item item : list.getItems()) {
                ListStorageFragment.getDatabaseHelper().getItemDao().update(item);
            }
            ListStorageFragment.getDatabaseHelper().getShoppingListDao().update(list);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteList(int id) {
        try {
            deleteListItems(id);
            ListStorageFragment.getDatabaseHelper().getShoppingListDao().deleteById(id);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    private void deleteListItems(int listId) {
        DeleteBuilder db;
        try {
            db = ListStorageFragment.getDatabaseHelper().getItemDao().deleteBuilder();
            db.where().eq(Item.FOREIGN_SHOPPINGLIST_FIELD_NAME, listId); // Delete all items that belong to this list
            ListStorageFragment.getDatabaseHelper().getItemDao().delete(db.prepare());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

@@ -40,16 +40,49 @@
 model;%0A%0A
+import com.j256.ormlite.dao.Dao;%0A
 import c
@@ -117,16 +117,16 @@
 uilder;%0A
-
 %0Aimport 
@@ -1662,32 +1662,212 @@
                %7D
+%0A%0A                    if(i.getLocation() != null) %7B%0A                        ListStorageFragment.getDatabaseHelper().getLocationDao().refresh(i.getLocation());%0A                    %7D
 %0A               
@@ -2817,32 +2817,200 @@
                %7D
+%0A%0A                if(i.getLocation() != null) %7B%0A                    ListStorageFragment.getDatabaseHelper().getLocationDao().refresh(i.getLocation());%0A                %7D
 %0A            %7D%0A%0A
@@ -3329,67 +3329,18 @@
     
-ListStorageFragment.getDatabaseHelper().getItemDao().
 update
+Item
 (ite
@@ -3540,32 +3540,32 @@
 alse;%0A        %7D%0A
-
         return t
@@ -3568,32 +3568,335 @@
 rn true;%0A    %7D%0A%0A
+    private void updateItem(Item item) throws SQLException %7B%0A%0A        if(item.getLocation() != null) %7B%0A            ListStorageFragment.getDatabaseHelper().getLocationDao().createIfNotExists(item.getLocation());%0A        %7D%0A        ListStorageFragment.getDatabaseHelper().getItemDao().update(item);%0A    %7D%0A%0A
     @Override%0A  

Save location for Item to Database