<?php

namespace App\Services;

use PhpOffice\PhpSpreadsheet\Worksheet\Drawing as WorksheetDrawing;
use Picqer\Barcode\BarcodeGeneratorJPG;
use Picqer\Barcode\BarcodeGeneratorPNG;
use Picqer\Barcode\BarcodeGeneratorSVG;

class AsignacionOperacionUtilityService
{
    private const BRAND_LOGO_PATH = 'imgs/avante-logo.png';

    public function resolveTipoRoute(int $tipo): ?string
    {
        return match ($tipo) {
            1 => 'alimentos',
            2 => 'dotacion',
            3 => 'transferencia',
            5 => 'devolucion-prestamo',
            6 => 'devolucion-transferencia',
            default => null,
        };
    }

    public function buildBarcodeValue(int $numero): string
    {
        if ($numero >= 2000000) {
            return (string) $numero;
        }

        return (string) (2000000 + $numero);
    }

    /**
     * @param array<int, string> $temporaryFiles
     */
    public function buildBarcodeDrawing(string $barcodeValue, array &$temporaryFiles = []): ?WorksheetDrawing
    {
        try {
            $barcodePath = $this->buildBarcodeImageFile($barcodeValue);
            if ($barcodePath === null) {
                return null;
            }

            $temporaryFiles[] = $barcodePath;

            $drawing = new WorksheetDrawing();
            $drawing->setPath($barcodePath);

            return $drawing;
        } catch (\Throwable $exception) {
            report($exception);
            return null;
        }
    }

    public function buildLogoDrawing(): ?WorksheetDrawing
    {
        $logoPath = public_path(self::BRAND_LOGO_PATH);

        if (! is_file($logoPath)) {
            return null;
        }

        $drawing = new WorksheetDrawing();
        $drawing->setPath($logoPath);

        return $drawing;
    }

    public function buildBarcodeBinary(string $barcodeValue): ?string
    {
        try {
            $generator = new BarcodeGeneratorPNG();
            return $generator->getBarcode($barcodeValue, $generator::TYPE_CODE_128, 3, 70);
        } catch (\Throwable $exception) {
            report($exception);
            return null;
        }
    }

    public function buildBarcodeSvg(string $barcodeValue): ?string
    {
        try {
            $generator = new BarcodeGeneratorSVG();

            return $generator->getBarcode($barcodeValue, $generator::TYPE_CODE_128, 3, 70);
        } catch (\Throwable $exception) {
            report($exception);
            return null;
        }
    }

    private function buildBarcodeImageFile(string $barcodeValue): ?string
    {
        $barcodeBinary = $this->buildBarcodeJpegBinary($barcodeValue);
        if ($barcodeBinary === null) {
            return null;
        }

        $temporaryPath = tempnam(sys_get_temp_dir(), 'barcode_');
        if ($temporaryPath === false) {
            return null;
        }

        $jpgPath = $temporaryPath . '.jpg';
        @unlink($temporaryPath);

        $written = @file_put_contents($jpgPath, $barcodeBinary);
        if ($written === false) {
            @unlink($jpgPath);

            return null;
        }

        return $jpgPath;
    }

    private function buildBarcodeJpegBinary(string $barcodeValue): ?string
    {
        try {
            $generator = new BarcodeGeneratorJPG();

            return $generator->getBarcode($barcodeValue, $generator::TYPE_CODE_128, 3, 70);
        } catch (\Throwable $exception) {
            report($exception);
            return null;
        }
    }
}
