<?php
/**
 *
 * Field: background
 *
 * @link       https://shapedplugin.com/
 *
 * @package Smart_Post_Show_Pro
 * @subpackage Smart_Post_Show_Pro/admin/views
 */

if ( ! defined( 'ABSPATH' ) ) {
	die;
} // Cannot access directly.

if ( ! class_exists( 'SP_PCP_Field_background_adv' ) ) {
	/**
	 *
	 * Field: background
	 *
	 * @since 1.0.0
	 * @version 1.0.0
	 */
	class SP_PCP_Field_background_adv extends SP_PCP_Fields {
		/**
		 * The class constructor.
		 *
		 * @param array  $field The field type.
		 * @param string $value The values of the field.
		 * @param string $unique The unique ID for the field.
		 * @param string $where To where show the output CSS.
		 * @param string $parent The parent args.
		 */
		public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
			parent::__construct( $field, $value, $unique, $where, $parent );
		}
		/**
		 * The render method.
		 *
		 * @return void
		 */
		public function render() {
			$args = wp_parse_args(
				$this->field,
				array(
					'background_color'              => true,
					'background_image'              => true,
					'background_position'           => true,
					'background_repeat'             => true,
					'background_attachment'         => true,
					'background_size'               => true,
					'background_origin'             => false,
					'background_clip'               => false,
					'background_blend-mode'         => false,
					'background_gradient'           => false,
					'background_gradient_color'     => true,
					'background_gradient_direction' => true,
					'background_image_preview'      => true,
					'preview'                       => true,
					'preview_text'                  => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
					'background_image_library'      => 'image',
					'background_image_placeholder'  => esc_html__( 'No background selected', 'smart-post-show-pro' ),
				)
			);

			$default_value = array(
				'background-color'              => '',
				'background-image'              => '',
				'background-position'           => '',
				'background-repeat'             => '',
				'background-attachment'         => '',
				'background-size'               => '',
				'background-origin'             => '',
				'background-clip'               => '',
				'background-blend-mode'         => '',
				'background-gradient-color'     => '',
				'background-gradient-direction' => '',
			);

			$default_value = ( ! empty( $this->field['default'] ) ) ? wp_parse_args( $this->field['default'], $default_value ) : $default_value;

			$this->value = wp_parse_args( $this->value, $default_value );

			echo wp_kses_post( $this->field_before() );

			echo '<div class="spf--blocks">';
			//
			// Background Color.
			if ( ! empty( $args['background_color'] ) ) {

				echo '<div class="spf--block">';
				echo ( ! empty( $args['background_gradient'] ) ) ? '<div class="spf--title">' . esc_html__( 'From', 'smart-post-show-pro' ) . '</div>' : '';

				SP_PCP::field(
					array(
						'id'      => 'background-color',
						'type'    => 'color',
						'default' => $default_value['background-color'],
					),
					$this->value['background-color'],
					$this->field_name(),
					'field/background'
				);

				echo '</div>';

			}

			//
			// Background Gradient Color.
			if ( ! empty( $args['background_gradient_color'] ) && ! empty( $args['background_gradient'] ) ) {

				echo '<div class="spf--block">';
				echo ( ! empty( $args['background_gradient'] ) ) ? '<div class="spf--title">' . esc_html__( 'To', 'smart-post-show-pro' ) . '</div>' : '';

				SP_PCP::field(
					array(
						'id'      => 'background-gradient-color',
						'type'    => 'color',
						'default' => $default_value['background-gradient-color'],
					),
					$this->value['background-gradient-color'],
					$this->field_name(),
					'field/background'
				);

				echo '</div>';

			}

			//
			// Background Gradient Direction.
			if ( ! empty( $args['background_gradient_direction'] ) && ! empty( $args['background_gradient'] ) ) {

				echo '<div class="spf--block spf--gradient">';
				echo ( ! empty( $args['background_gradient'] ) ) ? '<div class="spf--title">' . esc_html__( 'Direction', 'smart-post-show-pro' ) . '</div>' : '';

				SP_PCP::field(
					array(
						'id'      => 'background-gradient-direction',
						'type'    => 'select',
						'options' => array(
							''          => esc_html__( 'Gradient Direction', 'smart-post-show-pro' ),
							'to bottom' => esc_html__( '&#8659; top to bottom', 'smart-post-show-pro' ),
							'to right'  => esc_html__( '&#8658; left to right', 'smart-post-show-pro' ),
							'135deg'    => esc_html__( '&#8664; corner top to right', 'smart-post-show-pro' ),
							'-135deg'   => esc_html__( '&#8665; corner top to left', 'smart-post-show-pro' ),
						),
					),
					$this->value['background-gradient-direction'],
					$this->field_name(),
					'field/background'
				);

				echo '</div>';

			}

			echo '</div>';
			echo '<div class="clear"></div>';

			//
			// Background Image.
			if ( ! empty( $args['background_image'] ) ) {

				echo '<div class="spf--media">';

				SP_PCP::field(
					array(
						'id'          => 'background-image',
						'type'        => 'media',
						'library'     => $args['background_image_library'],
						'preview'     => $args['background_image_preview'],
						'placeholder' => $args['background_image_placeholder'],
					),
					$this->value['background-image'],
					$this->field_name(),
					'field/background'
				);

				echo '</div>';

				echo '<div class="clear"></div>';

			}

			//
			// Background Position.
			if ( ! empty( $args['background_position'] ) ) {
				echo '<div class="spf--block spf--select">';

				SP_PCP::field(
					array(
						'id'      => 'background-position',
						'type'    => 'select',
						'options' => array(
							''              => esc_html__( 'Background Position', 'smart-post-show-pro' ),
							'left top'      => esc_html__( 'Left Top', 'smart-post-show-pro' ),
							'left center'   => esc_html__( 'Left Center', 'smart-post-show-pro' ),
							'left bottom'   => esc_html__( 'Left Bottom', 'smart-post-show-pro' ),
							'center top'    => esc_html__( 'Center Top', 'smart-post-show-pro' ),
							'center center' => esc_html__( 'Center Center', 'smart-post-show-pro' ),
							'center bottom' => esc_html__( 'Center Bottom', 'smart-post-show-pro' ),
							'right top'     => esc_html__( 'Right Top', 'smart-post-show-pro' ),
							'right center'  => esc_html__( 'Right Center', 'smart-post-show-pro' ),
							'right bottom'  => esc_html__( 'Right Bottom', 'smart-post-show-pro' ),
						),
					),
					$this->value['background-position'],
					$this->field_name(),
					'field/background'
				);

				echo '</div>';

			}

			//
			// Background Repeat.
			if ( ! empty( $args['background_repeat'] ) ) {
				echo '<div class="spf--block spf--select">';

				SP_PCP::field(
					array(
						'id'      => 'background-repeat',
						'type'    => 'select',
						'options' => array(
							''          => esc_html__( 'Background Repeat', 'smart-post-show-pro' ),
							'repeat'    => esc_html__( 'Repeat', 'smart-post-show-pro' ),
							'no-repeat' => esc_html__( 'No Repeat', 'smart-post-show-pro' ),
							'repeat-x'  => esc_html__( 'Repeat Horizontally', 'smart-post-show-pro' ),
							'repeat-y'  => esc_html__( 'Repeat Vertically', 'smart-post-show-pro' ),
						),
					),
					$this->value['background-repeat'],
					$this->field_name(),
					'field/background'
				);

				echo '</div>';

			}

			//
			// Background Attachment.
			if ( ! empty( $args['background_attachment'] ) ) {
				echo '<div class="spf--block spf--select">';

				SP_PCP::field(
					array(
						'id'      => 'background-attachment',
						'type'    => 'select',
						'options' => array(
							''       => esc_html__( 'Background Attachment', 'smart-post-show-pro' ),
							'scroll' => esc_html__( 'Scroll', 'smart-post-show-pro' ),
							'fixed'  => esc_html__( 'Fixed', 'smart-post-show-pro' ),
						),
					),
					$this->value['background-attachment'],
					$this->field_name(),
					'field/background'
				);

				echo '</div>';

			}

			//
			// Background Size.
			if ( ! empty( $args['background_size'] ) ) {
				echo '<div class="spf--block spf--select">';

				SP_PCP::field(
					array(
						'id'      => 'background-size',
						'type'    => 'select',
						'options' => array(
							''        => esc_html__( 'Background Size', 'smart-post-show-pro' ),
							'cover'   => esc_html__( 'Cover', 'smart-post-show-pro' ),
							'contain' => esc_html__( 'Contain', 'smart-post-show-pro' ),
						),
					),
					$this->value['background-size'],
					$this->field_name(),
					'field/background'
				);

				echo '</div>';

			}

			//
			// Background Origin.
			if ( ! empty( $args['background_origin'] ) ) {
				echo '<div class="spf--block spf--select">';

				SP_PCP::field(
					array(
						'id'      => 'background-origin',
						'type'    => 'select',
						'options' => array(
							''            => esc_html__( 'Background Origin', 'smart-post-show-pro' ),
							'padding-box' => esc_html__( 'Padding Box', 'smart-post-show-pro' ),
							'border-box'  => esc_html__( 'Border Box', 'smart-post-show-pro' ),
							'content-box' => esc_html__( 'Content Box', 'smart-post-show-pro' ),
						),
					),
					$this->value['background-origin'],
					$this->field_name(),
					'field/background'
				);

				echo '</div>';

			}

			//
			// Background Clip.
			if ( ! empty( $args['background_clip'] ) ) {
				echo '<div class="spf--block spf--select">';

				SP_PCP::field(
					array(
						'id'      => 'background-clip',
						'type'    => 'select',
						'options' => array(
							''            => esc_html__( 'Background Clip', 'smart-post-show-pro' ),
							'border-box'  => esc_html__( 'Border Box', 'smart-post-show-pro' ),
							'padding-box' => esc_html__( 'Padding Box', 'smart-post-show-pro' ),
							'content-box' => esc_html__( 'Content Box', 'smart-post-show-pro' ),
						),
					),
					$this->value['background-clip'],
					$this->field_name(),
					'field/background'
				);

				echo '</div>';

			}

			//
			// Background Blend Mode.
			if ( ! empty( $args['background_blend_mode'] ) ) {
				echo '<div class="spf--block spf--select">';

				SP_PCP::field(
					array(
						'id'      => 'background-blend-mode',
						'type'    => 'select',
						'options' => array(
							''            => esc_html__( 'Background Blend Mode', 'smart-post-show-pro' ),
							'normal'      => esc_html__( 'Normal', 'smart-post-show-pro' ),
							'multiply'    => esc_html__( 'Multiply', 'smart-post-show-pro' ),
							'screen'      => esc_html__( 'Screen', 'smart-post-show-pro' ),
							'overlay'     => esc_html__( 'Overlay', 'smart-post-show-pro' ),
							'darken'      => esc_html__( 'Darken', 'smart-post-show-pro' ),
							'lighten'     => esc_html__( 'Lighten', 'smart-post-show-pro' ),
							'color-dodge' => esc_html__( 'Color Dodge', 'smart-post-show-pro' ),
							'saturation'  => esc_html__( 'Saturation', 'smart-post-show-pro' ),
							'color'       => esc_html__( 'Color', 'smart-post-show-pro' ),
							'luminosity'  => esc_html__( 'Luminosity', 'smart-post-show-pro' ),
						),
					),
					$this->value['background-blend-mode'],
					$this->field_name(),
					'field/background'
				);

				echo '</div>';

			}

			echo '<div class="clear"></div>';

			//
			// Preview.
			$always_preview = ( 'always' !== $args['preview'] ) ? ' hidden' : '';

			if ( ! empty( $args['preview'] ) ) {
				echo '<div class="spf--block spf--block-preview' . esc_attr( $always_preview ) . '">';
				echo '<div class="spf--preview">' . esc_html( $args['preview_text'] ) . '</div>';
				echo '</div>';
			}
			echo '<div class="clear"></div>';
			echo wp_kses_post( $this->field_after() );

		}

		/**
		 * Output
		 *
		 * @return statement
		 */
		public function output() {

			$output    = '';
			$bg_image  = array();
			$important = ( ! empty( $this->field['output_important'] ) ) ? '!important' : '';
			$element   = ( is_array( $this->field['output'] ) ) ? join( ',', $this->field['output'] ) : $this->field['output'];

			// Background image and gradient.
			$background_color        = ( ! empty( $this->value['background-color'] ) ) ? $this->value['background-color'] : '';
			$background_gd_color     = ( ! empty( $this->value['background-gradient-color'] ) ) ? $this->value['background-gradient-color'] : '';
			$background_gd_direction = ( ! empty( $this->value['background-gradient-direction'] ) ) ? $this->value['background-gradient-direction'] : '';
			$background_image        = ( ! empty( $this->value['background-image']['url'] ) ) ? $this->value['background-image']['url'] : '';

			if ( $background_color && $background_gd_color ) {
				$gd_direction = ( $background_gd_direction ) ? $background_gd_direction . ',' : '';
				$bg_image[]   = 'linear-gradient(' . $gd_direction . $background_color . ',' . $background_gd_color . ')';
			}

			if ( $background_image ) {
				$bg_image[] = 'url(' . $background_image . ')';
			}

			if ( ! empty( $bg_image ) ) {
				$output .= 'background-image:' . implode( ',', $bg_image ) . $important . ';';
			}

			// Common background properties.
			$properties = array( 'color', 'position', 'repeat', 'attachment', 'size', 'origin', 'clip', 'blend-mode' );

			foreach ( $properties as $property ) {
				$property = 'background-' . $property;
				if ( ! empty( $this->value[ $property ] ) ) {
					$output .= $property . ':' . $this->value[ $property ] . $important . ';';
				}
			}

			if ( $output ) {
				$output = $element . '{' . $output . '}';
			}

			$this->parent->output_css .= $output;

			return $output;

		}

	}
}
