const AppsStore = require('./../stores/apps.js').AppsStore

/**
 *  handler for apps
 *
 *  @author Björn Hase
 *  @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3
 *  @link https://gitea.node001.net/HerrHase/potato-launcher.git
 *
 */
class AppsHandler {

    /**
     *  use create in appStore and send event with result to app
     *
     *  @param  {object} data
     *  @return {object}
     *
     */
    create(connector, data) {
        const appsStore = new AppsStore()

        appsStore.create(data)
            .then((data) => {
                connector.send('pouchdb.apps.success', data)
            })
    }

    /**
     *  use update in appStore and send event with result to app
     *
     *  @param  {object} data
     *  @return {object}
     *
     */
    update(connector, data) {
        const appsStore = new AppsStore()

        appsStore.update(data)
            .then((data) => {
                connector.send('pouchdb.apps.success', data)
            })
    }

    /**
     *  use update in appStore and send event with result to app
     *
     *  @param  {object} data
     *  @return {object}
     *
     */
    findOne(connector, data) {
        const appsStore = new AppsStore()

        appsStore.findOneById(data.id)
            .then((data) => {
                if (data) {
                    connector.send('pouchdb.apps.readyOne', data)
                }
            })
    }

    /**
     *  use find in appStore and send event with result to app
     *
     *  @param  {object} connector
     *  @param  {object} data
     *
     */
    find(connector, data) {
        const appsStore = new AppsStore()

        appsStore.find()
            .then((data) => {
                console.error('send result', data)
                connector.send('pouchdb.apps.ready', data)
            })
    }
}

module.exports.AppsHandler = AppsHandler