<?php
/**
 * Simple QR Code Generator using Google Charts API
 * Alternative to phpqrcode library
 */

class SimpleQRGenerator {
    
    /**
     * Generate QR code using Google Charts API
     * @param string $data Data to encode
     * @param string $filename Output filename
     * @param int $size QR code size (default 300x300)
     * @return array Result with success status and path
     */
    public static function generateQR($data, $filename, $size = 300) {
        try {
            // Google Charts QR API URL
            $url = "https://chart.googleapis.com/chart?chs={$size}x{$size}&cht=qr&chl=" . urlencode($data);
            
            // Get QR code image data
            $context = stream_context_create([
                'http' => [
                    'timeout' => 10,
                    'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
                ]
            ]);
            
            $imageData = file_get_contents($url, false, $context);
            
            if ($imageData === false) {
                return ['success' => false, 'error' => 'Failed to fetch QR code from Google Charts API'];
            }
            
            // Save to file
            if (file_put_contents($filename, $imageData) === false) {
                return ['success' => false, 'error' => 'Failed to save QR code file'];
            }
            
            return [
                'success' => true, 
                'filename' => basename($filename),
                'path' => $filename,
                'size' => filesize($filename)
            ];
            
        } catch (Exception $e) {
            error_log('QR generation error: ' . $e->getMessage());
            return ['success' => false, 'error' => 'QR code generation failed: ' . $e->getMessage()];
        }
    }
    
    /**
     * Alternative QR generator using QR Server API
     * @param string $data Data to encode
     * @param string $filename Output filename
     * @param int $size QR code size
     * @return array Result with success status and path
     */
    public static function generateQRAlternative($data, $filename, $size = 300) {
        try {
            // QR Server API URL
            $url = "https://api.qrserver.com/v1/create-qr-code/?size={$size}x{$size}&data=" . urlencode($data);
            
            $context = stream_context_create([
                'http' => [
                    'timeout' => 10,
                    'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
                ]
            ]);
            
            $imageData = file_get_contents($url, false, $context);
            
            if ($imageData === false) {
                return ['success' => false, 'error' => 'Failed to fetch QR code from QR Server API'];
            }
            
            if (file_put_contents($filename, $imageData) === false) {
                return ['success' => false, 'error' => 'Failed to save QR code file'];
            }
            
            return [
                'success' => true, 
                'filename' => basename($filename),
                'path' => $filename,
                'size' => filesize($filename)
            ];
            
        } catch (Exception $e) {
            error_log('Alternative QR generation error: ' . $e->getMessage());
            return ['success' => false, 'error' => 'QR code generation failed: ' . $e->getMessage()];
        }
    }
    
    /**
     * Generate QR code with fallback methods
     * @param string $data Data to encode
     * @param string $filename Output filename
     * @param int $size QR code size
     * @return array Result with success status and path
     */
    public static function generate($data, $filename, $size = 300) {
        // Try primary method first
        $result = self::generateQR($data, $filename, $size);
        
        if (!$result['success']) {
            // Try alternative method
            $result = self::generateQRAlternative($data, $filename, $size);
        }
        
        return $result;
    }
}
?>
