<?php if ( ! defined( 'ABSPATH' ) ) {
	die; } // Cannot access directly.
/**
 *
 * Field: background
 *
 * @since 1.0.0
 * @version 1.0.0
 */
if ( ! class_exists( 'EDITH_Field_background' ) ) {
	class EDITH_Field_background extends EDITH_Fields {

		public function __construct( $field, $value = '', $unique = '', $where = '', $parent = '' ) {
			parent::__construct( $field, $value, $unique, $where, $parent );
		}

		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,
					'background_auto_attributes'    => false,
					'compact'                       => false,
					'background_image_library'      => 'image',
					'background_image_placeholder'  => esc_html__( 'Not selected', 'finch-cart-product-recommendations' ),
				)
			);

			if ( $args['compact'] ) {
				$args['background_color']           = false;
				$args['background_auto_attributes'] = true;
			}

			$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="edith--background-colors">';

			//
			// Background Color
			if ( ! empty( $args['background_color'] ) ) {

				echo '<div class="edith--color">';

				echo ( ! empty( $args['background_gradient'] ) ) ? '<div class="edith--title">' . esc_html__( 'From', 'finch-cart-product-recommendations' ) . '</div>' : '';

				EDITH::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="edith--color">';

				echo ( ! empty( $args['background_gradient'] ) ) ? '<div class="edith--title">' . esc_html__( 'To', 'finch-cart-product-recommendations' ) . '</div>' : '';

				EDITH::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="edith--color">';

				echo ( ! empty( $args['background_gradient'] ) ) ? '<div class="edith---title">' . esc_html__( 'Direction', 'finch-cart-product-recommendations' ) . '</div>' : '';

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

				echo '</div>';

			}

			echo '</div>';

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

				echo '<div class="edith--background-image">';

				EDITH::field(
					array(
						'id'          => 'background-image',
						'type'        => 'media',
						'class'       => 'edith-assign-field-background',
						'library'     => $args['background_image_library'],
						'preview'     => $args['background_image_preview'],
						'placeholder' => $args['background_image_placeholder'],
						'attributes'  => array( 'data-depend-id' => $this->field['id'] ),
					),
					$this->value['background-image'],
					$this->field_name(),
					'field/background'
				);

				echo '</div>';

			}

			$auto_class   = ( ! empty( $args['background_auto_attributes'] ) ) ? ' edith--auto-attributes' : '';
			$hidden_class = ( ! empty( $args['background_auto_attributes'] ) && empty( $this->value['background-image']['url'] ) ) ? ' edith--attributes-hidden' : '';

			echo '<div class="edith--background-attributes' . esc_attr( $auto_class . $hidden_class ) . '">';

			//
			// Background Position
			if ( ! empty( $args['background_position'] ) ) {

				EDITH::field(
					array(
						'id'      => 'background-position',
						'type'    => 'select',
						'options' => array(
							''              => esc_html__( 'Background Position', 'finch-cart-product-recommendations' ),
							'left top'      => esc_html__( 'Left Top', 'finch-cart-product-recommendations' ),
							'left center'   => esc_html__( 'Left Center', 'finch-cart-product-recommendations' ),
							'left bottom'   => esc_html__( 'Left Bottom', 'finch-cart-product-recommendations' ),
							'center top'    => esc_html__( 'Center Top', 'finch-cart-product-recommendations' ),
							'center center' => esc_html__( 'Center Center', 'finch-cart-product-recommendations' ),
							'center bottom' => esc_html__( 'Center Bottom', 'finch-cart-product-recommendations' ),
							'right top'     => esc_html__( 'Right Top', 'finch-cart-product-recommendations' ),
							'right center'  => esc_html__( 'Right Center', 'finch-cart-product-recommendations' ),
							'right bottom'  => esc_html__( 'Right Bottom', 'finch-cart-product-recommendations' ),
						),
					),
					$this->value['background-position'],
					$this->field_name(),
					'field/background'
				);

			}

			//
			// Background Repeat
			if ( ! empty( $args['background_repeat'] ) ) {

				EDITH::field(
					array(
						'id'      => 'background-repeat',
						'type'    => 'select',
						'options' => array(
							''          => esc_html__( 'Background Repeat', 'finch-cart-product-recommendations' ),
							'repeat'    => esc_html__( 'Repeat', 'finch-cart-product-recommendations' ),
							'no-repeat' => esc_html__( 'No Repeat', 'finch-cart-product-recommendations' ),
							'repeat-x'  => esc_html__( 'Repeat Horizontally', 'finch-cart-product-recommendations' ),
							'repeat-y'  => esc_html__( 'Repeat Vertically', 'finch-cart-product-recommendations' ),
						),
					),
					$this->value['background-repeat'],
					$this->field_name(),
					'field/background'
				);

			}

			//
			// Background Attachment
			if ( ! empty( $args['background_attachment'] ) ) {

				EDITH::field(
					array(
						'id'      => 'background-attachment',
						'type'    => 'select',
						'options' => array(
							''       => esc_html__( 'Background Attachment', 'finch-cart-product-recommendations' ),
							'scroll' => esc_html__( 'Scroll', 'finch-cart-product-recommendations' ),
							'fixed'  => esc_html__( 'Fixed', 'finch-cart-product-recommendations' ),
						),
					),
					$this->value['background-attachment'],
					$this->field_name(),
					'field/background'
				);

			}

			//
			// Background Size
			if ( ! empty( $args['background_size'] ) ) {

				EDITH::field(
					array(
						'id'      => 'background-size',
						'type'    => 'select',
						'options' => array(
							''        => esc_html__( 'Background Size', 'finch-cart-product-recommendations' ),
							'cover'   => esc_html__( 'Cover', 'finch-cart-product-recommendations' ),
							'contain' => esc_html__( 'Contain', 'finch-cart-product-recommendations' ),
							'auto'    => esc_html__( 'Auto', 'finch-cart-product-recommendations' ),
						),
					),
					$this->value['background-size'],
					$this->field_name(),
					'field/background'
				);

			}

			//
			// Background Origin
			if ( ! empty( $args['background_origin'] ) ) {

				EDITH::field(
					array(
						'id'      => 'background-origin',
						'type'    => 'select',
						'options' => array(
							''            => esc_html__( 'Background Origin', 'finch-cart-product-recommendations' ),
							'padding-box' => esc_html__( 'Padding Box', 'finch-cart-product-recommendations' ),
							'border-box'  => esc_html__( 'Border Box', 'finch-cart-product-recommendations' ),
							'content-box' => esc_html__( 'Content Box', 'finch-cart-product-recommendations' ),
						),
					),
					$this->value['background-origin'],
					$this->field_name(),
					'field/background'
				);

			}

			//
			// Background Clip
			if ( ! empty( $args['background_clip'] ) ) {

				EDITH::field(
					array(
						'id'      => 'background-clip',
						'type'    => 'select',
						'options' => array(
							''            => esc_html__( 'Background Clip', 'finch-cart-product-recommendations' ),
							'border-box'  => esc_html__( 'Border Box', 'finch-cart-product-recommendations' ),
							'padding-box' => esc_html__( 'Padding Box', 'finch-cart-product-recommendations' ),
							'content-box' => esc_html__( 'Content Box', 'finch-cart-product-recommendations' ),
						),
					),
					$this->value['background-clip'],
					$this->field_name(),
					'field/background'
				);

			}

			//
			// Background Blend Mode
			if ( ! empty( $args['background_blend_mode'] ) ) {

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

			}

			echo '</div>';

			echo wp_kses_post( $this->field_after() );
		}

		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 . ')';
				unset( $this->value['background-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;
		}
	}
}
