<?php

/* $Desc
 *
 * @version    $Id$
 * @package    wpbase
 * @author     Opal  Team <opalwordpressl@gmail.com >
 * @copyright  Copyright (C) 2014 wpopal.com. All Rights Reserved.
 * @license    GNU/GPL v2 or later http://www.gnu.org/licenses/gpl-2.0.html
 *
 * @website  http://www.wpopal.com
 * @support  http://www.wpopal.com/support/forum.html
 */

if(!class_exists('WPO_Params')){
	class WPO_Params{

		public static function getInstance(){
			static $_instance;
			if( !$_instance ){
				$_instance = new WPO_Params();
			}
			return $_instance;
		}

		public function getParam($option){
			$output = '<div class="form-group">';
				$output.='<label for="wpo_input_'.esc_attr( $option['id'] ).'">'.esc_html( $option["label"] ).'</label>';
				switch ($option['type']) {
					case 'text':
						$output .= '<input value="'.esc_attr( $option['default'] ).'" class="form-control" type="text" id="wpo_input_'.esc_attr( $option['id'] ).'" name="wpo_input['.esc_attr( $option['id'] ).']" >';
						break;
					case 'select':
						$output .= $this->renderSelect($option);
						break;
					case 'textarea':
						$output .='<textarea class="form-control" rows="4" id="wpo_input_'.esc_attr( $option['id'] ).'" name="wpo_input['.esc_attr( $option['id'] ).']">'.stripslashes($option['default']).'</textarea>';
						break;
					case 'radio':
						foreach($option['options'] as $key => $value){
									$output .= '
									<div class="radio">
										<label>
											<input type="radio" '.checked($option['default'],$key,false).' name="wpo_input['.esc_attr( $option['id'] ).']" id="wpo_input['.esc_attr( $option['id'] ).']_'.esc_attr( $key ).'" value="'.esc_attr( $key ).'">
											'.esc_html( $value ).'
										</label>
									</div>';
								}
						break;
					case 'sidebars':
						$sidebars = $GLOBALS['wp_registered_sidebars'];

			 			$values = array();

			 			$values['none'] = esc_html__('Select A Sidebar', 'unity');
			 			foreach( $sidebars as $key => $sidebar ){
							$values[$sidebar['id']] = $sidebar["name"];
			 			}
			 			$output.=$this->renderSelect($option,$values);
						break;
					case 'posttypes':
						$output .= $this->renderPosttypes($option);
						break;
					case 'editor':
						$output.=$this->renderEditor($option);
						break;
					case 'category_parent':
						$output.=$this->renderCategoryParent($option);
						break;
					case 'hidden_id':
						$output.=$this->renderHiddenID($option);
						break;
					case 'gmap':
						$output.=$this->renderGmap($option);
						break;
					case 'embed':
						$output.=$this->renderEmbed($option);
						break;
					default:
						$output .= '<input value="'.esc_attr( $option['default'] ).'" class="form-control" type="text" id="wpo_input_'.esc_attr( $option['id'] ).'" name="wpo_input['.esc_attr( $option['id'] ).']" >';
						break;
				}
			$output.='</div>';
			echo trim( $output );
		}

		private function renderEmbed($option){
			$output = '<div id="wpo-embed-'.esc_attr( $option["id"] ).'" class="wpo-embed">';
			$output .= '<input value="'.esc_attr( $option['default'] ).'" class="form-control" type="text" id="wpo_input_'.esc_attr( $option['id'] ).'" name="wpo_input['.esc_attr( $option['id'] ).']" >';
			ob_start();
		?>
			<div class="wpo_embed_view">
		        <span class="spinner" style="float:none;"></span>
		        <div class="result"></div>
		    </div>
		    <script>
		    	!function($) {
		    		WPO_Admin.params_Embed('#wpo_input_<?php echo esc_js( $option['id'] ); ?>','#wpo-embed-<?php echo esc_js( $option['id'] ); ?>');
	    		}(window.jQuery);
		    </script>
		<?php
			$output.=ob_get_clean();
			$output.='</div>';
			return $output;
		}

		/**
		 *
		 */
		private function renderGmap($option){
			$output ='<input value="'.esc_attr( $option['default'] ).'" type="hidden" id="wpo_input_'.esc_attr( $option['id'] ).'" name="wpo_input['.esc_attr( $option['id'] ).']" >';
			$output.='
				<div id="wpo-element-map">
					<div class="map_canvas" style="height:200px;"></div>

					<div class="vc_row-fluid googlefind">
						<input id="geocomplete_'.esc_attr( $option["id"] ).'" type="text" class="wpo-location" placeholder="Type in an address" size="90" />
						<button class="button-primary find">'.esc_html__("Find", 'unity').'</button>
					</div>

					<div class="row-fluid mapdetail">
						<div class="form-group">
							<label for="wpo_input_class">'.esc_html__('Latitude', 'unity').'</label>
							<input name="lat" class="form-control wpo-latgmap" type="text" value="">
						</div>
						<div class="form-group">
							<label for="wpo_input_class">'.esc_html__('Longitude', 'unity').'</label>
							<input name="lng" class="form-control wpo-lnggmap" type="text" value="">
						</div>
					</div>
				</div>
			';
			ob_start();
			?>
			<script>
				!function($) {
					if($('#geocomplete_<?php echo esc_js( $option['id'] ); ?>').length>0){
						var _lat_lng = $('#wpo_input_<?php echo esc_js( $option['id'] ); ?>').val();
						console.log(_lat_lng);
						var loca = _lat_lng;
						_lat_lng = _lat_lng.split(',');
						var center = new google.maps.LatLng(_lat_lng[0],_lat_lng[1]);
					    $("#geocomplete_<?php echo esc_js( $option['id'] ); ?>").geocomplete({
							map: ".map_canvas",
							types: ["establishment"],
							country: "de",
							details: ".mapdetail",
							markerOptions: {
								draggable: true
							},
							location:loca,
							mapOptions: {
								scrollwheel :true,
								zoom:15,
								center:center
							}
					    });
					    $(".googlefind button.find").click(function(){
							$("#geocomplete_<?php echo esc_js( $option['id'] ); ?>").trigger("geocode");
						});
					    $("#geocomplete_<?php echo esc_js( $option['id'] ); ?>").bind("geocode:dragged", function(event, latLng){
							$("input[name=lat]").val(latLng.lat());
							$("input[name=lng]").val(latLng.lng());
							$("#wpo_input_<?php echo esc_js( $option['id'] ); ?>").val(latLng.lat()+','+latLng.lng());
					    }).bind("geocode:result",function(event, result){
					    	$("#wpo_input_<?php echo esc_js( $option['id'] ); ?>").val(result.geometry.location.d+','+result.geometry.location.e);
					    });

					    $('.wpo-latgmap,.wpo-lnggmap').keyup(function(event) {
					    	var value = $('.wpo-latgmap').val()+','+$('.wpo-lnggmap').val();
					    	$("#wpo_input_<?php echo esc_js( $option['id'] ); ?>").val(value);
					    });
					}
				}(window.jQuery);
			</script>
			<?php
			$output.=ob_get_clean();
			return $output;
		}

		/**
		 *
		 */
		private function renderHiddenID($option){
			$value = ( $option['default'] )=='' ? time() : $option['default'];
			$output = '<input value="'.esc_attr( $value ).'" type="hidden" id="wpo_input_'.esc_attr( $option['id'] ).'" name="wpo_input['.esc_attr( $option['id'] ).']" >';
			return $output;
		}

		/**
		 *
		 */
		private function renderCategoryParent($option){
			$terms = get_categories(array('parent'=>0,'hide_empty'=>0));
			ob_start();
		?>
			<select class="form-control" name="wpo_input[<?php echo esc_attr( $option['id'] ); ?>]" id="wpo_input_<?php echo esc_attr( $option['id'] ) ?>">
				<?php foreach($terms as $key => $value){ ?>
					<option value="<?php echo esc_attr( $value->term_id ); ?>" <?php selected( $option['default'],$value->term_id ) ?>><?php echo esc_html( $value->name ); ?></option>
				<?php } ?>
			</select>
		<?php
			$output = ob_get_clean();
			return $output;
		}

		/**
		 *
		 */
		private function renderSelect($option,$values=array()){
			ob_start();
			echo '<select class="form-control" id="wpo_input_'.esc_attr( $option['id'] ).'" name="wpo_input['.esc_attr( $option['id'] ).']">';
				if(count($values)<=0){
					foreach($option['options'] as $key => $value){
						echo '<option value="'.esc_attr( $key ).'" '.selected( $option['default'],$key ).'>'.esc_html( $value ).'</option>';
					}
				}else{
					foreach($values as $key => $value){
						echo '<option value="'.esc_attr( $key ).'" '.selected( $option['default'],$key ).'>'.esc_html( $value ).'</option>';
					}
				}
			echo '</select>';
			$output = ob_get_clean();
			return $output;
		}

		/**
		 *
		 */
		private function renderPosttypes($option){
			$output = '';
			$args = array(
	                    'public'   => true
	                );
	    	$post_types = get_post_types($args);
	    	foreach ($post_types as $key => $value) {
	    		$checked="";
	    		if ( $value != 'attachment' ) {
	    			if ( in_array($value, explode(",", $option['default'])) ) $checked = ' checked="checked"';
		    		$output.='
				    	<div class="checkbox">
							<label>
								<input class="form-control" value="'.esc_attr( $value ).'" type="checkbox" name="wpo_input['.esc_attr( $option['id'] ).'][]" '.esc_attr( $checked ).'>'.esc_html( $value ).'
							</label>
						</div>';
				}
	    	}
			return $output;
		}

		/**
		 *
		 */
		private function renderEditor($option){
			$output = '';
	        wp_editor( '' , 'wpo_input['.$option['id'].']', array('editor_class' => 'textarea_html','textarea_rows'=>'20', 'media_buttons' => true, 'wpautop' => false ) );
	        $output = ob_get_contents();
	        ob_end_clean();
		    return $output;
		}
	}
}