package kr.co.icontrols.iotplatform.Database;

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

/**
 * DB 파일 생성 및 Table 생성
 */
public class DatabaseHelper  extends SQLiteOpenHelper {
    private static final String TAG = DatabaseHelper.class.getSimpleName();

    private static DatabaseHelper sInstance = null;

    public static final String DATABASE_NAME = "iot_platform.db";
    static final int DATABASE_VERSION = 2;
    private final Context mContext;

    public static final String TABLE_SMART_LIGHT = "smart_light";
    public static final String TABLE_DEVICE_HOME = "device_home";
    public static final String TABLE_DEVICE_RANK = "device_rank";


    public static final String COLUMN_ID = "id";
    public static final String COLUMN_NAME = "name";
    public static final String COLUMN_GROUP_ID = "group_id";
    public static final String COLUMN_GROUP_NAME = "group_name";
    public static final String COLUMN_SHARED_NAME = "shared_name";
    public static final String COLUMN_OWNER_ID = "owner_id";
    public static final String COLUMN_TYPE = "type";
    public static final String COLUMN_RANK = "rank";
    public static final String COLUMN_GROUP_RANK = "group_rank";

    private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

        mContext = context;
    }

    public static void init(Context context) {
        if (sInstance == null) {
            sInstance = new DatabaseHelper(context);
        }
    }

    public static DatabaseHelper getInstance() {
        return sInstance;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        createTables(db);
    }

    /**
     * 배송정보에 대한 DB 테이블 생성 함수
     * @param db
     */
    private void createTables(SQLiteDatabase db) {

        db.execSQL("CREATE TABLE " + TABLE_SMART_LIGHT + " ("
                + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COLUMN_ID + " TEXT NOT NULL, "
                + COLUMN_NAME + " TEXT NOT NULL, "
                + COLUMN_GROUP_ID + " TEXT NOT NULL, "
                + COLUMN_GROUP_NAME + " TEXT NOT NULL, "
                + COLUMN_SHARED_NAME + " TEXT NOT NULL, "
                + COLUMN_OWNER_ID + " TEXT NOT NULL, "
                + COLUMN_TYPE + " TEXT NOT NULL, "
                + COLUMN_RANK + " INTEGER NOT NULL, "
                + COLUMN_GROUP_RANK + " INTEGER NOT NULL);");

        db.execSQL("CREATE TABLE " + TABLE_DEVICE_HOME + " ("
                + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COLUMN_ID + " TEXT NOT NULL, "
                + COLUMN_NAME + " TEXT NOT NULL, "
                + COLUMN_GROUP_ID + " TEXT NOT NULL, "
                + COLUMN_GROUP_NAME + " TEXT NOT NULL, "
                + COLUMN_SHARED_NAME + " TEXT NOT NULL, "
                + COLUMN_OWNER_ID + " TEXT NOT NULL, "
                + COLUMN_TYPE + " TEXT NOT NULL, "
                + COLUMN_RANK + " INTEGER NOT NULL, "
                + COLUMN_GROUP_RANK + " INTEGER NOT NULL);");

        db.execSQL("CREATE TABLE " + TABLE_DEVICE_RANK + " ("
                + BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
                + COLUMN_ID + " TEXT NOT NULL, "
                + COLUMN_NAME + " TEXT NOT NULL, "
                + COLUMN_GROUP_ID + " TEXT NOT NULL, "
                + COLUMN_GROUP_NAME + " TEXT NOT NULL, "
                + COLUMN_SHARED_NAME + " TEXT NOT NULL, "
                + COLUMN_OWNER_ID + " TEXT NOT NULL, "
                + COLUMN_TYPE + " TEXT NOT NULL, "
                + COLUMN_RANK + " INTEGER NOT NULL, "
                + COLUMN_GROUP_RANK + " INTEGER NOT NULL);");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int currentVersion) {
        switch (oldVersion) {
            case 0:
            case 1:
                // on upgrade drop older tables
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_SMART_LIGHT);
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEVICE_HOME);
                db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEVICE_RANK);
                // create new tables
                createTables(db);
                return;
        }
    }
}
