package com.kfzs.duanduan.datashare;

import android.content.Context;
import android.text.TextUtils;

import com.kfzs.duanduan.data.graph.provider.channel.DaoMaster;
import com.kfzs.duanduan.data.graph.provider.channel.DaoSession;
import com.kfzs.duanduan.datashare.provider.appdownload.AppdownloadBean;
import com.kfzs.duanduan.datashare.provider.appdownload.AppdownloadBeanDao;

/**
 * Created by Administrator on 2017/12/25.
 */

public class AppDownloadDatabaseHelper {
    private static AppDownloadDatabaseHelper addHlper;

    public static synchronized AppDownloadDatabaseHelper getInstance() {
        if (addHlper == null) {
            addHlper = new AppDownloadDatabaseHelper();
        }
        return addHlper;
    }

    private AppDownloadDatabaseHelper() {
    }


    /**
     * 创建app 下载表
     * @param context
     * @return
     */
    private DaoSession getDaoSession(Context context) {
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(context, "appdownload.db");
        return new DaoMaster(openHelper.getWritableDb()).newSession();
    }

    /**
     * 设置最后一次下载app 信息
     * @param context
     * @param appdownloadBean
     * @return
     */
    public boolean sestLastApp(Context context, AppdownloadBean appdownloadBean){
        try{
            DaoSession daoSession = getDaoSession(context);
            AppdownloadBeanDao appDao = daoSession.getAppdownloadBeanDao();
            appDao.insert(appdownloadBean);

            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }

    public boolean getLastApp(Context context){

        return false;
    }

    /**
     * 保存下载游戏
     * @param context
     * @param appdownloadBean
     * @return true:可以保存(游戏未下载或下载成功并安装成功了)
     */
    public boolean setApps(Context context, AppdownloadBean appdownloadBean){
        DaoSession daoSession = getDaoSession(context);
        AppdownloadBeanDao appdownloadBeanDao = daoSession.getAppdownloadBeanDao();
        AppdownloadBean oldAppBean = null;
        if(!TextUtils.isEmpty(appdownloadBean.getPkgName())){
            oldAppBean = appdownloadBeanDao.queryBuilder().where(AppdownloadBeanDao.Properties.PkgName.eq(appdownloadBean.getPkgName())).build().unique();
        }else {
            return false;
        }
        if(oldAppBean == null){
            appdownloadBeanDao.insert(appdownloadBean);
            return true;
        }else {
            appdownloadBean.setMId(oldAppBean.getMId());
            appdownloadBeanDao.update(appdownloadBean);
            if(oldAppBean.getStaus() > 0){//下载成功或者安装成功后
                return true;
            }
        }
        return false;
    }

    /**
     * 获取安装app信息
     * @param context
     * @param packageName
     * @return
     */
    public AppdownloadBean getApps(Context context, String packageName){
        AppdownloadBeanDao appdownloadBeanDao = getDaoSession(context).getAppdownloadBeanDao();
        AppdownloadBean appdownloadBean = appdownloadBeanDao.queryBuilder().where(AppdownloadBeanDao.Properties.PkgName.eq(packageName+"")).build().unique();
        return appdownloadBean;
    }

    /**
     * 删除保存的app数据
     * @param context
     * @param packageName
     * @return
     */
    public boolean deleteApps(Context context, String packageName){
        AppdownloadBeanDao appdownloadBeanDao = getDaoSession(context).getAppdownloadBeanDao();
        AppdownloadBean appdownloadBean = appdownloadBeanDao.queryBuilder().where(AppdownloadBeanDao.Properties.PkgName.eq(packageName+"")).build().unique();
        if(appdownloadBean != null)
            appdownloadBeanDao.delete(appdownloadBean);
        return false;
    }
}
