package com.example.super_duckhunt;

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.SQLiteOpenHelper;
import android.widget.Toast;

public class Data {
	public static final String KEY_NAME="person_name";
	public static final String ROW_ID="_id";
	public static final String KEY_POINTS="person_point";
	public static final String KEY_RATE="rate_game";
	private static final String DATABASE_NAME="Retrieve";
	private static final String TABLE_NAME="Info";
	private static final int DATA_VERSION=1;
	private Context c;
	private Dbhelper help;
	private SQLiteDatabase database;
	private class Dbhelper extends SQLiteOpenHelper{
		
		public Dbhelper(Context context) {
			super(context,DATABASE_NAME,null,DATA_VERSION);
			// TODO Auto-generated constructor stub
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			// TODO Auto-generated method stub
			db.execSQL("CREATE TABLE " + TABLE_NAME + " (" + ROW_ID +" INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_NAME + " TEXT NOT NULL, " + KEY_POINTS + " TEXT NOT NULL, "+
					KEY_RATE + " TEXT NOT NULL);"
					
					 
					
					
					
					
					);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
			// TODO Auto-generated method stub
			db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
			onCreate(db);
		}
		
	}public  Data(Context our){
		c=our;
	}public Data open() throws SQLException{
		help=new Dbhelper(c);
		database=help.getWritableDatabase();
		return this;
	}
	public Data close(){
		help.close();
		return this;
	}
	public long createEntry(String x, String point,String rate) {
		// TODO Auto-generated method stub
		   ContentValues cv=new ContentValues();
		   
		   cv.put(KEY_NAME, x);
		   cv.put(KEY_POINTS, point);
		   cv.put(KEY_RATE, rate);
		   return database.insert(TABLE_NAME,null,cv);
		
	}
	public String getData() {
		// TODO Auto-generated method stub
		  
		String[] columns=new String[]{ROW_ID,KEY_NAME,KEY_POINTS,KEY_RATE};
		Cursor c=database.query(TABLE_NAME, columns, null, null, null, null, null);
		String result="";
		int id=c.getColumnIndex(ROW_ID);
		int n=c.getColumnIndex(KEY_NAME);
		int po=c.getColumnIndex(KEY_POINTS);
		int ra=c.getColumnIndex(KEY_RATE);
		for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
			result=result+c.getString(id)+" "+ c.getString(n)+ " "+c.getString(po)+ " "+c.getString(ra)+"\n";
		}
		
		return result;
	}

}
