package ai.pai.client.db;

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

public class DBHelper extends SQLiteOpenHelper{

    private static final String DB_NAME = "paiai";
    private static final int DB_VERSION = 3;
    private static DBHelper instance;

    public static final String GROUP_INFO_TABLE = "group_info_table";

    public static final String GROUP_LIST_TABLE = "group_list_table";

    public interface GROUP_INFO_COLUMNS {
        String GROUP_NAME = "group_name";
        String GROUP_ID = "group_id";
        String CREATE_TIME = "create_time";
        String GROUP_LOCK = "is_locked";
        String GROUP_DESC = "group_desc";
        String GROUP_FROM = "group_from";
        String GROUP_AVATAR_ID = "group_avatar_id";
        String GROUP_AVATAR_URL = "group_avatar_url";
        String GROUP_PHOTO_COUNT = "group_photo_count";
        String CURRENT_PHOTO_ID = "current_id";
        String LAST_ENTER_TIME = "last_enter_time";
    }


    public static final String PHOTO_INFO_TABLE = "photo_info_table";

    public static final String RECENT_PHOTO_TABLE = "recent_photos_table";


    public interface PHOTO_INFO_COLUMNS {
        String PHOTO_NAME = "photo_name";
        String PHOTO_ID = "photo_id";
        String CAPTURE_TIME = "capture_time";
        String EXPIRE_TIME = "expire_time";
        String THUMBNAIL_URL = "thumbnail_url";
        String PHOTO_URL = "photo_url";
        String PHOTO_GROUP_ID = "group_id";
        String PHOTO_UPLOADER_ID = "uploader_id";
        String THUMBNAIL_WIDTH = "width";
        String THUMBNAIL_HEIGHT = "height";
        String GROUP_AVATAR = "group_avatar";
        String UPLOADER_NAME = "uploader_name";
        String UPLOADER_AVATAR = "uploader_avatar";
        String COMMENT_NUM = "comment_num";
        String THUMBUP_NUM = "thumbup_num";
        String GROUP_NAME = "group_name";
        String GROUP_DEFAULT_AVATAR = "group_default_avatar";
        String PHOTO_FROM = "photo_from";
        String GROUP_FROM = "group_from";
        String THUMB_UP = "thumb_up";
        String BIG_THUMBNAIL_URL = "big_thumbnail_url";
        String MEDIUM_PHOTO_URL = "medium_photo_url";
        String RAW_PHOTO_URL = "raw_photo_url";
        String SHARE_PHOTO_URL = "share_url";
        String SESSION_ID = "session_id";
    }

    public static final String UPLOAD_PHOTO_TABLE = "upload_photos_table";

    public interface UPLOAD_PHOTO_COLUMNS {
        String PHOTO_PATH = "photo_path";
        String USER_ID = "user_id";
        String GROUP_ID = "group_id";
        String NICK_NAME = "nickname";
        String CREATE_TIME = "create_time";
    }

    private DBHelper(Context context){
        super(context,DB_NAME,null,DB_VERSION);
    }

    public synchronized static DBHelper getInstance(Context context){
        if(instance==null){
            instance = new DBHelper(context.getApplicationContext());
        }
        return instance;
    }

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

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        switch (oldVersion){
            case 1:
                upgradeToVersion2(db);
            case 2:
                upgradeToVersion3(db);
                break;
            default:
                dropAllTables(db);
                createTables(db);
                break;
        }
    }

    private void upgradeToVersion2(SQLiteDatabase db) {
        db.execSQL("Alter table "+ PHOTO_INFO_TABLE+" add column "+PHOTO_INFO_COLUMNS.EXPIRE_TIME +" LONG");
    }

    private void upgradeToVersion3(SQLiteDatabase db) {
        db.execSQL("Alter table "+ PHOTO_INFO_TABLE+" add column "+PHOTO_INFO_COLUMNS.SESSION_ID +" VARCHAR");
    }

    private void dropAllTables(SQLiteDatabase db){
        db.execSQL("DROP TABLE IF EXISTS " + GROUP_INFO_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + GROUP_LIST_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + PHOTO_INFO_TABLE);
        db.execSQL("DROP TABLE IF EXISTS " + RECENT_PHOTO_TABLE);
    }

    private void createTables(SQLiteDatabase db){
        createUploadTable(db);
        createGroupTable(db,GROUP_INFO_TABLE);
        createGroupTable(db,GROUP_LIST_TABLE);
        createPhotoTable(db,PHOTO_INFO_TABLE);
        createPhotoTable(db,RECENT_PHOTO_TABLE);
    }

    private void createGroupTable(SQLiteDatabase db, String tableName){
        StringBuilder sql = new StringBuilder();
        sql.append("CREATE TABLE IF NOT EXISTS ");
        sql.append(tableName).append("( ");
        sql.append(GROUP_INFO_COLUMNS.GROUP_ID).append(" VARCHAR PRIMARY KEY, ");
        sql.append(GROUP_INFO_COLUMNS.GROUP_NAME).append(" VARCHAR, ");
        sql.append(GROUP_INFO_COLUMNS.GROUP_DESC).append(" VARCHAR, ");
        sql.append(GROUP_INFO_COLUMNS.GROUP_FROM).append(" VARCHAR, ");
        sql.append(GROUP_INFO_COLUMNS.GROUP_AVATAR_URL).append(" VARCHAR, ");
        sql.append(GROUP_INFO_COLUMNS.GROUP_PHOTO_COUNT).append(" INTEGER, " );
        sql.append(GROUP_INFO_COLUMNS.GROUP_LOCK).append(" INTEGER, ");
        sql.append(GROUP_INFO_COLUMNS.LAST_ENTER_TIME).append(" LONG, ");
        sql.append(GROUP_INFO_COLUMNS.GROUP_AVATAR_ID).append(" INTEGER, ");
        sql.append(GROUP_INFO_COLUMNS.CURRENT_PHOTO_ID).append(" INTEGER, ");
        sql.append(GROUP_INFO_COLUMNS.CREATE_TIME).append(" VARCHAR ");
        sql.append(")");
        db.execSQL(sql.toString());
    }

    private void createPhotoTable(SQLiteDatabase db, String tableName){
        StringBuilder sql = new StringBuilder();
        sql.append("CREATE TABLE IF NOT EXISTS ");
        sql.append(tableName).append("(");
        sql.append(PHOTO_INFO_COLUMNS.PHOTO_ID).append(" LONG PRIMARY KEY, ");
        sql.append(PHOTO_INFO_COLUMNS.PHOTO_NAME).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.PHOTO_GROUP_ID).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.PHOTO_UPLOADER_ID).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.EXPIRE_TIME).append(" LONG, ");
        sql.append(PHOTO_INFO_COLUMNS.CAPTURE_TIME).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.PHOTO_URL).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.GROUP_AVATAR).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.GROUP_DEFAULT_AVATAR).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.GROUP_NAME).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.UPLOADER_AVATAR).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.UPLOADER_NAME).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.COMMENT_NUM).append(" INTEGER, ");
        sql.append(PHOTO_INFO_COLUMNS.THUMBUP_NUM).append(" INTEGER, ");
        sql.append(PHOTO_INFO_COLUMNS.GROUP_FROM).append(" INTEGER, ");
        sql.append(PHOTO_INFO_COLUMNS.THUMB_UP).append(" INTEGER, ");
        sql.append(PHOTO_INFO_COLUMNS.PHOTO_FROM).append(" INTEGER, ");
        sql.append(PHOTO_INFO_COLUMNS.THUMBNAIL_WIDTH).append(" SHORT, ");
        sql.append(PHOTO_INFO_COLUMNS.THUMBNAIL_HEIGHT).append(" SHORT, ");
        sql.append(PHOTO_INFO_COLUMNS.BIG_THUMBNAIL_URL).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.MEDIUM_PHOTO_URL).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.RAW_PHOTO_URL).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.SHARE_PHOTO_URL).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.SESSION_ID).append(" VARCHAR, ");
        sql.append(PHOTO_INFO_COLUMNS.THUMBNAIL_URL).append(" VARCHAR ");
        sql.append(")");
        db.execSQL(sql.toString());
    }


    private void createUploadTable(SQLiteDatabase db){
        StringBuilder sql = new StringBuilder();
        sql.append("CREATE TABLE IF NOT EXISTS ");
        sql.append(UPLOAD_PHOTO_TABLE).append("(");
        sql.append(UPLOAD_PHOTO_COLUMNS.PHOTO_PATH).append(" VARCHAR PRIMARY KEY, ");
        sql.append(UPLOAD_PHOTO_COLUMNS.GROUP_ID).append(" VARCHAR, ");
        sql.append(UPLOAD_PHOTO_COLUMNS.USER_ID).append(" VARCHAR, ");
        sql.append(UPLOAD_PHOTO_COLUMNS.CREATE_TIME).append(" LONG, ");
        sql.append(UPLOAD_PHOTO_COLUMNS.NICK_NAME).append(" VARCHAR ");
        sql.append(")");
        db.execSQL(sql.toString());
    }


}
