<?php

namespace Brandy\Customizer\Layouts;

use Brandy\Abstracts\AbstractLayout;
use Brandy\Builder\Header\ToggleOffCanvasBuilder;
use Brandy\Customizer\Elements\ElementsLoader;
use Brandy\Traits\SingletonTrait;

class ToggleOffCanvasSettings extends AbstractLayout {

	use SingletonTrait;

	protected $components = array();

	protected $section_id = 'toggle_off_canvas';

	public function add_layout( $layouts = array() ) {
		$layout = array(
			'general' => array(
				'sections' => array(
					array(
						'components'         => array(
							'canvas_type',
						),
					),
					array(
						'visible_conditions' => array(
							'relation' => 'AND',
							array(
								'value_path' => array( 'canvas_type' ),
								'value'      => 'default',
								'operator'   => 'NOT',
							),
							array(
								'devices' => array( 'desktop' ),
							),
						),
						'components'         => array(
							'popup_width',
						),
					),
					// array(
					// 	'components' => array(
					// 		'horizontal_alignment',
					// 	),
					// ),
					array(
						'components' => array(
							'css_class',
						),
					),
				),
			),
			'designs' => array(
				'sections' => array(
					array(
						'components' => array(
							'panel_reset',
							'panel_background',
							'panel_backdrop_color',

						),
					),
					array(
						'components' => array(
							'close_button_reset',
							'close_button_icon_color',
							'close_button_size',
						),
					),
					array(
						'components' => array( 'padding' ),
					),
				),
			),
		);
		$mapped_layout                = $this->map_layout( $layout );
		$layouts[ $this->section_id ] = $mapped_layout;
		return $layouts;
	}

	protected function register_components() {
		return array(
			'canvas_type'                   => array(
				'title'          => array(
					'text' => __( 'Canvas type', 'brandy' ),
					'type' => 'bold',
				),
				'type'           => 'ToggleOffCanvasType',
				'default_value'  => 'right_panel',
				'value_path'     => array( 'canvas_type' ),
				'render_options' => array(
					'type' => 'force_refresh',
				),
			),
			'popup_width'                   => array(
				'title'          => array(
					'text' => __( 'Popup width', 'brandy' ),
					'type' => 'bold',
				),
				'type'           => 'Dimension',
				'default_value'  => array(
					'unit'  => 'px',
					'value' => 300,
					'min'   => 100,
					'max'   => 1000,
				),
				'value_path'     => array( 'popup_width' ),
				'render_options' => array(
					'type' => 'variable',
					'data' => array(
						array(
							'type'       => 'dimension',
							'selector'   => '.brandy-toc-panel',
							'name'       => 'width',
							'value_path' => array( 'popup_width' ),
						),
					),
				),
			),
			// 'horizontal_alignment'          => array(
			// 	'title'          => array(
			// 		'text' => __( 'Content horizontal alignment', 'brandy' ),
			// 		'type' => 'bold',
			// 	),
			// 	'type'           => 'Alignment',
			// 	'default_value'  => 'left',
			// 	'value_path'     => array( 'horizontal_align' ),
			// 	'render_options' => array(
			// 		'type' => 'data_attribute',
			// 		'data' => array(
			// 			array(
			// 				'selector'   => '',
			// 				'name'       => 'horizontal-alignment',
			// 				'value_path' => array( 'horizontal_align' ),
			// 			),
			// 		),
			// 	),
			// ),
			'css_class'                     => array(
				'title'          => array(
					'text' => __( 'CSS classes', 'brandy' ),
					'type' => 'bold',
				),
				'value_path'     => array( 'css_class' ),
				'default_value'  => '',
				'type'           => 'TextInput',
				'render_options' => array(
					'type' => 'data_attribute',
					'data' => array(
						array(
							'selector'     => '.brandy-element-wrapper',
							'name'         => 'class',
							'custom_class' => true,
							'value_path'   => array( 'css_class' ),
						),
					),
				),
			),
			'panel_reset'                   => array(
				'title'        => array(
					'text' => __( 'Panel', 'brandy' ),
					'type' => 'bold',
				),
				'type'         => 'Reset',
				'reset_action' => 'row_settings',
				'reset_paths'  => array(
					array(
						'panel',
					),
				),
			),
			'panel_background'              => array(
				'title'          => array(
					'text' => __( 'Background', 'brandy' ),
					'type' => 'normal',
				),
				'type'           => 'ColorGroup',
				'value_path'     => array( 'panel', 'background' ),
				'default_value'  => array(
					'normal' => '#fff',
				),
				'render_options' => array(
					'type' => 'variable',
					'data' => array(
						array(
							'type'       => 'color',
							'selector'   => '.brandy-toc-panel',
							'name'       => '--toc-background',
							'value_path' => array( 'panel', 'background' ),
						),
					),
				),
			),
			'panel_backdrop_color'          => array(
				'title'          => array(
					'text' => __( 'Backdrop color', 'brandy' ),
					'type' => 'normal',
				),
				'type'           => 'ColorGroup',
				'value_path'     => array( 'panel', 'backdrop_color' ),
				'default_value'  => array(
					'normal' => '#d1ccccc2',
				),
				'render_options' => array(
					'type' => 'variable',
					'data' => array(
						array(
							'type'       => 'color',
							'selector'   => '.brandy-toc-backdrop',
							'name'       => '--toc-backdrop-color',
							'value_path' => array( 'panel', 'backdrop_color' ),
						),
					),
				),
			),
			'close_button_reset'            => array(
				'title'        => array(
					'text' => __( 'Close button', 'brandy' ),
					'type' => 'bold',
				),
				'type'         => 'Reset',
				'reset_action' => 'row_settings',
				'reset_paths'  => array(
					array(
						'close_button',
					),
				),
			),
			'close_button_icon_color'       => array(
				'title'          => array(
					'text' => __( 'Icon color', 'brandy' ),
					'type' => 'normal',
				),
				'type'           => 'ColorGroup',
				'value_path'     => array( 'close_button', 'icon_color' ),
				'default_value'  => array(
					'normal' => '#000',
					'hover'  => '#000',
				),
				'render_options' => array(
					'type' => 'variable',
					'data' => array(
						array(
							'type'       => 'color',
							'selector'   => '.brandy-toc-panel',
							'name'       => '--icon-color',
							'value_path' => array( 'close_button', 'icon_color' ),
						),
					),
				),
			),
			'close_button_size'             => array(
				'title'          => array(
					'text' => __( 'Icon size', 'brandy' ),
					'type' => 'normal',
				),
				'type'           => 'Dimension',
				'value_path'     => array( 'close_button', 'size' ),
				'default_value'  => ElementsLoader::get_default_icon_size(
					array(
						'min'   => 20,
						'value' => 22,
					)
				),
				'render_options' => array(
					'type' => 'variable',
					'data' => array(
						array(
							'type'       => 'dimension',
							'selector'   => '.brandy-toc-panel',
							'name'       => '--icon-size',
							'value_path' => array( 'close_button', 'size' ),
						),
					),
				),
			),
			'padding'                       => array(
				'value_path'     => array( 'padding' ),
				'title'          => array(
					'text' => 'Padding',
					'type' => 'bold',
				),
				'default_value'  => array(
					'unit'           => 'px',
					'top'            => 20,
					'right'          => 20,
					'bottom'         => 20,
					'left'           => 20,
					'is_constraints' => false,
				),
				'type'           => 'Spacing',
				'render_options' => array(
					'type' => 'variable',
					'data' => array(
						array(
							'type'       => 'spacing',
							'selector'   => '.brandy-toc-panel',
							'name'       => 'padding',
							'value_path' => array( 'padding' ),
						),
					),
				),
			),
		);
	}

	public function add_partial_refresh( $partials = array() ) {
		$partials[] = array(
			'configuration_type' => 'control',
			'id'                 => $this->section_id,
			'partial'            => array(
				'selector'            => '#toggle-off-canvas',
				'render_callback'     => array( ToggleOffCanvasBuilder::get_instance(), 'render_toggle_off_canvas' ),
				'container_inclusive' => true,
				'fallback_refresh'    => false,
			),
			'default'            => '',
			'transport'          => 'postMessage',
		);
		return $partials;
	}
}
