<?php

namespace FluentC\Services;

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

/**
 * HTML Processor for handling content translation
 */
class Html_Processor {
   
    /**
     * FluentC connect service
     *
     * @var Connect
     */
    private $fluentc_connect;

    /**
     * Constructor
     */
    public function __construct() { 
        $this->fluentc_connect = new Connect();
    }

    /**
     * Process HTML content using Base64 encoding
     *
     * @param string $widgetapikey The FluentC API key
     * @param string $html The HTML content to translate
     * @param string $sourceLanguage The source language code
     * @param string $targetLanguage The target language code
     * @param string $path The request path (optional)
     * @return string The translated HTML or the original if translation fails
     */
    public function processBase64Html($widgetapikey, $html, $sourceLanguage, $targetLanguage, $path = '') {
        // Validate inputs
        if (empty($widgetapikey) || empty($html) || empty($sourceLanguage) || empty($targetLanguage)) {
            return $html;
        }
        
        // Skip translation if source and target languages are the same
        if ($sourceLanguage === $targetLanguage) {
            return $html;
        }
        
        try {
            // Encode the HTML content
            $encoded_html = base64_encode($html);
            
            // Get translated content
            $processedHtml = $this->fluentc_connect->get_translation_content_html(
                $widgetapikey, 
                $sourceLanguage,
                $targetLanguage, 
                $encoded_html,
                $path
            );
            
            // Ensure we return a string
            if (is_string($processedHtml) && !empty($processedHtml)) {
                return $processedHtml;
            }
            
            // Log the error if debug mode is enabled
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('FluentC: Translation failed, returning original HTML');
                if (!is_string($processedHtml)) {
                    error_log('FluentC: Unexpected response type: ' . gettype($processedHtml));
                }
            }
        } catch (\Exception $e) {
            // Log the exception
            if (defined('WP_DEBUG') && WP_DEBUG) {
                error_log('FluentC: Exception during translation: ' . $e->getMessage());
            }
        }
        
        // Return the original HTML if translation failed
        return $html;
    }
    
    /**
     * Check if the processor can handle content
     *
     * @param string $widgetapikey API key to check
     * @param string $sourceLanguage Source language
     * @param string $targetLanguage Target language
     * @return bool Whether the processor can handle content
     */
    public function canProcessContent($widgetapikey, $sourceLanguage, $targetLanguage) {
        return !empty($widgetapikey) && 
               !empty($sourceLanguage) && 
               !empty($targetLanguage) && 
               $sourceLanguage !== $targetLanguage;
    }
}