package ai.pai.client.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.android.common.utils.LogHelper;

import java.util.ArrayList;

import ai.pai.client.beans.GroupInfo;
import ai.pai.client.beans.GroupPhotoItem;
import ai.pai.client.beans.UploadPhotoBean;


public class DBService {

    private static DBService instance;
    private static Object DB_LOCK = new Object();
    private DBHelper dbHelper;

    private static final String TAG = "czy";

    private DBService(Context context) {
        dbHelper = DBHelper.getInstance(context);
    }

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

    private void closeDB(SQLiteDatabase db) {
        if (db != null) {
            db.close();
        }
    }

    private void closeCursorAndDB(Cursor cursor, SQLiteDatabase db) {
        if (cursor != null && !cursor.isClosed()) {
            cursor.close();
        }
        if (db != null) {
            db.close();
        }
    }

    public int getCurrentIdByGroupId(String groupId) {
        int currentId = 0;
        GroupInfo groupInfo = null;
        SQLiteDatabase db = null;
        Cursor c = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getReadableDatabase();
                db.beginTransaction();
                c = db.rawQuery("select * from " + DBHelper.GROUP_INFO_TABLE + " where "
                        + DBHelper.GROUP_INFO_COLUMNS.GROUP_ID + " = '" + groupId + "'", null);
                if (c.moveToNext()) {
                    groupInfo = cursor2GroupInfo(c);
                    LogHelper.d(TAG, "dbservice getCurrentIdByGroupId found group with currentId= " + groupInfo.currentId);
                }
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice getCurrentIdByGroupId error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeCursorAndDB(c, db);
            }
        }
        if (groupInfo == null) {
            LogHelper.d(TAG, "dbservice getCurrentIdByGroupId not found group ");
            return currentId;
        } else {
            return groupInfo.currentId;
        }
    }

    public void updateCurrentIdByGroupId(String groupId, String currentId) {
        GroupInfo info = getGroupInfoByGroupId(groupId);
        if (info == null) {
            LogHelper.d(TAG, "dbservice updateCurrentIdByGroupId info null ");
            info = new GroupInfo();
            info.groupId = groupId;
        }
        info.currentId = Integer.parseInt(currentId);
        addNewGroup(info);
    }

    public void updateLastEnterTimeByGroupId(String groupId, long lastEnterTime) {
        GroupInfo info = getGroupInfoByGroupId(groupId);
        if (info == null) {
            LogHelper.d(TAG, "dbservice updateLastEnterTimeByGroupId info null ");
            info = new GroupInfo();
            info.lastEnterTime = lastEnterTime;
        }
        info.lastEnterTime = lastEnterTime;
        addNewGroup(info);
    }


    public ArrayList<GroupPhotoItem> getGroupPhotoListByGroupId(String groupId) {
        LogHelper.d(TAG, "dbservice getGroupPhotoListByGroupId :" + groupId);
        ArrayList<GroupPhotoItem> photoItems = new ArrayList<GroupPhotoItem>();
        SQLiteDatabase db = null;
        Cursor c = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getReadableDatabase();
                db.beginTransaction();
                c = db.rawQuery("select * from " + DBHelper.PHOTO_INFO_TABLE + " where "
                        + DBHelper.PHOTO_INFO_COLUMNS.PHOTO_GROUP_ID + " = '" + groupId + "' order by " + DBHelper.PHOTO_INFO_COLUMNS.CAPTURE_TIME + " desc", null);
                while (c.moveToNext()) {
                    photoItems.add(cursor2GroupPhotoItem(c));
                }
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice getGroupPhotoListByGroupId error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeCursorAndDB(c, db);
            }
        }
        LogHelper.d(TAG, "dbservice getGroupPhotoListByGroupId  and result set size :" + photoItems.size());
        return photoItems;
    }

    public ArrayList<GroupInfo> getRecentGroupList(int limit) {
        LogHelper.d(TAG, "dbservice getRecentGroupList by limit :" + limit);

        ArrayList<GroupInfo> groupList = new ArrayList<GroupInfo>();
        SQLiteDatabase db = null;
        Cursor c = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getReadableDatabase();
                db.beginTransaction();
                c = db.rawQuery("select * from " + DBHelper.GROUP_INFO_TABLE + "  order by "
                        + DBHelper.GROUP_INFO_COLUMNS.LAST_ENTER_TIME + " desc limit  " + limit, null);
                while (c.moveToNext()) {
                    groupList.add(cursor2GroupInfo(c));
                }
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice getRecentGroupList error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeCursorAndDB(c, db);
            }
        }
        LogHelper.d(TAG, "dbservice getRecentGroupList  and result set size :" + groupList.size());
        return groupList;
    }

    public void addGroupPhotoList(ArrayList<GroupPhotoItem> photoItems) {
        LogHelper.d(TAG, "dbservice addGroupPhotoList ");
        if (photoItems == null || photoItems.size() == 0) {
            return;
        }
        SQLiteDatabase db = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getWritableDatabase();
                db.beginTransaction();
                for (GroupPhotoItem item : photoItems) {
                    db.insertWithOnConflict(DBHelper.PHOTO_INFO_TABLE, null, groupPhotoItem2ContentValues(item),
                            SQLiteDatabase.CONFLICT_REPLACE);
                }
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice addGroupPhotoList error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeDB(db);
            }
        }
    }

    public void clearRecentPhotos() {
        SQLiteDatabase db = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getWritableDatabase();
                db.beginTransaction();
                db.execSQL("DELETE FROM " + DBHelper.RECENT_PHOTO_TABLE);
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice clearRecentPhotos error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeDB(db);
            }
        }
    }

    public void addRecentPhotos(ArrayList<GroupPhotoItem> photoItems) {
        LogHelper.d(TAG, "dbservice addRecentPhotos ");
        if (photoItems == null || photoItems.size() == 0) {
            return;
        }
        SQLiteDatabase db = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getWritableDatabase();
                db.beginTransaction();
                for (GroupPhotoItem item : photoItems) {
                    db.insertWithOnConflict(DBHelper.RECENT_PHOTO_TABLE, null, groupPhotoItem2ContentValues(item),
                            SQLiteDatabase.CONFLICT_REPLACE);
                }
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice addRecentPhotos error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeDB(db);
            }
        }
    }

    public ArrayList<GroupPhotoItem> getRecentPhotos() {
        LogHelper.d(TAG, "dbservice getRecentPhotos :");
        ArrayList<GroupPhotoItem> photoItems = new ArrayList<GroupPhotoItem>();
        SQLiteDatabase db = null;
        Cursor c = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getReadableDatabase();
                db.beginTransaction();
                c = db.rawQuery("select * from " + DBHelper.RECENT_PHOTO_TABLE, null);
                while (c.moveToNext()) {
                    photoItems.add(cursor2GroupPhotoItem(c));
                }
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice getRecentPhotos error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeCursorAndDB(c, db);
            }
        }
        LogHelper.d(TAG, "dbservice getRecentPhotos  and result set size :" + photoItems.size());
        return photoItems;
    }


    public boolean isGroupExist(String groupId) {

        boolean isExist = false;
        SQLiteDatabase db = null;
        Cursor c = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getReadableDatabase();
                db.beginTransaction();
                c = db.rawQuery("select * from " + DBHelper.GROUP_INFO_TABLE + " where "
                        + DBHelper.GROUP_INFO_COLUMNS.GROUP_ID + " = '" + groupId + "'", null);
                if (c.moveToNext()) {
                    isExist = true;
                }
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice getRecentGroupList error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeCursorAndDB(c, db);
            }
        }
        return isExist;
    }

    public GroupInfo getGroupInfoByGroupId(String groupId) {
        SQLiteDatabase db = null;
        Cursor c = null;
        GroupInfo info = null;

        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getReadableDatabase();
                db.beginTransaction();
                c = db.rawQuery("select * from " + DBHelper.GROUP_INFO_TABLE + " where "
                        + DBHelper.GROUP_INFO_COLUMNS.GROUP_ID + " = '" + groupId + "'", null);
                if (c.moveToNext()) {
                    info = cursor2GroupInfo(c);
                }
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice getGroupInfoByGroupId error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeCursorAndDB(c, db);
            }
        }
        return info;
    }

    public void addNewGroup(GroupInfo groupInfo) {
        if (groupInfo == null) {
            return;
        }
        SQLiteDatabase db = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getWritableDatabase();
                db.beginTransaction();
                db.insertWithOnConflict(DBHelper.GROUP_INFO_TABLE, null, groupInfo2ContentValues(groupInfo),
                        SQLiteDatabase.CONFLICT_REPLACE);
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice addNewGroup error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeDB(db);
            }
        }
    }

    public void deleteGroupByGroupId(String groupId) {
        if (TextUtils.isEmpty(groupId)) {
            return;
        }
        SQLiteDatabase db = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getReadableDatabase();
                db.beginTransaction();
                db.execSQL("delete  from " + DBHelper.GROUP_INFO_TABLE + " where "
                        + DBHelper.GROUP_INFO_COLUMNS.GROUP_ID + " = '" + groupId + "'");
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice deleteGroupByGroupId error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeCursorAndDB(null, db);
            }
        }
    }

    public void clearHistoryList() {
        SQLiteDatabase db = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getWritableDatabase();
                db.beginTransaction();
                db.execSQL("DELETE FROM " + DBHelper.GROUP_LIST_TABLE);
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice clearHistoryList error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeDB(db);
            }
        }
    }

    public void addHistoryList(ArrayList<GroupInfo> groupList) {
        LogHelper.d(TAG, "dbservice addHistoryList ");
        if (groupList == null || groupList.size() == 0) {
            return;
        }
        SQLiteDatabase db = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getWritableDatabase();
                db.beginTransaction();
                for (GroupInfo item : groupList) {
                    db.insertWithOnConflict(DBHelper.GROUP_LIST_TABLE, null, groupInfo2ContentValues(item),
                            SQLiteDatabase.CONFLICT_REPLACE);
                }
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice addHistoryList error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeDB(db);
            }
        }
    }

    public ArrayList<GroupInfo> getHistoryGroupList() {
        LogHelper.d(TAG, "dbservice getHistoryGroupList :");
        ArrayList<GroupInfo> groupList = new ArrayList<GroupInfo>();
        SQLiteDatabase db = null;
        Cursor c = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getReadableDatabase();
                db.beginTransaction();
                c = db.rawQuery("select * from " + DBHelper.GROUP_LIST_TABLE, null);
                while (c.moveToNext()) {
                    groupList.add(cursor2GroupInfo(c));
                }
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice getHistoryGroupList error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeCursorAndDB(c, db);
            }
        }
        LogHelper.d(TAG, "dbservice getHistoryGroupList  and result set size :" + groupList.size());
        return groupList;
    }

    public void addUploadPhoto(UploadPhotoBean item){
        LogHelper.d(TAG, "dbservice addUploadPhoto "+item.photoPath);
        SQLiteDatabase db = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getWritableDatabase();
                db.beginTransaction();
                db.insertWithOnConflict(DBHelper.UPLOAD_PHOTO_TABLE, null, uploadPhotoBean2ContentValue(item),
                        SQLiteDatabase.CONFLICT_REPLACE);
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice addUploadPhoto error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeDB(db);
            }
        }
    }

    public void deleteUploadPhoto(String photoPath){
        LogHelper.d(TAG, "dbservice deleteUploadPhoto "+photoPath);
        SQLiteDatabase db = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getReadableDatabase();
                db.beginTransaction();
                db.execSQL("delete  from " + DBHelper.UPLOAD_PHOTO_TABLE + " where "
                        + DBHelper.UPLOAD_PHOTO_COLUMNS.PHOTO_PATH + " = '" + photoPath + "'");
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice deleteUploadPhoto error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeCursorAndDB(null, db);
            }
        }
    }

    public UploadPhotoBean getNextUploadPhotoBean(){
        LogHelper.d(TAG, "dbservice getNextUploadPhotoBean ");
        UploadPhotoBean photoBean = null;
        SQLiteDatabase db = null;
        Cursor c = null;
        synchronized (DB_LOCK) {
            try {
                db = dbHelper.getReadableDatabase();
                db.beginTransaction();
                c = db.rawQuery("select * from " + DBHelper.UPLOAD_PHOTO_TABLE + "  order by "
                        + DBHelper.UPLOAD_PHOTO_COLUMNS.CREATE_TIME + " desc limit 1 ", null);
                if (c.moveToNext()) {
                    photoBean = cursor2UploadPhotoBean(c);
                }
                db.setTransactionSuccessful();
            } catch (Exception e) {
                LogHelper.d(TAG, "dbservice getNextUploadPhotoBean error happen " + e);
            } finally {
                try {
                    db.endTransaction();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                closeCursorAndDB(c, db);
            }
        }
        return photoBean;
    }

    public void clearDB(){
        SQLiteDatabase db = null;
        try{
            db = dbHelper.getReadableDatabase();
            db.beginTransaction();
            db.execSQL("delete from "+ DBHelper.UPLOAD_PHOTO_TABLE);
            db.execSQL("delete from "+ DBHelper.GROUP_INFO_TABLE);
            db.execSQL("delete from "+ DBHelper.GROUP_LIST_TABLE);
            db.execSQL("delete from "+ DBHelper.RECENT_PHOTO_TABLE);
            db.execSQL("delete from "+ DBHelper.PHOTO_INFO_TABLE);
        }catch (Exception e){
            LogHelper.e(TAG,"clear db ,error occurs "+e);
        }finally {
            try {
                db.endTransaction();
            } catch (Exception e) {
                e.printStackTrace();
            }
            closeDB(db);
        }
    }

    private ContentValues uploadPhotoBean2ContentValue(UploadPhotoBean bean){
        ContentValues cv = new ContentValues();
        cv.put(DBHelper.UPLOAD_PHOTO_COLUMNS.GROUP_ID,bean.groupId);
        cv.put(DBHelper.UPLOAD_PHOTO_COLUMNS.NICK_NAME,bean.nickName);
        cv.put(DBHelper.UPLOAD_PHOTO_COLUMNS.PHOTO_PATH,bean.photoPath);
        cv.put(DBHelper.UPLOAD_PHOTO_COLUMNS.USER_ID,bean.userId);
        cv.put(DBHelper.UPLOAD_PHOTO_COLUMNS.CREATE_TIME,bean.createTime);
        return cv;
    }

    private UploadPhotoBean cursor2UploadPhotoBean(Cursor c){
        UploadPhotoBean bean = new UploadPhotoBean();
        bean.groupId = c.getString(c.getColumnIndex(DBHelper.UPLOAD_PHOTO_COLUMNS.GROUP_ID));
        bean.nickName = c.getString(c.getColumnIndex(DBHelper.UPLOAD_PHOTO_COLUMNS.NICK_NAME));
        bean.photoPath = c.getString(c.getColumnIndex(DBHelper.UPLOAD_PHOTO_COLUMNS.PHOTO_PATH));
        bean.userId = c.getString(c.getColumnIndex(DBHelper.UPLOAD_PHOTO_COLUMNS.USER_ID));
        bean.createTime = c.getLong(c.getColumnIndex(DBHelper.UPLOAD_PHOTO_COLUMNS.CREATE_TIME));
        return bean;
    }

    private GroupPhotoItem cursor2GroupPhotoItem(Cursor c) {
        GroupPhotoItem item = new GroupPhotoItem();
        item.thumbnailUrl = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.THUMBNAIL_URL));
        item.bigThumbnailUrl = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.BIG_THUMBNAIL_URL));
        item.mediumPhotoUrl = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.MEDIUM_PHOTO_URL));
        item.rawPhotoUrl = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.RAW_PHOTO_URL));
        item.shareUrl = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.SHARE_PHOTO_URL));
        item.sessionId = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.SESSION_ID));
        item.photoId = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.PHOTO_ID));
        item.photoUrl = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.PHOTO_URL));
        item.groupId = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.PHOTO_GROUP_ID));
        item.captureTime = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.CAPTURE_TIME));
        item.originExpiredStamps = c.getLong(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.EXPIRE_TIME));
        item.photoName = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.PHOTO_NAME));
        item.uploaderId = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.PHOTO_UPLOADER_ID));
        item.thumbnailHeight = c.getShort(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.THUMBNAIL_HEIGHT));
        item.thumbnailWidth = c.getShort(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.THUMBNAIL_WIDTH));
        item.commentNum = c.getInt(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.COMMENT_NUM));
        item.thumbupNum = c.getInt(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.THUMBUP_NUM));
        item.groupAvatar = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.GROUP_AVATAR));
        item.uploaderAvatar = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.UPLOADER_AVATAR));
        item.uploaderName = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.UPLOADER_NAME));
        item.groupName = c.getString(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.GROUP_NAME));
        item.groupDefaultAvatar = c.getInt(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.GROUP_DEFAULT_AVATAR));
        item.photoFrom = c.getInt(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.PHOTO_FROM));
        item.groupFrom = c.getInt(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.GROUP_FROM));
        item.thumbUp = c.getInt(c.getColumnIndex(DBHelper.PHOTO_INFO_COLUMNS.THUMB_UP)) > 0;
        return item;
    }

    private GroupInfo cursor2GroupInfo(Cursor c) {
        GroupInfo item = new GroupInfo();
        item.createTime = c.getString(c.getColumnIndex(DBHelper.GROUP_INFO_COLUMNS.CREATE_TIME));
        item.desc = c.getString(c.getColumnIndex(DBHelper.GROUP_INFO_COLUMNS.GROUP_DESC));
        item.groupAvatarId = c.getInt(c.getColumnIndex(DBHelper.GROUP_INFO_COLUMNS.GROUP_AVATAR_ID));
        item.groupFrom = c.getInt(c.getColumnIndex(DBHelper.GROUP_INFO_COLUMNS.GROUP_FROM));
        item.groupId = c.getString(c.getColumnIndex(DBHelper.GROUP_INFO_COLUMNS.GROUP_ID));
        item.groupLock = c.getInt(c.getColumnIndex(DBHelper.GROUP_INFO_COLUMNS.GROUP_LOCK)) > 0;
        item.groupName = c.getString(c.getColumnIndex(DBHelper.GROUP_INFO_COLUMNS.GROUP_NAME));
        item.GroupAvatarUrl = c.getString(c.getColumnIndex(DBHelper.GROUP_INFO_COLUMNS.GROUP_AVATAR_URL));
        item.currentId = c.getInt(c.getColumnIndex(DBHelper.GROUP_INFO_COLUMNS.CURRENT_PHOTO_ID));
        item.lastEnterTime = c.getLong(c.getColumnIndex(DBHelper.GROUP_INFO_COLUMNS.LAST_ENTER_TIME));
        return item;
    }

    private ContentValues groupPhotoItem2ContentValues(GroupPhotoItem item) {
        ContentValues cv = new ContentValues();
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.CAPTURE_TIME, item.captureTime);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.EXPIRE_TIME, item.originExpiredStamps);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.PHOTO_GROUP_ID, item.groupId);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.PHOTO_ID, item.photoId);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.PHOTO_NAME, item.photoName);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.PHOTO_UPLOADER_ID, item.uploaderId);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.THUMBNAIL_URL, item.thumbnailUrl);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.BIG_THUMBNAIL_URL,item.bigThumbnailUrl);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.MEDIUM_PHOTO_URL,item.mediumPhotoUrl);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.RAW_PHOTO_URL,item.rawPhotoUrl);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.SHARE_PHOTO_URL,item.shareUrl);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.SESSION_ID,item.sessionId);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.THUMBNAIL_HEIGHT, item.thumbnailHeight);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.THUMBNAIL_WIDTH, item.thumbnailWidth);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.PHOTO_URL, item.photoUrl);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.COMMENT_NUM, item.commentNum);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.THUMBUP_NUM, item.thumbupNum);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.UPLOADER_AVATAR, item.uploaderAvatar);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.UPLOADER_NAME, item.uploaderName);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.GROUP_AVATAR, item.groupAvatar);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.GROUP_NAME, item.groupName);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.GROUP_DEFAULT_AVATAR, item.groupDefaultAvatar);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.PHOTO_FROM, item.photoFrom);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.GROUP_FROM, item.groupFrom);
        cv.put(DBHelper.PHOTO_INFO_COLUMNS.THUMB_UP, item.thumbUp ? 1 : 0);
        return cv;
    }

    private ContentValues groupInfo2ContentValues(GroupInfo item) {
        ContentValues cv = new ContentValues();
        cv.put(DBHelper.GROUP_INFO_COLUMNS.CREATE_TIME, item.createTime);
        cv.put(DBHelper.GROUP_INFO_COLUMNS.GROUP_AVATAR_ID, item.groupAvatarId);
        cv.put(DBHelper.GROUP_INFO_COLUMNS.GROUP_AVATAR_URL, item.GroupAvatarUrl);
        cv.put(DBHelper.GROUP_INFO_COLUMNS.GROUP_DESC, item.desc);
        cv.put(DBHelper.GROUP_INFO_COLUMNS.GROUP_FROM, item.groupFrom);
        cv.put(DBHelper.GROUP_INFO_COLUMNS.GROUP_LOCK, item.groupLock);
        cv.put(DBHelper.GROUP_INFO_COLUMNS.GROUP_NAME, item.groupName);
        cv.put(DBHelper.GROUP_INFO_COLUMNS.GROUP_ID, item.groupId);
        cv.put(DBHelper.GROUP_INFO_COLUMNS.GROUP_PHOTO_COUNT, item.photoCount);
        cv.put(DBHelper.GROUP_INFO_COLUMNS.CURRENT_PHOTO_ID, item.currentId);
        cv.put(DBHelper.GROUP_INFO_COLUMNS.LAST_ENTER_TIME, item.lastEnterTime);
        return cv;
    }

}
