package com.sxyz.rfid.greendao;

import android.content.Context;
import android.os.Build;
import android.os.Environment;

import com.sxyz.rfid.BuildConfig;

import org.greenrobot.greendao.query.QueryBuilder;

import java.io.File;

import sql.com.yz.db.DaoMaster;
import sql.com.yz.db.DaoSession;

/**
 * @Description: java类作用描述
 * @Author: chunyuan.wu
 * @CreateDate: 5/12/23 5:31 PM
 * @Version: 1.0
 */
public class GreenDaoManager {
    private static final String TAG = GreenDaoManager.class.getSimpleName();
    private static final String DB_NAME = "rfid.db";

    private Context context;

    //多线程中要被共享的使用volatile关键字修饰
    private volatile static GreenDaoManager manager = new GreenDaoManager();
    private static DaoMaster sDaoMaster;
    private static DaoMaster.DevOpenHelper sHelper;
    private static DaoSession sDaoSession;

    /**
     * 单例模式获得操作数据库对象
     *
     * @return
     */
    public static GreenDaoManager getInstance() {
        return manager;
    }

    private GreenDaoManager() {
        setDebug();
    }

    public void init(Context context) {
        this.context = context;
    }

    /**
     * 判断是否有存在数据库，如果没有则创建
     *
     * @return
     */
    public DaoMaster getDaoMaster() {
        if (sDaoMaster == null) {
            // 获取外部存储的数据库目录
            File dbDir = getExternalDatabaseDir(context);

            // 确保目录存在
            if (!dbDir.exists()) {
                dbDir.mkdirs();
            }
            // 数据库文件路径
            File dbFile = new File(dbDir, DB_NAME);
//            /storage/emulated/0/Android/data/com.sxyz.rfid/files/databases/rfid.db
            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, dbFile.getAbsolutePath(), null);
//            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, DB_NAME, null);
//            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "/mnt/sdcard/Android/data/com.sxyz.rfid/databases/" + DB_NAME, null);
//            DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, "/data/data/com.sxyz.rfid/databases/" + DB_NAME, null);
            sDaoMaster = new DaoMaster(helper.getWritableDatabase());

        }
        return sDaoMaster;
    }

    private static File getExternalDatabaseDir(Context context) {
        // Android 10 以下版本
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
            return new File(Environment.getExternalStorageDirectory(),
                    "Android/data/" + context.getPackageName() + "/databases");
        }
        // Android 10 及以上版本 - 使用应用专属外部存储
        return new File(context.getExternalFilesDir(null), "databases");
    }


    /**
     * 完成对数据库的添加、删除、修改、查询操作，仅仅是一个接口
     *
     * @return
     */
    public DaoSession getDaoSession() {
        if (sDaoSession == null) {
            if (sDaoMaster == null) {
                sDaoMaster = getDaoMaster();
            }
            sDaoSession = sDaoMaster.newSession();
        }
        return sDaoSession;
    }

    /**
     * 打开输出日志，默认关闭
     */
    public void setDebug() {
        if (BuildConfig.DEBUG) {
            QueryBuilder.LOG_SQL = true;
            QueryBuilder.LOG_VALUES = true;
        }
    }

    /**
     * 关闭所有的操作，数据库开启后，使用完毕要关闭
     */
    public void closeConnection() {
        closeHelper();
        closeDaoSession();
    }

    public void closeHelper() {
        if (sHelper != null) {
            sHelper.close();
            sHelper = null;
        }
    }

    public void closeDaoSession() {
        if (sDaoSession != null) {
            sDaoSession.clear();
            sDaoSession = null;
        }
    }
}
