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

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: 'hasFile',
            type: 'Boolean',
            description: 'True if has an attached file'
        },
        {
            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, options) => {
        const models = Self.app.models;
        const args = ctx.args;

        let tx;
        const 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, args.dmsTypeId);
            if (!hasWriteRole)
                throw new UserError(`You don't have enough privileges`);

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

            if (args.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 models = Self.app.models;
        const TempContainer = models.TempContainer;
        const DmsContainer = models.DmsContainer;
        const fileOptions = {};
        const tempContainer = await TempContainer.container('dms');
        const makeUpload = await TempContainer.upload(tempContainer.name, ctx.req, ctx.result, fileOptions);
        const keys = Object.values(makeUpload.files);
        const files = keys.map(file => file[0]);
        const uploadedFile = files[0];

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

            try {
                if (oldExtension != newExtension) {
                    const pathHash = DmsContainer.getHash(dms.id);

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

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

            const file = await TempContainer.getFile(tempContainer.name, uploadedFile.name);
            const srcFile = path.join(file.client.root, file.container, file.name);

            const pathHash = DmsContainer.getHash(updatedDms.id);
            const dmsContainer = await DmsContainer.container(pathHash);
            const dstFile = path.join(dmsContainer.client.root, pathHash, updatedDms.file);

            await fs.move(srcFile, dstFile, {
                overwrite: true
            });

            return updatedDms;
        }
    }
};