package com.example.eg17_2;

import java.sql.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class ToDoDBAdapter {
	 
	private static final String DATABASE_NAME = "todoList.db";
	private static final String DATABASE_TABLE = "todoItems";
	private static final int DATABASE_VERSION = 1;
	 
	private SQLiteDatabase mDb;
	private final Context mContext;
	 
	public static final String KEY_ID = "_id";
	public static final String KEY_TASK = "task";
	public static final int TASK_COLUMN = 1;
	 
	public static final String KEY_CREATION_DATE = "creation_date";
	public static final int CREATION_DATE_COLUMN = 2;
	 
	private ToDoDBOpenHelper dbOpenHelper;
	 
	public ToDoDBAdapter(Context context) {
		mContext = context;
		dbOpenHelper = new ToDoDBOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	 
	public void close() {
		mDb.close();
	}
	
	public String open() throws SQLiteException {
		try {
			mDb = dbOpenHelper.getWritableDatabase();
			return dbOpenHelper.getWritableDatabase().toString();
		} catch (SQLiteException ex) {
			mDb = dbOpenHelper.getReadableDatabase();
			return dbOpenHelper.getReadableDatabase().toString();
		}
	}
	
	//insert a new task
	public long insertTask(TodoItem task){
		 ContentValues newTaskValues=new ContentValues();
		 //assign values for each row
		 newTaskValues.put(KEY_TASK, task.getTask());
		 newTaskValues.put(KEY_CREATION_DATE, task.getCreated().getTime());
		 
		 //insert row
		 return mDb.insert(DATABASE_TABLE,null,newTaskValues);
	}
	
	 
	public boolean removeTask(long rowIndex){
		return mDb.delete(DATABASE_TABLE,KEY_ID+"="+rowIndex, null)>0;
	}
	
	 
	public boolean updateTask(long rowIndex,String task){
		 ContentValues newValue=new ContentValues();
		 newValue.put(KEY_TASK, task);
		 return mDb.update(DATABASE_TABLE, newValue,
		 KEY_ID+"="+rowIndex, null)>0;
	}
	
	
	public TodoItem getToDoItem(long rowIndex) throws SQLException {  
		 Cursor cursor=mDb.query(DATABASE_TABLE,  
		 new String[]{KEY_ID,KEY_TASK,KEY_CREATION_DATE},  
		 KEY_ID+"="+rowIndex, null, null, null, null);  
		 
		 if(cursor.getCount()==0 || !cursor.moveToFirst()) {  
			 //throw new SQLException ("No to do item found for row:" + rowIndex);  
			 return null;
		 }  
			 
		 String task=cursor.getString(TASK_COLUMN);  
		 long created=cursor.getLong(CREATION_DATE_COLUMN);  
		 TodoItem result=new TodoItem(task,new Date(created));
		 
		 return result;  	   
    }  
	
	
	private static class ToDoDBOpenHelper extends SQLiteOpenHelper {
		 
		public ToDoDBOpenHelper(Context context, String name,
			CursorFactory factory, int version) {
			super(context, name, factory, version);
		}
		 
		// SQL statement to create a new database
		private static final String DATABASE_CREATE = "create table "
			+ DATABASE_TABLE + " (" + KEY_ID
			+ " integer primary key autoincrement , " + KEY_TASK
			+ " text not null, " + KEY_CREATION_DATE + " long);";
			 
		@Override
		public void onCreate(SQLiteDatabase db) {
			db.execSQL(DATABASE_CREATE);
		}
		 
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion,
		int newVersion) {
			Log.w("TaskDBAdapter", "Upgrading from version " + oldVersion
			+ " to " + newVersion);
			 
			db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
			onCreate(db);
		 
		}
		 
	}
		
}