// const indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB;
const indexedDB = window.indexedDB;

class IndexedDB {
    private _db: IDBDatabase | null; // 数据库
    private _transaction: IDBTransaction | null; // 事务
    private _request: IDBOpenDBRequest | null;
    private _dbName: string; // 数据库名
    private _cacheTableName: string; // 表名
    private _dbversion: number; // 数据库版本

    constructor() {
        this._db = null;
        this._transaction = null;
        this._request = null;
        this._dbName = "userWritingDB";
        this._cacheTableName = "userWritingTable";
        this._dbversion = 1;
    }

    async initDB(val: string): Promise<Event> {
        return new Promise<Event>((resolve, reject) => {
            this._request = indexedDB.open(this._dbName, this._dbversion);
            // 数据库初始化成功
            this._request!.onsuccess = (event) => {
                this._db = this._request!.result;
                resolve(event);
            };
            // 数据库初始化失败
            this._request!.onerror = (event) => {
                reject(event);
            };
            // 数据库初次创建或更新时会触发
            this._request!.onupgradeneeded = (event) => {
                let db = this._request!.result;
                if (!db.objectStoreNames.contains(this._cacheTableName)) {
                    db.createObjectStore(this._cacheTableName, {
                        keyPath: val,
                    });
                }
                resolve(event);
            };
        });
    }

    closeDB() {
        if (this._db) {
            this._db.close();
            console.log(`关闭数据库`);
        }
    }

    async addData(params: any): Promise<Event> {
        return new Promise<Event>((resolve, reject) => {
            let transaction = this._db!.transaction(this._cacheTableName, "readwrite");
            let store = transaction.objectStore(this._cacheTableName);
            let response = store.add(params);
            // 操作成功
            response.onsuccess = (event) => {
                console.log("操作成功");
                resolve(event);
            };
            // 操作失败
            response.onerror = (event) => {
                console.log("操作失败");
                reject(event);
            };
        });
    }

    async getDataByKey(key: any): Promise<any> {
        return new Promise<any>((resolve, reject) => {
            let transaction = this._db!.transaction(this._cacheTableName);
            let objectStore = transaction.objectStore(this._cacheTableName);
            // 通过主键读取数据
            let request = objectStore.get(key);
            // 操作成功
            request.onsuccess = () => {
                resolve(request.result);
            };
            // 操作失败
            request.onerror = (event) => {
                reject(event);
            };
        });
    }

    async clearDB(): Promise<Event> {
        return new Promise<Event>((resolve, reject) => {
            let transaction = this._db && this._db.transaction(this._cacheTableName, "readwrite");
            let store = transaction && transaction.objectStore(this._cacheTableName);
            let response = store && store.clear();
            // 操作成功
            response.onsuccess = (event) => {
                console.log("清空数据库数据");
                resolve(event);
            };
            // 操作失败
            response.onerror = (event) => {
                reject(event);
            };
        });
    }

    async deleteDBAll(): Promise<void> {
        let deleteRequest = indexedDB.deleteDatabase(this._dbName);
        return new Promise<void>((resolve, reject) => {
            deleteRequest.onerror = function (event) {
                console.log("删除失败");
            };
            deleteRequest.onsuccess = function (event) {
                console.log("删除数据库成功");
            };
        });
    }
}

export default IndexedDB;