<?php
/**
 * Class to handle custom taxonomies creation.
 *
 * @package HBFWC
 */

namespace HBFWC\Utils;

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

/**
 * Class HBFWC\Utils\Custom_Taxonomies.
 */
class Custom_Taxonomies {
	/**
	 * Class init.
	 */
	public static function init() {
		add_action( 'init', array( __CLASS__, 'register_taxonomies' ), 5 );
	}

	/**
	 * Register taxonomies.
	 */
	public static function register_taxonomies() {
		$taxonomies = apply_filters(
			'hbfwc_custom_taxonomies',
			array(
				'rate_plan' => array(
					'hierarchical'          => false,
					'update_count_callback' => '_wc_term_recount',
					'label'                 => __( 'Rate Plans', 'hotel-booking-for-woocommerce' ),
					'labels'                => array(
						'name'                       => __( 'Rate Plans', 'hotel-booking-for-woocommerce' ),
						'singular_name'              => __( 'Rate Plan', 'hotel-booking-for-woocommerce' ),
						'menu_name'                  => _x( 'Rate Plans', 'Admin menu name', 'hotel-booking-for-woocommerce' ),
						'search_items'               => __( 'Search rate plans', 'hotel-booking-for-woocommerce' ),
						'all_items'                  => __( 'All rate plans', 'hotel-booking-for-woocommerce' ),
						'edit_item'                  => __( 'Edit rate plan', 'hotel-booking-for-woocommerce' ),
						'update_item'                => __( 'Update rate plan', 'hotel-booking-for-woocommerce' ),
						'add_new_item'               => __( 'Add new rate plan', 'hotel-booking-for-woocommerce' ),
						'new_item_name'              => __( 'New rate plan name', 'hotel-booking-for-woocommerce' ),
						'popular_items'              => __( 'Popular rate plans', 'hotel-booking-for-woocommerce' ),
						'separate_items_with_commas' => __( 'Separate rate plans with commas', 'hotel-booking-for-woocommerce' ),
						'add_or_remove_items'        => __( 'Add or remove rate plans', 'hotel-booking-for-woocommerce' ),
						'choose_from_most_used'      => __( 'Choose from the most used rate plans', 'hotel-booking-for-woocommerce' ),
						'not_found'                  => __( 'No rate plans found', 'hotel-booking-for-woocommerce' ),
						'item_link'                  => __( 'Rate Plan Link', 'hotel-booking-for-woocommerce' ),
						'item_link_description'      => __( 'A link to an rate plan.', 'hotel-booking-for-woocommerce' ),
					),
					'show_in_rest'          => true,
					'show_ui'               => false,
					'query_var'             => true,
					'capabilities'          => array(
						'manage_terms' => 'manage_product_terms',
						'edit_terms'   => 'edit_product_terms',
						'delete_terms' => 'delete_product_terms',
						'assign_terms' => 'assign_product_terms',
					),
					'rewrite'               => array(
						'slug'       => untrailingslashit( _x( 'rate-plans', 'slug', 'hotel-booking-for-woocommerce' ) ),
						'with_front' => false,
					),
				),
				'amenities' => array(
					'hierarchical'          => false,
					'update_count_callback' => '_wc_term_recount',
					'label'                 => __( 'Amenities', 'hotel-booking-for-woocommerce' ),
					'labels'                => array(
						'name'                       => __( 'Amenities', 'hotel-booking-for-woocommerce' ),
						'singular_name'              => __( 'Amenity', 'hotel-booking-for-woocommerce' ),
						'menu_name'                  => _x( 'Amenities', 'Admin menu name', 'hotel-booking-for-woocommerce' ),
						'search_items'               => __( 'Search amenities', 'hotel-booking-for-woocommerce' ),
						'all_items'                  => __( 'All amenities', 'hotel-booking-for-woocommerce' ),
						'edit_item'                  => __( 'Edit amenity', 'hotel-booking-for-woocommerce' ),
						'update_item'                => __( 'Update amenity', 'hotel-booking-for-woocommerce' ),
						'add_new_item'               => __( 'Add new amenity', 'hotel-booking-for-woocommerce' ),
						'new_item_name'              => __( 'New amenity name', 'hotel-booking-for-woocommerce' ),
						'popular_items'              => __( 'Popular amenities', 'hotel-booking-for-woocommerce' ),
						'separate_items_with_commas' => __( 'Separate amenities with commas', 'hotel-booking-for-woocommerce' ),
						'add_or_remove_items'        => __( 'Add or remove amenities', 'hotel-booking-for-woocommerce' ),
						'choose_from_most_used'      => __( 'Choose from the most used amenities', 'hotel-booking-for-woocommerce' ),
						'not_found'                  => __( 'No amenities found', 'hotel-booking-for-woocommerce' ),
						'item_link'                  => __( 'Amenity Link', 'hotel-booking-for-woocommerce' ),
						'item_link_description'      => __( 'A link to an amenity.', 'hotel-booking-for-woocommerce' ),
					),
					'show_in_rest'          => true,
					'show_ui'               => false,
					'query_var'             => true,
					'capabilities'          => array(
						'manage_terms' => 'manage_product_terms',
						'edit_terms'   => 'edit_product_terms',
						'delete_terms' => 'delete_product_terms',
						'assign_terms' => 'assign_product_terms',
					),
					'rewrite'               => array(
						'slug'       => untrailingslashit( _x( 'amenities', 'slug', 'hotel-booking-for-woocommerce' ) ),
						'with_front' => false,
					),
				),
			)
		);

		foreach ( $taxonomies as $tax => $args ) {
			if ( taxonomy_exists( $tax ) ) {
				continue;
			}

			self::register_taxonomy( $tax, $args );
		}

		self::create_terms();
	}

	/**
	 * Register single taxonomy.
	 *
	 * @param string $taxname The taxonomy name.
	 * @param array  $args The args.
	 */
	public static function register_taxonomy( $taxname, $args ) {
		register_taxonomy(
			$taxname,
			array( 'product' ),
			$args
		);
	}

	/**
	 * Create required terms.
	 */
	public static function create_terms() {
		$hbfwc_default_rate = (int) get_option( 'hbfwc_default_rate_id', 0 );

		if ( ! $hbfwc_default_rate || ! term_exists( $hbfwc_default_rate, 'rate_plan' ) ) {
			$default_rate_id   = 0;
			$default_rate_slug = sanitize_title( 'Standard' );
			$default_rate      = get_term_by( 'slug', $default_rate_slug, 'rate_plan' ); // @codingStandardsIgnoreLine.

			if ( $default_rate ) {
				$default_rate_id = absint( $default_rate->term_id );
			} else {
				$result = wp_insert_term(
					_x( 'Standard', 'Default rate plan name', 'hotel-booking-for-woocommerce' ),
					'rate_plan',
					array(
						'description' => 'Room only without discounts or extras.',
						'slug'        => $default_rate_slug,
					)
				);

				if ( ! is_wp_error( $result ) && ! empty( $result['term_id'] ) ) {
					$features = 'Non-refundable, Without Breakfast';
					update_term_meta( $result['term_id'], 'features', $features );
					$default_rate_id = absint( $result['term_id'] );
				}
			}

			if ( $default_rate_id ) {
				update_option( 'hbfwc_default_rate_id', $default_rate_id );
			}
		}
	}
}

Custom_Taxonomies::init();
