package com.sw_ss16.lc_app.content;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.sw_ss16.lc_app.backend.Database;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Gets and retrieves Data from Database easily
 *
 */
public class LearningCenterContent {


    // -------------------------------
    // Members
    // -------------------------------
    private Context app_context; //TODO give this at constructor, so it can be used in methots


    // -------------------------------
    // Methods
    // -------------------------------

    //
    public void setApplicationContext(Context app_context){
        this.app_context = app_context;
    }

    public void setLearningCeterToFavorite(int lc_id){

        /*
                                db.insertInDatabase("INSERT INTO favstudyrooms (ID, IS_FAV) " +
                                        "SELECT " +
                                        id + "," +
                                        "0 " +
                                        "WHERE NOT EXISTS (SELECT 1 FROM favstudyrooms WHERE ID = " + id +");");
        */

    }

    public void setLearningCeterToNoFavorite(int lc_id){

    }

    public List<String> getListOfFavLcIds(){
        ArrayList<String> all_lc_ids = new ArrayList<>();

        Database db = new Database(app_context);
        SQLiteDatabase sqldb = db.getReadableDatabase();
        String[] columns = new String[]{"ID"};

        Cursor c = sqldb.query("favstudyrooms", columns, null, null, null, null, null);

        c.moveToFirst();

        for (int i = 1; i <= c.getCount(); i++) {

            all_lc_ids.add(c.getString(c.getColumnIndex("ID")));
            c.moveToNext();
        }

        return all_lc_ids;
    }

    public List<String> getListOfLcIds(){
        ArrayList<String> all_lc_ids = new ArrayList<>();

        Database db = new Database(app_context);
        SQLiteDatabase sqldb = db.getReadableDatabase();
        String[] columns = new String[]{"ID"};

        Cursor c = sqldb.query("studyrooms", columns, null, null, null, null, null);

        c.moveToFirst();

        for (int i = 1; i <= c.getCount(); i++) {

            all_lc_ids.add(c.getString(c.getColumnIndex("ID")));
            c.moveToNext();
        }

        return all_lc_ids;
    }

    public LearningCenter getLcObject(String id){

        Database db = new Database(app_context);
        SQLiteDatabase sqldb = db.getReadableDatabase();

        String[] columns = new String[]{"ID", "NAME", "DESCRIPTION", "ADDRESS", "IMAGE_IN", "IMAGE_OUT", "CAPACITY"};
        String[] favcolumns = new String[]{"ID"};

        String query_string = "ID = " + id;

        Cursor c = sqldb.query("studyrooms", columns, query_string, null, null, null, "ID", "1");
        Cursor isfav = sqldb.query("favstudyrooms", favcolumns, query_string, null, null, null, "ID", "1");

        c.moveToFirst();
        isfav.moveToFirst();

        return new LearningCenter(
                c.getString(c.getColumnIndex("ID")),
                c.getString(c.getColumnIndex("NAME")),
                c.getString(c.getColumnIndex("DESCRIPTION")),
                c.getString(c.getColumnIndex("ADDRESS")),
                c.getString(c.getColumnIndex("IMAGE_IN")),
                c.getString(c.getColumnIndex("IMAGE_OUT")),
                c.getString(c.getColumnIndex("CAPACITY")),
                "0"); //TODO get status of fav from table
    }

}

@@ -34,37 +34,8 @@
 t;%0A%0A
-import android.app.Activity;%0A
 impo
@@ -94,16 +94,55 @@
 Cursor;%0A
+import android.database.DatabaseUtils;%0A
 import a
@@ -275,85 +275,12 @@
 til.
-Arrays;%0Aimport java.util.HashMap;%0Aimport java.util.List;%0Aimport java.util.Map
+List
 ;%0A%0A/
@@ -777,38 +777,41 @@
   %7D%0A%0A    public 
-void s
+boolean g
 etLearningCeterT
@@ -813,18 +813,16 @@
 eter
-To
 Favorite
 (int
@@ -809,32 +809,38 @@
 ingCeterFavorite
+Status
 (int lc_id)%7B%0A%0A  
@@ -849,129 +849,265 @@
     
-/*%0A                                db.insertInDatabase(%22INSERT INTO favstudyrooms (ID, IS_FAV) %22 +%0A              
+Database db = new Database(app_context);%0A        SQLiteDatabase sqldb = db.getReadableDatabase();%0A%0A        boolean is_fav = false;%0A%0A        if((DatabaseUtils.queryNumEntries(sqldb, %22favstudyrooms%22, %22ID = %22 + lc_id)) %3E 0)%7B%0A            is_fav = true;%0A
         
     
@@ -1098,32 +1098,35 @@
 ue;%0A        
+%7D%0A%0A
         
           %22S
@@ -1117,44 +1117,252 @@
     
- 
+return is_fav;%0A%0A
     
+%7D%0A%0A
     
- %22SELECT %22 +%0A              
+public void setLearningCeterFavoriteStatus(int lc_id, boolean set_is_favorite)%7B%0A%0A        if((getLearningCeterFavoriteStatus(lc_id) && set_is_favorite) %7C%7C (!getLearningCeterFavoriteStatus(lc_id) && !set_is_favorite))%7B%0A
     
@@ -1373,79 +1373,223 @@
     
+return;%0A
        
+ %7D%0A%0A
        
-id + %22,%22 +%0A                                        %220 %22 +
+ else if(getLearningCeterFavoriteStatus(lc_id))%7B%0A            Database db = new Database(app_context);%0A            db.insertInDatabase(%22DELETE FROM favstudyrooms WHERE ID = %22 + lc_id + %22;%22);
 %0A   
@@ -1589,32 +1589,35 @@
 %22);%0A        
+%7D%0A%0A
         
             
@@ -1596,32 +1596,38 @@
      %7D%0A%0A        
+else%7B%0A
                 
@@ -1626,52 +1626,93 @@
     
-            %22WHERE NOT EXISTS (SELECT 1 FROM
+Database db = new Database(app_context);%0A            db.insertInDatabase(%22INSERT INTO
  fav
@@ -1722,31 +1722,31 @@
 dyrooms 
-WHERE ID = 
+VALUES (
 %22 + 
+lc_
 id +%22);%22
@@ -1760,75 +1760,11 @@
     
-*/%0A%0A    %7D%0A%0A    public void setLearningCeterToNoFavorite(int lc_id)%7B
+%7D%0A%0A
 %0A%0A  

Add fav methods