<?php

namespace EDPW\Core\Controllers\Admin;

use EDPW\Core\Helpers\EmailTemplate;
use EDPW\Core\Helpers\Input;
use EDPW\Core\Helpers\Util;
use EDPW\Core\Helpers\WC;

defined( 'ABSPATH' ) || exit;

class Common {

	/**
	 * Initialize admin menu.
	 *
	 * @return void
	 */
	public static function addMenu() {
		if ( WC::hasAdminPrivilege() ) {
			$plugin_name = EmailTemplate::isPro() ? __( 'Spark Email Editor Pro', 'email-editor-plus' ) : __( 'Spark Email Editor', 'email-editor-plus' );
			add_menu_page( $plugin_name, $plugin_name, 'manage_options', EDPW_PLUGIN_SLUG, [
				Common::class,
				'managePages'
			], 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzAiIGhlaWdodD0iMzAiIHZpZXdCb3g9IjAgMCAzMCAzMCIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZmlsbC1ydWxlPSJldmVub2RkIiBjbGlwLXJ1bGU9ImV2ZW5vZGQiIGQ9Ik0yMC41IDEuNUg3LjE2NjY3QzMuMTY2NjcgMS41IDAuNSAzLjUgMC41IDguMTY2NjdWMTcuNUMwLjUgMjIuMTY2NyAzLjE2NjY3IDI0LjE2NjcgNy4xNjY2NyAyNC4xNjY3SDE0LjE2NUMxNC4xNjM4IDI0LjA1NDYgMTQuMTYzMyAyMy45MzY0IDE0LjE2MzMgMjMuODExMlYxOC44MzRDMTQuMTYzMyAxNi43ODk5IDE0LjI3NjcgMTYuNTk3NSAxNi4yMzI3IDE1LjMxNDJMMjEuMTY1MyAxMi41NTUyQzIyLjEwMDcgMTIuMDMyNyAyMy41NjU0IDEyLjAzMjcgMjQuNTAwOSAxMi41NTUyTDI3LjE2NjcgMTQuMDUwN1Y4LjE2NjY3QzI3LjE2NjcgMy41IDI0LjUgMS41IDIwLjUgMS41Wk0yMS4xMjY3IDkuNjJMMTYuOTUzMyAxMi45NTMzQzE2LjA3MzMgMTMuNjYgMTQu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yNy44NzI1IDE2LjYxMjNaTTI1LjU2MzMgMjAuNzNMMjUuMDcgMjEuMjFMMjIuMTYzMyAyNC4xM0MyMi4wMyAyNC4yNSAyMS43NzY3IDI0LjM4MzMgMjEuNTkgMjQuNDFMMjAuMjgzMyAyNC41OTY3QzE5LjgxNjcgMjQuNjYzMyAxOS40ODMzIDI0LjMzIDE5LjU1IDIzLjg2MzNMMTkuNzM2NyAyMi41NTY3QzE5Ljc3NDYgMjIuMzQzMSAxOS44NzE2IDIyLjE0NDUgMjAuMDE2NyAyMS45ODMzTDIyLjkyMzMgMTkuMDlMMjMuNDAzMyAxOC41OTY3QzIzLjcyMzMgMTguMjc2NyAyNC4wODMzIDE4LjA2MzMgMjQuNDU2NyAxOC4wNjMzQzI0Ljc3NjcgMTguMDYzMyAyNS4xMzY3IDE4LjIxIDI1LjUzNjcgMTguNTk2N0MyNi40MyAxOS40NjMzIDI2LjE1IDIwLjE0MzMgMjUuNTYzMyAyMC43M1oiIGZpbGw9ImJsYWNrIi8+Cjwvc3ZnPgo=', 56 );
		}
	}

	/**
	 * Set menu icon style.
	 *
	 * @return void
	 */
	public static function setMenuIconStyle() {
		?>
        <style>
            #toplevel_page_email-editor-plus-pro .wp-menu-image.svg {
                background-position: bottom !important;
            }
        </style>
		<?php
	}

	/**
	 * Admin main page.
	 *
	 * @return void
	 */
	public static function managePages() {
		if ( Input::get( 'page' ) != EDPW_PLUGIN_SLUG ) {
			return;
		}
		echo Util::renderTemplate( EDPW_PLUGIN_PATH . 'Core/Views/Admin/main.php' );
	}

	/**
	 * Add admin script.
	 *
	 * @return void
	 */
	public static function adminScripts() {
		if ( Input::get( 'page' ) != EDPW_PLUGIN_SLUG ) {
			return;
		}
		//wp_enqueue_media();
		wp_register_style( EDPW_PLUGIN_PREFIX . '-main-css', EDPW_PLUGIN_URL . 'Assets/Admin/Css/dist/main-edpw-style.css', [], EDPW_PLUGIN_VERSION, 'screen' );
		wp_enqueue_style( EDPW_PLUGIN_PREFIX . '-main-css' );
		wp_register_style( EDPW_PLUGIN_PREFIX . '-font', EDPW_PLUGIN_URL . 'Assets/Admin/Css/font.css', [], EDPW_PLUGIN_VERSION, 'screen' );
		wp_enqueue_style( EDPW_PLUGIN_PREFIX . '-font' );
		// remove admin notice
		remove_all_actions( 'admin_notices' );
		$common_path   = EDPW_PLUGIN_PATH . 'Assets/Admin/Js/dist';
		$js_files      = Util::getDirFileLists( $common_path );
		$localize_name = '';
		foreach ( $js_files as $file ) {
			$path         = str_replace( EDPW_PLUGIN_PATH, '', $file );
			$js_file_name = str_replace( $common_path . '/', '', $file );
			if ( $js_file_name == 'main.bundle.js' ) {
				$js_name       = EDPW_PLUGIN_SLUG . '-react-ui-' . substr( $js_file_name, 0, - 3 );
				$localize_name = $js_name;
				$js_file_url   = EDPW_PLUGIN_URL . $path;
				wp_register_script( $js_name, $js_file_url, [], EDPW_PLUGIN_VERSION . '&t=' . time(), true );
				wp_enqueue_script( $js_name );
			}
		}
		$localize = [
			'home_url'   => get_home_url(),
			'admin_url'  => admin_url( 'admin-ajax.php' ),
			'plugin_url' => EDPW_PLUGIN_URL,
			'ajax_url'   => admin_url( 'admin-ajax.php' ),
			'nonce'      => [
				'list_page' => wp_create_nonce( 'edpw_list_page' )
			]
		];

		wp_localize_script( $localize_name, 'edpw_localize_data', $localize );
	}

	/**
	 * Plugin page links.
	 *
	 * @param array $links Plugin links.
	 *
	 * @return array
	 */
	public static function pluginActionLinks( array $links ): array {
		$action_links = [];

		return (array) apply_filters( 'edpw_action_links', array_merge( $action_links, $links ) );
	}

	/**
	 * Load text domain.
	 *
	 * @return void
	 */
	public static function loadTextDomain() {
		load_plugin_textdomain( 'email-editor-plus', false, EDPW_PLUGIN_PATH . 'languages/' );
	}

	public static function getRecommendationList() {
		if ( ! WC::isSecurityValid( 'edpw-addon-nonce' ) ) {
			wp_send_json_error( [ 'message' => esc_html__( 'Basic check failed', 'email-editor-plus' ) ] );
		}
		$addons = get_transient( 'edpw_remote_recommendation_list' );

		if ( empty( $addons ) ) {
			$addons   = [];
			$response = wp_remote_get( 'https://static.flycart.net/recommendation/product/spark-email-editor.json' );
			if ( ! is_wp_error( $response ) ) {
				$response = (array) json_decode( wp_remote_retrieve_body( $response ), true );
				if ( ! empty( $response ) ) {
					$domain = $_SERVER['SERVER_NAME'];
					foreach ( $response as $addon ) {
						if ( ! empty( $addon['plugin_url'] ) ) {
							$addon['plugin_url'] = str_replace( '{site-name}', $domain, $addon['plugin_url'] );
						}
						$addons[] = $addon;
					}
					set_transient( 'edpw_remote_recommendation_list', $addons, 24 * 60 * 60 );
				}
			}
		}
		wp_send_json_success( [ 'items' => $addons ] );
	}
}