<?php
/**
 * Fired during plugin activation
 *
 * @link       https://www.francescopepe.com
 * @since      1.0.0
 *
 * @package    Popper
 * @subpackage Popper/includes
 */

namespace Popper;

/**
 * Fired during plugin activation.
 *
 * This class defines all code necessary to run during the plugin's activation.
 *
 * @since      1.0.0
 * @package    Popper
 * @subpackage Popper/includes
 * @author     Francesco Pepe <info@francescopepe.com>
 */
class Register {

	/**
	 * Register block category
	 *
	 * @return void
	 */
	public function register_block_category( $block_categories ) {
		$block_categories[] = array(
			'slug'  => 'popper',
			'title' => __( 'Popper popup', 'popper' ),
		);

		return $block_categories;
	}

	/**
	 * Register the CPT.
	 *
	 * @since    1.0.0
	 */
	public function register_cpt() {
		$args = array(
			'labels'              => array(
				'name'               => _x( 'Popups', 'Post Type General Name', 'popper' ),
				'singular_name'      => _x( 'Popup', 'Post Type Singular Name', 'popper' ),
				'menu_name'          => __( 'Popups', 'popper' ),
				'parent_item_colon'  => __( 'Parent Popup', 'popper' ),
				'all_items'          => __( 'Popups', 'popper' ),
				'view_item'          => __( 'View Popup', 'popper' ),
				'add_new_item'       => __( 'Add New Popup', 'popper' ),
				'add_new'            => __( 'Add New', 'popper' ),
				'edit_item'          => __( 'Edit Popup', 'popper' ),
				'update_item'        => __( 'Update Popup', 'popper' ),
				'search_items'       => __( 'Search Popup', 'popper' ),
				'not_found'          => __( 'Not Found', 'popper' ),
				'not_found_in_trash' => __( 'Not found in Trash', 'popper' ),
			),
			'public'              => false,
			'publicly_queryable'  => false,
			'has_archive'         => false,
			'show_ui'             => true,
			'menu_icon'           => 'dashicons-external',
			'exclude_from_search' => true,
			'show_in_nav_menus'   => true,
			'rewrite'             => false,
			'hierarchical'        => false,
			'show_in_menu'        => false,
			'show_in_admin_bar'   => true,
			'show_in_rest'        => true,
			'capability_type'     => 'post',
			'template'            => array(
				array(
					'popper/popup',
					array(
						'lock' => array(
							'move'   => false,
							'remove' => true,
						),
					),
				),
			),
			'template_lock'       => 'insert',
			'supports'            => array(
				'title',
				'editor',
				'custom-fields',
				'revisions',
				'author',
				'excerpt',
			),
		);
		register_post_type( 'popper', $args );
	}

	/**
	 * Register meta
	 *
	 * @return void
	 */
	public function register_meta() {
		$defaults = array(
			'running'  => true,
			'location' => array(),
			'exclude'  => array(),
			'user'     => array(),
			'date'     => array(
				'type'       => 'evergreen',
				'startDate'  => '',
				'endDate'    => '',
				'customTime' => false,
				'customDays' => array(),
			),
			'device'   => array(
				'desktop',
				'tablet',
				'mobile',
			),
		);

		register_meta(
			'post',
			'popper_rules',
			array(
				'single'         => true,
				'type'           => 'object',
				'default'        => $defaults,
				'show_in_rest'   => array(
					'schema' => array(
						'type'                 => 'object',
						'additionalProperties' => true,
						'properties'           => array(
							'running'  => array(
								'type' => 'boolean',
							),
							'location' => array(
								'type' => 'array',
							),
							'exclude'  => array(
								'type' => 'array',
							),
							'user'     => array(
								'type' => 'array',
							),
							'date'     => array(
								'type'                 => 'object',
								'additionalProperties' => true,
								'properties'           => array(
									'type'       => array(
										'type' => 'string',
									),
									'startDate'  => array(
										'type' => 'string',
									),
									'endDate'    => array(
										'type' => 'string',
									),
									'customTime' => array(
										'type' => 'boolean',
									),
									'customDays' => array(
										'type' => 'array',
									),
								),
							),
							'device'   => array(
								'type' => 'array',
							),
						),
					),
				),
				'object_subtype' => 'popper',
			)
		);

		register_meta(
			'post',
			'popper_triggers',
			array(
				'single'         => true,
				'type'           => 'object',
				'default'        => array(
					'close'    => array(),
					'open'     => array(),
					'settings' => array(),
				),
				'show_in_rest'   => array(
					'schema' => array(
						'type'                 => 'object',
						'additionalProperties' => true,
						'properties'           => array(
							'close'  => array(
								'type' => 'array',
							),
							'open' => array(
								'type' => 'array',
							),
							'settings'  => array(
								'type' => 'array',
							),
						),
					),
				),
				'object_subtype' => 'popper',
			)
		);

		register_post_meta(
			'popper',
			'_popper_actions',
			array(
				'show_in_rest'         => array(
					'schema' => array(
						'items' => array(
							'type'                 => 'object',
							'properties'           => array(),
							'additionalProperties' => true,
						),
					),
				),
				'default'              => array(),
				'single'               => true,
				'type'                 => 'array',
				'additionalProperties' => true,
				'auth_callback'        => function () {
					return current_user_can( 'edit_posts' );
				},
			)
		);

		register_post_meta(
			'popper',
			'_popper_fields',
			array(
				'show_in_rest'  => array(
					'schema' => array(
						'type'                 => 'object',
						'properties'           => array(),
						'additionalProperties' => true,
					),
				),
				'default'       => array(),
				'single'        => true,
				'type'          => 'object',
				'auth_callback' => function () {
					return current_user_can( 'edit_posts' );
				},
			)
		);

		register_rest_field(
			'popper',
			'popper_locations',
			array(
				'get_callback' => function ( $obj ) {
					$result = array(
						'include' => array(),
						'exclude' => array(),
						'user'    => array(),
						'dates'   => array(),
					);

					foreach ( $obj['meta']['popper_rules']['location'] as $value ) {
						if ( ! empty( $value['rule'] ) ) {
							array_push( $result['include'], \Popper\Conditions::get_saved_label( $value ) );
						}
					}
					foreach ( $obj['meta']['popper_rules']['exclude'] as $value ) {
						array_push( $result['exclude'], \Popper\Conditions::get_saved_label( $value ) );
					}
					$result['date'] = $obj['meta']['popper_rules']['date'];
					$result['user'] = \Popper\Conditions::get_user_label( $obj['meta']['popper_rules']['user'] ) ?? array();
					return $result;
				},
			),
		);
	}

	/**
	 * Register Settings
	 *
	 * @return void
	 */
	public function register_settings() {
		$settings = array(
			'log' => array(
				'type' => 'boolean',
			),
			'version' => array(
				'type' => 'string',
			),
			'captcha' => array(
				'type' => 'object',
			),
			'integrations' => array(
				'type' => 'object',
				'additionalProperties' => true,
			),
			'google' => array(
				'type' => 'object',
				'properties' => array(
					'client_id' => array(
						'type' => 'string',
					),
					'client_secret' => array(
						'type' => 'string',
					),
				),
			),
			'uninstall' => array(
				'type' => 'boolean',
			),
		);

		$settings = apply_filters( 'popper_register_settings', $settings );

		register_setting(
			'popper',
			'popper',
			array(
				'description'  => __(
					'Settings for the Popper Block plugin.',
					'popper'
				),
				'type'         => 'object',
				'show_in_rest' => array(
					'schema' => array(
						'type'       => 'object',
						'properties' => $settings,
						'additionalProperties' => true,
					),
				),
				'default' => array(
					'log' => false,
					'uninstall' => false,
					'integrations' => array(),
					'google' => array(
						'client_id' => '',
						'client_secret' => '',
					),
				),
			)
		);
	}
}
