<?php

function serpentsoft_review_description($score) {
    if ($score >= 0 && $score <= 30) {
        return __('Bad', 'serpentsoft');
    } else if ($score > 30 && $score <= 50) {
        return __('Not Bad', 'serpentsoft');
    } else if ($score > 50 && $score <= 70) {
        return __('Good', 'serpentsoft');
    } else if ($score > 70 && $score <= 90) {
        return __('Very Good', 'serpentsoft');
    } else if ($score > 90 && $score <= 100) {
        return __('Excellent', 'serpentsoft');
    } else {
        return '';
    }
}

function serpentsoft_draw_small_stars($post_id) {
    $output = '';

    $get_meta = get_post_custom($post_id);
    if (isset($get_meta['serpentsoft_review_item'][0])) {

        $review_items = unserialize($get_meta['serpentsoft_review_item'][0]);
        $total_score = 0;
        $count_score = 0;


        if (get_post_meta($post_id, 'serpentsoft_post_review_types', true) != 'disabled') {
            foreach ($review_items as $criteria) {
                if (!empty($criteria['name'])) {
                    $total_score += $criteria['score'];
                    $count_score++;
                }
            }

            $tooltip_desc = serpentsoft_review_style_types($post_id, 'serpentsoft_post_review_types', ( $total_score / $count_score));

            if ($count_score > 0) {
                $output .= '<div class="rating" title="' . serpentsoft_review_description($total_score / $count_score) . '! - ' . $tooltip_desc . '"><span class="stars-12">';

                $output .= '<span class="fill" style="width: ' . ($total_score / $count_score) . '%"></span></span></div>';
            } else {
                $output .= '<div class="rating" title="There\'s no criterias in this review post"><span class="stars-12">';
                $output .= '<span class="fill" style="width: 0%"></span></span></div>';
            }
        }
    }
    return $output;
}

function serpentsoft_draw_large_stars($post_id) {
    $output = '';

    $get_meta = get_post_custom($post_id);
    if (isset($get_meta['serpentsoft_review_item'][0])) {

        $review_items = unserialize($get_meta['serpentsoft_review_item'][0]);
        $total_score = 0;
        $count_score = 0;


        if (get_post_meta($post_id, 'serpentsoft_post_review_types', true) != 'disabled') {
            foreach ($review_items as $criteria) {
                if (!empty($criteria['name'])) {
                    $total_score += $criteria['score'];
                    $count_score++;
                }
            }

            $tooltip_desc = serpentsoft_review_style_types($post_id, 'serpentsoft_post_review_types', ( $total_score / $count_score));

            if ($count_score > 0) {
                $output .= '<div class="rating" title="' . serpentsoft_review_description($total_score / $count_score) . '! - ' . $tooltip_desc . '"><span class="stars-24">';

                $output .= '<span class="fill" style="width: ' . ($total_score / $count_score) . '%"></span></span></div>';
            } else {
                $output .= '<div class="rating" title="There\'s no criterias in this review post"><span class="stars-24">';
                $output .= '<span class="fill" style="width: 0%"></span></span></div>';
            }
        }
    }
    return $output;
}

function serpentsoft_post_review_progress_color($score) {
    if ($score >= 0 && $score <= 30) {
        return 'progress-danger';
    } else if ($score > 30 && $score <= 50) {
        return 'progress-warning';
    } else if ($score > 50 && $score <= 75) {
        return 'progress-info';
    } else if ($score > 75 && $score <= 100) {
        return 'progress-success';
    } else {
        return '';
    }
}

function serpentsoft_post_reviews() {
    global $post;
    $get_meta = get_post_custom($post->ID);
    $review_items = unserialize($get_meta['serpentsoft_review_item'][0]);
    $total_score = '';
    $count_score = '';
    $rev_color = '';
    ?>

    <?php if (get_post_meta($post->ID, 'serpentsoft_post_review_types', true) != 'disabled'): ?>

        <div class="post-review" itemscope itemtype="http://schema.org/Product">
            <div class="product-name">
                <h2 itemprop="name"><?php echo get_post_meta($post->ID, 'serpentsoft_post_review_title', true); ?></h2>
            </div>
            <div class="product-desc" itemprop="description">
                <p><?php echo get_post_meta($post->ID, 'serpentsoft_post_review_desc', true); ?></p>
            </div>

            <table class="row-fluid table">
                <tbody>

                    <!-- Progress Review System -->
                    <?php if (get_post_meta($post->ID, 'serpentsoft_post_review_types', true) == 'percent'): ?>
                        <?php foreach ($review_items as $criteria) { ?>
                            <?php if ($criteria['name']): ?>

                                <tr class="criteria" itemprop="review" itemscope itemtype="http://schema.org/Review">
                                    <td class="span4">
                                        <?php /* echo $criteria['name'] . ' - ' . $criteria['score'] . '%'; */ ?>
                                        <h3 itemprop="name"><?php echo $criteria['name']; ?></h3>
                                    </td>

                                    <td class="span8">
                                        <!-- progress-striped -->
                                        <div class="progress <?php echo serpentsoft_post_review_progress_color($criteria['score']); ?>" rel="tooltip" data-toggle="tooltip" data-placement="left" title="" data-original-title="<?php echo $criteria['name'] . ' - ' . $criteria['score'] . '%'; ?>" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">

                                            <span class="bar" style="width: <?php echo $criteria['score'] . '%'; ?>"></span>
                                            <span class="hidden" itemprop="worstRating">1</span>
                                            <span class="hidden" itemprop="ratingValue"><?php echo $criteria['score']; ?></span>
                                            <span class="hidden" itemprop="bestRating">100</span>
                                        </div>
                                    </td>
                                </tr>

                                <?php
                                $total_score += $criteria['score'];
                                $count_score++;
                                ?>

                            <?php endif; ?>
                        <?php } ?><!-- end of foreach -->
        <?php endif; ?>


                    <!-- Stars Review System -->
                    <?php if (get_post_meta($post->ID, 'serpentsoft_post_review_types', true) == 'stars'): ?>
                        <?php foreach ($review_items as $criteria) { ?>
                <?php if ($criteria['name']): ?>

                                <tr class="criteria" itemprop="review" itemscope itemtype="http://schema.org/Review">
                                    <td class="span4">
                                        <h3 itemprop="name"><?php echo $criteria['name']; ?></h3>
                                    </td>

                                    <td class="span8">
                                        <div class="rating" rel="tooltip" data-toggle="tooltip"
                                             data-placement="left" title="" data-original-title="<?php echo $criteria['name'] . ' - ' . round(($criteria['score'] / 20), 1); ?>" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">

                                            <span class="stars-24">
                                                <span class="fill" style="width: <?php echo $criteria['score'] . '%'; ?>"></span>
                                                <span class="hidden" itemprop="worstRating">1</span>
                                                <span class="hidden" itemprop="ratingValue"><?php echo $criteria['score']; ?></span>
                                                <span class="hidden" itemprop="bestRating">100</span>
                                            </span>
                                        </div>
                                    </td>
                                </tr>

                                <?php $total_score += $criteria['score'];
                                $count_score++;
                                ?>

                            <?php endif; ?>
            <?php } ?><!-- end of foreach -->
                    <?php endif; ?>              

                    <!-- Points System -->
                    <?php if (get_post_meta($post->ID, 'serpentsoft_post_review_types', true) == 'points'): ?>
            <?php foreach ($review_items as $criteria) { ?>
                <?php if ($criteria['name']): ?>

                                <tr class="criteria" itemprop="review" itemscope itemtype="http://schema.org/Review">
                                    <td class="span4">
                    <?php /* echo $criteria['name'] . ' - ' . round($criteria['score'] / 10, 1); */ ?>
                                        <h3 itemprop="name"><?php echo $criteria['name']; ?></h3>
                                    </td>

                                    <td class="span8">
                                        <!-- progress-striped -->
                                        <div class="progress <?php echo serpentsoft_post_review_progress_color($criteria['score']); ?>" rel="tooltip" data-toggle="tooltip" data-placement="left" title="" data-original-title="<?php echo $criteria['name'] . ' - ' . round($criteria['score'] / 10, 1); ?>" itemprop="reviewRating" itemscope itemtype="http://schema.org/Rating">
                                            <span class="bar" style="width: <?php echo $criteria['score'] . '%'; ?>"></span>
                                            <span class="hidden" itemprop="worstRating">1</span>
                                            <span class="hidden" itemprop="ratingValue"><?php echo $criteria['score']; ?></span>
                                            <span class="hidden" itemprop="bestRating">100</span>
                                        </div>
                                    </td>
                                </tr>
                                <?php
                                $total_score += $criteria['score'];
                                $count_score++;
                                ?>
                            <?php endif; ?>
                        <?php } ?><!-- end of foreach -->
        <?php endif; ?>

        <?php $tooltip_desc = serpentsoft_review_style_types($post->ID, 'serpentsoft_post_review_types', ( $total_score / $count_score)); ?>
                    <tr class="total-score" itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
                        <td class="span4 result" title="<?php echo __('Review Rate: ', 'serpentsoft') . $tooltip_desc; ?>">
                            <h4>
                                <span><?php echo serpentsoft_review_description(round(($total_score / $count_score), 1)); ?></span><span>!</span>
                            </h4>

                            <div class="rating">
                                <span class="stars-24">
                                    <span class="fill" style="width: <?php echo round(($total_score / $count_score), 1) . '%'; ?>"></span>
                                </span>
                            </div>

                            <!-- Rich Snippet Calculated -->
                            <meta itemprop="ratingValue" content="<?php echo round((($total_score / $count_score) / 20), 1); ?>" />
                            <meta itemprop="reviewCount" content="<?php echo $count_score; ?>" />

                        </td>
                        <td class="span8 summary">
                            <h3><?php
                                if (get_post_meta($post->ID, 'serpentsoft_post_review_summary_title', true) == ''):
                                    echo 'Summary';
                                else: echo get_post_meta($post->ID, 'serpentsoft_post_review_summary_title', true);
                                endif;
                                ?></h3>

                            <!-- Description -->
                            <p><?php echo get_post_meta($post->ID, 'serpentsoft_post_review_summary_desc', true); ?></p>
                        </td>
                    </tr>

                </tbody>
            </table>

        </div>

    <?php endif; ?>

<?php
}

// ===============================================
// Grab Review Values if have title
// ===============================================
function serpentsoft_grab_review_values($post_id = '', $rev_key = '') {

    global $post;
    $get_meta = get_post_custom($post_id);
    $review_items = unserialize($get_meta[$rev_key][0]);

    return $review_items;
}

function serpentsoft_review_style_types($post_id = '', $review_type = '', $criteria_score = 0, $final_grade = true) {

    global $post;
    $get_meta = get_post_custom($post_id);

    $rev_type = $get_meta[$review_type][0];

    switch ($rev_type) {
        case 'percent': {
                return round($criteria_score, 1) . '%';
            }
            break;

        case 'stars': {
                return round(($criteria_score / 20), 1) . ( $final_grade ? '/5' : '' );
            }
            break;

        case 'points': {
                return round(($criteria_score / 10), 1) . ( $final_grade ? '/10' : '' );
            }
            break;
    }
}

function serpentsoft_calculate_review_sum($post_id = '', $rev_key = '', $review_type = '') {

    global $post;
    $get_meta = get_post_custom($post_id);
    $review_items = unserialize($get_meta[$rev_key][0]);

    $rev_type = $get_meta[$review_type][0];

    $score_total = 0;
    $criterias_count = 0;
    $score_desc = '';
    $score_fill_stars = 0;
    $final_grade = '';

    foreach (serpentsoft_grab_review_values($post_id, $rev_key) as $criteria) {
        if ($criteria['name'] && $criteria['score']) {
            if (is_numeric($criteria['score'])) {
                $score_total += $criteria['score'];
                $criterias_count++;
            } else {
                $score_total += 0;
            }
        }
    }

    $score_total = ($score_total / $criterias_count);

    $score_desc = serpentsoft_review_description($score_total);

    /* if ($score_total >= 0 && $score_total <= 30) {
      $score_desc = 'Bad';
      } else if ($score_total > 30 && $score_total <= 50) {
      $score_desc = 'Good';
      } else if ($score_total > 50 && $score_total <= 75) {
      $score_desc = 'Very Good';
      } else if ($score_total > 75 && $score_total <= 100) {
      $score_desc = 'Excellent';
      } else {
      return '';
      } */

    $score_fill_stars = $score_total . '%';

    switch ($rev_type) {
        case 'percent': {
                $score_total = round($score_total, 1) . '%';
            }
            break;

        case 'stars': {
                $score_total = round(($score_total / 20), 1);
                $final_grade = '/5';
            }
            break;

        case 'points': {
                $score_total = round(($score_total / 10), 1);
                $final_grade = '/10';
            }
            break;
    }

    return array($score_total, $score_desc, $score_fill_stars, $final_grade);
}

// ===============================================
// Draw Reviews Category in Home Page
// ===============================================
function serpentsoft_reviews_cat($block_id, $cat_id = 0, $title, $color = '', $posts_num) {
    global $post;
    if (empty($color)) {
        $color = serpentsoft_get_theme_main_color();
    }
    $arr_rgb = hex2rgb($color, '0.87');
    ?>

    <style type="text/css">
        #<?php echo $block_id; ?> .flexslider .flex-direction-nav > li:hover {
            background-color: <?php echo $color; ?>;
        }
        #<?php echo $block_id; ?> .cat-widget-title h3:after{
            background-color: <?php echo $color; ?>;
        }
        #<?php echo $block_id; ?> .cat-review .review .img-container .img-meta .post-result .percent{
            background-color: rgba(<?php echo $arr_rgb[0]; ?>, <?php echo $arr_rgb[1]; ?>, <?php echo $arr_rgb[2]; ?>, <?php echo $arr_rgb[3]; ?>);
        }
        #<?php echo $block_id; ?> .cat-review a:hover{
            color: <?php echo $color; ?>;
        }
    </style>

    <?php
    $wp_query = new WP_Query(array('post_type' => 'post', 'posts_per_page' => $posts_num, 'cat' => $cat_id, 'post__not_in' => get_option('sticky_posts'),
        'meta_query' => array(
            array(
                'key' => 'serpentsoft_post_review_types',
                'value' => 'disabled',
                'compare' => '!='
            ),
            array(
                'key' => 'serpentsoft_post_review_types',
                'compare' => 'EXISTS'
            )
        ),
        'meta_key' => 'serpentsoft_post_reviews_summation', 'orderby' => 'meta_value', 'order' => 'DESC'
    ));

    if ($wp_query->have_posts()):
        ?>

        <section id="<?php echo $block_id; ?>" class="cat-widget review-cat-1">
            <div class="cat-widget-title">
                <h3>
        <?php if ($cat_id > 0): $catName = get_the_category_by_ID($cat_id); ?>
                        <a href="<?php echo get_category_link($cat_id); ?>" title="<?php echo esc_attr(sprintf(__('View All (%s) Category Posts', 'serpentsoft'), $catName)); ?>"><?php echo $title; ?></a>

                            <?php else: echo $title;
                            endif; ?>
                </h3>

            </div>

            <div class="cat-widget-content">
                <div class="row-fluid cat-review modern-items-list">
                    <div id="review_cat_slider_<?php echo $block_id; ?>" class="flexslider">
                        <ul class="slides">

        <?php $s_postcount = 1; ?>
            
            <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

            <?php if ($s_postcount == 1): ?>
                                    <li>
                                        <ul class="items left clearfix">
                                                                <?php endif; ?>

                                        <li>
                                            <article class="review clearfix">

                                                <div class="span5 img-container" title="">
            <?php serpentsoft_grab_post_banner_figure($post->ID); ?>

                                                    <div class="img-meta">
                                                        <div class="post-result">
                                                            <div class="percent">
            <?php $arr_review_sum = serpentsoft_calculate_review_sum($post->ID, 'serpentsoft_review_item', 'serpentsoft_post_review_types'); ?>

                                                                <span><?php echo $arr_review_sum[0]; ?></span>
                                                                <span class="grade"><?php echo $arr_review_sum[1]; ?></span>
                                                            </div>

                                                            <div class="stars">
                                                                <div class="rating" title="<?php echo $arr_review_sum[1] . ' - ' . $arr_review_sum[0] . $arr_review_sum[3]; ?>">
                                                                    <span class="stars-24">
                                                                        <span class="fill" style="width: <?php echo $arr_review_sum[2]; ?>"></span>
                                                                    </span>
                                                                </div>
                                                            </div>
                                                        </div>
                                                    </div>

                                                    <a href="<?php the_permalink(); ?>" class="more" title="<?php the_title(); ?>"></a>
                                                </div>

                                                <div class="span7 content">
                                                    <div class="post-info">
                                                        <div class="post-title clearfix">
                                                            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                                                <h4><?php the_title(); ?></h4>
                                                            </a>
                                                        </div>

                                                        <div class="post-desc">
                                                            <ul class="criteria clearfix">

            <?php foreach (serpentsoft_grab_review_values($post->ID, 'serpentsoft_review_item') as $criteria) {
                if ($criteria['name'] && $criteria['score']) { ?>

                                                            <li>
                                                                <div class="title">
                                                            <?php echo $criteria['name']; ?>
                                                                </div>
                                                                <div class="grade">
                                                            <?php echo serpentsoft_review_style_types($post->ID, 'serpentsoft_post_review_types', $criteria['score'], false); ?>
                                                                </div>
                                                            </li>
                                                                <?php } } ?>

                                                            </ul>
                                                        </div>

                                                    </div>
                                                </div>

                                            </article>
                                        </li>



            <?php if ($s_postcount == 2) { ?>
                                        </ul>
                                    </li>
                <?php $s_postcount = 0;
            } ?>

            <?php $s_postcount++; ?>

        <?php endwhile; ?>

                        </ul>
                    </div>
                </div>
            </div>

            <script type="text/javascript">
                //<![CDATA[
                jQuery(document).ready(function() {
                    "use strict";
                    var is_exist = jQuery('#review_cat_slider_<?php echo $block_id; ?>').length > 0 ? jQuery('#review_cat_slider_<?php echo $block_id; ?>').length : 0;

                    if (is_exist > 0) {
                        jQuery('#review_cat_slider_<?php echo $block_id; ?>').flexslider({
                            slideshow: false,
                            animation: "slide", //String: Select your animation type, "fade" or "slide"
                            easing: "swing", //{NEW} String: Determines the easing method used in $jqSlider transitions. $jqSlider easing plugin is supported!
                            direction: "horizontal"            // String: Select the sliding direction, "horizontal" or "vertical"
                        });
                    }
                });
                //]]>
            </script>

        </section>                    

    <?php
    endif; // have posts
}

// func serpentsoft_reviews_cat ?>