<?php

namespace Webien\Elementor\Extensions\Animations;

use Elementor\Element_Base;

class Animations
{
    function __construct()
    {
        $useAnimations = get_option('options_webien_use_animation_settings', false);
        if(!$useAnimations) return;

        $this->register_hooks();
    }

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

    private function register_hooks()
    {

        if (is_admin()) {
            //add_action('elementor/element/section/section_advanced/after_section_end', [$this, 'register_controls'], 10, 2);
            //add_action('elementor/element/column/section_advanced/after_section_end', [$this, 'register_controls'], 10, 2);
            add_action('elementor/element/after_section_end', [$this, 'register_controls'], 10, 2);
        }

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


        // Register scripts here and include only when needed later with script_depend function in widget file
        wp_enqueue_script( 'waypoints', 'https://cdnjs.cloudflare.com/ajax/libs/waypoints/4.0.1/noframework.waypoints.min.js', [ 'jquery' ], '1.0.0', true );
        wp_enqueue_script( 'webien-animations', plugin_dir_url(__FILE__) .'js/script.js', [ 'jquery' ], '1.0.0', true );

    }


    public function register_controls($element, $section_id )
    {

        $sections = [
            //'layout',
            //'section_layout',
            'section_layout_container',
        ];

        $element_name = $element->get_name();

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

        $element->start_controls_section(
            'webien_advanced_controls',
            [
                'label' => __( 'Webien', 'webien' ),
                'tab' => \Elementor\Controls_Manager::TAB_ADVANCED,
            ]
        );

        $element->add_control(
            'webien_scroll_events',
            [
                'label'     => __( 'Trigga scroll händelse' , 'webien' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'description'   => __( '' ),
                'default'   => 'no',
            ]
        );

        $element->add_control(
            'webien_scroll_events_note',
            [
                'type' => \Elementor\Controls_Manager::RAW_HTML,
                'raw' => 'Adderar en klass på sektionen när scrollhändelse inträffar. Klassen <b><i>scrolled</i></b> togglas på neråt scroll, <b><i>unscrolled</i></b> på uppåt scroll.',
                'content_classes' => 'webien-control-info',
                'condition' => ['webien_scroll_events' => 'yes',],
            ]
        );

        $element->add_control(
            'webien_scroll_offset',
            [
                'label' => esc_html__( 'Offset', 'webien' ),
                'type' => \Elementor\Controls_Manager::SLIDER,
                'description' => __('Hur långt innan toppen av fönstret det ska triggas.', 'webien'),
                'size_units' => [ 'px', '%' ],
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 1000,
                        'step' => 1,
                    ],
                    '%' => [
                        'min' => 0,
                        'max' => 100,
                        'step' => 1,
                    ],
                ],
                'default' => [
                    'unit' => '%',
                    'size' => 50,
                ],
                'condition' => [
                    'webien_scroll_events' => 'yes',
                ]
            ]
        );
/*
        $element->add_control(
            'webien_scroll_effect',
            [
                'label' => __( 'Välj effekt', 'webien' ),
                'type' => \Elementor\Controls_Manager::SELECT,
                'options' => [
                    'zoom-in' => 'Zooma in',
                    'zoom-out' => 'Zooma ut',
                    'slide-left' => 'Slide vänster',
                    'slide-right' => 'Slide höger',
                ],
                'separator' => 'before',
                'condition' => [
                    'webien_scroll_events' => 'yes',
                ],
            ]
        );

        $element->add_control(
            'webien_scroll_effect_duration',
            [
                'label'     => __( 'Tid för animation' , 'webien' ),
                'type'      => \Elementor\Controls_Manager::NUMBER,
                'min'       => 0,
                'max'       => 30,
                'step'      => 0.1,
                'default'   => 1,
                'condition' => [
                    'webien_scroll_events' => 'yes',
                ],
            ]
        );
*/
        $element->add_control(
            'webien_scroll_advanced',
            [
                'label'     => __( 'Använd avancerad scroll animering' , 'webien' ),
                'type'      => \Elementor\Controls_Manager::SWITCHER,
                'description'   => __( '' ),
                'default'   => 'no',
            ]
        );

        $element->add_control(
            'webien_scroll_advanced_info',
            [
                'type' => \Elementor\Controls_Manager::RAW_HTML,
                'raw' => 'Matcha dina klasser etc med koden du stoppar in här. Dokumentation och demos kan hittas <a href="https://greensock.com/st-demos/" target="_blank">här &rarr;</a>',
                'content_classes' => 'webien-control-info',
                'condition' => [
                    'webien_scroll_advanced' => 'yes',
                ],
            ]
        );

        $element->add_control(
            'webien_scroll_advanced_code',
            [
                'label' => esc_html__( 'Javascript med gsap funktioner', 'webien' ),
                'type' => \Elementor\Controls_Manager::CODE,
                'language' => 'javascript',
                'rows' => 30,
                'condition' => [
                    'webien_scroll_advanced' => 'yes',
                ],
            ]
        );

        $element->add_control(
            'webien_scroll_advanced_style',
            [
                'label' => esc_html__( 'CSS', 'webien' ),
                'type' => \Elementor\Controls_Manager::CODE,
                'language' => 'css',
                'rows' => 20,
                'condition' => [
                    'webien_scroll_advanced' => 'yes',
                ],
            ]
        );

        $element->end_controls_section();
    }

    public function add_wrapper_settings(Element_Base $element){

        $element_settings = $element->get_settings();

        if(isset($element_settings['webien_scroll_advanced']) && $element_settings['webien_scroll_advanced'] === 'yes'){


            // Get all previous attributes that needs to come along on in the new <a> tag
            $wrapper = in_array(get_class($element), [
                'Elementor\Element_Section',
                'Elementor\Includes\Elements\Container'
            ]) ? '_wrapper' : '_inner_wrapper';

            //$wrapper = get_class($element) === 'Elementor\Element_Section' ? '_wrapper' : '_inner_wrapper';

            $render_arr = [
                'class' => 'webien-scroll-advanced',
            ];

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



        if(isset($element_settings['webien_scroll_events']) && $element_settings['webien_scroll_events'] === 'yes'){

            $scrollData = [
                'offset' => 0,
            ];

            if(isset($element_settings['webien_scroll_offset']) ) {
                $measure = $element_settings['webien_scroll_offset']['size'];
                $measure.= $element_settings['webien_scroll_offset']['unit'] === '%' ? '%':'';
                $scrollData['offset'] = $measure;
            }

            $scrollData['effect'] = $element_settings['webien_scroll_effect'] ?? '';
            $scrollData['effect_duration'] = $element_settings['webien_scroll_effect_duration'] ?? '1s';


            //$wrapper = get_class($element) === 'Elementor\Element_Section' ? '_wrapper' : '_inner_wrapper';

            // Get all previous attributes that needs to come along on in the new <a> tag
            $wrapper = in_array(get_class($element), [
                'Elementor\Element_Section',
                'Elementor\Includes\Elements\Container'
            ]) ? '_wrapper' : '_inner_wrapper';

            $render_arr = [
                'class' => 'webien-scroll',
                'webien-scroll' => json_encode($scrollData)
            ];

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


        if(
            isset($element_settings['webien_scroll_advanced']) &&
            $element_settings['webien_scroll_advanced'] === 'yes' &&
            isset($element_settings['webien_scroll_advanced_code']) &&
            !empty($element_settings['webien_scroll_advanced_code'])
        ){
            echo '<script defer src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/gsap.min.js"></script>';
            echo '<script defer src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/ScrollTrigger.min.js"></script>';
            echo '<script defer src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.9.1/ScrollToPlugin.min.js"></script>';

            echo '<script defer>jQuery(document).ready(function ($) {' . $element_settings['webien_scroll_advanced_code'] . '});</script>';
            echo '<style>' . $element_settings['webien_scroll_advanced_style'] . '</style>';

            echo $element_settings['webien_scroll_advanced_html'];
        }
    }

}
