package com.gh.gamecenter.db;

import android.content.Context;

import com.gh.gamecenter.db.info.SuspectedGameInfo;
import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.List;

public class SuspectedGameDao {
	
	private DatabaseHelper helper;
	private Dao<SuspectedGameInfo, String> dao;

	public SuspectedGameDao(Context context) {
		try {
			helper = DatabaseHelper.getHelper(context);
			dao = helper.getDao(SuspectedGameInfo.class);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	/**
	 * 添加一个疑似游戏
	 */
	public void add(SuspectedGameInfo entity) {
		try {
			dao.create(entity);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	/**
	 * 删除一个疑似游戏
	 */
	public void delete(String packageName) {
		try {
			dao.deleteById(packageName);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	/**
	 * 根据包名获取某一个疑似游戏
	 */
	public SuspectedGameInfo find(String packageName) {
		try {
			return dao.queryForId(packageName);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取所有的疑似游戏
	 */
	public List<SuspectedGameInfo> getAll() {
		try {
			return dao.queryForAll();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 更新疑似游戏
	 */
	public void update(SuspectedGameInfo entity) {
		try {
			dao.update(entity);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	public void deleteAll() {
		try {
			List<SuspectedGameInfo> list = dao.queryForAll();
			for (int i = 0, size = list.size(); i < size; i++) {
				dao.deleteById(list.get(i).getPackageName());
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}
}
