<?php

/**
 * Fired during plugin activation
 *
 * @link        https://wpsmartpost.com/
 * @since      2.0.0
 *
 * @package    Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/includes
 */

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

/**
 * Custom post class to register the carousel.
 */
class Smart_Post_Show_Pro_Post_Type {



	/**
	 * The single instance of the class.
	 *
	 * @var self
	 * @since 2.0.0
	 */
	private static $instance;

	/**
	 * Allows for accessing single instance of class. Class should only be constructed once per call.
	 *
	 * @since 1.0.0
	 * @static
	 * @return self Main instance.
	 */
	public static function instance() {
		if ( is_null( self::$instance ) ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Post carousel post type
	 */
	public function post_carousel_post_type() {
		if ( post_type_exists( 'sp_post_carousel' ) ) {
			return;
		}
		$capability = apply_filters( 'pcp_user_role_permission', 'manage_options' );
		$show_ui    = current_user_can( $capability ) ? true : false;
		// Set the Post carousel post type labels.
		$labels = apply_filters(
			'sp_post_carousel_post_type_labels',
			array(
				'name'               => __( 'Manage Shows', 'smart-post-show-pro' ),
				'singular_name'      => __( 'Show', 'smart-post-show-pro' ),
				'menu_name'          => __( 'Smart Post', 'smart-post-show-pro' ),
				'all_items'          => __( 'Manage Shows', 'smart-post-show-pro' ),
				'add_new'            => __( 'Add New', 'smart-post-show-pro' ),
				'add_new_item'       => __( 'Add New Show', 'smart-post-show-pro' ),
				'edit'               => __( 'Edit', 'smart-post-show-pro' ),
				'edit_item'          => __( 'Edit Show', 'smart-post-show-pro' ),
				'new_item'           => __( 'New Show', 'smart-post-show-pro' ),
				'search_items'       => __( 'Search Shows', 'smart-post-show-pro' ),
				'not_found'          => __( 'No Shows found', 'smart-post-show-pro' ),
				'not_found_in_trash' => __( 'No Shows found in Trash', 'smart-post-show-pro' ),
				'parent'             => __( 'Parent Show', 'smart-post-show-pro' ),
			)
		);
		// $menu_icon = 'data:image/svg+xml;base64,' . base64_encode(
		// '<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="24px" height="24px" viewBox="0 0 288 288" enable-background="new 0 0 288 288" xml:space="preserve"><path fill="#A0A5AA" d="M262.102,20.977H27.615c-3.195,0-6.638,2.401-6.638,5.63v234.359c0,3.229,3.443,6.057,6.638,6.057h234.487c3.187,0,4.921-2.828,4.921-6.057V26.607C267.023,23.378,265.289,20.977,262.102,20.977z M118.37,53.441h51.26v43.571h-51.26V53.441z M55.15,53.441h51.26v43.571H55.15V53.441z M135.457,235.413H55.15v-46.134h80.307V235.413z M135.457,173.047H55.15v-46.134h80.307V173.047z M235.413,235.413h-80.307v-46.134h80.307V235.413z M235.413,173.047h-80.307v-46.134h80.307V173.047z M235.413,97.012h-51.26V53.441h51.26V97.012z"/><line fill="none" x1="-99" y1="-84" x2="-99" y2="-57"/><line fill="none" x1="-170" y1="61" x2="-170" y2="101"/></svg>'
		// );
		$menu_icon = 'data:image/svg+xml;base64,' . base64_encode(
			'<svg width="20" height="20" viewBox="0 0 20 20" fill="none" xmlns="http://www.w3.org/2000/svg">
				<path d="M1.25 12.2422C1.25 11.4871 1.8621 10.875 2.61715 10.875H7.75765C8.51271 10.875 9.12481 11.4871 9.12481 12.2422V17.3828C9.12481 18.1379 8.51271 18.75 7.75765 18.75H2.61715C1.8621 18.75 1.25 18.1379 1.25 17.3828V12.2422Z" fill="white"/>
				<path d="M10.8748 5.1875C10.8748 3.01288 12.6376 1.25 14.8122 1.25C16.9867 1.25 18.7496 3.01288 18.7496 5.1875C18.7496 7.36212 16.9867 9.125 14.8122 9.125C12.6376 9.125 10.8748 7.36212 10.8748 5.1875Z" fill="white"/>
				<path d="M1.25 2.61719C1.25 1.86211 1.8621 1.25 2.61715 1.25H7.75765C8.51271 1.25 9.12481 1.86211 9.12481 2.61719V7.75781C9.12481 8.51289 8.51271 9.125 7.75765 9.125H2.61715C1.8621 9.125 1.25 8.51289 1.25 7.75781V2.61719Z" fill="white"/>
				<rect x="10.8752" y="16.9999" width="7.87481" height="1.16667" rx="0.583333" fill="white"/>
				<rect x="10.8752" y="11.458" width="7.87481" height="1.16667" rx="0.583333" fill="white"/>
				<rect x="10.8752" y="14.0829" width="7.87481" height="1.16667" rx="0.583333" fill="white"/>
			</svg>'
		);
		// Set the Post carousel post type arguments.
		$args = apply_filters(
			'sp_post_carousel_post_type_args',
			array(
				'label'           => __( 'Manage Shows', 'smart-post-show-pro' ),
				'description'     => __( 'Manage Shows', 'smart-post-show-pro' ),
				'public'          => false,
				'show_ui'         => $show_ui,
				'show_in_menu'    => $show_ui,
				'menu_icon'       => $menu_icon,
				'hierarchical'    => false,
				'query_var'       => false,
				'menu_position'   => 5,
				'supports'        => array( 'title' ),
				'capability_type' => 'post',
				'labels'          => $labels,
			)
		);

		register_post_type( 'sp_post_carousel', $args );

		$badge_label = array(
			'name'              => __( 'Badges by Smart Post', 'smart-post-show-pro' ),
			'singular_name'     => __( 'Badge', 'smart-post-show-pro' ),
			'search_items'      => __( 'Search Badges', 'smart-post-show-pro' ),
			'all_items'         => __( 'All Badges', 'smart-post-show-pro' ),
			'parent_item'       => __( 'Parent Badge', 'smart-post-show-pro' ),
			'parent_item_colon' => __( 'Parent Badge:', 'smart-post-show-pro' ),
			'edit_item'         => __( 'Edit Badge', 'smart-post-show-pro' ),
			'update_item'       => __( 'Update Badge', 'smart-post-show-pro' ),
			'add_new_item'      => __( 'Add New Badge', 'smart-post-show-pro' ),
			'new_item_name'     => __( 'New Badge Name', 'smart-post-show-pro' ),
			'menu_name'         => __( 'Badges', 'smart-post-show-pro' ),
		);

		$badge_args = array(
			'hierarchical'      => true,
			'labels'            => $badge_label,
			'show_ui'           => true,
			'show_admin_column' => true,
			'query_var'         => true,
			'rewrite'           => array( 'slug' => 'badge' ),
			'show_in_rest'      => true,
		);

		$module_option = get_option( 'sp-pcp-blocks-modules-options', array() );

		$enable_badges = false;
		if ( ! empty( $module_option ) && ( is_array( $module_option ) || $module_option instanceof Traversable ) ) {
			foreach ( $module_option as $option ) {

				$module_name = is_array( $option ) ? ( $option['module_name'] ?? '' ) : ( $option->module_name ?? '' );

				$show = is_array( $option ) ? ( $option['show'] ?? false ) : ( $option->show ?? false );

				if ( 'post-badges' === $module_name ) {
					$enable_badges = (bool) $show;
					break;
				}
			}
		}

		// TODO: Post Badges Feature on post_type.
		// $active_post_type = null;
		// foreach ( $module_option as $option ) {
		// if ( 'post-badges' === $option->block_name ) {
		// $active_post_type = $option->settings->post_type;
		// break;
		// }
		// }

		if ( $enable_badges ) {
			register_taxonomy( 'sp_smart_badges', array( 'post' ), $badge_args );
		}
	}
}
