<?php
/**
 * Main Class
 *
 * @author   Fernando_Acosta
 * @since    1.0.0
 * @package  make-column-clickable-elementor
 */

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

if (!class_exists('Pakrco_Elementor_Parallax')) :

    /**
     * The main Make_Column_Clickable_Setup class
     */
    class Pakrco_Elementor_Parallax {
        public function __construct() {
            add_action('elementor/element/section/section_background/before_section_end', [$this, 'register_controls'], 10, 2);
            add_action('elementor/element/column/section_style/before_section_end', [$this, 'register_controls'], 10, 2);
            add_action('elementor/element/common/_section_background/before_section_end', [$this, 'register_controls'], 10, 2);
            add_action('pakrco_breadcrumb_page_settings_parallax', [$this, 'register_controls'], 10, 1);

            add_action('elementor/frontend/section/before_render', [$this, 'before_render_element'], 10, 1);
            add_action('elementor/frontend/column/before_render', [$this, 'before_render_element'], 10, 1);
            add_action('elementor/frontend/widget/before_render', [$this, 'before_render_element'], 10, 1);

            add_action('elementor/frontend/after_enqueue_scripts', [$this, 'scripts']);
        }

        public function scripts() {
            wp_enqueue_script('pakrco-elementor-parallax', get_theme_file_uri('includes/elementor/modules/parallax/assets/parallax-library.js'), ['jquery'], PAKRCO_VERSION, true);
        }

        public function before_render_element($element) {
            $settings = $element->get_settings();
            if (!empty($settings['lb_parallax_enable'])) {

                $class = 'lb-parallax-section';
                $type  = $element->get_type();

                if ($type == 'widget') {
                    $class = 'lb-parallax-widget';
                }
                if ($type == 'column') {
                    $class = 'lb-parallax-column';
                }

                $element->add_render_attribute('_wrapper', [
                    'class'                    => $class . ' lb-parallax',
                    'data-lb-parallax-speed' => $settings['lb_parallax_speed'],
                    'data-lb-parallax-bleed' => $settings['lb_parallax_bleed'],
                ]);

                if (!empty($settings['lb_parallax_enable_new_version'])) {
                    $element->add_render_attribute('_wrapper', [
                        'data-lb-parallax-new-version' => '',
                    ]);
                }

            }
        }

        public function register_controls($element, $args = []) {
            $description = esc_html__('Note that currently parallax are not visible in edit/preview mode & can only be viewed on the frontend.', 'pakrco');

            $element->add_control(
                'lb_parallax_enable',
                [
                    'label'        => esc_html__('Enable Parallax', 'pakrco'),
                    'type'         => \Elementor\Controls_Manager::SWITCHER,
                    'description'  => $description,
                    'default'      => '',
                    'label_on'     => 'Yes',
                    'label_off'    => 'No',
                    'return_value' => 'yes',
                ]
            );

            $element->add_control(
                'lb_parallax_enable_new_version',
                [
                    'label'        => esc_html__('Enable Parallax New Version', 'pakrco'),
                    'type'         => \Elementor\Controls_Manager::SWITCHER,
                    'description'  => esc_html__('Parallax effect will work on iPhone / iPad but it does not look good', 'pakrco'),
                    'default'      => '',
                    'label_on'     => 'Yes',
                    'label_off'    => 'No',
                    'return_value' => 'yes',
                    'condition'    => [
                        'lb_parallax_enable' => 'yes',
                    ],
                ]
            );

            $element->add_control(
                'lb_parallax_speed',
                [
                    'label'       => esc_html__('Speed', 'pakrco'),
                    'type'        => \Elementor\Controls_Manager::NUMBER,
                    'description' => esc_html__('Speed of the parallax. You can use any floating point number. But practically, give any fractional number between 0 and 1. Example: 0.2 or 0.5', 'pakrco'),
                    'default'     => 0.2,
                    'min'         => 0,
                    'max'         => 1,
                    'step'        => 0.1,
                    'condition'   => [
                        'lb_parallax_enable' => 'yes',
                    ],
                ]
            );

            $element->add_control(
                'lb_parallax_bleed',
                [
                    'label'       => esc_html__('Offset', 'pakrco'),
                    'type'        => \Elementor\Controls_Manager::NUMBER,
                    'description' => esc_html__('The parallax offset. Possible values: Any integer.', 'pakrco'),
                    'default'     => 0,
                    'condition'   => [
                        'lb_parallax_enable' => 'yes',
                    ],
                ]
            );
        }
    }

endif;

new Pakrco_Elementor_Parallax();
