package com.vikashkothary.life.data.local;

import android.content.ContentValues;
import android.database.Cursor;

import com.vikashkothary.life.data.model.Name;
import com.vikashkothary.life.data.model.Profile;

import java.util.Date;

public class Db {

    public Db() {
    }

    public abstract static class RibotProfileTable {
        public static final String TABLE_NAME = "ribot_profile";

        public static final String COLUMN_EMAIL = "email";
        public static final String COLUMN_FIRST_NAME = "first_name";
        public static final String COLUMN_LAST_NAME = "last_name";
        public static final String COLUMN_HEX_COLOR = "hex_color";
        public static final String COLUMN_DATE_OF_BIRTH = "date_of_birth";
        public static final String COLUMN_AVATAR = "avatar";
        public static final String COLUMN_BIO = "bio";

        public static final String CREATE =
                "CREATE TABLE " + TABLE_NAME + " (" +
                        COLUMN_EMAIL + " TEXT PRIMARY KEY, " +
                        COLUMN_FIRST_NAME + " TEXT NOT NULL, " +
                        COLUMN_LAST_NAME + " TEXT NOT NULL, " +
                        COLUMN_HEX_COLOR + " TEXT NOT NULL, " +
                        COLUMN_DATE_OF_BIRTH + " INTEGER NOT NULL, " +
                        COLUMN_AVATAR + " TEXT, " +
                        COLUMN_BIO + " TEXT" +
                        " ); ";

        public static ContentValues toContentValues(Profile profile) {
            ContentValues values = new ContentValues();
            values.put(COLUMN_EMAIL, profile.email());
            values.put(COLUMN_FIRST_NAME, profile.name().first());
            values.put(COLUMN_LAST_NAME, profile.name().last());
            values.put(COLUMN_HEX_COLOR, profile.hexColor());
            values.put(COLUMN_DATE_OF_BIRTH, profile.dateOfBirth().getTime());
            values.put(COLUMN_AVATAR, profile.avatar());
            if (profile.bio() != null) values.put(COLUMN_BIO, profile.bio());
            return values;
        }

        public static Profile parseCursor(Cursor cursor) {
            Name name = Name.create(
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_FIRST_NAME)),
                    cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_LAST_NAME)));
            long dobTime = cursor.getLong(cursor.getColumnIndexOrThrow(COLUMN_DATE_OF_BIRTH));

            return Profile.builder()
                    .setName(name)
                    .setEmail(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_EMAIL)))
                    .setHexColor(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_HEX_COLOR)))
                    .setDateOfBirth(new Date(dobTime))
                    .setAvatar(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_AVATAR)))
                    .setBio(cursor.getString(cursor.getColumnIndexOrThrow(COLUMN_BIO)))
                    .build();
        }
    }
}

@@ -939,17 +939,8 @@
 NAME
- + %22 (%22 +
 %0A   
@@ -951,32 +951,41 @@
                 
+ + %22 (%22 +
  COLUMN_EMAIL + 
@@ -1002,21 +1002,17 @@
 MARY KEY
-, %22 +
+%22
 %0A       
@@ -1019,32 +1019,41 @@
                 
+ + %22, %22 +
  COLUMN_FIRST_NA
@@ -1064,37 +1064,33 @@
  %22 TEXT NOT NULL
-, %22 +
+%22
 %0A               
@@ -1089,32 +1089,41 @@
                 
+ + %22, %22 +
  COLUMN_LAST_NAM
@@ -1133,37 +1133,33 @@
  %22 TEXT NOT NULL
-, %22 +
+%22
 %0A               
@@ -1158,32 +1158,41 @@
                 
+ + %22, %22 +
  COLUMN_HEX_COLO
@@ -1202,37 +1202,33 @@
  %22 TEXT NOT NULL
-, %22 +
+%22
 %0A               
@@ -1227,32 +1227,41 @@
                 
+ + %22, %22 +
  COLUMN_DATE_OF_
@@ -1286,21 +1286,17 @@
 NOT NULL
-, %22 +
+%22
 %0A       
@@ -1303,32 +1303,41 @@
                 
+ + %22, %22 +
  COLUMN_AVATAR +
@@ -1343,21 +1343,17 @@
 + %22 TEXT
-, %22 +
+%22
 %0A       
@@ -1368,16 +1368,25 @@
         
+ + %22, %22 +
  COLUMN_
@@ -1398,18 +1398,16 @@
  %22 TEXT%22
- +
 %0A       
@@ -1427,13 +1427,13 @@
     
++ 
 %22
- 
 );
- 
 %22;%0A%0A

Reformat database create string