<?php

if (!defined('ABSPATH'))
    exit; // Exit if accessed directly

class WCISPluginWidget extends WP_Widget {

    /**
     * Sets up the widgets name etc
     */
    public function __construct() {

        parent::__construct('isp_search_box_widget',
            __( 'Fast Simon Search Box', 'instantsearch-for-woocommerce' ),
            array('description' => __( 'Fast Simon search box for your site', 'instantsearch-for-woocommerce' ),
                'classname' => 'widget_isp_search_box_widget')
        );
    }

    public static $default_search_box_fields = array(
        'search_box_width'  	=> 10,
        'search_box_height'		=> 2.3,
        'search_box_inner_text'	=> 'Search...',
        'search_box_float'		=> 'none',
        'search_box_text_size'  => 1,
        'search_box_units'      => 'rem'
    );

    /**
     * Outputs the content of the widget
     *
     * @param array $args
     * @param array $instance
     */
    public function widget( $args, $instance ) {
        $title = isset( $instance['title'] ) ? apply_filters( 'widget_title', $instance['title'] ) : '';

        echo $args['before_widget'];

        if ( !empty($title) ) {
            echo $args['before_title'] . $title . $args['after_title'];
        }

        if (array_key_exists('search_box_width', $instance) && is_numeric($instance['search_box_width']) && $instance['search_box_width'] != 0)
            $search_box_width = $instance['search_box_width'];
        else
            $search_box_width = self::$default_search_box_fields['search_box_width'];

        if (array_key_exists('search_box_height', $instance) && is_numeric($instance['search_box_height']) && $instance['search_box_height'] != 0)
            $search_box_height = $instance['search_box_height'];
        else
            $search_box_height = self::$default_search_box_fields['search_box_height'];

        $search_box_inner_text = (array_key_exists('search_box_inner_text', $instance)) ?
            esc_attr($instance['search_box_inner_text']) :
            esc_attr(self::$default_search_box_fields['search_box_inner_text']);

        $search_box_float = (array_key_exists('search_box_float', $instance)) ?
            esc_attr($instance['search_box_float']) :
            esc_attr(self::$default_search_box_fields['search_box_float']);


        $action_url = esc_url(home_url('/'));
        $query_term = 's';
        $premium_serp_enabled = false;
        $options = get_option( 'wcis_general_settings' );
        if ($options && array_key_exists('serp_page_id', $options) && array_key_exists('is_serp_enabled', $options) && $options['is_serp_enabled']){
            $action_url = esc_url(str_replace(home_url(), "", get_permalink($options['serp_page_id'])));
            $premium_serp_enabled = true;
            $query_term = 'q';
        }

        $is_wpml_active = function_exists('icl_object_id');

        $search_box_units = (array_key_exists('search_box_units', $instance)) ?
            esc_attr($instance['search_box_units']) :
            esc_attr(self::$default_search_box_fields['search_box_units']);

        $search_box_width_form = $search_box_width;
        $search_box_width_input = $search_box_width;
        if ($search_box_units != 'rem'){
            $search_box_units = '%%';
            $search_box_width_input = '100';
        }

        $form =     '<form role="search" class="isp_search_box_form" isp_src="widget" name="isp_search_box" action="' . $action_url . '" style="width:'.$search_box_width_form.$search_box_units.'; float:'.$search_box_float.';">';
        $form .=        '<input type="text" name="' . $query_term . '" class="isp_search_box_input" placeholder="'.$search_box_inner_text.'" autocomplete="off" autocorrect="off" autocapitalize="off" style="outline: none; width:'.$search_box_width_input.$search_box_units.'; height:'.$search_box_height.$search_box_units.';">';
        if (!$premium_serp_enabled){
            $form .= 		'<input type="hidden" name="post_type" value="product" />';
        }
        $form .=       	'<input type="image" src="'. plugins_url('assets/images/magnifying_glass.png', __FILE__ ) .'" class="isp_widget_btn" value="" />';
        $form .=    '</form>';
        if ($is_wpml_active) {
            $form .= 	'<input type="hidden" id="isp_home_url" name="home_url" value="' . home_url() . '" />';
        }
        $form .=    '<style>form.isp_search_box_form{position:relative}form.isp_search_box_form input[type=text].isp_search_box_input{background:#fff;border:2px solid #C9CCCF;box-shadow:none;font-size:1em;padding:.5em 2em .5em 1em;margin:.2rem;font-family:inherit;box-sizing:border-box;opacity:.8;color:#60626b;border-radius:1000px;-webkit-border-radius:1000px;-moz-border-radius:1000px;-webkit-transition:border .5s,opacity .5s;-moz-transition:border .5s,opacity .5s;transition:border .5s,opacity .5s;float:none;display:block;max-width:100%}form.isp_search_box_form input[type=text].isp_search_box_input:focus{border:2px solid #707780;opacity:1}.isp_search_box_form input[type=image].isp_widget_btn{border:none;right:7px;top:calc(50% - 8px);padding:0;margin:0;position:absolute;opacity:.5}.isp_search_box_form input[type=image].isp_widget_btn:hover{opacity:1}</style>';
        echo $form;

        echo $args['after_widget'];
    }

    /**
     * Outputs the options form on admin
     *
     * @param array $instance The widget options
     */
    public function form( $instance ) {
        $search_box_width = (array_key_exists('search_box_width', $instance)) ?
            $instance['search_box_width'] :
            self::$default_search_box_fields['search_box_width'];

        $search_box_height = (array_key_exists('search_box_height', $instance)) ?
            $instance['search_box_height'] :
            self::$default_search_box_fields['search_box_height'];

        $search_box_inner_text = (array_key_exists('search_box_inner_text', $instance)) ?
            $instance['search_box_inner_text'] :
            self::$default_search_box_fields['search_box_inner_text'];

        $search_box_float = (array_key_exists('search_box_float', $instance)) ?
            esc_attr($instance['search_box_float']) :
            esc_attr(self::$default_search_box_fields['search_box_float']);

        $search_box_units = (array_key_exists('search_box_units', $instance)) ?
            $instance['search_box_units'] :
            self::$default_search_box_fields['search_box_units'];

        $search_box_units_checked_rem = '';
        $search_box_units_checked_percent = '';
        if ($search_box_units == 'rem'){
            $search_box_units_checked_rem = 'checked';
        } else if ($search_box_units == 'percent'){
            $search_box_units_checked_percent = 'checked';
        }

        $float_selecte = '';
        $options = array('none', 'left', 'right');
        foreach ($options as $value){
            if ($value == $search_box_float)
                $float_selecte .= '<option value="'.esc_attr($value).'" selected>'.esc_attr($value).'</option>';
            else
                $float_selecte .= '<option value="'.esc_attr($value).'">'.esc_attr($value).'</option>';
        }

        $search_box_inner_text_field_id = esc_attr($this->get_field_id('search_box_inner_text'));
        $search_box_inner_text_field_name = esc_attr($this->get_field_name('search_box_inner_text'));

        $search_box_width_field_id = esc_attr($this->get_field_id('search_box_width'));
        $search_box_width_field_name = esc_attr($this->get_field_name('search_box_width'));

        $search_box_height_field_id = esc_attr($this->get_field_id('search_box_height'));
        $search_box_height_field_name = esc_attr($this->get_field_name('search_box_height'));

        $search_box_units_field_id = esc_attr($this->get_field_id('search_box_units'));
        $search_box_units_field_name = esc_attr($this->get_field_name('search_box_units'));

        $search_box_float_field_id = esc_attr($this->get_field_id('search_box_float'));
        $search_box_float_field_name = esc_attr($this->get_field_name('search_box_float'));

        $form = '
			<p>
				<label for="'. $search_box_inner_text_field_id .'">'. __('Search box inner text: ', 'instantsearch-for-woocommerce'). '</label>
				<input type="text" class="widefat" id="'. $search_box_inner_text_field_id .'" name="'. $search_box_inner_text_field_name .'" value="'.  esc_attr($search_box_inner_text) .'"/>
			</p>
			<p>
				<label for="'. $search_box_width_field_id .'">'. __('Width - search box size: ', 'instantsearch-for-woocommerce'). '</label>
				<input type="text" class="widefat" id="'. $search_box_width_field_id .'" name="'. $search_box_width_field_name .'" value="'.  esc_attr($search_box_width) .'"/>
			</p>
			<p>
				<label for="'. $search_box_height_field_id.'">'. __('Height - search box size: ', 'instantsearch-for-woocommerce'). '</label>
				<input type="text" class="widefat" id="'. $search_box_height_field_id .'" name="'. $search_box_height_field_name .'" value="'.  esc_attr($search_box_height) .'"/>
			</p>  
				            
			<p>
				<label for="'. $search_box_units_field_id .'">'. __('Height/Width units: ', 'instantsearch-for-woocommerce'). '</label>
				
		        <label for="rem">'. __('rem', 'instantsearch-for-woocommerce'). '
				    <input type="radio" class="widefat" id="'. $search_box_units_field_id .'" name="'. $search_box_units_field_name .'" value="rem" '. esc_attr($search_box_units_checked_rem) . '/>
				</label>
	            <label for="percent">'. __('percent(%%)', 'instantsearch-for-woocommerce'). '
		          <input type="radio" class="widefat" id="'. $search_box_units_field_id .'" name="'. $search_box_units_field_name .'" value="percent" '. esc_attr($search_box_units_checked_percent) . '/>
                </label>
			</p>	
		                			        
			<p>
				<label for="'. $search_box_float_field_id .'">'. __('Float - push/move search box to the left or to the right side: ', 'instantsearch-for-woocommerce').'</label>
				<select id="'. $search_box_float_field_id .'" name="'. $search_box_float_field_name .'" class="widefat">
						'.$float_selecte.'
				</select>		
			</p>
		
		';

        printf($form);
    }

    /**
     * Processing widget options on save
     *
     * @param array $new_instance The new options
     * @param array $old_instance The previous options
     */
    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] 					= ( !empty($new_instance['title']) ) ? sanitize_text_field( $new_instance['title'] ) : '';
        $instance['search_box_inner_text'] 	= sanitize_text_field($new_instance['search_box_inner_text']);
        $instance['search_box_float'] 		= sanitize_text_field($new_instance['search_box_float']);
        $instance['search_box_units'] 		= sanitize_text_field($new_instance['search_box_units']);

        if (is_numeric(sanitize_text_field($new_instance['search_box_width'])) && sanitize_text_field($new_instance['search_box_width'] != 0))
            $instance['search_box_width'] = sanitize_text_field($new_instance['search_box_width']);
        if (is_numeric(sanitize_text_field($new_instance['search_box_height'])) && sanitize_text_field($new_instance['search_box_height'] != 0))
            $instance['search_box_height'] = sanitize_text_field($new_instance['search_box_height']);

        return $instance;
    }
}

?>