<?php
/**
 * The admin-dashboard of the plugin.
 *
 * @link       https://personalizewp.com
 * @since      1.0.0
 *
 * @package    PersonalizeWP
 * @subpackage PersonalizeWP/admin
 */

namespace PersonalizeWP\Admin;

use PersonalizeWP\Admin\Admin;
use PersonalizeWP\Admin\Admin_Base_Page;
use PersonalizeWP\Rule_Conditions\Users_Role;

defined( 'ABSPATH' ) || exit; // Exit if accessed directly.

/**
 * Class Dashboard
 */
class PageDashboard extends Admin_Base_Page {

	/**
	 * Slug for Dashboard admin page
	 */
	const SLUG = 'personalizewp';

	/**
	 * URL for main Dashboard admin page
	 *
	 * @since 3.2.0
	 *
	 * @var string
	 */
	public static $index_url = 'admin.php?page=' . self::SLUG;

	/**
	 * Constructor
	 */
	protected function __construct() {
		parent::__construct();
		$this->page_title = __( 'Dashboard', 'personalizewp' );
	}

	/**
	 * Route to the correct action within this page
	 */
	public function route() {
		Users_Role::setComparisonValues();

		$this->index_screen();
	}


	/**
	 * Display onboarded message, only after activation.
	 *
	 * @since 3.2.0
	 *
	 * @return void
	 */
	protected function maybe_display_onboarded() {
		// Display once onboarding wizard complete
		$has_completed_onboarding = isset( $_GET['onboarding_complete'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Used as a flag; data not used.
		if ( ! $has_completed_onboarding ) {
			return;
		}
		wp_admin_notice(
			sprintf(
				'<span class="dashicons dashicons-yes-alt"></span><p><strong>%1$s</strong></p><p>%2$s</p>',
				esc_html__( 'You’re all done!', 'personalizewp' ),
				wp_kses_post(
					sprintf(
						/* translators: 1: %s expands to a website link to https://help.personalizewp.com/, 2: </a> closing tag, 3: %s expands to a website link to https://personalizewp.com/contact/, 4: </a> closing tag. */
						__( 'Thanks, you’re now able to use the features below. If you get stuck or need help, please read our %1$shelp documentation%2$s, or if you are looking for an enterprise implementation talk to our %3$sperformance team%4$s.', 'personalizewp' ),
						'<a href="' . esc_url( 'https://help.personalizewp.com/' ) . '" target="_blank">',
						'</a>',
						'<a href="' . esc_url( 'https://personalizewp.com/contact/' ) . '" target="_blank">',
						'</a>'
					)
				)
			),
			array(
				'type'           => 'success',
				'dismissible'    => true,
				'paragraph_wrap' => false,
			)
		);
	}

	/**
	 * Index Screen
	 *
	 * Default when routing.
	 */
	protected function index_screen() {

		$this->display_header();

		$this->maybe_display_onboarded();

		/**
		 * Filters the tiles that are shown on the dashboard page.
		 *
		 * @since 1.0.0
		 *
		 * @param array $tiles {
		 *     Indexed array of tile information to show
		 *
		 *     @type string $status  Banner status to overlay on tile image, can be 'coming' or 'upgrade'
		 *     @type string $img     URL of image to show
		 *     @type string $title   Title of tile
		 *     @type string $content Tile paragraphs
		 *     @type string $url     URL for the whole tile
		 * }
		 */
		$tiles = apply_filters(
			'personalizewp_dashboard_tiles',
			array(
				// Main Rules should always be first
				'personalisation'     => array(
					'img'     => plugins_url( '/img/tile-rules.jpg', __FILE__ ),
					'title'   => __( 'Rules', 'personalizewp' ),
					'content' => __( 'Create and manage rules based on conditions such as day, time, location, referrer, and more.', 'personalizewp' ),
					'url'     => admin_url( PageRules::$index_url ),
				),
				'contacts'            => array(
					'img'     => plugins_url( '/img/tile-visitor-profiles.jpg', __FILE__ ),
					'title'   => __( 'Visitor Profiles', 'personalizewp' ),
					'content' => __( 'View the profiles of your site visitors and see their behavior and actions.', 'personalizewp' ),
					'url'     => admin_url( PageContacts::$index_url ),
				),
				'segments'            => array(
					'img'     => plugins_url( '/img/tile-segments.jpg', __FILE__ ),
					'title'   => __( 'Segments', 'personalizewp' ),
					'content' => __( 'Create groups of visitors based on their behavior to personalize their experience.', 'personalizewp' ),
					'url'     => admin_url( PageSegments::$index_url ),
				),
				'leadscoring'         => array(
					'img'     => plugins_url( '/img/tile-lead-scoring.jpg', __FILE__ ),
					'title'   => __( 'Lead Scoring', 'personalizewp' ),
					'content' => __( 'Apply a weighting score to potential leads based on their website activity.', 'personalizewp' ),
					'url'     => admin_url( PageScoringRules::$index_url ),
				),
				'customprofilefields' => array(
					'img'     => plugins_url( '/img/tile-custom-profile-fields.jpg', __FILE__ ),
					'title'   => __( 'Custom Profile Fields', 'personalizewp' ),
					'content' => __( 'Add custom fields to your Visitor Profiles and show / hide blocks based on the recorded data.', 'personalizewp' ),
					'url'     => sprintf( '%s#pwp-profile', admin_url( PageSettings::$settings_url ) ),
				),
				'privacydata'         => array(
					'img'     => plugins_url( '/img/tile-privacy.svg', __FILE__ ),
					'title'   => __( 'Privacy / Data Retention', 'personalizewp' ),
					'content' => __( 'Set your preferences for data collection and retention periods.', 'personalizewp' ),
					'url'     => sprintf( '%s#pwp-privacy', admin_url( PageSettings::$settings_url ) ),
				),
				'settings'            => array(
					'img'     => plugins_url( '/img/tile-settings.svg', __FILE__ ),
					'title'   => __( 'Settings', 'personalizewp' ),
					'content' => __( 'Edit your settings and join our mailing list.', 'personalizewp' ),
					'url'     => admin_url( PageSettings::$settings_url ),
				),
			)
		);

		require_once plugin_dir_path( __DIR__ ) . 'admin/partials/dashboard.php';
		$this->display_footer();
	}
}
