import { LanguageCode } from '@vendure/common/lib/generated-types';
import { Channel, RequestContext } from '@vendure/core';
import { Request, Router } from 'express';
import fs from 'fs-extra';
import path from 'path';

import { EmailEventHandler } from './handler/event-handler';
import { EmailPluginDevModeOptions, EventWithContext } from './types';

/**
 * An email inbox application that serves the contents of the dev mode `outputPath` directory.
 */
export class DevMailbox {
    private handleMockEventFn: (
        handler: EmailEventHandler<string, any>,
        event: EventWithContext,
    ) => void | undefined;

    serve(options: EmailPluginDevModeOptions): Router {
        const { outputPath, handlers } = options;
        const server = Router();
        server.get('/', (req, res) => {
            res.sendFile(path.join(__dirname, '../../dev-mailbox.html'));
        });
        server.get('/list', async (req, res) => {
            const list = await fs.readdir(outputPath);
            const contents = await this.getEmailList(outputPath);
            res.send(contents);
        });
        server.get('/types', async (req, res) => {
            res.send(handlers.map(h => h.type));
        });
        server.get('/generate/:type/:languageCode', async (req, res) => {
            const { type, languageCode } = req.params;
            if (this.handleMockEventFn) {
                const handler = handlers.find(h => h.type === type);
                if (!handler || !handler.mockEvent) {
                    res.statusCode = 404;
                    res.send({ success: false, error: `No mock event registered for type "${type}"` });
                    return;
                }
                try {
                    this.handleMockEventFn(handler, {
                        ...handler.mockEvent,
                        ctx: this.createRequestContext(languageCode as LanguageCode, req),
                    } as EventWithContext);
                    res.send({ success: true });
                } catch (e: any) {
                    res.statusCode = 500;
                    res.send({ success: false, error: e.message });
                }
                return;
            } else {
                res.send({ success: false, error: 'Mock email generation not set up.' });
            }
        });
        server.get('/item/:id', async (req, res) => {
            const fileName = req.params.id;
            const content = await this.getEmail(outputPath, fileName);
            res.send(content);
        });
        server.get('/placeholder-image', async (req, res) => {
            const img = Buffer.from(
                // eslint-disable-next-line max-len
                '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQoHBwYIDAoMDAsKCwsNDhIQDQ4RDgsLEBYQERMUFRUVDA8XGBYUGBIUFRT/2wBDAQMEBAUEBQkFBQkUDQsNFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBT/wAARCADgAO4DASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD7VooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACszXNeg0SENJ88rfdjB5NafTrXlPiHUW1PVZpScqDtX2AoGX7jxxqM0m5GWFf7qj/ABqL/hMtU/57/wDjorDooGbn/CZap/z3/wDHRR/wmWqf89//AB0Vh0UAbn/CZap/z3/8dFWLPx1fwSfvgs6Z5BGD+GK5uigD1vSdWg1i1E0J/wB5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fqH8j/hXeUUBc4P/AIV5d/8AP1D+R/wo/wCFeXf/AD9Q/kf8K7yigLnB/wDCvLv/AJ+ofyP+FWLX4ekMDc3KkZ6Rg12lFAXKmm6VbaVD5dvGEB6nuat0UUCCiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKACiiigAooooAKKKKAP/9k=',
                'base64',
            );

            res.writeHead(200, {
                'Content-Type': 'image/png',
                'Content-Length': img.length,
            });
            res.end(img);
        });

        return server;
    }

    handleMockEvent(handler: (handler: EmailEventHandler<string, any>, event: EventWithContext) => void) {
        this.handleMockEventFn = handler;
    }

    private async getEmailList(outputPath: string) {
        const list = await fs.readdir(outputPath);
        const contents: Array<{
            fileName: string;
            date: string;
            subject: string;
            recipient: string;
        }> = [];
        for (const fileName of list.filter(name => name.endsWith('.json'))) {
            const json = await fs.readFile(path.join(outputPath, fileName), 'utf-8');
            const content = JSON.parse(json);
            contents.push({
                fileName,
                date: content.date,
                subject: content.subject,
                recipient: content.recipient,
            });
        }
        contents.sort((a, b) => {
            return a.fileName < b.fileName ? 1 : -1;
        });
        return contents;
    }

    private async getEmail(outputPath: string, fileName: string) {
        const safeSuffix = path.normalize(fileName).replace(/^(\.\.(\/|\\|$))+/, '');
        const safeFilePath = path.join(outputPath, safeSuffix);
        const json = await fs.readFile(safeFilePath, 'utf-8');
        const content = JSON.parse(json);
        return content;
    }

    private createRequestContext(languageCode: LanguageCode, req: Request): RequestContext {
        return new RequestContext({
            languageCode,
            req,
            apiType: 'admin',
            session: {} as any,
            isAuthorized: false,
            authorizedAsOwnerOnly: true,
            channel: new Channel(),
        });
    }
}
