<?php
namespace Sifency\Traits;

use Elementor\Controls_Manager;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

trait Sifency_Addons_Flex_Controls {

	protected function add_sifency_addons_flex_alignment_controls( $args = array() ) {

		$defaults = array(
			'prefix'    => 'flex_',
			'label'     => esc_html__( 'Flex Layout', 'sifency-addons' ),
			'selector'  => '',
			'condition' => array(),
		);

		$args     = wp_parse_args( $args, $defaults );
		$prefix   = $args['prefix'];
		$base_sel = $args['selector'];

		$full_base_sel = ( strpos( $base_sel, '{{WRAPPER}}' ) === false ) ? '{{WRAPPER}} ' . $base_sel : $base_sel;

		$this->add_control(
			$prefix . 'heading',
			array(
				'label'     => $args['label'],
				'type'      => Controls_Manager::HEADING,
				'separator' => 'before',
				'condition' => $args['condition'],
			)
		);

		$this->add_responsive_control(
			$prefix . 'use_flex',
			array(
				'label'     => esc_html__( 'Display', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'default'   => '',
				'options'   => array(
					''             => esc_html__( 'Default', 'sifency-addons' ),
					'block'        => esc_html__( 'Block', 'sifency-addons' ),
					'inline-block' => esc_html__( 'Inline Block', 'sifency-addons' ),
					'flex'         => esc_html__( 'Flex', 'sifency-addons' ),
					'inline-flex'  => esc_html__( 'Inline Flex', 'sifency-addons' ),
					'none'         => esc_html__( 'None', 'sifency-addons' ),
				),
				'selectors' => array(
					$full_base_sel => 'display: {{VALUE}};',
				),
				'condition' => $args['condition'],
			)
		);

		$flex_condition = array_merge(
			$args['condition'],
			array(
				$prefix . 'use_flex' => array( 'flex', 'inline-flex' ),
			)
		);

		$this->add_responsive_control(
			$prefix . 'direction',
			array(
				'label'     => esc_html__( 'Direction', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'row'            => array(
						'title' => 'Row',
						'icon'  => 'eicon-arrow-right',
					),
					'column'         => array(
						'title' => 'Column',
						'icon'  => 'eicon-arrow-down',
					),
					'row-reverse'    => array(
						'title' => 'Row Reverse',
						'icon'  => 'eicon-arrow-left',
					),
					'column-reverse' => array(
						'title' => 'Column Reverse',
						'icon'  => 'eicon-arrow-up',
					),
				),
				'selectors' => array( $full_base_sel => 'flex-direction: {{VALUE}};' ),
				'condition' => $flex_condition,
			)
		);

		$this->add_responsive_control(
			$prefix . 'justify',
			array(
				'label'     => esc_html__( 'Justify Content', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start'    => array(
						'title' => 'Start',
						'icon'  => 'eicon-flex eicon-justify-start-h',
					),
					'center'        => array(
						'title' => 'Center',
						'icon'  => 'eicon-flex eicon-justify-center-h',
					),
					'flex-end'      => array(
						'title' => 'End',
						'icon'  => 'eicon-flex eicon-justify-end-h',
					),
					'space-between' => array(
						'title' => 'Space Between',
						'icon'  => 'eicon-flex eicon-justify-space-between-h',
					),
					'space-around'  => array(
						'title' => 'Space Around',
						'icon'  => 'eicon-flex eicon-justify-space-around-h',
					),
				),
				'selectors' => array( $full_base_sel => 'justify-content: {{VALUE}};' ),
				'condition' => $flex_condition,
			)
		);

		$this->add_responsive_control(
			$prefix . 'align',
			array(
				'label'     => esc_html__( 'Align Items', 'sifency-addons' ),
				'type'      => Controls_Manager::CHOOSE,
				'options'   => array(
					'flex-start' => array(
						'title' => 'Top',
						'icon'  => 'eicon-flex eicon-align-start-v',
					),
					'center'     => array(
						'title' => 'Middle',
						'icon'  => 'eicon-flex eicon-align-center-v',
					),
					'flex-end'   => array(
						'title' => 'Bottom',
						'icon'  => 'eicon-flex eicon-align-end-v',
					),
					'stretch'    => array(
						'title' => 'Stretch',
						'icon'  => 'eicon-flex eicon-align-stretch-v',
					),
				),
				'selectors' => array( $full_base_sel => 'align-items: {{VALUE}};' ),
				'condition' => $flex_condition,
			)
		);

		$this->add_control(
			$prefix . 'wrap',
			array(
				'label'     => esc_html__( 'Flex Wrap', 'sifency-addons' ),
				'type'      => Controls_Manager::SELECT,
				'options'   => array(
					'nowrap'       => 'No Wrap',
					'wrap'         => 'Wrap',
					'wrap-reverse' => 'Wrap Reverse',
				),
				'default'   => 'wrap',
				'selectors' => array( $full_base_sel => 'flex-wrap: {{VALUE}};' ),
				'condition' => $flex_condition,
			)
		);

		$this->add_responsive_control(
			$prefix . 'gap',
			array(
				'label'      => esc_html__( 'Gap', 'sifency-addons' ),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => array( 'px', '%', 'em' ),
				'selectors'  => array( $full_base_sel => 'gap: {{SIZE}}{{UNIT}};' ),
				'condition'  => $flex_condition,
			)
		);
	}
}
