<?php defined('ABSPATH') or die("No script kiddies please!");

//echo "epgr-places-shortcode.php";

$place_id = $atts['place_id'];
if(isset( $atts['language_code']) &&  $atts['language_code'] != ''){
  $language_code = $atts['language_code'];
}else{
  $language_code = 'en';
}

if(isset( $atts['total_review_count']) &&  $atts['total_review_count'] != ''){
  $total_review_count = $atts['total_review_count'];
}else{
  $total_review_count = '6';
}

if(isset( $atts['price_range']) &&  $atts['price_range'] != ''){
  $price_range = $atts['price_range'];
}else{
  $price_range = '40';
}

$random_num = rand(111111111, 999999999);
/* Get API KEY */
$egpr_reviews = get_option('egp_reviews');
$default_obj = new EGPR_Activation();
$default_array  = $default_obj->egpr_default_settings();
$nverified_api_key = (isset($egpr_reviews['nverified_api_key']) && $egpr_reviews['nverified_api_key'] != '') ? esc_attr($egpr_reviews['nverified_api_key']) : '';
$check_cache = (isset($egpr_reviews['check_cache']) && $egpr_reviews['check_cache'] == 'true') ? 'true' : 'false';
$cache_period = (isset($egpr_reviews['cache_period']) && $egpr_reviews['cache_period'] != '') ? intval($egpr_reviews['cache_period']) : $default_array['cache_period'];
/* Get API KEY END */

$all_reviews = EGPR_API_Settings::get_google_review_api($check_cache, $cache_period, $nverified_api_key, $place_id,$language_code);
if (isset($all_reviews['result']['reviews']) && !empty($all_reviews['result']['reviews'])) {
    $reviews = $all_reviews['result']['reviews'];
} else {
    $reviews = array();
}
if (isset($atts['show_5star_rating']) && $atts['show_5star_rating'] == 1) {
    $cnt = 1;
} else {
    $cnt = 0;
}
if(isset($atts['number_reviews']) && $atts['number_reviews'] != ''){
   $total_number_reviews = $atts['number_reviews'];
}else{
    $total_number_reviews = 5;
}

if (isset($atts['show_4star_rating']) && $atts['show_4star_rating'] == 1) {
    $cnt2 = 1;
} else {
    $cnt2 = 0;
}

if($cnt == 1 && $cnt2 == 0){
 $reviews = $this->get_filtered_reviews($reviews, $total_number_reviews, $cnt);
}else if($cnt2 == 1 && $cnt == 0){
 $reviews = $this->get_filtered_reviews_4star($reviews, $total_number_reviews, $cnt2);
}else if($cnt2 == 1 && $cnt == 1){
 $reviews = $this->get_filtered_both_reviews( $reviews, $total_number_reviews );
}

if (isset($all_reviews['result']) && !empty($all_reviews['result'])) {
    $all_results = $all_reviews['result'];
} else {
    $all_results = array();
}
$business_name = (isset($all_results['name']) && $all_results['name'] != '') ? $all_results['name'] : '';
$url = (isset($all_results['url']) && $all_results['url'] != '') ? $all_results['url'] : '';
$website = (isset($all_results['website']) && $all_results['website'] != '') ? $all_results['website'] : '';
$photo_avatar = (isset($all_results['photos'][0]['photo_reference']) && $all_results['photos'][0]['photo_reference'] != '' ) ? $all_results['photos'][0]['photo_reference'] : '';
$avg_star = (isset($all_results['rating']) && $all_results['rating'] != '') ? $all_results['rating'] : '';
$formatted_phone_number = (isset($all_results['formatted_phone_number']) && $all_results['formatted_phone_number'] != '') ? $all_results['formatted_phone_number'] : '';
$international_phone_number = (isset($all_results['international_phone_number']) && $all_results['international_phone_number'] != '') ? $all_results['international_phone_number'] : '';
if($formatted_phone_number != ''){
  $phone_number = $formatted_phone_number;
}else{
  $phone_number = $international_phone_number;
}
$address_components = (isset($all_results['address_components']) && $all_results['address_components'] != '') ? $all_results['address_components'] : array();


if(isset($atts['badge_template']) && $atts['badge_template'] != ''){
   $badge_template =  $atts['badge_template'];
}else{
    $badge_template = 'template1';
}
if(isset($atts['header_template']) && $atts['header_template'] != ''){
   $header_template =  $atts['header_template'];
}else{
    $header_template = 'template1';
}
if(isset($atts['review_template']) && $atts['review_template'] != ''){
   $choose_template =  $atts['review_template'];
}else{
    $choose_template = 'template1';
}


if(isset($atts['layout_type']) && $atts['layout_type'] != ''){
   $layout_type =  $atts['layout_type'];
}else{
    $layout_type = 'list';
}
//horizontal or vertical postion
if(isset($atts['badge_position_type']) && $atts['badge_position_type'] != ''){
   $badge_position_class =  $atts['badge_position_type'];
}else{
    $badge_position_class = 'horizontal';
}

//show badge
if(isset($atts['show_badge'])){
	if($atts['show_badge'] == 1){
        $show_badge = "true";
	}else{
        $show_badge = "false";
	}
}else{
$show_badge = "true";
}
//show reviews
if(isset($atts['show_review'])){
	if($atts['show_review'] == 1){
        $show_reviews = "true";
	}else{
        $show_reviews = "false";
	}
}else{
$show_reviews = "true";
}
//show header
if(isset($atts['show_header'])){
	if($atts['show_header'] == 1){
        $show_header = "true";
	}else{
        $show_header = "false";
	}
}else{
$show_header = "true";
}
//show rate us button attribute
if(isset($atts['enable_rateus'])){
	if($atts['enable_rateus'] == 1){
        $enable_rateus = 1;
	}else{
        $enable_rateus = 0;
	}
}else{
$enable_rateus = 1;
}
if(isset($atts['text_title'])  && $atts['text_title'] != ''){
      $text_title = $atts['text_title'];
}else{
     $text_title = __("Write a review",EGPR_TD);
}
if(isset($atts['rateus_link_target'])  && $atts['rateus_link_target'] != ''){
      $link_target = $atts['rateus_link_target'];
}else{
      $link_target =  "_self";
}
if(isset($atts['custom_url']) && $atts['custom_url']  != ''){
   $rateus_custom_url = $atts['custom_url'];
}else{
   $rateus_custom_url = "https://search.google.com/local/writereview?placeid=".$place_id;
}
//view all button attribute
if(isset($atts['enable_viewall']) ){
	if($atts['enable_viewall'] == 1){
        $enable_viewall = 1;
	}else{
        $enable_viewall = 0;
	}
}else{
$enable_viewall = 1;
}
if(isset($atts['text_vbtitle']) && $atts['text_vbtitle'] != ''){
      $text_vbtitle = $atts['text_vbtitle'];
}else{
     $text_vbtitle = __("See All Reviews",EGPR_TD);
}
if(isset($atts['viewall_link_target'])  && $atts['viewall_link_target'] != ''){
      $vblink_target = $atts['viewall_link_target'];
}else{
      $vblink_target =  "_self";
}
if(isset($atts['viewall_custom_url']) && $atts['viewall_custom_url']  != ''){
   $viewall_custom_url = $atts['viewall_custom_url'];
}else{
   $viewall_custom_url = $url;
}
$animation_name = (isset($badge_settings['animation_name']) && $badge_settings['animation_name'] != '') ? $badge_settings['animation_name'] : '';
//animation here
if(isset($atts['badge_animation_type']) && $atts['badge_animation_type'] != ''){
      $badge_animation_type = 'wow animated '.$atts['badge_animation_type'];
      $badge_animation = $atts['badge_animation_type'];
}else{
     $badge_animation_type = '';
     $badge_animation = $animation_name;
}
if(isset($atts['review_animation_type']) && $atts['review_animation_type'] != ''){

      $review_animation_type = 'wow animated '.$atts['review_animation_type'];
      $review_animation = $atts['review_animation_type'];

}else{
     $review_animation_type = '';
     $review_animation = '';
}

$hide_avg_rating = "0";
$hide_usr_image  = "0";
$hide_user_name = "0";
$disable_user_links = "0";
$disable_date = "0";
$hide_user_rating  = "0";
if(isset($atts['show_description'])){
	if($atts['show_description'] == 1){
       $show_description = "true";
	}else{
      $show_description = "false";
	}

}else{
     $show_description = "true";
}

if(isset($atts['word_limit'])){
  if($atts['word_limit'] != ''){
       $word_limit = (int) $atts['word_limit'];
  }else{
      $word_limit = 100;
  }

}else{
     $word_limit = 100;
}

if(isset($atts['show_text'])){
      $desc_text_show = $atts['show_text'];
}else{
     $desc_text_show = "Read More";
}
if(isset($atts['hide_text'])){
      $desc_text_hide = $atts['hide_text'];
}else{
     $desc_text_hide = "Read Less";
}


//slider option layout
if($layout_type == "slider"){
	if(isset($atts['speed']) && $atts['speed'] != ''){
	      $speed = $atts['speed'];
	}else{
	     $speed = "1000";
	}
	if(isset($atts['pause']) && $atts['pause'] != ''){
	      $pause = $atts['pause'];
	}else{
	     $pause = "4000";
	}
	//dot and pagination
	if(isset($atts['pager_type']) && $atts['pager_type'] != ''){
	      $pager_type = $atts['pager_type'];
	}else{
	     $pager_type = "dot";
	}
	if(isset($atts['mode']) && $atts['mode'] != ''){
	      $mode = $atts['mode'];
	}else{
	     $mode = "horizontal";
	}
    if(isset($atts['controls'])){
        if($atts['controls']  == 1){
               $controls = 1;
    }else{
               $controls = 0;
    }
  }else{
       $controls = 1;
  }
	if(isset($atts['controls_type']) && $atts['controls_type'] != ''){
	      $controls_type = $atts['controls_type'];
	}else{
	     $controls_type = "arrow";
	}

	if(isset($atts['adaptive_height'])){
	      if($atts['adaptive_height']  == 1){
               $adaptive_height = 1;
		}else{
               $adaptive_height = 0;
		}
	}else{
	     $adaptive_height = 1;
	}
	if(isset($atts['pager_show'])){
		if($atts['pager_show']  == 1){
               $pager_show = 1;
		}else{
                  $pager_show = 0;
		}
	}else{
	     $pager_show = 1;
	}
	if(isset($atts['autoplay'])){
		if($atts['autoplay']  == 1){
               $autoplay = 1;
		}else{
                  $autoplay = 0;
		}
	}else{
	     $autoplay = 1;
	}

}else{

}

if(isset($atts['badge_link']) && $atts['badge_link'] == "disabled"){
    $badge_link = 1;
} else {
    $badge_link = 0;
}
/* New features with custom styling */
if (isset($atts['description_font_family']) && $atts['description_font_family'] != '') {
    $description_font_family = esc_attr($atts['description_font_family']);
} else {
    $description_font_family = '';
}
if (isset($atts['description_font_color']) && $atts['description_font_color'] != '') {
    $description_font_color = esc_attr($atts['description_font_color']);
} else {
    $description_font_color = '';
}
if (isset($atts['description_font_size']) && $atts['description_font_size'] != '') {
    $description_font_size = esc_attr($atts['description_font_size']);
} else {
    $description_font_size = '';
}
if (isset($atts['date_font_color']) && $atts['date_font_color'] != '') {
    $date_font_color = esc_attr($atts['date_font_color']);
} else {
    $date_font_color = '';
}
if (isset($atts['rating_color']) && $atts['rating_color'] != '') {
    $rating_color = esc_attr($atts['rating_color']);
} else {
    $rating_color = '';
}
if (isset($atts['button_font_color']) && $atts['button_font_color'] != '') {
    $button_font_color = esc_attr($atts['button_font_color']);
} else {
    $button_font_color = '';
}
if (isset($atts['button_font_hover_color']) && $atts['button_font_hover_color'] != '') {
    $button_font_hover_color = esc_attr($atts['button_font_hover_color']);
} else {
    $button_font_hover_color = '';
}
//rate us custom styling//
if (isset($atts['rateus_fontsize']) && $atts['rateus_fontsize'] != '') {
    $rateus_fontsize = esc_attr($atts['rateus_fontsize']);
} else {
    $rateus_fontsize = '';
}
if (isset($atts['rateus_fontcolor']) && $atts['rateus_fontcolor'] != '') {
    $rateus_fontcolor = esc_attr($atts['rateus_fontcolor']);
} else {
    $rateus_fontcolor = '';
}
if (isset($atts['rateus_font_hover_color']) && $atts['rateus_font_hover_color'] != '') {
    $rateus_fhcolor = esc_attr($atts['rateus_font_hover_color']);
} else {
    $rateus_fhcolor = '';
}
if (isset($atts['rateus_background_color']) && $atts['rateus_background_color'] != '') {
    $rateus_bgcolor = esc_attr($atts['rateus_background_color']);
} else {
    $rateus_bgcolor = '';
}
if (isset($atts['rateus_background_hover_color']) && $atts['rateus_background_hover_color'] != '') {
    $rateus_bghcolor = esc_attr($atts['rateus_background_hover_color']);
} else {
    $rateus_bghcolor = '';
}
if (isset($atts['rateus_font_family']) && $atts['rateus_font_family'] != '') {
    $rateus_family = esc_attr($atts['rateus_font_family']);
} else {
    $rateus_family = '';
}
/* end */

if(isset($all_reviews['error_message']) && $all_reviews['error_message'] != ''){ ?>
<div class="egpr_error_msg_wrapper">
  <?php echo $all_reviews['error_message'];?>
</div>
<?php }else{
 ?>
<div class="egpr-reviews-main-wrapper egpr-review-<?php echo esc_attr($choose_template); ?> egpr-layout-<?php echo esc_attr($layout_type); ?> <?php echo esc_attr($badge_position_class);?>-position" id="egpr-reviews-outer-wrap-<?php echo $random_num; ?>">
        <div class="egpr-inner-wrap egpr-clearfix">
        <div itemscope itemtype="http://schema.org/LocalBusiness" style="display:none;">
              <span itemprop="name"><?php echo $business_name;?></span>
              <span itemprop="telephone"><?php echo $phone_number;?></span>
              <span itemprop="priceRange"><?php echo $price_range;?></span>
              <div itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
               <span itemprop="ratingValue"><?php echo $avg_star; ?></span>
                <span itemprop="reviewCount"><?php echo $total_review_count ;?></span>
               </div>
            <img itemprop="image" src="https://maps.googleapis.com/maps/api/place/photo?photoreference=<?php echo esc_attr($photo_avatar); ?>&maxwidth=1300&maxheight=500&key=<?php echo $nverified_api_key; ?>">
            <div itemprop="address" itemscope itemtype="http://schema.org/PostalAddress">
                <?php if(!empty($address_components)){
                   for($i = 0; $i < count($address_components); $i++){
                     $types = (isset($address_components[$i]['types'][0]) && $address_components[$i]['types'][0] != '') ? $address_components[$i]['types'][0] : '';
                     if( $types  == "route" ){
                      $streetAddress = (isset($address_components[$i]['long_name']) && $address_components[$i]['long_name'] != '') ? $address_components[$i]['long_name'] : '';
                      ?>
                      <span itemprop="streetAddress"><?php echo $streetAddress;?></span>
                     <?php  }else if($types  == "locality"){
                      $addressLocality = (isset($address_components[$i]['long_name']) && $address_components[$i]['long_name'] != '') ? $address_components[$i]['long_name'] : '';
                      ?>
                      <span itemprop="addressLocality"><?php echo $addressLocality;?></span>
                     <?php
                     }else if($types  == "postal_code"){
                      $postalCode = (isset($address_components[$i]['long_name']) && $address_components[$i]['long_name'] != '') ? $address_components[$i]['long_name'] : '';
                      ?>
                      <span itemprop="postalCode"><?php echo $postalCode;?></span>
                     <?php
                     }else if($types  == "administrative_area_level_1"){
                     $addressRegion = (isset($address_components[$i]['long_name']) && $address_components[$i]['long_name'] != '') ? $address_components[$i]['long_name'] : '';
                      ?>
                      <span itemprop="addressRegion"><?php echo $addressRegion;?></span>
                     <?php
                     }

                    }
                  }?>
            </div>
          </div>
            <?php
            if ($show_badge == "true") {
                include(EGPR_PATH . 'inc/frontend/shortcode/egpr-business-badge.php');
            }
            ?>
          <?php if ($show_reviews == "true") {  ?>
          <div class="egpr-google-reviews-wrap egpr-review-<?php echo esc_attr($choose_template); ?>">
          <?php } ?>
            <?php if ($show_reviews == "true") {
              if (isset($reviews) && !empty($reviews)) { 
                ?>

               <div class="egpr-review-section-outer-new-wrap egpr-<?php echo $review_animation; ?>">
                 <div class="egpr-review-section">
                   <div class="egpr-all-reviews-wrapper <?php echo $review_animation_type;?>" <?php if($review_animation_type != '') echo 'data-wow-duration="2s" data-wow-delay="0.4s"'; else echo '';?>>
                      <?php
                         include(EGPR_PATH . 'inc/frontend/shortcode/templates/egpr-review-'.$choose_template.'.php');
                        ?>
                  </div>
                </div>
              </div>
                       <?php  }
                } ?>
             <?php
             if ($enable_rateus == 1 || $enable_viewall == 1) { ?>
                <div class="egpr-rateus-button-wrap">

                <?php if($enable_rateus == 1){ ?>
                      <a href="<?php echo $rateus_custom_url;?>" target="<?php echo esc_attr($link_target); ?>" class="egpr-rate-button"><?php echo esc_attr($text_title); ?></a>
                 <?php } ?>
                 <?php if($enable_viewall == 1){ ?>
                 <a href="<?php echo esc_url($viewall_custom_url); ?>" class="egpr-viewall-button" target="<?php echo esc_attr($vblink_target); ?>"><?php echo 'asd'.esc_attr($text_vbtitle); ?></a>
                <?php } ?>

                </div>
             <?php } ?>
            <?php if ($show_reviews == "true") {  ?>
            </div>
          <?php } ?>
        </div>
</div>
<?php }
?>
<?php if($description_font_family != $rateus_family){
if($description_font_family != ''){?>
<link rel='stylesheet' href='//fonts.googleapis.com/css?family=<?php echo $description_font_family;?>' type='text/css' media='all' />
<?php }
}else{
  if($description_font_family != ''){?>
<link rel='stylesheet' href='//fonts.googleapis.com/css?family=<?php echo $description_font_family;?>' type='text/css' media='all' />
<?php }
}?>
<style>
<?php
 include(EGPR_PATH . 'inc/frontend/advanced-custom-style.php');
?>
</style>
