<?php
/**
 * FluentC WordPress Language File.
 *
 * This file contains the functions for managing Widget
 *
 * @category Translation
 * @package  FluentCPlugin
 * @author   FluentC <mvp@fluentc.io>
 * @license  http://www.gnu.org/copyleft/gpl.html GNU General Public License
 * @link     https://www.fluentc.ai
 */

namespace FluentC\Services;

use FluentC\Services\Connect;
use FluentC\Utils\Language;

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

/**
 * Inserting of FluentC JavaScript
 */
class Widget {

	/**
	 * FluentC connection class
	 *
	 * @var object
	 */
	protected $fluentc_connect;
	/**
	 * FluentC language class
	 *
	 * @var object
	 */
	protected $fluentc_language;
	/**
	 * Constructor.
	 *
	 * @since 1.2
	 */
	public function __construct() {

		$this->fluentc_connect  = new Connect();
		$this->fluentc_language = new Language();
	}

	/**
	 *  Insert FluentC Div
	 *
	 * @since 1.2
	 */
	public function insert_fluentc_div() {

		?><div id="fluentc-widget"></div>
		<?php
	}
	/**
	 * Inserts the FluentC JS
	 *
	 * @return wp_enqueue_script
	 */
	public function insert_fluentc_js() {

		return wp_enqueue_script( 'fluentc-js', 'https://widget.fluentc.io/fluentcWidgetWordpress.min.js', false );
	}

	/**
	 * Inserts the FluentC web widget code into the header of the site and the "<div id="fluentc-widget"></div>" in the top of the content body
	 *
	 * @param bool   $default_lang Is the default language enabled.
	 * @param string|null $lang Language Variable.
	 * @return wp_add_inline_script
	 */
	public function insert_fluentc_widget( $default_lang, $lang = null ) {

		$widgetapikey  = get_option( 'fluentc_api_key' );
		if(!$widgetapikey){
			return;
		}
		$languages     = $this->fluentc_connect->get_display_language_list( $widgetapikey );
		if(!$languages){
			return;
		}
		$widget        = $this->fluentc_connect->fetch_widget_options( $widgetapikey );
		if(!$widget  ){
			return;
		}
		$widget->data->fetchSiteOptions->appearance;
		$apperance     = $widget->data->fetchSiteOptions->appearance;
		$site_language = $this->fluentc_language->fluentc_site_language();
		if ( ! $apperance ) {
			$apperance = 'float';
		}
		$language_string = '';
		foreach ( $languages as $language ) {
			$language_string .= '{ code: "' . $language[1] . '", name: "' . $language[0] . '" },';
		}

		if ( $widgetapikey ) {
			if ( false === $default_lang ) {
				$init_lang = '';
			} else {
				$init_lang = '';
			}

				$header_code  = '    document.addEventListener("DOMContentLoaded", function () {' . "\n";
				$header_code .= '        f = new fluentcWidgetWordpress({
					defaultLanguage: \'' . $site_language . '\',
					display: "' . $apperance . '",
					languages: [' . $language_string . ']
					' . $init_lang . ',
				  });' . "\n";
				$header_code .= '        f.setupWidget(\'fluentc-widget\' );' . "\n";

        if ($lang !== null) {
            $header_code .= '        // Add language to AJAX requests' . "\n";
            $header_code .= '        (function() {' . "\n";
            $header_code .= '            var ajaxOpen = XMLHttpRequest.prototype.open;' . "\n";
            $header_code .= '            XMLHttpRequest.prototype.open = function() {' . "\n";
            $header_code .= '                var method = arguments[0];' . "\n";
            $header_code .= '                var url = arguments[1];' . "\n";
            $header_code .= '                if (typeof url === "string" && url.indexOf("admin-ajax.php") !== -1) {' . "\n";
            $header_code .= '                    url += (url.indexOf("?") !== -1 ? "&" : "?") + "fluentc_language=' . $lang . '";' . "\n";
            $header_code .= '                    arguments[1] = url;' . "\n";
            $header_code .= '                }' . "\n";
            $header_code .= '                return ajaxOpen.apply(this, arguments);' . "\n";
            $header_code .= '            };' . "\n";
            $header_code .= '        })();' . "\n";
        }
				$header_code .= '    });';

				return wp_add_inline_script( 'fluentc-js', $header_code );
		} else {
			return null;

		}
	}
}
