<?php

class Intense_Widget_Snippet extends WP_Widget {

	function __construct() {

		$widget_ops = array( 'classname' => 'snippets', 'description' => 'Add snippet content' );
		$control_ops = array( 'id_base' => 'snippet-widget' );
		parent::__construct( 'snippet-widget', '(Intense) Snippet', $widget_ops, $control_ops );

	}

	function widget( $args, $instance ) {
		global $intense_visions_options;

		extract( $args );

		echo $before_widget;

		$title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '' );
		
		if ( $title ) {
			echo $before_title . $title . $after_title;
		}

		?>

		<div id="snippet_<?php echo $widget_id; ?>" class="intense widget-snippet" >
			<?php echo intense_run_shortcode( 'intense_snippet', array( 'snippet_id' => $instance[ 'snippet_id'] ) ); ?>
		</div>

		<?php

		echo $after_widget;
	}

	function update( $new_instance, $old_instance ) {
		global $intense_visions_options;

		$instance = $old_instance;

		$instance[ 'snippet_id' ] = $new_instance[ 'snippet_id' ];

		return $instance;

	}

	function form( $instance ) {
		global $intense_visions_options;

		$defaults = array(
			'snippet_id' => ''
		);

		$instance = wp_parse_args( (array) $instance, $defaults );

		$query = new WP_Query( array( 'post_type' => 'intense_snippets', 'nopaging' => 'true' ) );
		$snippets = array();

		$snippets['Saved in WordPress'] = array(
			'title' => 'Saved in WordPress',
			'snippets' => array()
		);

		while ( $query->have_posts() ) : $query->the_post();
			$id = get_the_ID();
			$title = get_the_title( $id );
			$content = get_the_content();

			$snippets['Saved in WordPress']['snippets'][] = array(
				'id' => $id,
				'title' => $title,
				'content' => $content
			);
		endwhile;

		$file_snippets = intense_locate_available_plugin_snippets();

		foreach ( $file_snippets as $snippet_location_name => $snippet_location ) {
			$root = intense_get_snippet_location_path( $snippet_location_name );

			foreach ( $snippet_location as $directory => $snippet_list ) {
				$location_key = $snippet_location_name . ( !empty( $directory ) ? ' - ' . $directory : '' );

				if ( !isset( $snippets[ $location_key ] ) ) {
					$snippets[ $location_key ] = array(
						'title' => $location_key,
						'snippets' => array()
					);
				}

				foreach ( $snippet_list as $snippet => $title ) {
					$snippet_path = $root . ( !empty( $directory ) ? $directory . '/' : '' ) . $snippet . '.php';
					$content = intense_load_plugin_snippet( $snippet_path );

					$snippets[ $location_key ]['snippets'][] = array(
						'id' => $snippet_location_name . ' | ' . str_replace( '.php', '', str_replace( $root, '', $snippet_path ) ),
						'title' => $title,
						'content' => $content
					);
				}
			}
		}
		
		?>

		<p>
			<label for="<?php echo $this->get_field_id( 'snippet_id' ); ?>"><?php _e( 'Snippet:', 'intense' ); ?></label>			
			<select id="<?php echo $this->get_field_id( 'snippet_id' ); ?>" name="<?php echo $this->get_field_name( 'snippet_id' ); ?>" class="widefat" style="width:100%;">
				<?php

				foreach ($snippets as $snippet_location_name => $snippet_location) {
					echo '<optgroup label="' . $snippet_location_name . '">';

					foreach ($snippet_location['snippets'] as $key => $snippet) {						
						echo '<option ' . ($instance['snippet_id'] == $snippet['id'] ? 'selected="selected"' : '') . ' value="' . esc_attr( $snippet['id'] ) . '">' . esc_html( $snippet['title'] ) . '</option>';
					}

					echo '</optgroup>';
				}
					// foreach ($snippets as $key => $snippet) {
					// 	echo '<pre>'; print_r($snippet); echo '</pre>';
					// 	//echo '<option value="' . $snippet["id"] . 'if ( 'Top' == $instance['snippet_id']) echo 'selected="selected"'; > _e( 'Top', 'intense' );</option>';
					// }
				?>
			</select>
		</p>

		<?php
	}
}
