<?php
/**
 * Trip Faqs.
 *
 * @package tripzzy
 * @since 1.3.8
 */

namespace Tripzzy\Core\Helpers;

use Tripzzy\Core\Traits\TripTrait;

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

if ( ! class_exists( 'Tripzzy\Core\Helpers\TripDuration' ) ) {

	/**
	 * Our main helper class that provides.
	 *
	 * @since 1.3.8
	 */
	class TripDuration {
		use TripTrait;

		/**
		 * Trip Object.
		 *
		 * @var $trip.
		 */
		public static $trip;

		/**
		 * All Post Metas.
		 *
		 * @var $all_meta.
		 */
		public static $all_meta;

		/**
		 * Only trip Metas.
		 *
		 * @var $trip_meta.
		 */
		public static $trip_meta;


		/**
		 * Trip Init.
		 *
		 * @param mixed $trip either trip id or trip object.
		 */
		public function __construct( $trip = null ) {
			if ( is_object( $trip ) ) {
				self::$trip = $trip;
			} elseif ( is_numeric( $trip ) ) {
				self::$trip = get_post( $trip );
			} else {
				self::$trip = get_post( get_the_ID() );
			}
			self::$all_meta  = get_post_meta( self::$trip->ID );
			self::$trip_meta = MetaHelpers::get_post_meta( self::$trip->ID, 'trip' );
		}

		/**
		 * Get Trip Infos data as per trip id for the frontend.
		 *
		 * @param int  $trip_id Trip id.
		 * @param bool $return_duration_key Either return key or label.
		 * @since 1.3.8
		 *
		 * @return array
		 */
		public static function get( $trip_id, $return_duration_key = true ) {
			return Trip::get_duration( $trip_id, $return_duration_key );
		}

		/**
		 * Render Trip infos to display it in frontend.
		 *
		 * @param int     $trip_id Trip id.
		 * @param boolean $display Deiplay or return the markup.
		 * @return void
		 */
		public static function render( $trip_id = 0, $display = true ) {
			if ( ! $trip_id ) {

				global $post;
				if ( ! $post ) {
					return;
				}
				$trip_id = $post->ID;

			}

			$duration_data = self::get( $trip_id, false ); // Saved In post meta.
			ob_start();
			if ( is_array( $duration_data ) && count( $duration_data ) > 0 ) :
				printf( '%s %s', esc_html( $duration_data['duration'][0] ), esc_html( $duration_data['duration_unit'][0] ) );
			endif;
			$content = ob_get_contents();
			ob_end_clean();
			if ( ! $display ) {
				return $content;
			}
			echo wp_kses_post( $content );
		}
	}
}
