package ai.pai.lensman.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper{

    private static final String DB_NAME = "paiai";
    private static final int DB_VERSION = 1;
    private static DBHelper instance;

    public static final String PHOTO_INFO_TABLE = "photo_info_table";

    public interface PHOTO_INFO_COLUMNS {
        String PHOTO_ID = "photo_id";
        String PHOTO_NAME = "photo_name";
        String PHOTO_PATH = "photo_path";
        String CAPTURE_TIME = "capture_time";
        String SESSION_ID = "session_id";
        String LENSMAN_ID = "lensman_id";
        String SESSION_DATE = "session_date";
        String SESSION_SEQ = "session_seq";
        String IS_RAW_PHOTO= "is_raw_photo";
        String UPLOADED_STATUS = "upload_status";

    }


    private DBHelper(Context context){
        super(context,DB_NAME,null,DB_VERSION);
    }

    public synchronized static DBHelper getInstance(Context context){
        if(instance==null){
            instance = new DBHelper(context.getApplicationContext());
        }
        return instance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createTables(db);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    private void createTables(SQLiteDatabase db){
        createPhotoTable(db,PHOTO_INFO_TABLE);
    }


    private void createPhotoTable(SQLiteDatabase db, String tableName){
        StringBuilder sql = new StringBuilder();
        sql.append("CREATE TABLE IF NOT EXISTS ");
        sql.append(tableName).append("(");
        sql.append(PHOTO_INFO_COLUMNS.PHOTO_ID).append(" LONG PRIMARY KEY, ");
        sql.append(PHOTO_INFO_COLUMNS.PHOTO_NAME).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.PHOTO_PATH).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.LENSMAN_ID).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.SESSION_ID).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.IS_RAW_PHOTO).append(" INTEGER, ");
        sql.append(PHOTO_INFO_COLUMNS.SESSION_SEQ).append(" INTEGER, ");
        sql.append(PHOTO_INFO_COLUMNS.SESSION_DATE).append(" LONG, ");
        sql.append(PHOTO_INFO_COLUMNS.UPLOADED_STATUS).append(" INTEGER, ");
        sql.append(PHOTO_INFO_COLUMNS.CAPTURE_TIME).append(" LONG ");
        sql.append(")");
        db.execSQL(sql.toString());
    }

}
