<?php

namespace EasyContentManager\Classes;

use EasyContentManager\Admin\Menu;
use EasyContentManager\Admin\SeLicense;
use EasyContentManager\Utils\Helper;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

class ScriptBase {


	protected function get_scripts_data(): array {
		global $ecm_settings;
		$menu                    = new Menu();
		$current_user            = wp_get_current_user();
		$content_manager_globals = array(
			'nonce'                  => wp_create_nonce( 'wp_rest' ),
			'ecm_nonce'              => wp_create_nonce( 'easy_content_manager_nonce' ),
			'ecm_app_slug'           => Helper::get_ecm_app_slug(),
			'rest_url'               => esc_url_raw( rest_url() ),
			'namespace'              => EASY_CONTENT_MANAGER_PLUGIN_SLUG . '/v1/',
			'plugin_root_url'        => EASY_CONTENT_MANAGER_PLUGIN_ROOT_URI,
			'plugin_root_path'       => EASY_CONTENT_MANAGER_ROOT_DIR_PATH,
			'ajaxurl'                => esc_url( admin_url( 'admin-ajax.php' ) ),
			'admin_url'              => admin_url(),
			'site_url'               => site_url(),
			'route_path'             => wp_parse_url( admin_url(), PHP_URL_PATH ),
			'is_plain_permalink'     => $this->is_plain_permalink(),
			'menu'                   => wp_json_encode( $this->modify_menu_post_type() ),
			'current_user_id'        => get_current_user_id(),
			'current_user_roles'     => (array) $current_user->roles,
			'is_rtl'                 => is_rtl(),
			'is_admin'               => is_admin(),
			'current_user_can'       => [
				'manage_options' => current_user_can( 'manage_options' ),
			],
			'toplevel_menu_icon_url' => $menu->get_toplevel_menu_icon_url(),
			'toplevel_menu_title'    => $menu->get_toplevel_menu_title(),
			'is_premium'             => SeLicense::is_pro(),
			'logo_url'               => $menu->get_logo_url(),
			'ecm_settings'           => Helper::get_ecm_settings(),
			'field_groups'           => Helper::get_all_custom_field_groups(),
			'post_types'             => Helper::get_all_custom_post_types_with_modified_data(),
			'taxonomies'             => Helper::get_all_custom_taxonomies_with_modified_data(),
			'ecm_settings_slugs'     => Helper::get_ecm_settings_formatted_global_data(),
			'color_theme_mode'       => $ecm_settings->color_theme_mode,
		);

		return $content_manager_globals;
	}

	protected function get_frontend_scripts_data(): array {
		$content_manager_globals = array(
			'nonce'                        => wp_create_nonce( 'wp_rest' ),
			'ecm_nonce'                    => wp_create_nonce( 'easy_content_manager_nonce' ),
			'rest_url'                     => esc_url_raw( rest_url() ),
			'namespace'                    => EASY_CONTENT_MANAGER_PLUGIN_SLUG . '/v1/',
			'ajaxurl'                      => esc_url( admin_url( 'admin-ajax.php' ) ),
			'site_url'                     => site_url(),
			'current_user_id'              => get_current_user_id(),
			'current_post_id'              => get_the_ID(),
			'addons'                       => json_decode( get_option( EASY_CONTENT_MANAGER_ADDONS_SETTINGS_NAME, '{}' ) ),
			'upvote_settings'              => Helper::get_settings( 'upvote_addon', [] ),
			'frontend_submission_settings' => Helper::get_settings( 'frontend_submission_addon', [] ),
			'svg_icons'                    => Helper::get_localize_able_svg_icon(),
			'users_can_register'           => get_option( 'users_can_register' ),
			'is_admin'                     => current_user_can( 'manage_options' ),
			'post_types'                   => Helper::get_all_custom_post_types_with_modified_data(),
			'field_groups'                 => Helper::get_all_custom_field_groups(),
			'current_user_roles'           => wp_get_current_user()->roles,
		);

		return apply_filters(
			'easy_content_manager/assets/frontend_scripts_data',
			$content_manager_globals
		);
	}

	public function is_plain_permalink(): bool {
		$permalink_structure = get_option( 'permalink_structure' );
		if ( empty( $permalink_structure ) ) {
			return true;
		}
		return false;
	}

	private function modify_menu_post_type() {
		$menus = Helper::get_admin_menu_list();
		foreach ( $menus as $key => $menu ) {
			$menus[ $key ]['slug'] = Helper::create_slugs( $menu['title'] );
		}
		return $menus;
	}

	public function has_ecm_shortcode() {
		global $wp_query;
		
		if ( ! $wp_query ) {
			return false;
		}
		
		if ( ! empty( $wp_query->posts ) ) {
			foreach ( $wp_query->posts as $post_obj ) {
				if ( $this->check_if_shortcode_exists( $post_obj->post_content ) ) {
					return true;
				}
			}
		}
		
		if ( $this->check_widgets_for_shortcode() ) {
			return true;
		}
		
		return false;
	}

	private function check_widgets_for_shortcode() {
		global $wp_registered_sidebars;
		
		foreach ( array_keys( $wp_registered_sidebars ) as $sidebar_id ) {
			if ( is_active_sidebar( $sidebar_id ) ) {
				$sidebars_widgets = wp_get_sidebars_widgets();
				if ( ! empty( $sidebars_widgets[ $sidebar_id ] ) ) {
					foreach ( $sidebars_widgets[ $sidebar_id ] as $widget_id ) {
						// Check text widgets, custom HTML, etc.
						if ( strpos( $widget_id, 'text-' ) === 0 || strpos( $widget_id, 'custom_html-' ) === 0 ) {
							$widget_data = get_option( 'widget_' . substr( $widget_id, 0, strrpos( $widget_id, '-' ) ) );
							$widget_number = substr( $widget_id, strrpos( $widget_id, '-' ) + 1 );
							if ( isset( $widget_data[ $widget_number ]['content'] ) ) {
								if ( $this->check_if_shortcode_exists( $widget_data[ $widget_number ]['content'] ) ) {
									return true;
								}
							}
						}
					}
				}
			}
		}
		return false;
	}

	public function check_if_shortcode_exists( $content ) {
        if ( $content && (
                has_shortcode( $content, 'ecm_bookmark_button' ) ||
                has_shortcode( $content, 'ecm_bookmark_badge' ) ||
                has_shortcode( $content, 'ecm_bookmark_list' ) ||
                has_shortcode( $content, 'ecm_claim_button' ) ||
                has_shortcode( $content, 'ecm_claim_badge' ) ||
                has_shortcode( $content, 'ecm_reaction' ) ||
                has_shortcode( $content, 'ecm_review_form' ) ||
                has_shortcode( $content, 'ecm_review_list' ) ||
                has_shortcode( $content, 'ecm_upvote' ) ||
                has_shortcode( $content, 'ecm_view_counter' ) ||
                has_shortcode( $content, 'ecm_frontend_submission' )
            )
        ) {
            return true;
        }
        return false;
    }
}
