import { athenaConfigOptions } from '../athena-config.js';
import promiseDatabase from './indexed-db.js';

/**
 * Perform Query Class
 */
class PerformQuery {
    //constructor
    constructor() {}

    /**
     * Perform Get All Query
     */
    async getAll() {
        try {
            const database = await promiseDatabase;
            const transaction = database.transaction([athenaConfigOptions.indexedObjectStore], 'readonly');
            const objectStore = transaction.objectStore(athenaConfigOptions.indexedObjectStore);

            return new Promise((resolve, reject) => {
                const getRequest = objectStore.getAll();

                getRequest.onsuccess = event => resolve(event.target.result);
                getRequest.onerror = event => reject(event.target.error);
            });
        } catch (error) {
            throw error;
        }
    }

    /**
     * Perform GET Query
     *
     * @param id
     */
    async get(id) {
        if (!id) {
            throw new Error('To perform get database request you have to provide id');
        }

        try {
            const database = await promiseDatabase;
            const transaction = database.transaction([athenaConfigOptions.indexedObjectStore], 'readonly');
            const objectStore = transaction.objectStore(athenaConfigOptions.indexedObjectStore);

            return new Promise((resolve, reject) => {
                const getRequest = objectStore.get(id);

                getRequest.onsuccess = event => resolve(event.target.result);
                getRequest.onerror = event => reject(event.target.error);
            });
        } catch (error) {
            throw error;
        }
    }

    /**
     * Perform Store Query
     *
     * @param data
     */
    store(data) {
        if(!data) {
            throw new Error('To perform store database request you have to provide data object');
        }

        promiseDatabase
            .then(database => {
                const transaction = database.transaction([athenaConfigOptions.indexedObjectStore], 'readwrite');
                const objectStore = transaction.objectStore(athenaConfigOptions.indexedObjectStore);

                const request = objectStore.add(data);
                request.onsuccess = () => console.log('Data stored successfully.');
            })
            .catch(error => {
                console.error(error);
            });
    }

    /**
     * Perform Update Query
     *
     * @param data
     */
    update(data) {
        if(!data) {
            throw new Error('To perform update database request you have to provide data object');
        }

        promiseDatabase
            .then(database => {
                const transaction = database.transaction([athenaConfigOptions.indexedObjectStore], 'readwrite');
                const objectStore = transaction.objectStore(athenaConfigOptions.indexedObjectStore);

                const putRequest = objectStore.put(data);
                putRequest.onsuccess = () => console.log('Data updated successfully.');
            })
            .catch(error => {
                console.error(error);
            });
    }

    /**
     * Perform Delete Query
     *
     * @param id
     */
    delete(id) {
        if(!id) {
            throw new Error('To perform delete database request you have to provide id');
        }

        promiseDatabase
            .then(database => {
                const transaction = database.transaction([athenaConfigOptions.indexedObjectStore], 'readwrite');
                const objectStore = transaction.objectStore(athenaConfigOptions.indexedObjectStore);

                const deleteRequest = objectStore.delete(id);
                deleteRequest.onsuccess = () =>  console.log('Data deleted successfully.');
            })
            .catch(error => {
                console.error(error);
            });
    }

    deleteAllStoreData() {
        promiseDatabase
            .then(database => {
                const transaction = database.transaction([athenaConfigOptions.indexedObjectStore], 'readwrite');
                const objectStore = transaction.objectStore(athenaConfigOptions.indexedObjectStore);

                const deleteRequest = objectStore.clear();
                deleteRequest.onsuccess = () =>  console.log('All Store Data cleared successfully.');
            })
            .catch(error => {
                console.error(error);
            });
    }
}

export default new PerformQuery();