<?php
/**
* @package CembraPay Payment Gateway for WooCommerce
* @author  Adrian Emil Tudorache
* @license GPL-2.0+
* @link    https://www.tudorache.me/
**/

namespace CembraPayWoo;

if ( ! defined( 'ABSPATH' ) ) {
    header( 'Status: 403 Forbidden' );
	header( 'HTTP/1.1 403 Forbidden' );
    exit;
}

class Translations {

    function __construct() {
        add_action( 'admin_enqueue_scripts', array($this, 'scripts'), 200 );

        $ajax_actions = array(
			'get_field_translations',
			'update_field_translations'
		);

		foreach( $ajax_actions as $action ) {
			add_action( 'wp_ajax_' . $action, array($this, $action) );
			add_action( 'wp_ajax_nopriv_' . $action, array($this, $action) );
		}

    }

    function scripts() {

        $page    = isset($_GET['page']) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
        $tab     = isset($_GET['tab']) ? sanitize_text_field( wp_unslash( $_GET['tab'] ) ) : '';
        $section = isset($_GET['section']) ? sanitize_text_field( wp_unslash( $_GET['section'] ) ) : '';

        if(
			$page && $page == 'wc-settings'
            && $tab && $tab == 'checkout'
            && $section && $section == 'cembrapay'
		) {

            include( CEMBRAPATH . 'inc/data/strings.php' );

            wp_enqueue_style('cembrapay-admin-tr', CEMBRAURL . 'assets/css/admin/translations-css.css');
            wp_enqueue_style('cembrapay-admin-tr-js', CEMBRAURL . 'assets/css/admin/translations-js.css');
			wp_enqueue_script('cembrapay-admin-tr', CEMBRAURL . 'assets/build/admin/translations.js', array('jquery'), '', true);
			wp_localize_script('cembrapay-admin-tr', 'cembrapayTranslations', array(
				'ajaxUrl'     => admin_url('admin-ajax.php'),
				'nonceGet'    => wp_create_nonce('get_field_translations'),
				'nonceUpdate' => wp_create_nonce('update_field_translations'),
				'pluginUrl'   => CEMBRAURL,
				'strings'     => $translations_strings
			));

        }

    }

    function get_field_translations() {

		if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( $_POST['security'], 'get_field_translations' ) ) {
			wp_send_json_error( array('message' => esc_html__( 'Security verification failed', 'cembrapay' ) ) );
		}

		$field = isset($_POST['field']) ? sanitize_text_field( wp_unslash( $_POST['field'] ) ) : '';

		if( ! $field ) {
			wp_send_json_error( array('message' => esc_html__( 'Field is required', 'cembrapay' )) );
		}

		$translations = get_option('cembrapay_field_translations', array());
		if( $translations && isset($translations[$field]) ) {
			wp_send_json_success( $translations[$field] );
		}

		wp_send_json_error( array('message' => esc_html__( 'No translations found', 'cembrapay' )) );

    }

	function update_field_translations() {

		if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( $_POST['security'], 'update_field_translations' ) ) {
			wp_send_json_error( array('message' => esc_html__( 'Security verification failed', 'cembrapay' ) ) );
		}

		$field = isset($_POST['field']) ? sanitize_text_field( wp_unslash( $_POST['field'] ) ) : '';

		if( ! $field ) {
			wp_send_json_error( array('message' => 'The field is required') );
		}

		$field_translations = isset($_POST['translations']) ? $_POST['translations'] : '';
		$field_translations = array_map( function( $value ) {

			$value = array(
				'lang' => sanitize_text_field( wp_unslash( $value['lang'] ) ),
				'value' => sanitize_text_field( wp_unslash( $value['value'] ) )
			);

			return $value;

		}, $field_translations );

		if( ! $field_translations ) {
			wp_send_json_error( array('message' => esc_html__( 'Translations are required', 'cembrapay' )) );
		}

		$translations = get_option('cembrapay_field_translations', array());

		$save_translations = array();
		foreach( $field_translations as $translation ) {
			$save_translations[$translation['lang']] = $translation['value'];
		}

		$translations[$field] = $save_translations;

		$update = update_option('cembrapay_field_translations', $translations);

		if( $update ) {
			wp_send_json_success( array('message' => esc_html__( 'Translations updated', 'cembrapay' )) );
		}

		wp_send_json_error( array('message' => esc_html__( 'Failed to update translations either because it already exists and it has not been modified or because an unexpected error has occured', 'cembrapay' )) );

	}

}
?>