<?php
namespace EcafeAddons;

use Elementor\Utils;
use Elementor\Controls_Stack;

if ( ! defined( 'WPINC' ) ) {
	die;
}
if ( ! class_exists( 'Ec_Live_Copy_Paste' ) ) {
	class Ec_Live_Copy_Paste {
		private static $instance = null;
		public function init() {
			add_action( 'wp_ajax_ec_live_copy_nonce', array( $this, 'Ec_Live_Copy_Media' ) );
		}
		public static function Ec_Live_Copy_Media() {

			check_ajax_referer( 'ec_live_copy_nonce', 'nonce' );

			if ( ! current_user_can( 'edit_posts' ) ) {
				wp_send_json_error( __( 'Invalid', 'essential-classy-addons-for-elementor' ), 403 );
			}

			$media = isset( $_POST['copy_content'] ) ? wp_unslash( $_POST['copy_content'] ) : '';

			if ( empty( $media ) ) {
				wp_send_json_error( __( 'Empty', 'essential-classy-addons-for-elementor' ) );
			}

			$media = array( json_decode( $media, true ) );
			$media = self::ec_id_replace( $media );
			$media = self::ec_media_download( $media );

			wp_send_json_success( $media );
		}
		protected static function ec_id_replace( $media ) {

			return \Elementor\Plugin::instance()->db->iterate_data(
				$media,
				function ( $elements ) {
					$elements['id'] = Utils::generate_random_string();
					return $elements;
				}
			);
		}
		protected static function ec_media_download( $media ) {

			return \Elementor\Plugin::instance()->db->iterate_data(
				$media,
				function ( $element ) {
					$elements = \Elementor\Plugin::instance()->elements_manager->create_element_instance( $element );

					if ( ! $elements ) {
						return null;
					}

					return self::ec_download_content_put( $elements );
				}
			);
		}
		protected static function ec_download_content_put( Controls_Stack $element ) {
			$data     = $element->get_data();
			$dataload = 'on_import';

			if ( method_exists( $element, $dataload ) ) {
				$data = $element->{$dataload}( $data );
			}

			foreach ( $element->get_controls() as $get ) {
				$type = \Elementor\Plugin::instance()->controls_manager->get_control( $get['type'] );
				$name = $get['name'];

				if ( ! $type ) {
					return $data;
				}

				if ( method_exists( $type, $dataload ) ) {
					$data['settings'][ $name ] = $type->{$dataload}( $element->get_settings( $name ), $get );
				}
			}

			return $data;
		}
		public static function Ec_Get_Instance( $shortcodes = array() ) {

			if ( null == self::$instance ) {
				self::$instance = new self( $shortcodes );
			}
			return self::$instance;
		}
	}
}

if ( ! class_exists( 'Ec_Live_Copy' ) ) {
	class Ec_Live_Copy {
		private static $instance = null;
		public function init() {
			$ecafe_extension = get_option( 'ecafe_extension' );
			if ( ! empty( $ecafe_extension ) && isset( $ecafe_extension['extrasList'] ) && ! empty( $ecafe_extension['extrasList'] ) ) {
				if ( in_array( 'ecafe-live-copy', $ecafe_extension['extrasList'] ) ) {
					add_action( 'elementor/editor/before_enqueue_scripts', array( $this, 'Ec_Live_Copy_Enqueue_Script' ), 10 );
					function Ec_Live_Copy_Paste_Func() {
						return Ec_Live_Copy_Paste::Ec_Get_Instance();
					}
					Ec_Live_Copy_Paste_Func()->init();
				}
			}
		}
		public function Ec_Live_Copy_Enqueue_Script() {
			wp_enqueue_script( 'ec-xdstorage', ECAFE_ASSETS_PATH . 'js/livecopy/xdlocalstorage.js', null, ECAFE_VERSION, true );
			wp_enqueue_script( 'ec-live-copy', ECAFE_ASSETS_PATH . 'js/livecopy/ec-live-copy.js', array( 'jquery', 'elementor-editor', 'ec-xdstorage' ), ECAFE_VERSION, true );
			wp_localize_script(
				'jquery',
				'ec_live_copy',
				array(
					'ajax_url' => admin_url( 'admin-ajax.php' ),
					'nonce'    => wp_create_nonce( 'ec_live_copy_nonce' ),
				)
			);
		}

		public static function Ec_Get_Instance( $shortcodes = array() ) {

			if ( null == self::$instance ) {
				self::$instance = new self( $shortcodes );
			}
			return self::$instance;
		}
	}
}

function Ec_Live_Copy_Func() {
	return Ec_Live_Copy::Ec_Get_Instance();
}
Ec_Live_Copy_Func()->init();
