<?php
/**
* Customizer Custom Classes.
*
* @package Vintuna
*/

/** Customizer Custom Control **/
if ( class_exists( 'WP_Customize_Control' ) ) {
    
    // Radio Image Custom Control Class.
    class Vintuna_Separator extends WP_Customize_Control {

        public $type = 'vintuna-separator';
    
        public function render_content() { ?>
            
            <span class="customize-control-title-custom">

                <?php echo esc_attr( $this->label ); ?>

                <?php if ( ! empty( $this->description ) ) : ?>
                    <span class="description customize-control-description"><?php echo esc_html( $this->description ); ?></span>
                <?php endif; ?>

            </span>

        <?php }
    }
    
}

/** Customizer Custom Control **/
if ( class_exists( 'WP_Customize_Control' ) ) {
    
    // Radio Image Custom Control Class.
    class Vintuna_Custom_Radio_Image_Control extends WP_Customize_Control {

        public $type = 'vintuna-radio-image';
    
        public function render_content() {
           
            if ( empty( $this->choices ) ) {
                return;
            }           
            
            $name = '_customize-radio-' . $this->id; ?>
            
            <span class="customize-control-title">
                <?php echo esc_html( $this->label ); ?>
                <?php if ( ! empty( $this->description ) ) : ?>
                    <span class="description customize-control-description"><?php echo esc_html( $this->description ); ?></span>
                <?php endif; ?>
            </span>
            
            <div id="input_<?php echo esc_attr($this->id); ?>" class="image vintuna-radio-image-buttenset">
                <?php foreach ( $this->choices as $value => $label ) : ?>
                    <input class="image-select" type="radio" value="<?php echo esc_attr( $value ); ?>" id="<?php echo esc_attr($this->id) . esc_attr($value); ?>" name="<?php echo esc_attr( $name ); ?>" <?php $this->link(); checked( $this->value(), $value ); ?>>
                        <label for="<?php echo esc_attr($this->id) . esc_attr($value); ?>">
                            <img src="<?php echo esc_attr( $label ); ?>" alt="<?php echo esc_attr( $value ); ?>" title="<?php echo esc_attr( $value ); ?>">
                        </label>
                    </input>
                <?php endforeach; ?>
            </div>
            
        <?php }
    }
    
}

/**
 * Upsell customizer section.
 *
 * @since  1.0.0
 * @access public
 */
class Vintuna_Customize_Section_Upsell extends WP_Customize_Section {

    /**
     * The type of customize section being rendered.
     *
     * @since  1.0.0
     * @access public
     * @var    string
     */
    public $type = 'vintuna_upsell';

    /**
     * Custom button text to output.
     *
     * @since  1.0.0
     * @access public
     * @var    string
     */
    public $pro_text = '';

    /**
     * Custom pro button URL.
     *
     * @since  1.0.0
     * @access public
     * @var    string
     */
    public $pro_url = '';

    public $notice = '';
    public $nonotice = '';

    /**
     * Add custom parameters to pass to the JS via JSON.
     *
     * @since  1.0.0
     * @access public
     * @return void
     */
    public function json() {
        $json = parent::json();

        $json['pro_text'] = $this->pro_text;
        $json['pro_url']  = esc_url( $this->pro_url );
        $json['notice']  = esc_attr( $this->notice );
        $json['nonotice']  = esc_attr( $this->nonotice );

        return $json;
    }

    /**
     * Outputs the Underscore.js template.
     *
     * @since  1.0.0
     * @access public
     * @return void
     */
    protected function render_template() { ?>

        <li id="accordion-section-{{ data.id }}" class="accordion-section control-section control-section-{{ data.type }} cannot-expand">

                <# if ( data.notice ) { #>
                    <h3 class="accordion-section-notice">
                        {{ data.title }}
                    </h3>
                <# } #>

                <# if ( !data.notice ) { #>
                    <h3 class="accordion-section-title">
                        {{ data.title }}

                        <# if ( data.pro_text && data.pro_url ) { #>
                            <a href="{{ data.pro_url }}" class="button button-secondary alignright" target="_blank">{{ data.pro_text }}</a>
                        <# } #>
                    </h3>
                <# } #>
            
        </li>
    <?php }
}


/**
 * Repeater Custom Control
*/
class Vintuna_Repeater_Controler extends WP_Customize_Control {
    /**
     * The control type.
     *
     * @access public
     * @var string
    */
    public $type = 'repeater';

    public $vintuna_box_label = '';

    public $vintuna_box_add_control = '';

    public $vintuna_box_add_button = '';

    private $cats = '';

    /**
     * The fields that each container row will contain.
     *
     * @access public
     * @var array
     */
    public $fields = array();

    /**
     * Repeater drag and drop controler
     *
     * @since  1.0.0
     */
    public function __construct( $manager, $id, $args = array(), $fields = array() ) {
        $this->fields = $fields;
        $this->vintuna_box_label = $args['vintuna_box_label'] ;
        $this->vintuna_box_add_control = $args['vintuna_box_add_control'];
        $this->vintuna_box_add_button = $args['vintuna_box_add_button'];
        $this->cats = get_categories(array( 'hide_empty' => false ));
        parent::__construct( $manager, $id, $args );
    }

    public function render_content() {

        $values = json_decode($this->value());
        ?>
        <span class="customize-control-title"><?php echo esc_html( $this->label ); ?></span>

        <?php if($this->description){ ?>
            <span class="description customize-control-description">
            <?php echo wp_kses_post($this->description); ?>
            </span>
        <?php } ?>

        <ul class="vintuna-repeater-field-control-wrap">
            <?php
            $this->vintuna_get_fields();
            ?>
        </ul>
        <input type="hidden" <?php esc_attr( $this->link() ); ?> class="vintuna-repeater-collector" value="<?php echo esc_attr( $this->value() ); ?>" />
        <?php

        if( isset( $this->vintuna_box_add_button ) && $this->vintuna_box_add_button ){ ?>
            
            <button type="button" class="button vintuna-add-control-field"><?php echo esc_html( $this->vintuna_box_add_control ); ?></button>
        <?php
        }

    }

    private function ToObject($Array) { 
      
        // Create new stdClass object 
        $object = new stdClass(); 
          
        // Use loop to convert array into 
        // stdClass object 
        foreach ($Array as $key => $value) { 
            if (is_array($value)) { 
                $value = $this->ToObject($value); 
            } 
            $object->$key = $value; 
        } 
        return $object; 
    } 

    private function vintuna_get_fields(){

        $fields = $this->fields;

        $values = json_decode( $this->value() );

        if( is_array( $values ) ){

            foreach($values as $value){ ?>

                <li class="vintuna-repeater-field-control">

                <div class="title-rep-wrap">
                    <span class="dashicons dashicons-move wpi-filter-icon"></span>
                    <h3 class="vintuna-repeater-field-title"><?php echo esc_html( $this->vintuna_box_label ); ?></h3>
                    <span class="dashicons dashicons-arrow-down wpi-filter-icon"></span>
                </div>

                <div class="vintuna-repeater-fields">

                    <?php
                    foreach ($fields as $key => $field) {

                        $class = isset( $field['class'] ) ? $field['class'] : ''; ?>

                        <div class="vintuna-fields vintuna-type-<?php echo esc_attr($field['type']).' '.esc_attr( $class ); ?>">
                                
                            <?php 
                            $label = isset($field['label']) ? $field['label'] : '';
                            $description = isset($field['description']) ? $field['description'] : '';
                            if($field['type'] != 'checkbox'){ ?>
                                <span class="customize-control-title"><?php echo esc_html( $label ); ?></span>
                                <span class="description customize-control-description"><?php echo esc_html( $description ); ?></span>
                            <?php 
                            }

                            $new_value = isset($value->$key) ? $value->$key : '';
                            $default = isset($field['default']) ? $field['default'] : '';

                            switch ($field['type']) {
                                case 'text':

                                    echo '<input data-default="'.esc_attr($default).'" data-name="'.esc_attr($key).'" type="text" value="'.esc_attr($new_value).'"/>';
                                
                                break;

                                case 'link':

                                    echo '<input data-default="'.esc_attr($default).'" data-name="'.esc_attr($key).'" type="text" value="'.esc_url($new_value).'"/>';
                                
                                break;

                                case 'textarea':

                                    echo '<textarea data-default="'.esc_attr($default).'"  data-name="'.esc_attr($key).'">'.esc_html( $new_value ).'</textarea>';
                                    break;

                                case 'select':

                                    $options = $field['options'];
                                    echo '<select  data-default="'.esc_attr($default).'"  data-name="'.esc_attr($key).'">';
                                        foreach ( $options as $option => $val )
                                        {
                                            printf('<option value="%s" %s>%s</option>', esc_attr($option), selected($new_value, $option, false), esc_html($val));
                                        }
                                    echo '</select>';
                                
                                break;

                                case 'checkbox':

                                    echo '<label>';
                                    echo '<input data-default="'.esc_attr($default).'" value="'.esc_attr( $new_value ).'" data-name="'.esc_attr($key).'" type="checkbox" '.checked($new_value, 'yes', false).'/>';
                                    echo esc_html( $label );
                                    echo '<span class="description customize-control-description">'.esc_html( $description ).'</span>';
                                    echo '</label>';
                                
                                break;

                                case 'selector_color':

                                    $options = $field['options'];
                                    echo '<div class="vintuna-type-radio">';
                                    echo '<div class="radio-labels">';
                                    foreach ( $options as $option => $val ){

                                        $class_active = '';
                                        $checked = '';
                                        if( $new_value == '' ){
                                            $new_value = $default;
                                        }
                                        if( $new_value == $option  ){
                                            $class_active = 'radio-active';
                                            $checked = 'checked';
                                        }

                                        echo '<label class="bg-color-label '.esc_attr( $class_active ).'">';
                                        echo '<input name="'.esc_attr($key).'" value="'.esc_attr($option).'" type="radio" '.esc_attr( $checked ).'/>';
                                        ?>
                                        <div class="color-scheme-panel"><div class="color-scheme-stripe" style="background:  <?php echo esc_attr(  $val['color'] ) ?>"></div></div>
                                        <?php
                                         echo '<div class="color-ribbon-title" >'.esc_html( $val['title'] ).'</div>';
                                        echo '</label>'; 
                                    }
                                    echo '</div>';
                                    echo '<input data-default="'.esc_attr($default).'" type="hidden" value="'.esc_attr($new_value).'" data-name="'.esc_attr($key).'"/>';
                                    echo '</div>';

                                break;

                                case 'seperator':

                                    echo '<div class="seperator-control-title">'.esc_html( $field['seperator_text'] ).'</div>';
                                
                                break;

                                case 'colorpicker':

                                    echo '<input data-default="'.esc_attr($default).'" class="customizer-color-picker" data-alpha="true" data-name="'.esc_attr($key).'" type="text" value="'.esc_attr($new_value).'"/>';
                                
                                break;

                                case 'upload':

                                        $image = $image_class= "";
                                        if($new_value){ 
                                            $image = '<img src="'.esc_url($new_value).'" style="max-width:100%;"/>';    
                                            $image_class = ' hidden';
                                        }
                                        echo '<div class="wpi-img-fields-wrap">';
                                        echo '<div class="attachment-media-view">';
                                        echo '<div class="placeholder'.esc_attr( $image_class ).'">';
                                        esc_html_e('No image selected', 'vintuna');
                                        echo '</div>';
                                        echo '<div class="thumbnail thumbnail-image">';
                                        echo $image;
                                        echo '</div>';
                                        echo '<div class="actions clearfix">';
                                        echo '<button type="button" class="button wpi-img-delete-button align-left">'.esc_html__('Remove', 'vintuna').'</button>';
                                        echo '<button type="button" class="button wpi-img-upload-button alignright">'.esc_html__('Select Image', 'vintuna').'</button>';
                                        echo '<input data-default="'.esc_attr($default).'" class="upload-id" data-name="'.esc_attr($key).'" type="hidden" value="'.esc_attr($new_value).'"/>';
                                        echo '</div>';
                                        echo '</div>';
                                        echo '</div>';
                                    
                                    break;

                                default:
                                
                                break;

                            } ?>

                        </div>

                    <?php } ?>

                    <div class="clearfix vintuna-repeater-footer">
                        <div class="alignright">
                        <?php if( isset( $this->vintuna_box_add_button ) && $this->vintuna_box_add_button ){ ?>
                            <a class="vintuna-repeater-field-remove" href="#remove"><?php esc_html_e('Delete', 'vintuna') ?>|</a> 
                        <?php } ?>
                        <a class="vintuna-repeater-field-close" href="#close"><?php esc_html_e('Close', 'vintuna') ?></a>
                        </div>
                    </div>
                </div>
                </li>
            <?php   
            }
        }
    }

}


if ( class_exists( 'WP_Customize_Control' ) ) {
    
    // Repeator Info
    class Vintuna_Info_Notiece_Control extends WP_Customize_Control {

        public $type = 'infonotice';
    
        public function render_content() {
           
            $name = '_customize-radio-' . $this->id; ?>
            
            <span class="customize-control-title">
                <span class="wpi-notiect-bar"><?php echo esc_html( $this->label ); ?></span>

                <div class="wpi-info-icon">
                    <div class="icon-notice-wrap">
                        <span class="dashicons dashicons-move wpi-filter-icon"></span>
                        <p><?php esc_html_e( 'Click hold and drag to re-order.', 'vintuna' ); ?></p>
                    </div>
                    <div class="icon-notice-wrap">
                        <span class="dashicons dashicons-arrow-down wpi-filter-icon"></span>
                        <p><?php esc_html_e( 'Click to expand settings.', 'vintuna' ); ?></p>
                    </div>
                </div>
            </span>
            
        <?php }
    }
    
}

if ( class_exists( 'WP_Customize_Control' ) ) {
    
    // Repeator Info
    class Vintuna_Premium_Notiece_Control extends WP_Customize_Control {

        public $type = 'premiuminfonotice';
    
        public function render_content() {
           
            $name = '_customize-notice-' . $this->id; ?>
            
            <span class="customize-control-title">
                <div class="theme-info-icon">
                    <div class="icon-notice-wrap">
                        <span class="dashicons dashicons-lightbulb wpi-filter-icon"></span>
                        <span><?php echo esc_html__('More ','vintuna'). esc_html( $this->label ). esc_html__(' Available on Premium Version.', 'vintuna' ); ?></span>
                    </div>
                </div>
            </span>
            
        <?php }
    }
    
}


if ( ! function_exists( 'vintuna_sanitize_number_range' ) ) :

    /**
     * Sanitize number range.
     *
     * @since 1.0.0
     *
     * @see absint() https://developer.wordpress.org/reference/functions/absint/
     *
     * @param int                  $input Number to check within the numeric range defined by the setting.
     * @param WP_Customize_Setting $setting WP_Customize_Setting instance.
     * @return int|string The number, if it is zero or greater and falls within the defined range; otherwise, the setting default.
     */
    function vintuna_sanitize_number_range( $input, $setting ) {

        // Ensure input is an absolute integer.
        $input = absint( $input );

        // Get the input attributes associated with the setting.
        $atts = $setting->manager->get_control( $setting->id )->input_attrs;

        // Get min.
        $min = ( isset( $atts['min'] ) ? $atts['min'] : $input );

        // Get max.
        $max = ( isset( $atts['max'] ) ? $atts['max'] : $input );

        // Get Step.
        $step = ( isset( $atts['step'] ) ? $atts['step'] : 1 );

        // If the input is within the valid range, return it; otherwise, return the default.
        return ( $min <= $input && $input <= $max && is_int( $input / $step ) ? $input : $setting->default );

    }

endif;
