package ch.liip.timeforcoffee.helper;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.location.Location;
import android.provider.BaseColumns;

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

import ch.liip.timeforcoffee.api.Station;

public class FavoritesDatabaseHelper extends SQLiteOpenHelper {

    public static abstract class FavoriteStationColumn implements BaseColumns {
        public static final String OLD_TABLE_NAME = "favorites";
        public static final String TABLE_NAME = "favorite_stations";
        public static final String COLUMN_ID = "id";
        public static final String COLUMN_STATION_ID = "station_id";
        public static final String COLUMN_NAME = "name";
        public static final String COLUMN_LATITUDE = "latitude";
        public static final String COLUMN_LONGITUDE = "longitude";
    }

    public static abstract class FavoriteLineColumn implements BaseColumns {
        public static final String TABLE_NAME = "favorite_lines";
        public static final String COLUMN_ID = "id";
        public static final String COLUMN_NAME = "name";
        public static final String COLUMN_DESTINATION_ID = "destination_id";
    }

    public static final int DATABASE_VERSION = 2;
    public static final String DATABASE_NAME = "FavoritesReader.db";

    private static final String TEXT_TYPE = " TEXT";
    private static final String INT_TYPE = " INTEGER";
    private static final String DOUBLE_TYPE = " DOUBLE";
    private static final String FLOAT_TYPE = " FLOAT";
    private static final String COMMA_SEP = ",";

    private static final String SQL_CREATE_STATION_TABLE =
            "CREATE TABLE " + FavoriteStationColumn.TABLE_NAME + " (" +
                    FavoriteStationColumn.COLUMN_ID + " INTEGER PRIMARY KEY," +
                    FavoriteStationColumn.COLUMN_STATION_ID + INT_TYPE + COMMA_SEP +
                    FavoriteStationColumn.COLUMN_NAME + TEXT_TYPE + COMMA_SEP +
                    FavoriteStationColumn.COLUMN_LATITUDE + DOUBLE_TYPE + COMMA_SEP +
                    FavoriteStationColumn.COLUMN_LONGITUDE + DOUBLE_TYPE + " )";

    private static final String SQL_CREATE_LINE_TABLE =
            "CREATE TABLE " + FavoriteLineColumn.TABLE_NAME + " (" +
                    FavoriteLineColumn.COLUMN_ID + " INTEGER PRIMARY KEY," +
                    FavoriteLineColumn.COLUMN_NAME + TEXT_TYPE + COMMA_SEP +
                    FavoriteLineColumn.COLUMN_DESTINATION_ID + INT_TYPE + " )";

    private static final String SQL_DELETE_STATION_TABLE = "DROP TABLE IF EXISTS " + FavoriteStationColumn.TABLE_NAME;
    private static final String SQL_DELETE_OLD_STATION_TABLE = "DROP TABLE IF EXISTS " + FavoriteStationColumn.OLD_TABLE_NAME;
    private static final String SQL_DELETE_LINE_TABLE = "DROP TABLE IF EXISTS " + FavoriteLineColumn.TABLE_NAME;

    public FavoritesDatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public void onCreate(SQLiteDatabase db) {
        db.execSQL(SQL_CREATE_STATION_TABLE);
        db.execSQL(SQL_CREATE_LINE_TABLE);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if(oldVersion == 1 && newVersion == 2) {
            // get old favorite stations
            List<Station> oldFavoriteStations = new ArrayList<>();

            Cursor cursor = db.query("favorites", new String[] {"id", "station_id", "name", "latitude", "longitude", "distance"}, null, null, null, null, null);
            cursor.moveToFirst();
            while (!cursor.isAfterLast()) {
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(FavoriteStationColumn.COLUMN_STATION_ID));
                String name = cursor.getString(cursor.getColumnIndexOrThrow(FavoriteStationColumn.COLUMN_NAME));
                double latitude = cursor.getDouble(cursor.getColumnIndexOrThrow(FavoriteStationColumn.COLUMN_LATITUDE));
                double longitude = cursor.getDouble(cursor.getColumnIndexOrThrow(FavoriteStationColumn.COLUMN_LONGITUDE));

                Location location = new Location("fav");
                location.setLatitude(latitude);
                location.setLongitude(longitude);

                oldFavoriteStations.add(new Station(id, name, 0, location, true));
                cursor.moveToNext();
            }
            cursor.close();

            // remove old table and create the new ones
            db.execSQL(SQL_DELETE_OLD_STATION_TABLE);
            onCreate(db);

            // add old favorite stations in the new table
            for(Station oldFavoriteStation : oldFavoriteStations) {
                ContentValues values = new ContentValues();

                values.put(FavoriteStationColumn.COLUMN_STATION_ID, oldFavoriteStation.getId());
                values.put(FavoriteStationColumn.COLUMN_NAME, oldFavoriteStation.getName());
                values.put(FavoriteStationColumn.COLUMN_LATITUDE, oldFavoriteStation.getLocation().getLatitude());
                values.put(FavoriteStationColumn.COLUMN_LONGITUDE, oldFavoriteStation.getLocation().getLongitude());

                db.insert(FavoriteStationColumn.TABLE_NAME, null, values);
            }
        } else {
            db.execSQL(SQL_DELETE_STATION_TABLE);
            db.execSQL(SQL_DELETE_LINE_TABLE);
            onCreate(db);
        }
    }

    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        onUpgrade(db, oldVersion, newVersion);
    }
}
@@ -3607,20 +3607,8 @@
 ude%22
-, %22distance%22
 %7D, n
@@ -3637,16 +3637,16 @@
  null);%0A
+
         
@@ -3731,19 +3731,22 @@
         
+Str
 in
-t
+g
  id = cu
@@ -3757,11 +3757,14 @@
 .get
-Int
+String
 (cur
@@ -3793,47 +3793,20 @@
 row(
-FavoriteStationColumn.COLUMN_STATION_ID
+%22station_id%22
 ));%0A
@@ -3885,41 +3885,14 @@
 row(
-FavoriteStationColumn.COLUMN_NAME
+%22name%22
 ));%0A
@@ -3975,45 +3975,18 @@
 row(
-FavoriteStationColumn.COLUMN_LATITUDE
+%22latitude%22
 ));%0A
@@ -4070,46 +4070,19 @@
 row(
-FavoriteStationColumn.COLUMN_LONGITUDE
+%22longitude%22
 ));%0A
@@ -4290,18 +4290,36 @@
 Station(
+Integer.parseInt(
 id
+)
 , name, 

Convert string ids to integer ids in the migration