<?php

namespace Plover\Kit\Services;

use Plover\Core\Assets\Scripts;
use Plover\Core\Assets\Styles;
use Plover\Core\Framework\ServiceProvider;
use Plover\Kit\Toolkits\Theme;

/**
 * @since 1.0.0
 */
class DashboardServiceProvider extends ServiceProvider {

	/**
	 * Bootstrap this service.
	 *
	 * @param Styles $styles
	 * @param Scripts $scripts
	 *
	 * @return void
	 */
	public function boot( Styles $styles, Scripts $scripts ) {
		add_action( 'admin_menu', [ $this, 'add_admin_menu' ] );
		add_filter( 'plover_core_dashboard_data', [ $this, 'dashboard_data' ] );
		add_filter( 'plover_core_should_localize_dashboard_data', [ $this, 'is_dashboard_screen' ] );
		add_action( 'admin_action_install_plover_theme', [ $this, 'install_plover_theme' ] );
		add_filter( 'themes_api_result', [ $this, 'add_custom_themes_to_popular' ], 10, 3 );

		$styles->enqueue_dashboard_asset( 'plover-dashboard-style', array(
			'src'       => plover_kit()->app_url( 'assets/js/dashboard/style.min.css' ),
			'path'      => plover_kit()->app_path( 'assets/js/dashboard/style.min.css' ),
			'ver'       => $this->core->is_debug() ? time() : PLOVER_KIT_VERSION,
			'rtl'       => 'replace',
			'condition' => [ $this, 'is_dashboard_screen' ],
		) );

		$styles->enqueue_dashboard_asset( 'plover-admin-style', array(
			'src'  => plover_kit()->app_url( 'assets/css/admin.css' ),
			'path' => plover_kit()->app_path( 'assets/css/admin.css' ),
			'ver'  => $this->core->is_debug() ? time() : PLOVER_KIT_VERSION,
		) );

		$scripts->enqueue_dashboard_asset( 'plover-dashboard-script', array(
			'src'         => plover_kit()->app_url( 'assets/js/dashboard/index.min.js' ),
			'path'        => plover_kit()->app_path( 'assets/js/dashboard/index.min.js' ),
			'asset'       => plover_kit()->app_path( 'assets/js/dashboard/index.min.asset.php' ),
			'ver'         => $this->core->is_debug() ? time() : PLOVER_KIT_VERSION,
			'condition'   => [ $this, 'is_dashboard_screen' ],
			'deps'        => [ 'plover-dashboard-data' ],
			'translation' => 'plover-kit',
		) );
	}

	public function add_admin_menu() {
		add_menu_page(
			__( 'Plover Kit', 'plover-kit' ),
			__( 'Plover Kit', 'plover-kit' ),
			'manage_options',
			'plover-kit',
			'',
			plover_kit()->app_url( 'assets/images/plover-menu-logo.svg' ),
			'58.7'
		);

		add_submenu_page(
			'plover-kit',
			__( 'Plover Kit Modules', 'plover-kit' ),
			__( 'Modules', 'plover-kit' ),
			'manage_options',
			'plover-kit',
			array( $this, 'show_admin_menu' )
		);
	}

	public function is_dashboard_screen() {
		if ( ! function_exists( 'get_current_screen' ) ) {
			return false;
		}

		$screen = get_current_screen();

		return $screen && $screen->base === 'toplevel_page_plover-kit';
	}

	public function dashboard_data( $data ) {
		$data['root']              = 'plover-kit-dashboard-page';
		$data['expert_page']       = 'https://wpplover.com/expert/';
		$data['affiliation']       = esc_url( admin_url( 'admin.php?page=plover-kit-affiliation' ) );
		$data['registered_blocks'] = $this->get_registered_blocks();

		if ( get_template() !== 'plover' ) {
			$data['theme'] = [
				'install'     => add_query_arg( array(
					'action'   => 'install_plover_theme',
					'theme'    => 'plover',
					'_wpnonce' => wp_create_nonce( 'install_plover_theme' )
				), admin_url( 'admin.php' ) ),
				'homepage'    => 'https://wpplover.com/themes/plover/',
				'playground'  => 'https://tastewp.com/template/ploverKit',
				'name'        => __( 'Plover Theme', 'plover-kit' ),
				'description' => __( 'Looking for a WordPress theme? Plover Theme works perfectly with Plover Kit. As a Full Site Editing theme, Plover utilizes the WordPress block editor and the extensions provided by the Plover Kit to create unique and eye-catching web design.', 'plover-kit' ),
				'screenshot'  => plover_kit()->app_url( 'assets/images/plover-screenshot.png' ),
			];
		}

		return $data;
	}

	public function show_admin_menu() {
		?>
        <div id="plover-kit-dashboard-page" class="wrap plover-kit-dashboard-page"></div>
		<?php
	}

	/**
	 * Add custom themes to popular page
	 *
	 * @param $themes
	 *
	 * @return void
	 */
	public function add_custom_themes_to_popular( $themes, $action, $args ) {
		// Ignore internal themes api call
		if ( $args->request === 'plover' ) {
			return $themes;
		}
		// Only add custom themes in popular fist page ajax request
		if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
			return $themes;
		}
		if ( absint( $_POST['request']['page'] ) > 0 || empty( $_POST['request']['browse'] ) || 'popular' !== $_POST['request']['browse'] ) {
			return $themes;
		}

		$recommended_themes = $this->recommended_themes();
		$themes->themes     = array_merge(
			$recommended_themes,
			$themes->themes,
		);

		return $themes;
	}

	/**
	 * Generate recommended themes list
	 *
	 * @return array
	 */
	protected function recommended_themes() {
		$cache_key = 'recommended_plover_themes';
		$cached    = get_transient( $cache_key );
		if ( false !== $cached ) {
			return $cached;
		}

		$max_display   = 6;
		$plover_themes = array_filter( Theme::themes_by_author( 'plover' ), function ( $theme ) {
			return $theme->slug !== 'plover';
		} );

		if ( empty( $plover_themes ) ) {
			return array();
		}

		$plover_theme = Theme::meta( 'plover' );
		$latest       = array_splice( $plover_themes, 0, 3 );
		if ( $plover_theme ) {
			array_unshift( $latest, $plover_theme );
		}

		mt_srand( crc32( date( 'Y-m-d' ) ) );
		shuffle( $plover_themes );
		$plover_themes = array_merge( $latest, array_slice( $plover_themes, 0, $max_display ) );
		mt_srand();

		set_transient( $cache_key, $plover_themes, 24 * HOUR_IN_SECONDS );

		return $plover_themes;
	}

	/**
	 * Install plover theme
	 *
	 * @return void
	 */
	public function install_plover_theme() {
		check_ajax_referer( 'install_plover_theme' );

		$theme_slug     = $_GET['theme'] ?? '';
		$allowed_themes = apply_filters( 'plover-kit/allowed_theme_slugs', [ 'plover' ] );

		if ( ! in_array( $theme_slug, $allowed_themes ) ) {
			wp_die(
				'<h1>' . __( 'Sorry, you are not allowed to install this theme.', 'plover-kit' ) . '</h1>',
				403
			);
		}

		Theme::install( $theme_slug, true );
	}

	/**
	 * Get all registered block types
	 *
	 * @return array|array[]
	 */
	protected function get_registered_blocks() {
		$block_types       = \WP_Block_Type_Registry::get_instance()->get_all_registered();
		$registered_blocks = array();

		foreach ( $block_types as $name => $block ) {
			$registered_blocks[] = array(
				'name'     => $name,
				'title'    => $block->title ?? $name,
				'category' => $block->category,
			);
		}

		return $registered_blocks;
	}
}
