package com.placeholder.rich.todotreeprototype.infrastructure;

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

import com.placeholder.rich.todotreeprototype.model.ListTree;
import com.placeholder.rich.todotreeprototype.model.TagList;
import com.placeholder.rich.todotreeprototype.model.When;

import java.util.Locale;
import java.util.UUID;

public class ListStoreSQLite implements ListStore {

    private final TodoDbHelper todoDbHelper;
    private final SQLiteDatabase todoDb;

    public ListStoreSQLite(Context context) {
        todoDbHelper = new TodoDbHelper(context);
        todoDb = todoDbHelper.getWritableDatabase();
    }

    @Override
    public void save(ListTree currentSave) {
        //TODO
    }

    @Override
    public ListTree load(UUID currentId) {
        //TODO
        return null;
    }

    @Override
    public TagList loadTagged(When tag) {
        //TODO
        return null;
    }

    @Override
    public ListTree loadRoot() {
        //TODO
        return null;
    }

    @Override
    public void addEntry(String name, boolean completed, When when, UUID parentId, String parent) {
        //TODO
    }

    private static final class EntryTable {
        private static final String NAME = "todo_entry";
        private static final String COL_ID = "id";
        private static final String COL_ID_TYPE = "INTEGER PRIMARY KEY";
        private static final String COL_NAME = "name";
        private static final String COL_NAME_TYPE = "TEXT NOT NULL";

        private static final String SQL_CREATE = String.format((Locale) null,
                "CREATE TABLE %s (%s %s, %s %s);",
                NAME,
                COL_ID, COL_ID_TYPE,
                COL_NAME, COL_NAME_TYPE);
    }

    private class TodoDbHelper extends SQLiteOpenHelper {

        private static final String DB_NAME = "todo";
        private static final int DB_VERSION = 1;

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

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(EntryTable.SQL_CREATE);
        }

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

@@ -1426,15 +1426,12 @@
  = %22
-INTEGER
+TEXT
  PRI
@@ -1564,16 +1564,851 @@
 T NULL%22;
+%0A        private static final String COL_COMPLETE = %22complete%22;%0A        private static final String COL_COMPLETE_TYPE = %22BOOLEAN NOT NULL DEFAULT FALSE%22;%0A        private static final String COL_PARENT = %22parent%22;%0A        private static final String COL_PARENT_TYPE = %22TEXT%22;%0A        private static final String COL_SUB_ITEMS = %22sub_items%22;%0A        private static final String COL_SUB_ITEMS_TYPE = %22INTEGER NOT NULL DEFAULT 0%22;%0A        private static final String COL_ITEMS_LEFT = %22items_left%22;%0A        private static final String COL_ITEMS_LEFT_TYPE = %22INTEGER NOT NULL DEFAULT 0%22;%0A        private static final String COL_WHEN = %22when_tag%22;%0A        private static final String COL_WHEN_TYPE = %22TEXT NOT NULL%22;%0A        private static final String CONSTRAINT_PARENT_ID =%0A                %22FOREIGN KEY (parent) REFERENCES todo_entry (id)%22;
 %0A%0A      
@@ -2522,16 +2522,55 @@
 s %25s, %25s
+ %25s, %25s %25s, %25s %25s, %25s %25s, %25s %25s, %25s %25s,
  %25s);%22,%0A
@@ -2667,16 +2667,293 @@
 AME_TYPE
+,%0A                COL_COMPLETE, COL_COMPLETE_TYPE,%0A                COL_PARENT, COL_PARENT_TYPE,%0A                COL_SUB_ITEMS, COL_SUB_ITEMS_TYPE,%0A                COL_ITEMS_LEFT, COL_ITEMS_LEFT_TYPE,%0A                COL_WHEN, COL_WHEN_TYPE,%0A                CONSTRAINT_PARENT_ID
 );%0A    %7D

Add full DB implementation to creation in SQLite store