<?php
/**
 * Widget API: WP_Widget_Text class
 *
 * @package    WordPress
 * @subpackage Widgets
 * @since      4.4.0
 */

/**
 * Core class used to implement a Text widget.
 *
 * @since 2.8.0
 *
 * @see   WP_Widget
 */
if ( ! class_exists( 'Unero_Language_Currency_Widget' ) ) {
	class Unero_Language_Currency_Widget extends WP_Widget {

		/**
		 * Sets up a new Text widget instance.
		 *
		 * @since  2.8.0
		 * @access public
		 */
		public function __construct() {
			$widget_ops  = array(
				'classname'   => 'widget_text unero-language-currency',
				'description' => esc_html__( 'Shows language list by WPML plugin and currency list by WooCommerce Currency Switcher plugin', 'unero-vc-addons' ),
			);
			$control_ops = array( 'width' => 400, 'height' => 350 );
			parent::__construct( 'unero-language-currency', esc_html__( 'Unero Language & Currency', 'unero-vc-addons' ), $widget_ops, $control_ops );
		}

		/**
		 * Outputs the content for the current Text widget instance.
		 *
		 * @since  2.8.0
		 * @access public
		 *
		 * @param array $args Display arguments including 'before_title', 'after_title',
		 *                        'before_widget', and 'after_widget'.
		 * @param array $instance Settings for the current Text widget instance.
		 */
		public function widget( $args, $instance ) {

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

			$css_class = $instance['style'] . '-layout';

			$show_name = 'code';
			if ( $instance['style'] == 'horizontal' ) {
				$show_name = 'name';
			}
			?>
            <div class="widget-language widget-lan-cur <?php echo esc_attr( $css_class ); ?>">
				<?php
				if ( $instance['language'] ) {
					echo '<h4 class="widget-title">' . $instance['language'] . '</h4>';
				}
				if( function_exists('unero_language_switcher')) {
					echo apply_filters( 'unero_language_switcher_widget', unero_language_switcher( $show_name ), $show_name );
				}
				?>
            </div>
            <div class="widget-currency widget-lan-cur <?php echo esc_attr( $css_class ); ?>">
				<?php
				if ( $instance['currency'] ) {
					echo '<h4 class="widget-title">' . $instance['currency'] . '</h4>';
				}
				if( function_exists('unero_currency_switcher')) {
					echo unero_currency_switcher( $show_name );
				}
				?>
            </div>
			<?php
			echo $args['after_widget'];
		}

		/**
		 * Handles updating settings for the current Text widget instance.
		 *
		 * @since  2.8.0
		 * @access public
		 *
		 * @param array $new_instance New settings for this instance as input by the user via
		 *                            WP_Widget::form().
		 * @param array $old_instance Old settings for this instance.
		 *
		 * @return array Settings to save or bool false to cancel saving.
		 */
		public function update( $new_instance, $old_instance ) {
			$instance             = $old_instance;
			$instance['style']    = sanitize_title( $new_instance['style'] );
			$instance['language'] = sanitize_text_field( $new_instance['language'] );
			$instance['currency'] = sanitize_text_field( $new_instance['currency'] );


			return $instance;
		}

		/**
		 * Outputs the Text widget settings form.
		 *
		 * @since  2.8.0
		 * @access public
		 *
		 * @param array $instance Current settings.
		 */
		public function form( $instance ) {
			$instance = wp_parse_args( (array) $instance, array( 'style' => '', 'language' => '', 'currency' => '' ) );
			$style    = sanitize_title( $instance['style'] );
			$language = sanitize_text_field( $instance['language'] );
			$currency = sanitize_text_field( $instance['currency'] );
			?>

            <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'language' ) ); ?>"><?php esc_html_e( 'Language Text:', 'unero-vc-addons' ); ?></label>
                <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'language' ) ); ?>"
                       name="<?php echo esc_attr( $this->get_field_name( 'language' ) ); ?>" type="text"
                       value="<?php echo esc_attr( $language ); ?>"/>
            </p>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'currency' ) ); ?>"><?php esc_html_e( 'Currency Text:', 'unero-vc-addons' ); ?></label>
                <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'currency' ) ); ?>"
                       name="<?php echo esc_attr( $this->get_field_name( 'currency' ) ); ?>" type="text"
                       value="<?php echo esc_attr( $currency ); ?>"/>
            </p>

            <p>
                <label for="<?php echo esc_attr( $this->get_field_id( 'style' ) ); ?>"><?php esc_html_e( 'Style:', 'unero-vc-addons' ); ?></label>
                <select class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'style' ) ); ?>"
                        name="<?php echo esc_attr( $this->get_field_name( 'style' ) ); ?>">
                    <option value="horizontal" <?php echo $style == 'horizontal' ? 'selected' : '' ?>><?php esc_html_e( 'Horizontal', 'unero-vc-addons' ); ?></option>
                    <option value="vertical" <?php echo $style == 'vertical' ? 'selected' : '' ?>><?php esc_html_e( 'Vertical', 'unero-vc-addons' ); ?></option>
                </select>
            </p>


			<?php
		}
	}
}