const UserError = require('vn-loopback/util/user-error');
const fs = require('fs-extra');

module.exports = Self => {
    Self.remoteMethodCtx('updateFile', {
        description: 'updates a file properties or file',
        accessType: 'WRITE',
        accepts: [{
            arg: 'id',
            type: 'Number',
            description: 'The document id',
            http: {source: 'path'}
        },
        {
            arg: 'warehouseId',
            type: 'Number',
            description: 'The warehouse id'
        }, {
            arg: 'companyId',
            type: 'Number',
            description: 'The company id'
        }, {
            arg: 'dmsTypeId',
            type: 'Number',
            description: 'The dms type id'
        }, {
            arg: 'reference',
            type: 'String'
        }, {
            arg: 'description',
            type: 'String'
        }, {
            arg: 'hasFileAttached',
            type: 'Boolean',
            description: 'True if has an attached file'
        }],
        returns: {
            type: 'Object',
            root: true
        },
        http: {
            path: `/:id/updateFile`,
            verb: 'POST'
        }
    });

    Self.updateFile = async(ctx, id, warehouseId, companyId,
        dmsTypeId, reference, description, hasFileAttached, options) => {
        const models = Self.app.models;

        let tx;
        let myOptions = {};

        if (typeof options == 'object')
            Object.assign(myOptions, options);

        if (!myOptions.transaction) {
            tx = await Self.beginTransaction({});
            myOptions.transaction = tx;
        }

        try {
            const hasWriteRole = await models.DmsType.hasWriteRole(ctx, dmsTypeId);
            if (!hasWriteRole)
                throw new UserError(`You don't have enough privileges`);

            const dms = await Self.findById(id, null, myOptions);
            await dms.updateAttributes({
                dmsTypeFk: dmsTypeId,
                companyFk: companyId,
                warehouseFk: warehouseId,
                reference: reference,
                description: description
            }, myOptions);

            if (hasFileAttached)
                await uploadNewFile(ctx, dms, myOptions);

            if (tx) await tx.commit();
            return dms;
        } catch (e) {
            if (tx) await tx.rollback();
            throw e;
        }
    };

    async function uploadNewFile(ctx, dms, myOptions) {
        const storageConnector = Self.app.dataSources.storage.connector;
        const models = Self.app.models;
        const fileOptions = {};

        const tempContainer = await getContainer('temp');
        const makeUpload = await models.Container.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
        const keys = Object.values(makeUpload.files);
        const files = keys.map(file => file[0]);
        const file = files[0];

        if (file) {
            const oldExtension = storageConnector.getFileExtension(dms.file);
            const newExtension = storageConnector.getFileExtension(file.name);
            const fileName = `${dms.id}.${newExtension}`;

            try {
                if (oldExtension != newExtension) {
                    const pathHash = storageConnector.getPathHash(dms.id);

                    await models.Container.removeFile(pathHash, dms.file);
                }
            } catch (err) {}

            const updatedDms = await dms.updateAttributes({
                contentType: file.type,
                file: fileName
            }, myOptions);

            const pathHash = storageConnector.getPathHash(updatedDms.id);
            const container = await getContainer(pathHash);

            const originPath = `${tempContainer.client.root}/${tempContainer.name}/${file.name}`;
            const destinationPath = `${container.client.root}/${pathHash}/${updatedDms.file}`;

            fs.rename(originPath, destinationPath);

            return updatedDms;
        }
    }

    /**
     * Returns a container instance
     * If doesn't exists creates a new one
     *
     * @param {String} name Container name
     * @return {Object} Container instance
     */
    async function getContainer(name) {
        const models = Self.app.models;
        let container;
        try {
            container = await models.Container.getContainer(name);
        } catch (err) {
            if (err.code === 'ENOENT') {
                container = await models.Container.createContainer({
                    name: name
                });
            } else throw err;
        }

        return container;
    }
};