package com.example.lenovo.addressbook;

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

/**
 * Created by lenovo on 2017/3/30.
 */

public class DatabaseManager extends SQLiteOpenHelper {
    private static DatabaseManager dbInstance;                // 实例
    private static final String DB_NAME = "AddressBook";      // 数据库名称
    private static final int DB_VERSION = 1;                  // 数据库版本
    private static final String TABLE_NAME = "Contacts";      //表名称

    // Contacts表的字段
    private static final String ID = "_id";                         //姓名
    private static final String NAME = "name";                      //姓名
    private static final String PHONE = "phone";                    // 电话
    private static final String EMAIL = "email";                    // 邮箱
    private static final String WEIXIN = "weixin";                  // 微信号
    private static final String QQ = "qq";                          // QQ号
    private static final String ORGANIZATION = "organization";   // 机构
    private static final String ADDRESS = "address";                // 地址
    private static final String COLOR = "color";                    // 颜色

    private static final String CREATE_TABLE = "CREATE TABLE "
            + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + NAME + " TEXT,"
            + PHONE + " TEXT,"
            + EMAIL + " TEXT,"
            + WEIXIN + " TEXT,"
            + QQ + " TEXT,"
            + ORGANIZATION + " TEXT,"
            + ADDRESS + " TEXT,"
            + COLOR + " TEXT)";

    //私有构造函数可以防止直接对该类实例化，应调用静态的getInstance方法获得单例
    private DatabaseManager(Context context) {
        super(context,DB_NAME,null,DB_VERSION);
    }

    //获取单例
    public static synchronized DatabaseManager getInstance(Context context) {
        if (dbInstance == null) {
            dbInstance = new DatabaseManager(context.getApplicationContext());
        }
        return dbInstance;
    }

    //创建表
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_TABLE);
    }

    //升级数据库
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        if (newVersion > oldVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
        }
    }

    //添加记录
    public Long add(Person person) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(NAME, person.getName());
        values.put(PHONE, person.getPhone());
        values.put(EMAIL, person.getEmail());
        values.put(WEIXIN, person.getWeixin());
        values.put(QQ, person.getQq());
        values.put(ORGANIZATION, person.getOrganization());
        values.put(ADDRESS, person.getAddress());
        values.put(COLOR, person.getColor());
        return db.insert(TABLE_NAME, null, values);
    }

    //删除记录
    public void remove(long person_id) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_NAME, ID + " = ?", new String[]{String.valueOf(person_id)});
    }

    //获取一个联系人
    public Person getPerson(long person_id) {
        SQLiteDatabase db = this.getWritableDatabase();
        String selectQuery = "SELECT  * FROM " + TABLE_NAME
                                                 + " WHERE " + ID + "= " + person_id;
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor != null)
            cursor.moveToFirst();
        Person person = new Person();
        person.setId(cursor.getInt(cursor.getColumnIndex(ID)));
        person.setName(cursor.getString(cursor.getColumnIndex(NAME)));
        person.setPhone(cursor.getString(cursor.getColumnIndex(PHONE)));
        person.setEmail(cursor.getString(cursor.getColumnIndex(EMAIL)));
        person.setWeixin(cursor.getString(cursor.getColumnIndex(WEIXIN)));
        person.setQq(cursor.getString(cursor.getColumnIndex(QQ)));
        person.setOrganization(cursor.getString(cursor.getColumnIndex(ORGANIZATION)));
        person.setAddress(cursor.getString(cursor.getColumnIndex(ADDRESS)));
        person.setColor(cursor.getString(cursor.getColumnIndex(COLOR)));
        cursor.close();
        return person;
    }

    //获取全部联系人（返回联系人游标）
    public Cursor getContacts() {
        String selectQuery = "SELECT  * FROM " + TABLE_NAME;
        SQLiteDatabase db = this.getWritableDatabase();
        return db.rawQuery(selectQuery, null);
    }

    //更新联系人记录
    public int update(Person person) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(NAME, person.getName());
        values.put(PHONE, person.getPhone());
        values.put(EMAIL, person.getEmail());
        values.put(WEIXIN, person.getWeixin());
        values.put(QQ, person.getQq());
        values.put(ORGANIZATION, person.getOrganization());
        values.put(ADDRESS, person.getAddress());
        values.put(COLOR, person.getColor());
        return db.update(TABLE_NAME, values, ID + " = ?", new String[] {String.valueOf(person.getId())});
    }
}
