<?php
defined( 'ABSPATH' ) || exit();
/**
 * @since 1.1.5
 * @param $content
 * @param $to
 * @param $translator
 *
 * @return string
 */
function wpcp_translate_post( $content, $to, $translator ) {
    switch ( $translator ) {
        case 'yandex':
            return wpcp_pro_yandex_translate( $content, $to );
            break;
        case 'deepl':
            return wpcp_pro_deepl_translate( $content, $to );
            break;
        default:
            return $content;
    }
}

/**
 * @param $content
 * @param $to
 *
 * @return string
 * @since 1.0.0
 */
function wpcp_pro_yandex_translate( $content, $to ) {
    $api = wpcp_get_settings( 'yandex_translate_api_key', 'wpcp_settings_misc' );
    if ( empty( $api ) ) {
        wpcp_logger()->error( __( 'Yandex API is not set skipping translation', 'wp-content-pilot-pro' ) );
        $settings_url = admin_url( 'edit.php?post_type=wp_content_pilot&page=wpcp-settings' );
        $message      = __( sprintf( 'WP Content Pilot will skip translation as Yandex API is not set, please set it from <a href="%s">here</a>.', $settings_url ), 'wp-content-pilot-pro' );
        wpcp_admin_notice( $message );

        return $content;
    }


    $failed             = false;
    $translated_strings = '';
    $parts              = str_split( $content, 3500 );

    $curl = new Curl\Curl();
    $curl->setOpt( CURLOPT_FOLLOWLOCATION, true );
    $curl->setOpt( CURLOPT_TIMEOUT, 30 );
    $curl->setOpt( CURLOPT_RETURNTRANSFER, true );

    foreach ( $parts as $part ) {
        $endpoint = add_query_arg( [
            'text'   => urlencode( $part ),
            'key'    => $api,
            'lang'   => $to,
            'format' => 'html',
        ], 'https://translate.yandex.net/api/v1.5/tr.json/translate' );

        $curl->post( $endpoint );
        $response = $curl->getResponse();
        if ( $curl->isError() || empty( $response->text[0] ) ) {
            $failed = true;
            wpcp_logger()->error( sprintf( 'Yandex translation error [%s] translation skipping.', $response->message ) );
            break;
        }
        $translated_strings .= $response->text[0];
    }

    return $failed ? $content : force_balance_tags( $translated_strings );
}

/**
 * @param $content
 * @param $to
 *
 * @return string
 * @since 1.1.5
 */
function wpcp_pro_deepl_translate( $content, $to ) {
    $api = wpcp_get_settings( 'deepl_translate_api_key', 'wpcp_settings_misc' );
    if ( empty( $api ) ) {
        wpcp_logger()->error( __( 'deepL API is not set skipping translation', 'wp-content-pilot-pro' ) );
        $settings_url = admin_url( 'edit.php?post_type=wp_content_pilot&page=wpcp-settings' );
        $message      = __( sprintf( 'WP Content Pilot will skip translation as deepL API is not set, please set it from <a href="%s">here</a>.', $settings_url ), 'wp-content-pilot-pro' );
        wpcp_admin_notice( $message );

        return $content;
    }
    $failed             = false;
    $translated_strings = '';
    $parts              = str_split( $content, 3500 );

    $curl = new Curl\Curl();
    $curl->setOpt( CURLOPT_FOLLOWLOCATION, true );
    $curl->setOpt( CURLOPT_TIMEOUT, 30 );
    $curl->setOpt( CURLOPT_RETURNTRANSFER, true );

    foreach ( $parts as $part ) {
        $endpoint = add_query_arg( [
            'text'         => urlencode( $part ),
            'auth_key'     => $api,
            'target_lang'  => strtoupper( $to ),
            'tag_handling' => 'xml',
        ], 'https://api.deepl.com/v2/translate' );

        $curl->post( $endpoint );
        $response = $curl->getResponse();
        if ( $curl->isError() || empty( $response->translations[0]->text ) ) {
            $failed = true;
            wpcp_logger()->error( sprintf( 'DeepL translation error [%s] translation skipping.', $response->message ) );
            break;
        }
        $translated_strings .= $response->translations[0]->text;
    }

    return $failed ? $content : force_balance_tags( $translated_strings );
}
