package com.gh.gamecenter.db;

import android.content.Context;

import com.gh.gamecenter.db.info.ConcernInfo;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.QueryBuilder;

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

public class ConcernDao {
	
	private DatabaseHelper helper;
	private Dao<ConcernInfo, String> dao;

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

	/**
	 * 获取权重和流量排序靠前的关注
	 */
	public List<ConcernInfo> get(int number) {
		List<ConcernInfo> concernEntities = new ArrayList<ConcernInfo>();
		try {
			QueryBuilder<ConcernInfo, String> builder = dao.queryBuilder();

			builder.orderBy("weight", false).orderBy("traffic", false);

			List<ConcernInfo> list = builder.query();

			if (number == 0 || number >= list.size()) {
				return list;
			} else {
				for (int i = 0; i < number; i++) {
					concernEntities.add(list.get(i));
				}
				return concernEntities;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取所有的已关注游戏的集合
	 */
	public List<ConcernInfo> getConcern(int number) {
		List<ConcernInfo> concernEntities = new ArrayList<ConcernInfo>();
		try {
			QueryBuilder<ConcernInfo, String> builder = dao.queryBuilder();
			builder.where().eq("isConcern", true);

			List<ConcernInfo> list = builder.query();

			if (number == 0 || number >= list.size()) {
				return list;
			} else {
				for (int i = 0; i < number; i++) {
					concernEntities.add(list.get(i));
				}
				return concernEntities;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取所有的已安装游戏的集合
	 */
	public List<ConcernInfo> getInstalled() {
		try {
			QueryBuilder<ConcernInfo, String> builder = dao.queryBuilder();
			builder.where().eq("isInstalled", true);
			return builder.query();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public boolean isConcern(String id){
		try {
			QueryBuilder<ConcernInfo, String> builder = dao.queryBuilder();
			builder.where().eq("id", id).eq("isConcern", true);
			ConcernInfo concernEntity = builder.queryForFirst();
			if (concernEntity != null) {
				return true;
			}
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 添加一个关注
	 */
	public void add(ConcernInfo entity) {
		try {
			dao.create(entity);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	/**
	 * 删除一个关注
	 */
	public void delete(String id) {
		try {
			dao.deleteById(id);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}

	/**
	 * 根据id获取某一个关注
	 */
	public ConcernInfo find(String id) {
		try {
			return dao.queryForId(id);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 获取所有的关注
	 */
	public List<ConcernInfo> getAll() {
		try {
			return dao.queryForAll();
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 更新关注
	 */
	public void update(ConcernInfo entity) {
		try {
			dao.update(entity);
		} catch (SQLException e) {
			
			e.printStackTrace();
		}
	}
}
