const path = require('path');
const smtp = require('./smtp');
const Component = require('./component');
const Report = require('./report');

if (!process.env.OPENSSL_CONF)
    process.env.OPENSSL_CONF = '/etc/ssl/';

class Email extends Component {
    constructor(name, args) {
        super(name);

        this.args = args;
    }

    get path() {
        return `../templates/email/${this.name}`;
    }

    async getSubject() {
        return (await this.getUserLocale())['subject'];
    }

    /**
     * @param {Object} [options] - Additional options
     * @param {Boolean} [options.overrideAttachments] - Overrides default PDF attachments
     * @param {Array} [options.attachments] - Array containing attachment objects
     * @return {Promise} SMTP Promise
     */
    async send(options = {}) {
        const instance = this.build();
        const rendered = await this.render();
        const attachments = [];
        const getAttachments = async(componentPath, files) => {
            for (const file of files) {
                const fileCopy = Object.assign({}, file);
                const fileName = fileCopy.filename;

                if (options.overrideAttachments && !fileName.includes('.png')) continue;

                if (fileCopy.cid) {
                    const templatePath = `${componentPath}/${file.path}`;
                    const fullFilePath = path.resolve(__dirname, templatePath);

                    fileCopy.path = path.resolve(__dirname, fullFilePath);
                } else {
                    const reportName = fileName.replace('.pdf', '');
                    const report = new Report(reportName, this.args);
                    fileCopy.content = await report.toPdfStream();
                    fileCopy.filename = await report.getFileName();
                }

                attachments.push(fileCopy);
            }
        };

        async function getSubcomponentAttachments(instance) {
            if (instance.components) {
                const components = instance.components;
                for (let componentName in components) {
                    const component = components[componentName];
                    const componentPath = `./components/${componentName}`;
                    await getAttachments(componentPath, component.attachments);

                    if (component.components)
                        await getSubcomponentAttachments(component)
                }
            }
        }

        await getSubcomponentAttachments(instance)

        if (this.attachments)
            await getAttachments(this.path, this.attachments);

        if (options.attachments) {
            for (let attachment of options.attachments)
                attachments.push(attachment);
        }

        const localeSubject = await this.getSubject();
        const mailOptions = {
            to: this.args.recipient,
            replyTo: this.args.replyTo || '',
            subject: localeSubject,
            html: rendered,
            attachments: attachments
        };

        return smtp.send(mailOptions);
    }
}

module.exports = Email;