import { UnifiedVfsRouter } from '../services/vfs/unified-vfs-router.js';
import path from 'path';
import { spawn, execSync } from 'child_process';
import { getErrorMessage } from '../types/index.js';
/**
 * Screenshot Tool for capturing screen, windows, and regions
 * Works on Linux (with scrot/gnome-screenshot), macOS (screencapture), and Windows (PowerShell)
 */
export class ScreenshotTool {
    defaultOutputDir = path.join(process.cwd(), '.codebuddy', 'screenshots');
    vfs = UnifiedVfsRouter.Instance;
    /**
     * Capture a screenshot
     */
    async capture(options = {}) {
        try {
            // Ensure output directory exists
            await this.vfs.ensureDir(this.defaultOutputDir);
            const format = options.format || 'png';
            const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
            const filename = `screenshot_${timestamp}.${format}`;
            const outputPath = options.outputPath || path.join(this.defaultOutputDir, filename);
            const platform = process.platform;
            let result;
            if (platform === 'darwin') {
                result = await this.captureMacOS(outputPath, options);
            }
            else if (platform === 'linux') {
                result = await this.captureLinux(outputPath, options);
            }
            else if (platform === 'win32') {
                result = await this.captureWindows(outputPath, options);
            }
            else {
                return {
                    success: false,
                    error: `Unsupported platform: ${platform}`
                };
            }
            return {
                success: true,
                output: this.formatResult(result),
                data: result
            };
        }
        catch (error) {
            return {
                success: false,
                error: `Screenshot capture failed: ${getErrorMessage(error)}`
            };
        }
    }
    /**
     * Capture screenshot on macOS using screencapture
     */
    async captureMacOS(outputPath, options) {
        return new Promise((resolve, reject) => {
            const args = [];
            if (options.delay) {
                args.push('-T', options.delay.toString());
            }
            if (options.region) {
                args.push('-R', `${options.region.x},${options.region.y},${options.region.width},${options.region.height}`);
            }
            else if (options.window) {
                args.push('-l', options.window);
            }
            else if (!options.fullscreen) {
                // Interactive selection mode
                args.push('-i');
            }
            if (options.format === 'jpg') {
                args.push('-t', 'jpg');
            }
            args.push(outputPath);
            const screencapture = spawn('screencapture', args);
            screencapture.on('close', async (code) => {
                if (code === 0 && await this.vfs.exists(outputPath)) {
                    const stats = await this.vfs.stat(outputPath);
                    resolve({
                        path: outputPath,
                        size: this.formatSize(stats.size),
                        timestamp: new Date().toISOString()
                    });
                }
                else {
                    reject(new Error(`screencapture failed with code ${code}`));
                }
            });
            screencapture.on('error', (err) => {
                reject(err);
            });
        });
    }
    /**
     * Capture screenshot on Linux using scrot or gnome-screenshot
     */
    async captureLinux(outputPath, options) {
        // Try scrot first, then gnome-screenshot, then import (ImageMagick)
        const tools = ['scrot', 'gnome-screenshot', 'import'];
        let availableTool = null;
        for (const tool of tools) {
            try {
                execSync(`which ${tool}`, { stdio: 'ignore' });
                availableTool = tool;
                break;
            }
            catch {
                continue;
            }
        }
        if (!availableTool) {
            throw new Error('No screenshot tool found. Install scrot, gnome-screenshot, or imagemagick.');
        }
        return new Promise((resolve, reject) => {
            let args = [];
            if (availableTool === 'scrot') {
                if (options.delay) {
                    args.push('-d', options.delay.toString());
                }
                if (options.region) {
                    args.push('-a', `${options.region.x},${options.region.y},${options.region.width},${options.region.height}`);
                }
                else if (options.window) {
                    args.push('-u'); // Focused window
                }
                if (options.quality && options.format === 'jpg') {
                    args.push('-q', options.quality.toString());
                }
                args.push(outputPath);
            }
            else if (availableTool === 'gnome-screenshot') {
                if (options.delay) {
                    args.push('-d', options.delay.toString());
                }
                if (options.window) {
                    args.push('-w'); // Active window
                }
                else if (options.region) {
                    args.push('-a'); // Area selection
                }
                args.push('-f', outputPath);
            }
            else if (availableTool === 'import') {
                // ImageMagick import
                if (options.window) {
                    args.push('-window', 'root');
                }
                args.push(outputPath);
            }
            const screenshot = spawn(availableTool, args);
            screenshot.on('close', async (code) => {
                if (code === 0 && await this.vfs.exists(outputPath)) {
                    const stats = await this.vfs.stat(outputPath);
                    resolve({
                        path: outputPath,
                        size: this.formatSize(stats.size),
                        timestamp: new Date().toISOString()
                    });
                }
                else {
                    reject(new Error(`${availableTool} failed with code ${code}`));
                }
            });
            screenshot.on('error', (err) => {
                reject(err);
            });
        });
    }
    /**
     * Capture screenshot on Windows using PowerShell
     */
    async captureWindows(outputPath, _options) {
        return new Promise((resolve, reject) => {
            const script = `
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$screen = [System.Windows.Forms.Screen]::PrimaryScreen
$bounds = $screen.Bounds
$bitmap = New-Object System.Drawing.Bitmap($bounds.Width, $bounds.Height)
$graphics = [System.Drawing.Graphics]::FromImage($bitmap)
$graphics.CopyFromScreen($bounds.Location, [System.Drawing.Point]::Empty, $bounds.Size)
$bitmap.Save('${outputPath.replace(/\\/g, '\\\\')}')
$graphics.Dispose()
$bitmap.Dispose()
      `;
            const powershell = spawn('powershell', ['-Command', script]);
            powershell.on('close', async (code) => {
                if (code === 0 && await this.vfs.exists(outputPath)) {
                    const stats = await this.vfs.stat(outputPath);
                    resolve({
                        path: outputPath,
                        size: this.formatSize(stats.size),
                        timestamp: new Date().toISOString()
                    });
                }
                else {
                    reject(new Error(`PowerShell screenshot failed with code ${code}`));
                }
            });
            powershell.on('error', (err) => {
                reject(err);
            });
        });
    }
    /**
     * Capture active window
     */
    async captureWindow(windowTitle) {
        return this.capture({ window: windowTitle || 'active' });
    }
    /**
     * Capture a region
     */
    async captureRegion(x, y, width, height) {
        return this.capture({ region: { x, y, width, height } });
    }
    /**
     * Capture fullscreen with delay
     */
    async captureDelayed(seconds) {
        return this.capture({ fullscreen: true, delay: seconds });
    }
    /**
     * List saved screenshots
     */
    async listScreenshots() {
        try {
            if (!await this.vfs.exists(this.defaultOutputDir)) {
                return {
                    success: true,
                    output: 'No screenshots found'
                };
            }
            const entries = await this.vfs.readDirectory(this.defaultOutputDir);
            const screenshots = entries.filter(e => e.isFile && /\.(png|jpg|jpeg)$/i.test(e.name));
            if (screenshots.length === 0) {
                return {
                    success: true,
                    output: 'No screenshots found'
                };
            }
            const listPromises = screenshots.map(async (s) => {
                const fullPath = path.join(this.defaultOutputDir, s.name);
                const stats = await this.vfs.stat(fullPath);
                const time = stats.mtime.toLocaleString();
                return `  📸 ${s.name} (${this.formatSize(stats.size)}) - ${time}`;
            });
            const screenshotList = (await Promise.all(listPromises)).join('\n');
            return {
                success: true,
                output: `Screenshots in ${this.defaultOutputDir}:\n${screenshotList}`
            };
        }
        catch (error) {
            return {
                success: false,
                error: `Failed to list screenshots: ${getErrorMessage(error)}`
            };
        }
    }
    /**
     * Read a screenshot and convert to base64
     */
    async toBase64(filePath) {
        try {
            const resolvedPath = path.resolve(process.cwd(), filePath);
            if (!await this.vfs.exists(resolvedPath)) {
                return {
                    success: false,
                    error: `Screenshot not found: ${filePath}`
                };
            }
            const buffer = await this.vfs.readFileBuffer(resolvedPath);
            const base64 = buffer.toString('base64');
            const ext = path.extname(resolvedPath).toLowerCase();
            const mediaType = ext === '.png' ? 'image/png' : 'image/jpeg';
            return {
                success: true,
                output: `Screenshot converted to base64 (${base64.length} characters)`,
                data: {
                    base64,
                    mediaType,
                    filename: path.basename(filePath)
                }
            };
        }
        catch (error) {
            return {
                success: false,
                error: `Failed to convert screenshot: ${getErrorMessage(error)}`
            };
        }
    }
    /**
     * Delete a screenshot
     */
    async deleteScreenshot(filePath) {
        try {
            const resolvedPath = filePath.startsWith('/')
                ? filePath
                : path.join(this.defaultOutputDir, filePath);
            if (!await this.vfs.exists(resolvedPath)) {
                return {
                    success: false,
                    error: `Screenshot not found: ${filePath}`
                };
            }
            await this.vfs.remove(resolvedPath);
            return {
                success: true,
                output: `Screenshot deleted: ${resolvedPath}`
            };
        }
        catch (error) {
            return {
                success: false,
                error: `Failed to delete screenshot: ${getErrorMessage(error)}`
            };
        }
    }
    /**
     * Clear all screenshots
     */
    async clearScreenshots() {
        try {
            if (!await this.vfs.exists(this.defaultOutputDir)) {
                return {
                    success: true,
                    output: 'No screenshots to clear'
                };
            }
            const entries = await this.vfs.readDirectory(this.defaultOutputDir);
            let deleted = 0;
            for (const entry of entries) {
                if (entry.isFile && /\.(png|jpg|jpeg)$/i.test(entry.name)) {
                    await this.vfs.remove(path.join(this.defaultOutputDir, entry.name));
                    deleted++;
                }
            }
            return {
                success: true,
                output: `Cleared ${deleted} screenshots`
            };
        }
        catch (error) {
            return {
                success: false,
                error: `Failed to clear screenshots: ${getErrorMessage(error)}`
            };
        }
    }
    /**
     * Format file size
     */
    formatSize(bytes) {
        if (bytes < 1024)
            return `${bytes} B`;
        if (bytes < 1024 * 1024)
            return `${(bytes / 1024).toFixed(2)} KB`;
        return `${(bytes / (1024 * 1024)).toFixed(2)} MB`;
    }
    /**
     * Format result for display
     */
    formatResult(result) {
        const lines = [
            `📸 Screenshot captured`,
            `   Path: ${result.path}`,
            `   Size: ${result.size}`,
            `   Time: ${result.timestamp}`
        ];
        if (result.width && result.height) {
            lines.push(`   Dimensions: ${result.width}x${result.height}`);
        }
        return lines.join('\n');
    }
}
//# sourceMappingURL=screenshot-tool.js.map