File size: 14,411 Bytes
52ec77f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
document.addEventListener('DOMContentLoaded', function() {
    // DOM Elements
    const dropZone = document.getElementById('drop-zone');
    const pdfDropZone = document.getElementById('pdf-drop-zone');
    const fileList = document.getElementById('file-list');
    const processBtn = document.getElementById('process-btn');
    const pdfProcessBtn = document.getElementById('pdf-process-btn');
    const transporterSection = document.getElementById('transporter-section');
    const transporterStatus = document.getElementById('transporter-status');
    const completedSection = document.getElementById('completed-section');
    const completedFiles = document.getElementById('completed-files');
    
    // File storage
    let filesToProcess = [];
    let currentPdfFile = null;
    
    // Initialize Feather Icons
    feather.replace();
    
    // File type icons mapping
    const fileTypeIcons = {
        'text/plain': 'file-text',
        'application/msword': 'file-text',
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'file-text',
        'image/png': 'image',
        'image/jpeg': 'image',
        'image/gif': 'image',
        'application/pdf': 'file'
    };
    
    // Supported file types
    const supportedTypes = [
        'text/plain',
        'application/msword',
        'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        'text/markdown',
        'image/png',
        'image/jpeg',
        'image/gif'
    ];
    
    // Setup drag and drop for file upload
    setupDragAndDrop(dropZone, handleFiles);
    
    // Setup drag and drop for PDF upload
    setupDragAndDrop(pdfDropZone, handlePdfFiles);
    
    // Process button event
    processBtn.addEventListener('click', processFiles);
    
    // PDF process button event
    pdfProcessBtn.addEventListener('click', processPdfFile);
    
    // Setup drag and drop functionality
    function setupDragAndDrop(zone, handler) {
        zone.addEventListener('dragover', (e) => {
            e.preventDefault();
            zone.classList.add('drop-zone-active');
        });
        
        zone.addEventListener('dragleave', () => {
            zone.classList.remove('drop-zone-active');
        });
        
        zone.addEventListener('drop', (e) => {
            e.preventDefault();
            zone.classList.remove('drop-zone-active');
            const files = e.dataTransfer.files;
            handler(files);
        });
        
        zone.addEventListener('click', () => {
            const fileInput = document.createElement('input');
            fileInput.type = 'file';
            fileInput.multiple = true;
            fileInput.accept = supportedTypes.join(',');
            
            fileInput.addEventListener('change', (e) => {
                handler(e.target.files);
            });
            
            fileInput.click();
        });
    }
    
    // Handle file selection for document creation
    function handleFiles(files) {
        for (let i = 0; i < files.length; i++) {
            const file = files[i];
            
            // Check if file type is supported
            if (supportedTypes.includes(file.type) && filesToProcess.length < 30) {
                // Check for duplicates
                if (!filesToProcess.some(f => f.name === file.name && f.size === file.size)) {
                    filesToProcess.push(file);
                    renderFileItem(file);
                }
            } else if (filesToProcess.length >= 30) {
                alert('Maximum of 30 files allowed');
                break;
            }
        }
    }
    
    // Handle PDF file selection
    function handlePdfFiles(files) {
        if (files.length > 0) {
            const file = files[0];
            if (file.type === 'application/pdf') {
                currentPdfFile = file;
                pdfDropZone.innerHTML = `
                    <i data-feather="file" class="w-12 h-12 mx-auto text-fuchsia-400 mb-4"></i>
                    <p class="mb-2">${file.name}</p>
                    <p class="text-sm text-gray-400">${(file.size / 1024 / 1024).toFixed(2)} MB</p>
                `;
                feather.replace();
            } else {
                alert('Please select a PDF file');
            }
        }
    }
    
    // Render file item in the list
    function renderFileItem(file) {
        const fileItem = document.createElement('div');
        fileItem.className = 'file-item mb-2';
        fileItem.dataset.fileName = file.name;
        
        const fileType = file.type.split('/')[1] || 'file';
        const iconType = fileTypeIcons[file.type] || 'file';
        
        fileItem.innerHTML = `
            <div class="file-icon bg-lime-500">
                <i data-feather="${iconType}" class="text-gray-900"></i>
            </div>
            <div class="file-info">
                <div class="file-name">${file.name}</div>
                <div class="file-size">${(file.size / 1024).toFixed(1)} KB</div>
            </div>
            <button class="remove-file" data-file-name="${file.name}">
                <i data-feather="x"></i>
            </button>
        `;
        
        fileList.appendChild(fileItem);
        feather.replace();
        
        // Add remove event
        fileItem.querySelector('.remove-file').addEventListener('click', () => {
            filesToProcess = filesToProcess.filter(f => f.name !== file.name);
            fileItem.remove();
        });
    }
    
    // Process files to create PDF
    function processFiles() {
        if (filesToProcess.length === 0) {
            alert('Please add files to process');
            return;
        }
        
        // Show transporter animation
        transporterSection.classList.remove('hidden');
        transporterStatus.textContent = 'Initializing transport sequence...';
        
        // Simulate processing with transporter animation
        simulateTransporterAnimation(() => {
            // Create a mock PDF file
            const fileName = `replicated_document_${Date.now()}.pdf`;
            const mockPdf = new Blob(['Mock PDF content'], { type: 'application/pdf' });
            
            // Add to completed files
            addCompletedFile(fileName, mockPdf, 'PDF Document');
            
            // Reset
            filesToProcess = [];
            fileList.innerHTML = '';
            transporterSection.classList.add('hidden');
        });
    }
    
    // Process PDF file
    function processPdfFile() {
        if (!currentPdfFile) {
            alert('Please select a PDF file to process');
            return;
        }
        
        const format = document.querySelector('input[name="output-format"]:checked').value;
        const fileName = currentPdfFile.name.replace('.pdf', `.${format}`);
        
        // Show transporter animation
        transporterSection.classList.remove('hidden');
        transporterStatus.textContent = 'Decomposing document...';
        
        // Simulate processing
        simulateTransporterAnimation(() => {
            // Create mock output file
            const content = format === 'md' ? '# Mock Markdown Content' : '{"content": "Mock JSON Content"}';
            const mimeType = format === 'md' ? 'text/markdown' : 'application/json';
            const mockFile = new Blob([content], { type: mimeType });
            
            // Add to completed files
            addCompletedFile(fileName, mockFile, `${format.toUpperCase()} Document`);
            
            // Reset
            currentPdfFile = null;
            pdfDropZone.innerHTML = `
                <i data-feather="file" class="w-12 h-12 mx-auto text-fuchsia-400 mb-4"></i>
                <p class="mb-2">Drag & drop PDF files here or click to browse</p>
                <p class="text-sm text-gray-400">Convert PDF to MD or JSON format</p>
            `;
            feather.replace();
            transporterSection.classList.add('hidden');
        });
    }
    
    // Simulate transporter animation
    function simulateTransporterAnimation(callback) {
        const messages = [
            'Initializing transport sequence...',
            'Locking coordinates...',
            'Energizing pattern buffer...',
            'Dematerializing subject...',
            'Transport in progress...',
            'Rematerializing at destination...',
            'Transport complete!'
        ];
        
        let index = 0;
        const interval = setInterval(() => {
            transporterStatus.textContent = messages[index];
            index++;
            
            if (index >= messages.length) {
                clearInterval(interval);
                setTimeout(callback, 500);
            }
        }, 800);
        
        // Create sparkle effects
        createSparkles(20);
    }
    
    // Create sparkle effects
    function createSparkles(count) {
        const transporterAnimation = document.getElementById('transporter-animation');
        for (let i = 0; i < count; i++) {
            setTimeout(() => {
                const sparkle = document.createElement('div');
                sparkle.className = 'sparkle';
                sparkle.style.left = `${Math.random() * 100}%`;
                sparkle.style.top = `${Math.random() * 100}%`;
                sparkle.style.animation = `sparkle 0.8s ease-out`;
                transporterAnimation.appendChild(sparkle);
                
                setTimeout(() => {
                    sparkle.remove();
                }, 800);
            }, i * 100);
        }
    }
    
    // Add completed file to the display
    function addCompletedFile(fileName, blob, fileType) {
        completedSection.classList.remove('hidden');
        
        const fileElement = document.createElement('div');
        fileElement.className = 'completed-file';
        fileElement.innerHTML = `
            <div class="ribbon"></div>
            <div class="mb-4">
                <i data-feather="file" class="w-16 h-16 mx-auto text-yellow-400"></i>
            </div>
            <h3 class="font-bold text-lg mb-2 truncate">${fileName}</h3>
            <p class="text-gray-400 text-sm mb-4">${fileType}</p>
            <div class="flex justify-center space-x-2">
                <button class="download-btn bg-lime-500 hover:bg-lime-600 text-gray-900 py-2 px-4 rounded flex items-center">
                    <i data-feather="download" class="mr-1"></i> Save
                </button>
                <button class="open-btn bg-blue-500 hover:bg-blue-600 text-white py-2 px-4 rounded flex items-center">
                    <i data-feather="eye" class="mr-1"></i> View
                </button>
            </div>
        `;
        
        completedFiles.appendChild(fileElement);
        feather.replace();
        
        // Add event listeners
        const downloadBtn = fileElement.querySelector('.download-btn');
        const openBtn = fileElement.querySelector('.open-btn');
        
        downloadBtn.addEventListener('click', () => {
            showSaveModal(fileName, blob);
        });
        
        openBtn.addEventListener('click', () => {
            const url = URL.createObjectURL(blob);
            window.open(url, '_blank');
        });
    }
    
    // Show save modal
    function showSaveModal(fileName, blob) {
        // Create modal overlay
        const overlay = document.createElement('div');
        overlay.className = 'modal-overlay';
        document.body.appendChild(overlay);
        
        // Create modal
        const modal = document.createElement('div');
        modal.className = 'save-modal';
        modal.innerHTML = `
            <div class="modal-header">
                <h3>Save Document</h3>
                <button class="close-modal">&times;</button>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label for="file-name">File Name</label>
                    <input type="text" id="file-name" class="form-control" value="${fileName}">
                </div>
                <div class="form-group">
                    <label for="save-location">Save Location</label>
                    <select id="save-location" class="form-control">
                        <option value="downloads">Downloads Folder</option>
                        <option value="documents">Documents Folder</option>
                        <option value="desktop">Desktop</option>
                    </select>
                </div>
            </div>
            <div class="modal-footer">
                <button class="btn btn-secondary cancel-btn">Cancel</button>
                <button class="btn btn-success save-btn">Save Now</button>
                <button class="btn btn-primary consume-btn">Consume Now</button>
            </div>
        `;
        
        document.body.appendChild(modal);
        
        // Add event listeners
        const closeBtn = modal.querySelector('.close-modal');
        const cancelBtn = modal.querySelector('.cancel-btn');
        const saveBtn = modal.querySelector('.save-btn');
        const consumeBtn = modal.querySelector('.consume-btn');
        
        const closeModal = () => {
            overlay.remove();
            modal.remove();
        };
        
        closeBtn.addEventListener('click', closeModal);
        cancelBtn.addEventListener('click', closeModal);
        overlay.addEventListener('click', closeModal);
        
        saveBtn.addEventListener('click', () => {
            const fileName = document.getElementById('file-name').value;
            saveFile(blob, fileName);
            closeModal();
        });
        
        consumeBtn.addEventListener('click', () => {
            const fileName = document.getElementById('file-name').value;
            saveFile(blob, fileName);
            const url = URL.createObjectURL(blob);
            window.open(url, '_blank');
            closeModal();
        });
    }
    
    // Save file to user's device
    function saveFile(blob, fileName) {
        const url = URL.createObjectURL(blob);
        const a = document.createElement('a');
        a.href = url;
        a.download = fileName;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
    }
});