/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.onecable.admin.common.db;

import com.ntels.onecable.admin.common.db.entity.TblCodeList;
import com.ntels.onecable.admin.common.db.entity.TblLoginInfo;
import com.ntels.onecable.admin.common.util.Dlog;

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

/**
 * <pre>
 * 1. ClassName :   DatabaseOpenHelper
 * 2. FileName  :   DatabaseOpenHelper.java
 * 3. Package   :   com.ntels.onecable.admin.common.db
 * 4. Date      :   2016-12-05
 * 5. Author    :   aaron
 * 6. Comment   :   Initialize
 * </pre>
 */
public class DatabaseOpenHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "KneetAdim.db";

    private static int DB_VERSION = 1;

    private int oldDbVersion;

    /**
     * Helper의 단일 인스턴스
     */
    private static DatabaseOpenHelper _instance = null;

    private DatabaseOpenHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        Dlog.v("DB_VERSION =======> " + Integer.toString(DB_VERSION));
    }

    /**
     * SingleTon pattern으로 정의된 DatabaseHelper getter. 어플리케이션에서 단 하나의 DBHelper에서 Dagabase를 얻어와서 써야
     * Tracsaction 충돌이 일어나지 않는다. (상이한 Helper에서 생성된 DB들은 서로 Transaction을 간섭하여 프로그램 오류를 발생시키므로 반드시
     * SingleTon을 유지하여야 한다.)
     *
     * @param context : mContext
     * @return DatabaseOpenHelper
     */
    public synchronized static DatabaseOpenHelper getInstance(Context context) {
        if (_instance == null) {
            _instance = new DatabaseOpenHelper(context.getApplicationContext());
        }
        return _instance;
    }

    /**
     * DB가 최초로 생성될때 호출되는 메소드이며 테이블을 생성한다.
     *
     * @param db 안드로이드폰내 DB파일 명칭
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 콘텐츠 테이블 생성
        String sSql;
        sSql = "";
        sSql += "\n CREATE TABLE " + TblCodeList.TABLE_NAME;
        sSql += "\n ( " + TblCodeList._SEQ + " INTEGER PRIMARY KEY AUTOINCREMENT";
        sSql += "\n , " + TblCodeList.ID + " TEXT ";
        sSql += "\n , " + TblCodeList.NAME + " TEXT ";
        sSql += "\n , " + TblCodeList.UPDATE_DATE + " TEXT ";
        sSql += "\n , " + TblCodeList.USE_YN + " TEXT ";
        sSql += "\n );";
        Dlog.v(sSql);
        db.execSQL(sSql);

        //		#create table TB_CODELIST\
        //		(_seq integer primary key auto increment not null, \
        //		 id text, name text, update_date text, use_yn text);
        //		create table TB_CODE
        //		(_seq integer primary key autoincrement not null, id text, list_id text, name text, \
        //		ord text, update_date text, use_yn text);

        //		sSql = "";
        //		sSql += "\n CREATE TABLE " + TblCode.TABLE_NAME;
        //		sSql += "\n ( " + TblCode._SEQ + " INTEGER PRIMARY KEY AUTOINCREMENT";
        //		sSql += "\n , " + TblCode.LIST_ID + " TEXT ";
        //		sSql += "\n , " + TblCode.ID + " TEXT ";
        //		sSql += "\n , " + TblCode.NAME + " TEXT ";
        //		sSql += "\n , " + TblCode.ORD + " TEXT ";
        //		sSql += "\n , " + TblCode.UPDATE_DATE + " TEXT ";
        //		sSql += "\n , " + TblCode.USE_YN + " TEXT ";
        //		sSql += "\n );";
        //		Dlog.v(sSql);
        //		db.execSQL(sSql);

        //		sSql = "";
        //		sSql += "\n CREATE TABLE " + TblTimeZone.TABLE_NAME;
        //		sSql += "\n ( " + TblTimeZone._SEQ + " INTEGER PRIMARY KEY AUTOINCREMENT";
        //		sSql += "\n , " + TblTimeZone.ID + " TEXT ";
        //		sSql += "\n , " + TblTimeZone.NAME + " TEXT ";
        //		sSql += "\n );";
        //		Dlog.v(sSql);
        //		db.execSQL(sSql);

        sSql = "";
        sSql += "\n CREATE TABLE " + TblLoginInfo.TABLE_NAME;
        sSql += "\n ( " + TblLoginInfo._SEQ + " INTEGER PRIMARY KEY AUTOINCREMENT";
        sSql += "\n , " + TblLoginInfo.LOGIN_ID + " TEXT ";
        sSql += "\n , " + TblLoginInfo.LOGIN_PASS + " TEXT ";
        sSql += "\n , " + TblLoginInfo.SAVE_ID_YN + " TEXT ";
        sSql += "\n , " + TblLoginInfo.AUTO_LOGIN_YN + " TEXT ";
        sSql += "\n , " + TblLoginInfo.MANUAL_NOT_VIEW_YN + " TEXT ";
        sSql += "\n , " + TblLoginInfo.PUSH_YN + " TEXT ";
        sSql += "\n );";
        Dlog.v(sSql);
        db.execSQL(sSql);

        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0001', '영국(런던)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0002', '포르투갈(리스본)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0003', '독일(베를린)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0004', '에스파냐(마드리드)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0005', '리비아(트리폴리)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0006', '이탈리아(로마)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0007', '그리스(아테네)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0008', '터키(이스탄불)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0009', '남아프리카공화국(케이프타운)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0010', '핀란드(헬싱키)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0011', '이라크(바그다드)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0012', '러시아(모스크바)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0013', '아랍에미리트(아부다비)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0014', '파키스탄(이슬라마바드)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0015', '방글라데시(다카)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0016', '카자흐스탄(알마티)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0017', '베트남(하노이)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0018', '타이(방콕)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0019', '인도네시아(자카르타)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0020', '싱가포르(싱가포르)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0021', '타이완(타이베이)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0022', '중국(베이징)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0023', '홍콩/중국(홍콩)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0024', '대한민국(서울)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0025', '일본(도쿄)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0026', '오스트레일리아(시드니)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0027', '오스트레일리아(캔버라)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0028', '뉴칼레도니아(누메아)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0029', '솔로몬제도(호니아라)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0030', '피지(수바)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0031', '아메리칸사모아(파고파고)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0032', '미국(하와이주)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0033', '미국(알래스카주)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0034', '미국(로스앤젤레스)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0035', '미국(샌프란시스코)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0036', '미국(시애틀)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0037', '캐나다(밴쿠버)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0038', '미국(덴버)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0039', '캐나다(에드먼턴)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0040', '멕시코(멕시코시티)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0041', '미국(뉴욕)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0042', '미국(워싱턴D.C.)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0043', '캐나다(몬트리올)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0044', '캐나다(오타와)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0045', '볼리비아(라파스)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0046', '칠레(산티아고)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0047', '브라질(상파울루)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0048', '아르헨티나(부에노스아이레스)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0049', '브라질(페르난두데누로냐)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0050', '그린란드(덴마크령)(이토코르토르미우트)')");
        //		db.execSQL("insert into TB_TIMEZONE(ID, NAME) VALUES ('0051', '포르투갈(아조레스)')");
    }

    /**
     * DB가 업그레이드 되었을경우 호출되는 메소드. DB버젼이 올라갈때마다 버젼별 관리 로직을 추가해주어야 한다.
     *
     * @param db         데이터베이스
     * @param oldVersion 데이터베이스 구버전
     * @param newVersion 데이터베이스 신규버전
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        this.oldDbVersion = oldVersion;

        upgradeDatabase();
    }

    /**
     * DB_VERSION이 올라가면 실행 되는 DB Upgrade Methd
     */
    private void upgradeDatabase() {

        Dlog.v("database version = " + oldDbVersion);
        //		db.execSQL("ALTER TABLE " + TblLoginInfo.TABLE_NAME + " ADD COLUMN " + TblLoginInfo.PUSH_YN + " TEXT ;");

    }

}
