<?php
/**
 * Making compatible for ACF plugin
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/public/helper
 *
 * @since 2.0.0
 */

if ( ! class_exists( 'SP_PCP_ACF' ) ) {
	/**
	 * Making compatible to the ACF plugin fields.
	 *
	 * @internal component.
	 * @todo ACF function work.
	 */
	class SP_PCP_ACF {

		/**
		 * Show out from ACF
		 *
		 * @param object $field_object The field object.
		 * @return mixed
		 */
		public static function show_acf_output( $field_object ) {
			if ( ! $field_object ) {
				return '';
			}

			$value = $field_object['value'];
			$type  = $field_object['type'];
			if ( 'true_false' !== $type && empty( $value ) ) {
				return '';
			}

			$separator = ',';
			switch ( $type ) {
				case 'select':
				case 'checkbox':
				case 'radio':
					$result = array();
					foreach ( (array) $value as $key ) {
						$result[] = isset( $field_object['choices'][ $key ] ) ? $field_object['choices'][ $key ] : '';
					}
					$value = implode( $separator, $result );

					break;

				case 'true_false':
					$value = $value ? __( 'Yes', 'smart-post-show-pro' ) : __( 'No', 'smart-post-show-pro' );
					break;
				case 'date_picker':
				case 'date_time_picker':
					if ( isset( $field_object['return_format'], $field_object['display_format'] ) ) {
						$date               = DateTime::createFromFormat( esc_attr( $field_object['return_format'] ), $value );
						$modify_date_format = apply_filters( 'pcp_custom_fields_custom_date_format', esc_attr( $field_object['display_format'] ) );
						// Show date with 'display_format'.
						if ( $date ) {
							$value = $date->format( $modify_date_format );
						}
					}
					break;
				case 'color_picker':
					$value = sprintf( '<div class="%1$s" style="height:%2$s;width:%2$s;background:%3$s;"></div>', 'sp-pcp-cf-color', '25px', $value );

					break;

				case 'page_link':
					$value = sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( $value ), __( 'Click here', 'smart-post-show-pro' ) );

					break;
				case 'link':
					if ( is_array( $value ) && ! empty( $value['url'] ) ) {
						$value['title'] = ! empty( $value['title'] ) ? $value['title'] : $value['url'];
						$value          = sprintf( '<a href="%s" target="%s">%s</a>', esc_url( $value['url'] ), esc_attr( $value['target'] ), esc_attr( $value['title'] ) );
					} else {
						$value = sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( $value ), __( 'Click here', 'smart-post-show-pro' ) );
					}
					break;
				case 'post_object':
					if ( ! is_array( $value ) ) {
						$value = array( $value );
					}

					$result = array();
					foreach ( $value as $post_object ) {
						$output   = sprintf( '<a href="%s" target="_blank">%s</a>', get_permalink( $post_object->ID ), get_the_post_thumbnail( $post_object->ID ) . get_the_title( $post_object->ID ) );
						$result[] = apply_filters( 'sp_pcp_acf_post_object_output', $output, $post_object );
					}
					$value = implode( $separator, $result );
					break;

				// Custom output from file.
				case 'image':
				case 'file':
				case 'relationship':
				case 'taxonomy':
				case 'gallery':
					$file_path = SP_PCP_PATH . sprintf( 'public/helpers/compatible/acf-fields/%s.php', $field_object['type'] );

					if ( file_exists( $file_path ) ) {
						ob_start();
						include $file_path;
						$value = ob_get_clean();
					}

					break;
			}

			return $value;
		}
	}
}
