package beyondsoft.bewomitarbeiterapp;

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

import java.util.ArrayList;
import java.util.List;

/**
 * Created by bib on 07.06.2016.
 */
public class DatabaseHandler extends SQLiteOpenHelper {

    //DataBase version
    private static final int DATABASE_VERSION = 1;
    //Database name
    private static  final String DATABASE_NAME = "Contacts_Manager";
    //Table Name
    private static final String TABLE_CONTACTS = "Contactlist";


    //Table Columns name
    private static final String KEY_ID ="id";
    private static final String KEY_KontaktName = "kontaktname";
    private static final String KEY_ART = "art";
    private static final String KEY_Picture_BLOB = "bild";


    public DatabaseHandler(Context context){
        super(context,DATABASE_NAME,null,DATABASE_VERSION);
    }

    // Erstelle Tabelle | Tabellen construct
    public void onCreate(SQLiteDatabase db){
        String CREATE_CONTACTS_TABLE ="CREATE TABLE "+TABLE_CONTACTS +"("+
                                      KEY_ID +" INTEGER PRIMARY KEY,"+KEY_KontaktName+ " TEXT,"+
                                      KEY_ART +" TEXT," + KEY_Picture_BLOB+ " BLOB"+")";

        db.execSQL(CREATE_CONTACTS_TABLE);
    }


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

        //drop older Table if existed
        db.execSQL("DROP TABLE IF EXISTS "+ TABLE_CONTACTS);

        onCreate(db);
    }

    //Add Neuen Contact
    void addContact(ContactList contact) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        //Values übergabe
        values.put(KEY_KontaktName,contact.getKontaktName());
        values.put(KEY_ART,contact.getArt());
        values.put(KEY_Picture_BLOB,contact.getPicture());

        db.insert(TABLE_CONTACTS,null,values);
        db.close();
    }

    //Hole ein Bestimten Contact
    ContactList getContact(int id) {

        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_CONTACTS, new String[]{KEY_ID,KEY_KontaktName,
                                 KEY_ART,KEY_Picture_BLOB},KEY_ID +"=?",new String[]{String.valueOf(id)}
                                 ,null,null,null,null);

        if(cursor !=null)
            cursor.moveToFirst();

        ContactList clist = new ContactList(Integer.parseInt(cursor.getString(0)),cursor.getString(1),
                                            cursor.getString(2),cursor.getBlob(3));
        return clist;
    }

    //Holle alle Contacts
    public List<ContactList> getAllContacts(){
        List<ContactList> contactList = new ArrayList<ContactList>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                ContactList contact = new ContactList();
                contact.setID(Integer.parseInt(cursor.getString(0)));
                contact.setKontaktName(cursor.getString(1));
                contact.setArt(cursor.getString(2));
                contact.setPicture(cursor.getBlob(3));

                // Adding contact to list
                contactList.add(contact);
            } while (cursor.moveToNext());
        }

        return contactList;
    }

    //updating single Contact
    public int updateContact(ContactList contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_KontaktName, contact.getKontaktName());
        values.put(KEY_ART, contact.getArt());
        values.put(KEY_Picture_BLOB, contact.getPicture());

        // updating row
        return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
    }

    //Delet single Contact
    public void deletContacts(ContactList contact){
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
                new String[] { String.valueOf(contact.getID()) });
        db.close();
    }

    public int getContactsCount(){
        String countQuery = "SELECT  * FROM " + TABLE_CONTACTS;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        cursor.close();

        // return count
        return cursor.getCount();
    }


}
