<?php // Adds widget: anews_info_widget
class anewsinfowidget_Widget extends WP_Widget {

	function __construct() {
		parent::__construct(
			'anewsinfowidget_widget',
			esc_html__( 'Anews Info Widget', 'anews' ),
			array( 'description' => esc_html__( 'Anews Info Widget', 'anews' ), ) // Args
		);
		add_action( 'admin_footer', array( $this, 'media_fields' ) );
		add_action( 'customize_controls_print_footer_scripts', array( $this, 'media_fields' ) );
	}

	private $widget_fields = array(
		array(
			'label' => 'Image',
			'id' => 'anews_info_img',
			'type' => 'media',
		),
		array(
			'label' => 'Content',
			'id' => 'anews_info_content',
			'type' => 'textarea',
		),
	);

	public function widget( $args, $instance ) {
		echo wp_kses_post($args['before_widget']);

		if ( ! empty( $instance['title'] ) ) {
			echo wp_kses_post( $args['before_title'] ) . wp_kses_post( apply_filters( 'widget_title', $instance['title'] ) ) . wp_kses_post( $args['after_title'] );
		}
		?>
		<div class="anews-info-widget">
			<?php if(!empty($instance['anews_info_img'])) :
				$anews_info_logo = wp_get_attachment_image_src( $instance['anews_info_img'],'full' );
			?>
			<div class="info-img">
				<img src="<?php echo esc_url($anews_info_logo[0]); ?>" alt="<?php echo esc_attr__('anews company logo','anews'); ?>">
			</div>
			<?php endif; ?>
			<div class="info-content">
				<p><?php echo esc_html($instance['anews_info_content']); ?></p>
			</div>
		</div>
		<?php 
		echo wp_kses_post($args['after_widget']);
	}

	public function media_fields() {
		?><script>
			jQuery(document).ready(function($){
				if ( typeof wp.media !== 'undefined' ) {
					var _custom_media = true,
					_orig_send_attachment = wp.media.editor.send.attachment;
					$(document).on('click','.custommedia',function(e) {
						var send_attachment_bkp = wp.media.editor.send.attachment;
						var button = $(this);
						var id = button.attr('id');
						_custom_media = true;
							wp.media.editor.send.attachment = function(props, attachment){
							if ( _custom_media ) {
								$('input#'+id).val(attachment.id);
								$('span#preview'+id).css('background-image', 'url('+attachment.url+')');
								$('input#'+id).trigger('change');
							} else {
								return _orig_send_attachment.apply( this, [props, attachment] );
							};
						}
						wp.media.editor.open(button);
						return false;
					});
					$('.add_media').on('click', function(){
						_custom_media = false;
					});
					$(document).on('click', '.remove-media', function() {
						var parent = $(this).parents('p');
						parent.find('input[type="media"]').val('').trigger('change');
						parent.find('span').css('background-image', 'url()');
					});
				}
			});
		</script><?php
	}

	public function field_generator( $instance ) {
		foreach ( $this->widget_fields as $widget_field ) {
			$default = '';
			if ( isset($widget_field['default']) ) {
				$default = $widget_field['default'];
			}
			$widget_value = ! empty( $instance[$widget_field['id']] ) ? $instance[$widget_field['id']] : esc_html( $default );
			switch ( $widget_field['type'] ) {
				case 'media':
					$media_url = '';
					if ($widget_value) {
						$media_url = wp_get_attachment_url($widget_value);
					}
					?>
					<p>
						<label for="<?php echo esc_attr( $this->get_field_id( $widget_field['id'] ) ) ?>"><?php echo esc_html( $widget_field['label'], 'anews' ); ?></label> 
						<input style="display:none;" class="widefat" id="<?php echo esc_attr( $this->get_field_id( $widget_field['id'] ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $widget_field['id'] ) ); ?>" type="<?php echo esc_attr($widget_field['type']); ?>" value="<?php echo esc_attr($widget_value); ?>">
						<span id="preview<?php echo esc_attr( $this->get_field_id( $widget_field['id'] ) ); ?>" style="margin-right:10px;border:2px solid #eee;display:block;width: 100px;height:100px;background-image:url(<?php echo esc_url($media_url); ?>);background-size:contain;background-repeat:no-repeat;"></span>
						<button id="<?php echo esc_attr($this->get_field_id( $widget_field['id'] )); ?>" class="button select-media custommedia"><?php echo esc_html__('Add Media','anews'); ?></button>
						<input style="width: 19%;" class="button remove-media" id="buttonremove" name="buttonremove" type="button" value="Clear" />
					</p>
					<?php 
					break;
				case 'textarea':
					?>
					<p>
					<label for="<?php echo esc_attr( $this->get_field_id( $widget_field['id'] ) ); ?>"><?php echo esc_html( $widget_field['label'], 'anews' ); ?></label>
					<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( $widget_field['id'] ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $widget_field['id'] ) ); ?>" rows="6" cols="6" value="<?php echo esc_attr( $widget_value ); ?>"><?php echo esc_html($widget_value); ?></textarea>
					</p>
					<?php 
					break;
				default:
				?>
				<p>
					<label for="<?php echo esc_attr( $this->get_field_id( $widget_field['id'] ) ); ?>"><?php echo esc_html( $widget_field['label'], 'anews' ); ?></label>
					<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( $widget_field['id'] ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( $widget_field['id'] ) ); ?>" type="<?php echo esc_attr($widget_field['type']); ?>" value="<?php echo esc_attr( $widget_value ); ?>">
				</p>
				<?php 
			}
		}
	}

	public function form( $instance ) {
		$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'Company Info', 'anews' );
		?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php echo esc_html__( 'Title:', 'anews' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
		</p>
		<?php
		$this->field_generator( $instance );
	}

	public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
		foreach ( $this->widget_fields as $widget_field ) {
			switch ( $widget_field['type'] ) {
				default:
					$instance[$widget_field['id']] = ( ! empty( $new_instance[$widget_field['id']] ) ) ? strip_tags( $new_instance[$widget_field['id']] ) : '';
			}
		}
		return $instance;
	}
}

function register_anewsinfowidget_widget() {
	register_widget( 'anewsinfowidget_Widget' );
}
add_action( 'widgets_init', 'register_anewsinfowidget_widget' );