<?php

namespace App\Services;

class BarcodeService
{
    /**
     * Generate barcode HTML using a simple SVG-based approach
     *
     * @param string $code
     * @param string $type
     * @return string
     */
    public function getBarcodeHTML($code, $type = 'EAN13')
    {
        // For EAN13, we need exactly 13 digits
        $code = $this->formatEAN13($code);
        
        // Generate simple barcode representation using SVG
        return $this->generateSVGBarcode($code, $type);
    }
    
    /**
     * Format code to EAN13 standard (13 digits)
     *
     * @param string $code
     * @return string
     */
    private function formatEAN13($code)
    {
        // Remove any non-numeric characters
        $code = preg_replace('/[^0-9]/', '', $code);
        
        // Pad with zeros if too short, or truncate if too long
        if (strlen($code) < 13) {
            $code = str_pad($code, 12, '0', STR_PAD_LEFT);
            // Calculate check digit
            $code .= $this->calculateEAN13CheckDigit($code);
        } elseif (strlen($code) > 13) {
            $code = substr($code, 0, 13);
        }
        
        return $code;
    }
    
    /**
     * Calculate EAN13 check digit
     *
     * @param string $code
     * @return int
     */
    private function calculateEAN13CheckDigit($code)
    {
        $sum = 0;
        for ($i = 0; $i < 12; $i++) {
            $sum += $code[$i] * (($i % 2) ? 3 : 1);
        }
        return (10 - ($sum % 10)) % 10;
    }
    
    /**
     * Generate SVG barcode representation
     *
     * @param string $code
     * @param string $type
     * @return string
     */
    private function generateSVGBarcode($code, $type)
    {
        // EAN13 encoding patterns
        $leftOddPattern = [
            '0' => '0001101', '1' => '0011001', '2' => '0010011', '3' => '0111101',
            '4' => '0100011', '5' => '0110001', '6' => '0101111', '7' => '0111011',
            '8' => '0110111', '9' => '0001011'
        ];
        
        $leftEvenPattern = [
            '0' => '0100111', '1' => '0110011', '2' => '0011011', '3' => '0100001',
            '4' => '0011101', '5' => '0111001', '6' => '0000101', '7' => '0010001',
            '8' => '0001001', '9' => '0010111'
        ];
        
        $rightPattern = [
            '0' => '1110010', '1' => '1100110', '2' => '1101100', '3' => '1000010',
            '4' => '1011100', '5' => '1001110', '6' => '1010000', '7' => '1000100',
            '8' => '1001000', '9' => '1110100'
        ];
        
        $leftPatternSelect = [
            '0' => 'OOOOOO', '1' => 'OOEOEE', '2' => 'OOEEOE', '3' => 'OOEEEO',
            '4' => 'OEOOEE', '5' => 'OEEOOE', '6' => 'OEEEOO', '7' => 'OEOEOE',
            '8' => 'OEOEEO', '9' => 'OEEOEO'
        ];
        
        $firstDigit = $code[0];
        $leftDigits = substr($code, 1, 6);
        $rightDigits = substr($code, 7, 6);
        $patternSelect = $leftPatternSelect[$firstDigit];
        
        // Build barcode pattern
        $pattern = '101'; // Start guard
        
        // Left side
        for ($i = 0; $i < 6; $i++) {
            $digit = $leftDigits[$i];
            if ($patternSelect[$i] == 'O') {
                $pattern .= $leftOddPattern[$digit];
            } else {
                $pattern .= $leftEvenPattern[$digit];
            }
        }
        
        $pattern .= '01010'; // Center guard
        
        // Right side
        for ($i = 0; $i < 6; $i++) {
            $digit = $rightDigits[$i];
            $pattern .= $rightPattern[$digit];
        }
        
        $pattern .= '101'; // End guard
        
        // Generate SVG - match production layout (no text inside SVG)
        $width = 200;
        $height = 50;
        $barWidth = $width / strlen($pattern);
        
        $svg = '<svg width="' . $width . '" height="' . $height . '" xmlns="http://www.w3.org/2000/svg">';
        
        $x = 0;
        for ($i = 0; $i < strlen($pattern); $i++) {
            if ($pattern[$i] == '1') {
                $svg .= '<rect x="' . $x . '" y="0" width="' . $barWidth . '" height="' . $height . '" fill="black"/>';
            }
            $x += $barWidth;
        }
        
        $svg .= '</svg>';
        
        return $svg;
    }
}