<?php
/**
 * Listing Post Type
 *
 * Utilities and functions.
 *
 * @package CNP
 */

namespace CNP;

/**
 * ListingPostType Class
 */
class ListingPostType {

	/**
	 * Object query cache
	 *
	 * @var array
	 */
	private $cache;

	/**
	 * ListingPostType constructor
	 */
	public function __construct() {

		add_action( 'updated_postmeta', array( $this, 'action_postmeta_updated' ), 10, 4 );

	}

	/**
	 * Get the status ID
	 *
	 * @param string $status State taxonomy term slug.
	 */
	public function get_status_id( $status ) {

		$cache_key = $status . '_id';
		if ( empty( $this->cache[ $cache_key ] ) ) {
			$args = array(
				'fields'     => 'ids',
				'hide_empty' => false,
				'slug'       => $status,
				'taxonomy'   => 'statuses',
			);

			$term_query = new \WP_Term_Query( $args );
			if ( empty( $term_query->terms ) ) {

				return false;
			}

			$this->cache[ $cache_key ] = array_slice( $term_query->terms, 0, 1 );
		}

		return $this->cache[ $cache_key ];
	}

	/**
	 * Get a count of listings by status. Returns a count of new listings by default.
	 *
	 * @param string $status Status taxonomy term slug. Defaults to new.
	 */
	public function get_listings_count_with_status( $status = 'new' ) {

		$cache_key = $status . '_count';
		if ( empty( $this->cache[ $cache_key ] ) ) {

			$status_id = $this->get_status_id( $status );

			$args = array(
				array(
					'key'   => 'listing_status',
					'value' => $status_id,
				),
			);

			$this->cache[ $cache_key ] = $this->get_listings_count( $args );
		}

		return $this->cache[ $cache_key ];
	}

	/**
	 * Get a count of listings by meta_query
	 *
	 * @param array $args Array of meta_query args.
	 */
	public function get_listings_count( $args ) {

		$args = array(
			'post_type'  => 'listing',
			'meta_query' => $args,
		);

		$listings_query = new \WP_Query( $args );

		return $listings_query->post_count;
	}

	/**
	 * Saves the date if the listing status has changed
	 *
	 * @param int $post_id The post ID.
	 * @param int $status_id The ID of the Statuses term.
	 */
	public function action_date_of_status( $post_id, $status_id ) {

		$status   = get_term( $status_id, 'statuses' );
		$meta_key = 'date_' . $status->slug;
		update_post_meta( $post_id, $meta_key, gmdate( 'Y-m-d' ) );

	}

	/**
	 * Checks if the status of a listing is being updated.
	 *
	 * @param int    $meta_id    ID of updated metadata entry.
	 * @param int    $object_id  Post ID.
	 * @param string $meta_key   Meta key.
	 * @param mixed  $meta_value Meta value. This will be a PHP-serialized string representation of the value if
	 *                           the value is an array, an object, or itself a PHP-serialized string.
	 */
	public function action_postmeta_updated( $meta_id, $object_id, $meta_key, $meta_value ) {

		if ( 'listing_status' !== $meta_key ) {

			return;
		}

		$this->action_date_of_status( $object_id, $meta_value );

	}

	/**
	 * Gets listings with a selected status as of a given date.
	 *
	 * @param string $status The listing status slug.
	 * @param string $date The date of the status. Use Y-m-d format. Defaults to yesterday.
	 */
	public function get_listings_count_with_status_as_of_date( $status, $date = '' ) {

		if ( empty( $date ) ) {

			$d = new \DateTime();
			$d->add( \DateInterval::createFromDateString( 'yesterday' ) );
			$date = $d->format( 'Y-m-d' );

		}

		$cache_key = 'status_' . $date . '_count';
		if ( empty( $this->cache[ $cache_key ] ) ) {

			$args = array(
				array(
					'key'   => 'date_' . $status,
					'value' => $date,
				),
			);

			$this->cache[ $cache_key ] = $this->get_listings_count( $args );

		}

		return $this->cache[ $cache_key ];
	}

}
