<?php

namespace Brandy\Customizer\Layouts;

use Brandy\Abstracts\AbstractLayout;
use Brandy\Builder\RowBuilder;
use Brandy\Traits\SingletonTrait;

class HeaderRowSettings extends AbstractLayout {

	use SingletonTrait;

	/**
	 * Components display in row settings
	 *
	 * @var array
	 */
	protected $components = array();

	/**
	 * Available rows
	 *
	 * @var array
	 */
	private $rows = array();

	protected function __construct() {

		$this->rows = array(
			'top'    => array(
				'title' => __( 'Top header', 'brandy' ),
			),
			'middle' => array(
				'title' => __( 'Middle header', 'brandy' ),
			),
			'bottom' => array(
				'title' => __( 'Bottom header', 'brandy' ),
			),

		);

		parent::__construct();

	}

	public function add_registered_settings( $settings = array() ) {
		foreach ( array_keys( $this->rows ) as $row ) {
			$id              = $row . '_header';
			$settings[ $id ] = $this->components;
		}
		return $settings;
	}

	/**
	 * Add row settings layout to the general
	 *
	 * @param array $layouts Registered layouts
	 *
	 * @return array Layout after adding
	 */
	public function add_layout( $layouts = array() ) {
		$layout        = array(
			'general' => array(
				'sections' => array(
					array(
						'components' => array( 'height_reset', 'height' ),
					),
					array(
						'components' => array( 'item_spacing' ),
					),
					array(
						'components' => array( 'enabled_devices' ),
					),
				),
			),
			'designs' => array(
				'sections' => array(
					array(
						'components' => array( 'background' ),
					),
					array(
						'components' => array( 'bottom_stroke_reset', 'bottom_stroke_color', 'bottom_stroke_width' ),
					),
					array(
						'components' => array( 'padding' ),
					)
				),
			),
		);
		$mapped_layout = $this->map_layout( $layout );
		foreach ( array_keys( $this->rows ) as $row ) {
			$id             = $row . '_header';
			$layouts[ $id ] = $mapped_layout;
		}
		return $layouts;
	}

	/**
	 * Register all components that display in row settings
	 */
	protected function register_components() {
		return array(
			'height_reset'               => array(
				'title'        => array(
					'text'         => __( 'Min height', 'brandy' ),
					'type'         => 'bold',
					'show_devices' => true,
				),
				'type'         => 'Reset',
				'reset_action' => 'row_settings',
				'reset_paths'  => array(
					array( 'height' ),
				),
			),
			'height'                     => array(
				'value_path'     => array( 'height' ),
				'type'           => 'Dimension',
				'default_value'  => array(
					'desktop' => array(
						'min'   => 10,
						'max'   => 500,
						'unit'  => 'px',
						'value' => 70,
					),
					'tablet'  => null,
					'mobile'  => null,
				),
				'hide_units'     => true,
				'render_options' => array(
					'type' => 'variable',
					'data' => array(
						array(
							'type'       => 'dimension',
							'name'       => '--brandy-header-height',
							'value_path' => array( 'height' ),
						),
					),
				),
			),
			'item_spacing'               => array(
				'value_path'     => array( 'item_spacing' ),
				'title'          => array(
					'text'         => __( 'Item Spacing', 'brandy' ),
					'type'         => 'bold',
					'show_devices' => true,
				),
				'units'          => array( 'px' ),
				'default_value'  => array(
					'desktop' => array(
						'unit'  => 'px',
						'value' => 26,
						'min'   => 0,
						'max'   => 100,
					),
					'tablet'  => null,
					'mobile'  => null,
				),
				'type'           => 'Dimension',
				'render_options' => array(
					'type' => 'variable',
					'data' => array(
						array(
							'type'       => 'dimension',
							'name'       => '--brandy-header-row-element-spacing',
							'value_path' => array( 'item_spacing' ),
						),
					),
				),
			),
			'enabled_devices'            => array(
				'value_path'     => array( 'enabled_devices' ),
				'title'          => array(
					'text' => __( 'Visible on', 'brandy' ),
					'type' => 'bold',
				),
				'default_value'  => array( 'desktop', 'mobile' ),
				'type'           => 'EnabledDevices',
				'render_options' => array(
					'type'     => 'custom',
					'selector' => '',
				),
			),
			'background'                 => array(
				'value_path'     => array( 'background' ),
				'title'          => array(
					'text' => __( 'Background', 'brandy' ),
					'type' => 'bold',
				),
				'default_value'  => array(
					'type'           => 'solid',
					'solid_color'    => '#ffffff',
					'gradient_color' => 'linear-gradient(90deg, RGBA(27, 60, 221, 1) 0%, rgba(251,208,238,1) 100%)',
					'image'          => array(
						'url'           => '',
						'top'           => array(
							'unit'  => 'px',
							'min'   => -200,
							'max'   => 200,
							'value' => 0,
						),
						'left'          => array(
							'unit'  => 'px',
							'min'   => -200,
							'max'   => 200,
							'value' => 0,
						),
						'overlay_color' => '#ffffff00',
						'size'          => 'auto',
						'position'      => 'left',
					),
				),
				'type'           => 'Background',
				'render_options' => array(
					'type' => 'variable',
					'data' => array(
						array(
							'type'       => 'background',
							'name'       => '--brandy-background',
							'value_path' => array( 'background' ),
						),
					),
				),
			),
			'bottom_stroke_reset'        => array(
				'type'         => 'Reset',
				'title'        => array(
					'text' => __( 'Bottom stroke', 'brandy' ),
					'type' => 'bold',
				),
				'reset_action' => 'row_settings',
				'reset_paths'  => array(
					array( 'bottom_stroke' ),
				),
			),
			'bottom_stroke_color'        => array(
				'type'           => 'ColorGroup',
				'title'          => array(
					'text' => __( 'Color', 'brandy' ),
					'type' => 'normal',
				),
				'value_path'     => array( 'bottom_stroke', 'color' ),
				'default_value'  => array(
					'normal' => 'var(--wp--preset--color--brandy-border)',
				),
				'render_options' => array(
					'type' => 'variable',
					'data' => array(
						array(
							'type'       => 'color',
							'name'       => '--brandy-stroke-color',
							'value_path' => array( 'bottom_stroke', 'color' ),
						),
					),
				),
			),
			'bottom_stroke_width'        => array(
				'type'           => 'Dimension',
				'title'          => array(
					'text' => __( 'Width', 'brandy' ),
					'type' => 'normal',
				),
				'value_path'     => array( 'bottom_stroke', 'width' ),
				'default_value'  => array(
					'unit'  => 'px',
					'min'   => 0,
					'max'   => 20,
					'value' => 0,
				),
				'render_options' => array(
					'type' => 'variable',
					'data' => array(
						array(
							'type'       => 'dimension',
							'name'       => '--brandy-stroke-width',
							'value_path' => array( 'bottom_stroke', 'width' ),
						),
					),
				),
			),
			'padding'                    => array(
				'value_path'     => array( 'padding' ),
				'title'          => array(
					'text'         => 'Padding',
					'type'         => 'bold',
					'show_devices' => true,
				),
				'default_value'  => array(
					'desktop' => array(
						'unit'           => 'px',
						'top'            => 0,
						'right'          => 0,
						'bottom'         => 0,
						'left'           => 0,
						'is_constraints' => false,
					),
					'tablet'  => null,
					'mobile'  => null,
				),
				'type'           => 'Spacing',
				'render_options' => array(
					'type' => 'variable',
					'data' => array(
						array(
							'type'       => 'spacing',
							'name'       => '--brandy-header-padding',
							'value_path' => array( 'padding' ),
						),
					),
				),
			),
		);
	}

	/**
	 * Add partial refresh for row settings
	 */
	public function add_partial_refresh( $partials = array() ) {
		foreach ( brandy_get_devices() as $device ) {
			foreach ( array_keys( $this->rows ) as $row ) {
				$id         = $row . '_header_' . $device;
				$partials[] = array(
					'configuration_type' => 'control',
					'id'                 => $id,
					'partial'            => array(
						'selector'            => '#brandy-header [device=' . $device . '] #brandy-' . $row . '-header',
						'render_callback'     => array( new RowBuilder( 'header', $row, $device ), 'render' ),
						'container_inclusive' => true,
						'fallback_refresh'    => false,
					),
					'default'            => '',
					'transport'          => 'postMessage',
				);
			}
		}
		return $partials;
	}
}
