package com.gh.gamecenter.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.util.ArrayMap;

import com.gh.common.util.Utils;
import com.gh.gamecenter.db.info.ConcernInfo;
import com.gh.gamecenter.db.info.DataCollectionInfo;
import com.gh.gamecenter.db.info.FilterInfo;
import com.gh.gamecenter.db.info.GameInfo;
import com.gh.gamecenter.db.info.SearchHistoryInfo;
import com.gh.gamecenter.db.info.SuspectedGameInfo;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

	private static final String DATABASE_NAME = "gh_assist.db";
	private static final int DATABASE_VERSION = 2;

	private static DatabaseHelper instance;
	private ArrayMap<String, Dao> daos = new ArrayMap<String, Dao>();

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

	/**
	 * 单例获取该Helper
	 * 
	 * @param context
	 * @return
	 */
	public static synchronized DatabaseHelper getHelper(Context context) {
		context = context.getApplicationContext();
		if (instance == null) {
			synchronized (DatabaseHelper.class) {
				if (instance == null) {
					instance = new DatabaseHelper(context);
				}
			}
		}
		return instance;
	}

	@Override
	public void onCreate(SQLiteDatabase database,
			ConnectionSource connectionSource) {
		
		try {
			Utils.log("onCreate");
			TableUtils.createTable(connectionSource, ConcernInfo.class);
			TableUtils.createTable(connectionSource, SearchHistoryInfo.class);
			TableUtils.createTable(connectionSource, GameInfo.class);
			TableUtils.createTable(connectionSource, SuspectedGameInfo.class);
			TableUtils.createTable(connectionSource, FilterInfo.class);
			TableUtils.createTable(connectionSource, DataCollectionInfo.class);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	@Override
	public void onUpgrade(SQLiteDatabase database,
			ConnectionSource connectionSource, int oldVersion, int newVersion) {
		
		try {
			Utils.log("onUpgrade");
			TableUtils.dropTable(connectionSource, ConcernInfo.class, true);
			TableUtils.dropTable(connectionSource, SearchHistoryInfo.class, true);
			TableUtils.dropTable(connectionSource, GameInfo.class, true);
			TableUtils.dropTable(connectionSource, SuspectedGameInfo.class, true);
			TableUtils.dropTable(connectionSource, FilterInfo.class, true);
			TableUtils.dropTable(connectionSource, DataCollectionInfo.class, true);
			onCreate(database, connectionSource);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	public synchronized Dao getDao(Class clazz) throws SQLException {
		Dao dao = null;
		String className = clazz.getSimpleName();

		if (daos.containsKey(className)) {
			dao = daos.get(className);
		}
		if (dao == null) {
			dao = super.getDao(clazz);
			daos.put(className, dao);
		}
		return dao;
	}

	/**
	 * 释放资源
	 */
	@Override
	public void close() {
		super.close();
		for (String key : daos.keySet()) {
			Dao dao = daos.get(key);
			dao = null;
		}
	}
}
