<?php

/**
 * Display feedback overview widget.
 */
class EQ3T_Display_Overview extends WP_Widget {

  public function __construct() {

    parent::__construct( 
        'remsto_feedback_widget_overview', 
        'Enquir3 Testimonial', 
        array( 'description' => 'Display a testimonial overview' )
    );
  }

  // Code to render options form
  public function form( $instance ) {
    
    $theme = ( ! empty($instance['theme'] ) ?
        esc_attr($instance['theme']) :
        'medium' );
      
        $max_char = ( ! empty($instance['max-char']) ?
        esc_attr($instance['max-char']) :
        '150' );   
    ?>

    <p>
        <label for="<?php echo $this->get_field_id('theme');
    ?>">
    <?php echo 'Theme:<br>'; ?>		
 Light <input type="radio" name="<?php echo $this->get_field_name('theme'); ?>" 
       id="<?php echo $this->get_field_id('theme'); ?>" <?php checked( $theme, 'light' ); ?> value="light">
 Medium <input type="radio" name="<?php echo $this->get_field_name('theme'); ?>" 
       id="<?php echo $this->get_field_id('theme'); ?>" <?php checked( $theme, 'medium' ); ?> value="medium">
 Dark <input type="radio" name="<?php echo $this->get_field_name('theme'); ?>" 
       id="<?php echo $this->get_field_id('theme'); ?>" <?php checked( $theme, 'dark' ); ?> value="dark">	
        </label>
    </p> 
    
       
        <p>
        <label for="<?php echo $this->get_field_id('max-char');
    ?>">
    <?php echo 'Max chars:'; ?>			
            <input type="text" 
                   id="<?php echo $this->get_field_id('max-char'); ?>"
                   name="<?php echo $this->get_field_name('max-char'); ?>"
                   value="<?php echo $max_char; ?>" />			
        </label>
    </p> 
    
    
    <?php
    wp_reset_query();
  }
  


  // Function to perform user input validation
  public function update( $new_instance, $instance ) {

    if ( $new_instance['theme'] === 'light' || $new_instance['theme'] === 'medium' || $new_instance['theme'] === 'dark' ) {
      $instance['theme'] = $new_instance['theme'];
    } else {
      $instance['theme'] = 'medium';
    }
    
    if ( is_numeric($new_instance['max-char']) && $new_instance['max-char'] > 0 ) {
      $instance['max-char'] = $new_instance['max-char'];
    } else {
      $instance['max-char'] = 150;
    }
         
    return $instance;
  }

  /**
   * display overview widget
   *
   * @param Array $args
   * @param Array $instance
   * @return void
   */
  public function widget( $args, $instance ) {

    wp_enqueue_style('eq3t-overview');
    wp_enqueue_script('eq3t-carousel');

    $client_options = get_option('eq3t_options', array());
    $fc = new RemstoFeedbackClient($client_options);
    $summaey_data = $fc->getFeedbackSummary($client_options['parent_type'], $client_options['parent_id']);
    $performance_data = (array) $fc->getPerformance($client_options['parent_type'], $client_options['parent_id']);
    $rating_data = $fc->getRatings($client_options['parent_type'], $client_options['parent_id']);

    $total = 0;
    $recommend = 0;

    foreach ( $summaey_data as $row ) {
      $total += $row->total;
      if ( $row->recommend === '1' ) {
        $recommend = $row->total;
      }
    }

    $rating_summary = RemstoClientUtils::remsto_client_feedback_rating_display(
        array(
              'rating' => $rating_data->total,
              'label' => '',
              'stars' => 5,
        )
    );

    unset($performance_data['total']);
    arsort($performance_data);
    $overview_text = '';

    foreach ( $performance_data as $key => $value ) {
      switch ( $key ) {
        case 'excellent':
          $overview_text = 'Excellent';
          break;
        case 'vgood':
          $overview_text = 'Very Good';
          break;
        case 'average':
          $overview_text = 'Average';
          break;
        case 'bad':
          $overview_text = 'Bad';
          break;
        case 'terrible':
          $overview_text = 'Terrible';
          break;
      }
      break;
    }
    
    $overview_data = array(
      'link_addr' => $client_options['link_addr'],
      'overview_text' => $overview_text,
      'rating_average' => $rating_summary,
      'total_feedback_count' => $total,
      'would_recommend_count' => $recommend,
      'feedback_items' => $this->render_feedback($instance),
      'images_path' => plugins_url('/css/images/', EQ3T_PLUGIN_BASENAME),
      'overview_theme' => 'overview-theme-' . $instance['theme'],
    );

	EQ3T_Testimonial_Display::render_template(
		EQ3T_PLUGIN_PATH . 'includes/views/feedback_widget_overview.tpl.php',
		$overview_data
	);
  }

  private function render_feedback( $instance ) {

      $number_of_items = 20;

      wp_enqueue_script('jquery-jcarousel');
      /*
      $options = array(
        'jcarousel' => array(
          'wrap' => 'circular',
          'vertical' => FALSE,
          'animation' => array(
            'duration' => 900,
            'easing' => 'linear'
          ),
        ),
        'target' => '.feedback-carousel-items',
      );
*/
    $client_options = get_option('eq3t_options', array());
    $fc = new RemstoFeedbackClient($client_options);
    $testimonials = $fc->getFeedback(
        $client_options['parent_type'], 
        $client_options['parent_id'],
        1, // Published
        $number_of_items,
        0,
        null
    );
    $output = '';
    $output .= "<ul class='feedback-landscape-carousel'>";
    foreach ( $testimonials->data as $testimonial ) {
      $output .= '<li>';
      $output .= $this->render_feedback_item($testimonial, $instance['max-char']);
      $output .= '</li>';
    }
    $output .= '</ul>';
    
    //$output .= $this->init_carousel($options);

    return $output;
  }

  private function render_feedback_item( $testimonial, $summary_description_char_limit ) {

    $created_date = date('d/m/Y', $testimonial->created);
    if ( ! empty($summary_description_char_limit ) ) {
      $stood_out = RemstoClientUtils::truncate_utf8(RemstoClientUtils::check_plain($testimonial->stood_out), $summary_description_char_limit, true, true);
    } else {
      $stood_out = RemstoClientUtils::check_plain($testimonial->stood_out);
    }

    if ( $testimonial->anonymous == 1 ) {
      $testimonial->first_name = 'Anonymous';
    }
    
    $rating_stars = RemstoClientUtils::remsto_client_feedback_rating_display(
        array(
              'rating' => $testimonial->rating_promised,
              'label' => '',
              'stars' => 5,
        ), 
        false
    );

    $feedback_data = array(
      'text' => $stood_out,
      'first_name' => RemstoClientUtils::check_plain($testimonial->first_name),
      'date' => $created_date,
      'rating_stars' => $rating_stars,
    );

	return EQ3T_Testimonial_Display::compile_template(
		EQ3T_PLUGIN_PATH . 'includes/views/remsto_feedback_block_item.tpl.php',
		$feedback_data
	);
  }
  
}
