package com.bookbase.app.utils;

import android.arch.persistence.room.TypeConverter;

import com.bookbase.app.mainscreen.HomeScreen;
import com.bookbase.app.database.AppDatabase;
import com.bookbase.app.model.entity.Author;
import com.bookbase.app.model.entity.Genre;
import com.bookbase.app.model.entity.Review;

import java.sql.Date;
import java.util.Calendar;

public class Converters {

    private static final AppDatabase db = AppDatabase.getDatabase(HomeScreen.context);

    @TypeConverter
    public static Long dateToLong(Date date){
        return date == null ? null : date.getTime();
    }

    @TypeConverter
    public static Calendar toCalendar(String date){
        // TODO: Complete implementation.
        return Calendar.getInstance();
    }

    @TypeConverter
    public static String calendarToString(Calendar calendar){
        // TODO: Finish implementation.
        return "";
    }

    @TypeConverter
    public static int authorAsId(Author author){
        if(db.authorDao().getAuthorById(author.getAuthorId()) != null) {
            return author.getAuthorId();
        }
        return (int) db.authorDao().insert(author);
    }

    @TypeConverter
    public static Author intToAuthor(int id){
        return db.authorDao().getAuthorById(id);
    }

    @TypeConverter
    public static int genreAsId(Genre genre){
        if(db.genreDao().getGenreById(genre.getGenreId()) != null) {
            return genre.getGenreId();
        }
        return (int) db.genreDao().insert(genre);
    }

    @TypeConverter
    public static Genre intToGenre(int id){
        return db.genreDao().getGenreById(id);
    }

    @TypeConverter
    public static int reviewAsId(Review review){ return (int) db.reviewDao().insert(review); }

    @TypeConverter
    public static Review intToReview(int id) {return db.reviewDao().getReviewById(id);}

}

@@ -310,29 +310,170 @@
 ew;%0A
-%0Aimport java.sql.Date
+import com.crashlytics.android.Crashlytics;%0A%0Aimport java.sql.Date;%0Aimport java.text.DateFormat;%0Aimport java.text.ParseException;%0Aimport java.text.SimpleDateFormat
 ;%0Aim
@@ -496,16 +496,41 @@
 alendar;
+%0Aimport java.util.Locale;
 %0A%0Apublic
@@ -846,66 +846,173 @@
     
-// TODO: Complete implementation.%0A        return C
+final Calendar calendar = Calendar.getInstance();%0A        final DateFormat df = new SimpleDateFormat(%22ddMMyyyy%22, Locale.ENGLISH);%0A        try %7B%0A            c
 alendar.
 getI
@@ -1011,168 +1011,348 @@
 dar.
-getInstance();%0A    %7D%0A%0A    @TypeConverter%0A    public static String calendarToString(Calendar calendar)%7B%0A        // TODO: Finish implementation.%0A        return %22%22
+setTime(df.parse(date));%0A        %7D catch (ParseException e) %7B%0A            Crashlytics.logException(e);%0A        %7D%0A        return calendar;%0A    %7D%0A%0A    @TypeConverter%0A    public static String calendarToString(Calendar calendar)%7B%0A        final DateFormat df = new SimpleDateFormat(%22ddMMyyyy%22, Locale.ENGLISH);%0A        return df.format(calendar)
 ;%0A  

Fix calendar converter methods