import apiClient from './ApiService';

/**
 * Bulk Print Service
 * Handles bulk document printing operations
 */
class BulkPrintService {
    /**
     * Print documents for multiple orders
     *
     * @param {number[]} orderIds - Array of order IDs
     * @param {string} documentType - Document type: 'marking', 'invoice', or 'international'
     * @returns {Promise<{success: boolean, data: string, summary: object}>}
     */
    async printBulkDocuments(orderIds, documentType) {
        try {
            const response = await apiClient.post(
                '/shipments/bulk-print',
                {
                    orderIds,
                    type: documentType,
                },
                true
            );

            return response;
        } catch (error) {
            console.error('[BulkPrintService] Error printing documents:', error);
            throw error;
        }
    }

    /**
     * Convert base64 string to Blob
     *
     * @param {string} base64 - Base64 encoded data
     * @param {string} contentType - MIME type
     * @returns {Blob}
     */
    base64ToBlob(base64, contentType = 'application/pdf') {
        const byteCharacters = atob(base64);
        const byteArrays = [];

        for (let offset = 0; offset < byteCharacters.length; offset += 512) {
            const slice = byteCharacters.slice(offset, offset + 512);
            const byteNumbers = new Array(slice.length);

            for (let i = 0; i < slice.length; i++) {
                byteNumbers[i] = slice.charCodeAt(i);
            }

            const byteArray = new Uint8Array(byteNumbers);
            byteArrays.push(byteArray);
        }

        return new Blob(byteArrays, { type: contentType });
    }

    /**
     * Trigger browser print dialog for PDF
     *
     * @param {string} base64Data - Base64 encoded PDF
     */
    printPDF(base64Data) {
        const blob = this.base64ToBlob(base64Data);
        const url = URL.createObjectURL(blob);

        const iframe = document.createElement('iframe');
        iframe.style.position = 'fixed';
        iframe.style.right = '0';
        iframe.style.bottom = '0';
        iframe.style.width = '0';
        iframe.style.height = '0';
        iframe.style.border = 'none';
        iframe.src = url;
        document.body.appendChild(iframe);

        iframe.onload = () => {
            setTimeout(() => {
                try {
                    iframe.contentWindow.print();

                    // Listen for afterprint event to clean up
                    const cleanup = () => {
                        setTimeout(() => {
                            if (iframe.parentNode) {
                                document.body.removeChild(iframe);
                            }
                            URL.revokeObjectURL(url);
                        }, 100);
                    };

                    // Try to listen for afterprint event
                    if (iframe.contentWindow.matchMedia) {
                        const mediaQueryList = iframe.contentWindow.matchMedia('print');
                        mediaQueryList.addListener((mql) => {
                            if (!mql.matches) {
                                cleanup();
                            }
                        });
                    }

                    // Fallback: clean up after 60 seconds if user doesn't print
                    setTimeout(cleanup, 60000);
                } catch (e) {
                    console.error('[BulkPrintService] Print error:', e);
                    // Clean up on error
                    if (iframe.parentNode) {
                        document.body.removeChild(iframe);
                    }
                    URL.revokeObjectURL(url);
                }
            }, 500);
        };
    }

    /**
     * Download PDF file
     *
     * @param {string} base64Data - Base64 encoded PDF
     * @param {string} filename - Filename for download
     */
    downloadPDF(base64Data, filename) {
        // Use data URL instead of blob URL to avoid "insecure download" warning
        const dataUrl = `data:application/pdf;base64,${base64Data}`;

        const a = document.createElement('a');
        a.href = dataUrl;
        a.download = filename;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
    }

    /**
     * Get filename for document type
     *
     * @param {string} documentType - Document type
     * @returns {string}
     */
    getFilename(documentType) {
        const now = new Date();
        const date = now.toISOString().split('T')[0];
        const time = now.toTimeString().split(' ')[0].substring(0, 5).replace(':', '-');

        const typeMap = {
            marking: 'markings',
            invoice: 'invoices',
            international: 'customs',
        };

        const typeName = typeMap[documentType] || documentType;
        return `nova-post-${typeName}-${date}-${time}.pdf`;
    }
}

export default new BulkPrintService();
