package com.wasu.cs.module;

import android.util.Log;

import com.wasu.module.db.DBManage;

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

import basic.db.model.DBProgramFavorite;
import basic.db.model.DBProgramHistory;

public class FavAndHisModule {
	private final String TAG="stone";
	private static FavAndHisModule mInstance;
	private FavAndHisModule(){
		
	}
	public static FavAndHisModule getInstance() {
		if (mInstance == null) {
			synchronized (FavAndHisModule.class) {
				if (mInstance == null) {
					mInstance = new FavAndHisModule();
				}
			}
		}
		return mInstance;
	}
	/**
	 * 根据影片id读取数据库中的历史数据
	 * @param programId 影片id
	 * @return true:成功    false:失败
	 * **/
	public DBProgramHistory readHistory(String programId) {
		DBProgramHistory mHistory=null;
		try {
			mHistory = DBManage.queryBy(DBProgramHistory.class, "programId", programId);
		} catch (Exception e) {
			Log.e(TAG, "read history erro");
			e.printStackTrace();
		}
		return mHistory;
	}
	/**
	 * 读取数据库中的所有历史数据
	 * @return 异常返回null
	 * **/
	public List<DBProgramHistory> readHistory(){
		List<DBProgramHistory> mHistoryList=new ArrayList<DBProgramHistory>();
		try {
			mHistoryList = DBManage.queryAndOrder(DBProgramHistory.class, "savehistorytime", false);
		} catch (Exception e) {
			Log.e(TAG, "read history erro");
			e.printStackTrace();
			return null;
		}
		return mHistoryList;
	}

	/**
	 * 读取最新的一条历史记录
	 *  @return
	 */
	public DBProgramHistory readFirstHistory() {
		List<DBProgramHistory> list = readHistory();
		if (list == null || list.size() <= 0) {
			return null;
		} else return list.get(0);
	}

	/**
	 * 保存历史数据
	 * @param mHistory
	 * @return true：成功     false：失败
	 * **/
	public boolean saveHistory( DBProgramHistory mHistory){
		try {
			DBManage.deleteBy(DBProgramHistory.class, "programId", mHistory.programId);
			mHistory.insertOrUpdate();
			
			/** 记录超过100条就自动清除前面的 **/
			if (DBManage.countOf(DBProgramHistory.class) > 100){
			    List<DBProgramHistory> list = DBManage.queryAndOrder(DBProgramHistory.class, "id", true);
			    DBManage.deleteBy(DBProgramHistory.class, "id", list.get(0).id);
			}
		} catch (SQLException e) {
			Log.e(TAG, "save history erro");
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 根据影片id删除历史数据
	 * @param programId 影片id
	 * @return true：成功   false：失败
	 * **/
	public boolean deleteHistory(String programId){
		try {
			DBManage.deleteBy(DBProgramHistory.class, "programId", programId);
			return true;
		} catch (Exception e) {
			Log.e(TAG, "delete favorite erro");
			e.printStackTrace();
			return false;
		}
	}
	public boolean deleteAllHistory(){
		try {
			DBManage.clearTable(DBProgramHistory.class);
			return true;
		} catch (Exception e) {
			Log.e(TAG, "delete favorite erro");
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 根据影片id判断当前影片是否有历史记录
	 * @param programId 影片id
	 * @return true：有    false：没有
	 * **/
	public boolean hasHistory(String programId){
		return readHistory(programId)!=null;
	}
	
	
	/**
	 * 根据影片id读取数据库中的收藏的数据
	 * @param programId 影片id
	 * @return true:成功    false:失败
	 * **/
	public DBProgramFavorite readFavorite(String programId) {
		List<DBProgramFavorite> mFavoriteList=null;
		DBProgramFavorite mFavorite=null;
		try {
			mFavorite = DBManage.queryBy(DBProgramFavorite.class, "programId", programId);
		} catch (Exception e) {
			Log.e(TAG, "read favorite erro");
			e.printStackTrace();
		}
		return mFavorite;
	}
	/**
	 * 读取数据库中的所有收藏数据
	 * @return 异常返回null
	 * **/
	public List<DBProgramFavorite> readFavorite() {
		try {
			return DBManage.queryAndOrder(DBProgramFavorite.class, "id", false);
		} catch (Exception e) {
			Log.e(TAG, "read favoriet erro--------------------"+e.toString());
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 保存需要收藏的数据
	 * @param mFavorite
	 * @return true：成功     false：失败
	 * **/
	public boolean saveFavorite(DBProgramFavorite mFavorite){
		try {
			mFavorite.insertOrUpdate();
			return true;
		} catch (Exception e) {
			Log.e(TAG, "save favorite erro");
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 根据影片id删除收藏的数据
	 * @param programId 影片id
	 * @return true：成功   false：失败
	 * **/
	public boolean deleteFavorite(String programId){
		try {
			DBManage.deleteBy(DBProgramFavorite.class, "programId",programId);
			return true;
		} catch (Exception e) {
			Log.e(TAG, "delete favorite erro");
			e.printStackTrace();
			return false;
		}
	}
	public boolean deleteAllFavorite(){
		try {
			DBManage.clearTable(DBProgramFavorite.class);
			return true;
		} catch (Exception e) {
			Log.e(TAG, "delete favorite erro");
			e.printStackTrace();
			return false;
		}
	}
	/**
	 * 根据影片id判断当前影片是否收藏
	 * @param programId 影片id
	 * @return true：已收藏    false：未收藏
	 * **/
	public boolean hasFavorite(String programId){
		DBProgramFavorite exist = readFavorite(programId);
		if (exist == null) {
			return false;
		} else {
			return exist.programId != 0;
		}
	}
}
