<?php

defined( 'ABSPATH' ) or die( __( 'Make love not war.', 'so-page-builder-animate' ) );

add_filter( 'siteorigin_panels_widget_style_groups', 'quidus_SO_animations_style_groups', 2, 3 );
add_filter( 'siteorigin_panels_widget_style_fields', 'quidus_SO_animations_style_fields', 1, 3 );
add_filter( 'siteorigin_panels_widget_style_attributes', 'quidus_SO_animations_style_attributes', 1, 2 );

add_filter( 'siteorigin_panels_row_style_groups', 'quidus_SO_animations_style_groups', 2, 3 );
add_filter( 'siteorigin_panels_row_style_fields', 'quidus_SO_animations_style_fields', 1, 3 );
add_filter( 'siteorigin_panels_row_style_attributes', 'quidus_SO_animations_style_attributes', 1, 2 );

function quidus_SO_animations_style_groups( $groups ) 
{
    
    $groups['animations'] = array(
    	'name' => __('Animations', 'so-page-builder-animate'),
   	 	'priority' => 30
    );

    return $groups;
}

function quidus_SO_animations_style_fields( $fields ) 
{
    /**/
    $quidus_animations = array(
		'' => 'No Animations',
		'fadeIn' => 'Fade In',
		'fadeInDown' => 'Fade In Down',
		'fadeInLeft' => 'Fade In Left',
		'fadeInRight' => 'Fade In Right',
		'fadeInUp' => 'Fade In Up',
		'flipInX' => 'Flip In Horizontal',
		'flipInY' => 'Flip In Vertical',
		'rotateIn' => 'Rotate In',
		'rotateInDownLeft' => 'Rotate In Down Left',
		'rotateInDownRight' => 'Rotate In Down Right',
		'rotateInUpLeft' => 'Rotate In Up Left',
		'rotateInUpRight' => 'Rotate In Up Right',
		'zoomIn' => 'Zoom In',
		'slideInLeft' => 'Slide In Left',
		'slideInDown' => 'Slide In Down',
		'slideInRight' => 'Slide In Right',
		'slideInUp' => 'Slide In Up',
    );
    
    $fields['quidus_animation_type'] = array(
		'name' => 'Type',
		'type' => 'select',
		'options' => (array)$quidus_animations,
		'group' => 'animations',
		'description' => 'Pick an animation style.',
		'priority' => 5
    );

	$fields['quidus_animation_duration'] = array(
		'name' => 'Duration',
		'type' => 'text',
		'group' => 'animations',
		'default' => '0.5s',
		'description' => 'Change the animation duration. Measured in seconds.',
		'priority' => 10
    );

    $fields['quidus_animation_delay'] = array(
		'name' => 'Delay',
		'type' => 'text',
		'default' => '0s',
		'group' => 'animations',
		'description' => 'Delay before the animation starts. Measured in seconds.',
		'priority' => 15
    );

    $fields['quidus_animation_offset'] = array(
	    'name' => 'Offset',
		'type' => 'text',
	    'default' => '10',
	    'group' => 'animations',
	    'description' => 'Distance to start the animation (related to the browser bottom). Measured in pixels.',
	    'priority' => 20
    );

    $fields['quidus_animation_iteration'] = array(
	    'name' => 'Iteration',
	    'type' => 'select',
	    'options' => array(
	        '1' => 'Don\'t Repeat',
	        '2' => 'Repeat 2 times',
	        '3' => 'Repeat 3 times',
	        '4' => 'Repeat 4 times',
	    ),
	    'group' => 'animations',
	    'description' => 'Number of times the animation is repeated.',
	    'priority' => 25
    );

    return $fields;
}

function quidus_SO_animations_style_attributes( $atts, $value ) 
{

    if ( empty( $value['quidus_animation_type'] ) ) {
        return $atts;
    }

    // Add the animate class to the class attribute.
    if ( ! empty( $value['quidus_animation_type'] ) ) {
        $atts['class'] = array( 'wow', $value['quidus_animation_type'] );
    }

    if ( ! empty( $value['quidus_animation_duration'] ) ) {
        $atts['data-wow-duration'] = wp_kses_post($value['quidus_animation_duration']);
 	}
    
    if ( ! empty( $value['quidus_animation_delay'] ) ) {
        $atts['data-wow-delay'] = wp_kses_post($value['quidus_animation_delay']);
    }
    
    if ( ! empty( $value['quidus_animation_offset'] ) ) {
        $atts['data-wow-offset'] = wp_kses_post($value['quidus_animation_offset']);
    }
    
    if ( ! empty($value['quidus_animation_iteration'] ) ) {
        $atts['data-wow-iteration'] = esc_attr($value['quidus_animation_iteration']);
    }

    return $atts;
}