<?php

namespace Webien\Elementor\Extensions;

use Elementor\Element_Base;

class ContainerPadding
{
    function __construct()
    {
        $this->register_hooks();
    }

//-------------------------------------------------------------------//
// Hooks
//-------------------------------------------------------------------//

    private function register_hooks()
    {
        if (is_admin()) {
            add_action('elementor/element/before_section_end', [$this, 'register_controls'], 10, 2);
        }
        add_action('elementor/frontend/container/before_render', [$this, 'before_render'], 20, 1);
    }


    public static function before_render(Element_Base $element)
    {
        $settings = $element->get_settings();

        // If no padding selected, set default as value
        if(!isset($settings['webien_container_padding'])){
            $settings['webien_container_padding'] = 'default';
        }

        if (isset($settings['webien_container_padding'])) {

            $wrapper = get_class($element) === 'Elementor\Includes\Elements\Container' ? '_wrapper' : '_inner_wrapper';

            $class = $settings['webien_container_padding'] ? 'webien-container-padding-' . $settings['webien_container_padding'] : '';

            $render_arr = [
                'class' => $class,
            ];


            echo '<style>
                .elementor-element-' . $element->get_id() . '{
                --padding-top: var(--webien-padding-top-' . $settings['webien_container_padding'] . ');
                --padding-right: var(--webien-padding-right-' . $settings['webien_container_padding'] . ');
                --padding-bottom: var(--webien-padding-bottom-' . $settings['webien_container_padding'] . ');
                --padding-left: var(--webien-padding-left-' . $settings['webien_container_padding'] . ');
                }
            </style>';


            $element->add_render_attribute($wrapper, $render_arr);
        }
    }


    public function register_controls($element, $section_id)
    {

        $sections = [
            'layout_container',
            'section_layout_container'
        ];

        if (!in_array($section_id, $sections)) {
            return;
        }

        $element->add_control(
            'webien_container_padding',
            [
                'label' => __('Padding', 'webien'),
                'type' => \Elementor\Controls_Manager::SELECT,
                'description' => __('Ev. värden från Avancerat fliken trumfar detta.'),
                'options' => [
                    'default' => __('Standard', 'webien'),
                    'none' => __('Ingen padding', 'webien'),
                    'xs' => __('Extra liten', 'webien'),
                    's' => __('Liten', 'webien'),
                    'm' => __('Medium', 'webien'),
                    'l' => __('Stor', 'webien'),
                    'xl' => __('Extra stor', 'webien'),
                ],
                'default' => 'default',
                'selectors' => [
                    '{{WRAPPER}}' => '--padding-top: var(--webien-padding-top-{{VALUE}}); --padding-right: var(--webien-padding-right-{{VALUE}}); --padding-bottom: var(--webien-padding-bottom-{{VALUE}}); --padding-left: var(--webien-padding-left-{{VALUE}});',
                    //'{{WRAPPER}}' => '--padding-block-start: var(--webien-padding-top-{{VALUE}}); --padding-inline-end: var(--webien-padding-right-{{VALUE}}); --padding-block-end: var(--webien-padding-bottom-{{VALUE}}); --padding-inline-start: var(--webien-padding-left-{{VALUE}});',
                ],
                'separator' => 'before',
            ]
        );

    }

}
