<?php
namespace Veno\QrCdr\Lib;

use Exception;

/**
 * QR Code Main Encoding Facade (Private Fork for Veno QrCdr)
 *
 * Configures settings and orchestrates the encoding process.
 * Provides methods for RAW, PNG (optional), SVG, and EPS output.
 *
 * @package   QrCdr
 * @version   1.1.4-patched
 */

if ( ! defined( 'ABSPATH' ) && ! defined( 'QRCDR_OPEN_ACCESS' ) ) {
	exit;
}

class QrcdrEncode {

	public $case_sensitive = true;
	public $eight_bit      = false;
	public $version        = 0;
	public $size           = 3;
	public $margin         = 4;
	public $back_color     = 0xFFFFFF;
	public $fore_color     = 0x000000;
	public $cmyk           = false;
	public $structured     = 0;
	public $level          = QRCDR_ECLEVEL_L;
	public $hint           = QRCDR_MODE_8;

	/**
	 * Factory method.
	 */
	public static function factory( $level = QRCDR_ECLEVEL_L, $size = 3, $margin = 4, $back_color = 0xFFFFFF, $fore_color = 0x000000, $cmyk = false ) {
		$encoder             = new QrcdrEncode();
		$encoder->size       = $size;
		$encoder->margin     = $margin;
		$encoder->fore_color = $fore_color;
		$encoder->back_color = $back_color;
		$encoder->cmyk       = $cmyk;

		switch ( (string) $level ) {
			case '0':
			case '1':
			case '2':
			case '3':
				$encoder->level = (int) $level;
				break;
			case 'l':
			case 'L':
				$encoder->level = QRCDR_ECLEVEL_L;
				break;
			case 'm':
			case 'M':
				$encoder->level = QRCDR_ECLEVEL_M;
				break;
			case 'q':
			case 'Q':
				$encoder->level = QRCDR_ECLEVEL_Q;
				break;
			case 'h':
			case 'H':
				$encoder->level = QRCDR_ECLEVEL_H;
				break;
		}

		return $encoder;
	}

	/**
	 * Encode to RAW data.
	 */
	public function encodeRAW( $in_text, $out_file = false ) {
		$code = new QrcdrCode();

		if ( $this->eight_bit ) {
			$code->encodeString8bit( $in_text, $this->version, $this->level );
		} else {
			$code->encodeString( $in_text, $this->version, $this->level, $this->hint, $this->case_sensitive );
		}

		return $code->data;
	}

	/**
	 * Encode and binarize.
	 */
	public function encode( $in_text, $out_file = false ) {
		$code = new QrcdrCode();

		if ( $this->eight_bit ) {
			$code->encodeString8bit( $in_text, $this->version, $this->level );
		} else {
			$code->encodeString( $in_text, $this->version, $this->level, $this->hint, $this->case_sensitive );
		}

		$binarized_data = QrcdrTools::binarize( $code->data );

		if ( false !== $out_file ) {
			// Using native PHP file_put_contents here as this is generic library code.
			// The caller (wrapper) should handle safe saving.
			file_put_contents( $out_file, join( "\n", $binarized_data ) );
		} else {
			return $binarized_data;
		}
	}

	/**
	 * Encode to PNG.
	 * NOTE: Disabled/Deprecated because we don't include QRimage (GD).
	 */
	public function encodePNG( $in_text, $out_file = false, $save_and_print = false ) {
		// Deprecated: QRimage dependency removed for this custom build.
		// If you need PNG, rely on your custom SVG-to-Image converter or re-enable QRimage.
		/*
		try {
			ob_start();
			$binarized_data = $this->encode( $in_text );
			// ... logic removed ...
			QRimage::png( $binarized_data, $out_file, ... );
		} catch ( Exception $e ) {
			QrcdrTools::log( $out_file, $e->getMessage() );
		}
		*/
	}

	/**
	 * Encode to EPS.
	 */
	public function encodeEPS( $in_text, $out_file = false, $save_and_print = false ) {
		try {
			ob_start();
			$binarized_data = $this->encode( $in_text );
			$error_output   = ob_get_contents();
			ob_end_clean();

			if ( '' !== $error_output ) {
				QrcdrTools::log( $out_file, $error_output );
			}

			QrcdrVect::eps( $binarized_data, $out_file, $this->size, $this->margin, $save_and_print, $this->back_color, $this->fore_color, $this->cmyk );

		} catch ( Exception $e ) {
			QrcdrTools::log( $out_file, $e->getMessage() );
		}
	}

	/**
	 * Encode to SVG.
	 */
	public function encodeSVG( $in_text, $out_file = false, $save_and_print = false ) {
		try {
			ob_start();
			$binarized_data = $this->encode( $in_text );
			$error_output   = ob_get_contents();
			ob_end_clean();

			if ( '' !== $error_output ) {
				QrcdrTools::log( $out_file, $error_output );
			}

			QrcdrVect::svg( $binarized_data, $out_file, $this->size, $this->margin, $save_and_print, $this->back_color, $this->fore_color );

		} catch ( Exception $e ) {
			QrcdrTools::log( $out_file, $e->getMessage() );
		}
	}
}
