<?php

namespace WP_Rplg_Google_Reviews\Includes;

class View {

    const G_AVA_SIZE = 's120';

    public function render($feed_id, $businesses, $reviews, $options, $is_admin = false) {
        ob_start();

        $max_width = $options->max_width;
        if (is_numeric($max_width)) {
            $max_width = $max_width . 'px';
        }
        $max_height = $options->max_height;
        if (is_numeric($max_height)) {
            $max_height = $max_height . 'px';
        }

        $style = '';
        if (isset($max_width) && strlen($max_width) > 0) {
            $style .= 'width:' . $max_width . '!important;';
        }
        if (isset($max_height) && strlen($max_height) > 0) {
            $style .= 'height:' . $max_height . '!important;overflow-y:auto!important;';
        }
        if ($options->centered) {
            $style .= 'margin:0 auto!important;';
        }
        if (isset($options->style_vars) && strlen($options->style_vars) > 0) {
            $style .= $options->style_vars;
        }

        $cls = empty($options->style) || $options->style === 'legacy' ? ' wpac' : '';
        if ($options->dark_theme) {
            $cls .= ' wp-dark';
        }

        ?><div class="wp-gr rpi<?php echo $cls; ?>"<?php if ($style) { ?> style="<?php echo esc_attr($style); ?>"<?php } ?> data-id="<?php echo esc_attr($feed_id); ?>" data-layout="<?php echo esc_attr($options->view_mode); ?>" data-exec="false" data-options='<?php echo esc_attr($this->options($options)); ?>'>
            <svg xmlns="http://www.w3.org/2000/svg" style="display:none!important">
                <symbol id="grw-tripadvisor" viewBox="0 0 713.496 713.496">
                    <g><circle fill="#34E0A1" cx="356.749" cy="356.748" r="356.748"/><path d="M577.095,287.152l43.049-46.836h-95.465c-47.792-32.646-105.51-51.659-167.931-51.659   c-62.342,0-119.899,19.054-167.612,51.659H93.432l43.049,46.836c-26.387,24.075-42.929,58.754-42.929,97.259   c0,72.665,58.914,131.578,131.579,131.578c34.519,0,65.968-13.313,89.446-35.077l42.172,45.919l42.172-45.879   c23.478,21.764,54.887,35.037,89.406,35.037c72.665,0,131.658-58.913,131.658-131.578   C620.024,345.866,603.483,311.188,577.095,287.152z M225.17,473.458c-49.188,0-89.047-39.859-89.047-89.047   s39.86-89.048,89.047-89.048c49.187,0,89.047,39.86,89.047,89.048S274.357,473.458,225.17,473.458z M356.788,381.82   c0-58.595-42.61-108.898-98.853-130.383c30.413-12.716,63.776-19.771,98.813-19.771s68.439,7.055,98.853,19.771   C399.399,272.962,356.788,323.226,356.788,381.82z M488.367,473.458c-49.188,0-89.048-39.859-89.048-89.047   s39.86-89.048,89.048-89.048s89.047,39.86,89.047,89.048S537.554,473.458,488.367,473.458z M488.367,337.694   c-25.79,0-46.677,20.887-46.677,46.677c0,25.789,20.887,46.676,46.677,46.676c25.789,0,46.676-20.887,46.676-46.676   C535.042,358.621,514.156,337.694,488.367,337.694z M271.846,384.411c0,25.789-20.887,46.676-46.676,46.676   s-46.676-20.887-46.676-46.676c0-25.79,20.887-46.677,46.676-46.677C250.959,337.694,271.846,358.621,271.846,384.411z"/></g>
                </symbol>
                <symbol id="grw-google" viewBox="0 0 512 512">
                    <g fill="none" fill-rule="evenodd"><path d="M482.56 261.36c0-16.73-1.5-32.83-4.29-48.27H256v91.29h127.01c-5.47 29.5-22.1 54.49-47.09 71.23v59.21h76.27c44.63-41.09 70.37-101.59 70.37-173.46z" fill="#4285f4"/><path d="M256 492c63.72 0 117.14-21.13 156.19-57.18l-76.27-59.21c-21.13 14.16-48.17 22.53-79.92 22.53-61.47 0-113.49-41.51-132.05-97.3H45.1v61.15c38.83 77.13 118.64 130.01 210.9 130.01z" fill="#34a853"/><path d="M123.95 300.84c-4.72-14.16-7.4-29.29-7.4-44.84s2.68-30.68 7.4-44.84V150.01H45.1C29.12 181.87 20 217.92 20 256c0 38.08 9.12 74.13 25.1 105.99l78.85-61.15z" fill="#fbbc05"/><path d="M256 113.86c34.65 0 65.76 11.91 90.22 35.29l67.69-67.69C373.03 43.39 319.61 20 256 20c-92.25 0-172.07 52.89-210.9 130.01l78.85 61.15c18.56-55.78 70.59-97.3 132.05-97.3z" fill="#ea4335"/><path d="M20 20h472v472H20V20z"/></g>
                </symbol>
            </svg><?php
            $rows = false;
            switch ($options->view_mode) {
                case 'slider':
                    $this->render_slider($businesses, $reviews, $options, $is_admin);
                    $rows = true;
                    break;
                case 'grid':
                    $this->render_grid($businesses, $reviews, $options, $is_admin);
                    $rows = true;
                    break;
                case 'list':
                    $this->render_list($businesses, $reviews, $options, $is_admin);
                    break;
                case 'rating':
                    $this->render_rating($businesses, $reviews, $options);
                    break;
                case 'badge':
                    $this->render_badge($businesses, $reviews, $options);
                    break;
                default:
                    $this->render_list($businesses, $reviews, $options, $is_admin);
            }

            if ($rows) {
                ?><script>(function(p){if(!p)return;let w=p.offsetWidth,m=function(a,b){return Math.min(a,b)===a};p.querySelector('.grw-row').classList.replace('grw-row-m','grw-row-'+(m(w,510)?'xs':m(w,750)?'x':m(w,1100)?'s':m(w,1450)?'m':m(w,1800)?'l':'xl'))})(document.currentScript?.parentElement)</script><?php
            }
        ?></div><?php
        return preg_replace('/[\n\r]|(>)\s+(<)/', '$1$2', ob_get_clean());
    }

    private function options($options) {
        return json_encode(
            array(
                'text_size' => $options->text_size,
                'trans'     => array(
                    'read more' => __('read more', 'widget-google-reviews')
                )
            )
        );
    }

    private function render_slider($businesses, $reviews, $options, $is_admin = false) {
        ?><div class="grw-row grw-row-m" data-options='<?php
            echo json_encode(
                array(
                    'speed'       => $options->slider_speed ? $options->slider_speed : 3,
                    'autoplay'    => $options->slider_autoplay,
                    'mousestop'   => $options->slider_mousestop,
                    'breakpoints' => $options->slider_breakpoints
                )
            ); ?>'><?php
            if (count($businesses) > 0) {
                $this->grw_place($businesses[0]->rating, $businesses[0], $businesses[0]->photo, $reviews, $options, true, true);
            }
            $count = count($reviews);
            if ($count > 0) {
            ?><div class="rpi-slides-root grw-content">
                <div class="grw-content-inner"><?php
                $cls = 'rpi-slides grw-reviews';
                if (!$options->slider_hide_prevnext) {
                    $cls .= ' rpi-slides-bite';
                    $aria_label = $this->grw_aria_label($options, 'Previous');
                    ?><button class="rpi-ltgt rpi-lt grw-prev"<?php echo $aria_label; ?> tabindex="0"></button><?php
                }
                    ?><div class="<?php echo $cls; ?>" data-count="<?php echo $count; ?>" data-offset="<?php echo $count; ?>"><?php
                        foreach ($reviews as $review) {
                            $this->grw_slider_review($review, false, $options, $is_admin);
                        }
                    ?></div><?php
                if (!$options->slider_hide_prevnext) {
                    $aria_label = $this->grw_aria_label($options, 'Next');
                    ?><button class="rpi-ltgt rpi-gt grw-next"<?php echo $aria_label; ?> tabindex="0"></button><?php
                }
                if (!$options->slider_hide_dots) {
                    ?><div class="rpi-dots-wrap"><div class="rpi-dots"></div></div><?php
                }
                ?></div>
            </div><?php
            }
        ?></div><?php
    }

    private function render_grid($businesses, $reviews, $options, $is_admin = false) {
        $hr = false;
        if (count($businesses) > 0) {
            $this->grw_place(
                $businesses[0]->rating,
                $businesses[0],
                $businesses[0]->photo,
                $reviews,
                $options,
                true,
                true
            );
        }
        ?><div class="grw-row grw-row-m" data-options='<?php
            echo json_encode(
                array(
                    'breakpoints' => $options->slider_breakpoints
                )
            ); ?>'>
            <?php if (count($reviews) > 0) { ?>
            <div class="grw-content">
                <div class="grw-content-inner">
                    <div class="grw-reviews">
                        <?php
                        if (count($reviews) > 0) {
                            $i = 0;
                            foreach ($reviews as $review) {
                                if ($options->pagination > 0 && $options->pagination <= $i++) {
                                    $hr = true;
                                }
                                $this->grw_slider_review($review, $hr, $options, $is_admin);
                            }
                        }
                        ?>
                    </div>
                </div>
                <?php if ($options->pagination > 0 && $hr) { ?>
                <a class="wp-google-url" href="#" onclick="return rplg_next_reviews.call(this, 'grw', <?php echo $options->pagination; ?>);">
                    <?php echo __('More reviews', 'widget-google-reviews'); ?>
                </a>
                <?php } ?>
            </div>
            <?php } ?>
        </div><?php
    }

    private function render_list($businesses, $reviews, $options, $is_admin = false) {
        ?><div class="wp-google-list"><?php
            foreach ($businesses as $business) {
                $this->grw_place(
                    $business->rating,
                    $business,
                    $business->photo,
                    $reviews,
                    $options
                );
            }
            if (!$options->hide_reviews) {
                $this->grw_place_reviews($reviews, $options, $is_admin);
            }
        ?></div><?php
    }

    private function render_rating($businesses, $reviews, $options, $is_admin = false) {
        ?><div class="wp-google-list"><?php
            foreach ($businesses as $business) {
                $this->grw_place(
                    $business->rating,
                    $business,
                    $business->photo,
                    $reviews,
                    $options
                );
            }
        ?></div><?php
    }

    private function render_badge($businesses, $reviews, $options) {
        ?>
        <script type="text/javascript">
        function grw_badge_init(el) {
            var btn = el.querySelector('.wp-google-badge'),
                form = el.querySelector('.wp-google-form');

            var wpac = document.createElement('div');
            wpac.className = 'wp-gr wpac';
            wpac.appendChild(form);
            document.body.appendChild(wpac);

            btn.onclick = function() {
                form.style.display='block';
            };
        }
        </script>
        <?php foreach ($businesses as $business) { ?>
        <div class="wp-google-badge<?php if ($options->view_mode == 'badge') { ?> wp-google-badge-fixed<?php } ?>">
            <div class="wp-google-border"></div>
            <div class="wp-google-badge-btn">
                <svg height="44" width="44" role="none"><use href="#grw-google"></use></svg>
                <div class="wp-google-badge-score">
                    <div><?php echo __('Google Rating', 'widget-google-reviews'); ?></div>
                    <span class="rpi-stars" style="--rating:<?php echo $business->rating; ?>"><?php echo $business->rating; ?></span>
                </div>
            </div>
        </div>
        <?php } ?>
        <div class="wp-google-form" style="display:none">
            <?php foreach ($businesses as $business) { ?>
            <div class="wp-google-head">
                <div class="wp-google-head-inner">
                    <?php
                    $this->grw_place(
                        $business->rating,
                        $business,
                        $business->photo,
                        $reviews,
                        $options,
                        false
                    ); ?>
                </div>
                <button class="wp-google-close" type="button" onclick="this.parentNode.parentNode.style.display='none'">×</button>
            </div>
            <?php } ?>
            <div class="wp-google-body"></div>
            <div class="wp-google-content">
                <div class="wp-google-content-inner">
                    <?php $this->grw_place_reviews($reviews, $options); ?>
                </div>
            </div>
            <?php $this->grw_powered(); ?>
        </div><?php
    }

    function grw_place($rating, $place, $place_img, $reviews, $options, $show_powered = true, $show_writereview = false) {
        $style = $options->header_center ? 'style="--dir:column;--align:center;--star-align-self:center"' : '';
        ?><div class="grw-header">
            <div class="grw-header-inner rpi-flx rpi-row12"<?php echo $style; ?>><?php
                if (!$options->header_hide_photo) {
                    $alt_val = sprintf(__('%s place picture', 'widget-google-reviews'), $place->name);
                    $alt = empty($options->aria_label) ? $alt_val : ($options->header_hide_name ? $alt_val : '');
                    $this->grw_image($place_img, $alt, $options->lazy_load_img);
                }
                ?><div class="rpi-flx rpi-col8"<?php echo $style; ?>><?php
                    if (!$options->header_hide_name) {
                        ?><div class="wp-google-name"><?php
                        echo $this->grw_anchor($place->url, '', $place->name, $options, sprintf(__('%s place profile', 'widget-google-reviews'), $place->name));
                        ?></div><?php
                    }
                    $this->grw_place_rating($rating, $place->review_count, $options);
                    if ($show_powered) $this->grw_powered();
                    if (!$options->hide_writereview) {
                        ?><div class="wp-google-wr"><?php
                            echo $this->grw_anchor(
                                'https://search.google.com/local/writereview?placeid=' . $place->id,
                                '',
                                __('review us on', 'widget-google-reviews'),
                                $options,
                                __('review us on Google', 'widget-google-reviews'),
                                'return rplg_leave_review_window.call(this)',
                                '<svg height="16" width="16" role="none"><use href="#grw-google"></use></svg>'
                            );
                        ?></div><?php
                    }
                ?></div>
            </div>
        </div><?php
    }

    function grw_place_rating($rating, $review_count, $opts) {
        $aria_label = $this->grw_aria_label($opts, sprintf(__('Rating: %s out of 5', 'widget-google-reviews'), $rating), 'img');
        ?><span class="rpi-stars"<?php echo $aria_label; ?> style="--rating:<?php echo $rating; ?>"><?php echo $rating; ?></span><?php
        if (!$opts->hide_based_on && isset($review_count)) {
        ?><div class="wp-google-based"><?php echo vsprintf(__('Based on %s reviews', 'widget-google-reviews'), $this->grw_array($review_count)); ?></div><?php
        }
    }

    function grw_powered() {
        ?><div class="wp-google-powered">powered by <span><span style="color:#3c6df0!important">G</span><span style="color:#d93025!important">o</span><span style="color:#fb8e28!important">o</span><span style="color:#3c6df0!important">g</span><span style="color:#188038!important">l</span><span style="color:#d93025!important">e</span></span></div><?php
    }

    function grw_place_reviews($reviews, $options, $is_admin = false) {
        ?><div class="rpi-flx rpi-col16"><?php
        $place_id = null;
        $place_url = null;

        $hr = false;
        if (count($reviews) > 0) {
            $i = 0;
            foreach ($reviews as $review) {
                if (!$place_id) {
                    $place_id = $review->biz_id;
                    $place_url = $review->biz_url;
                }
                if ($options->pagination > 0 && $options->pagination <= $i++) {
                    $hr = true;
                }
                $this->grw_place_review($review, $hr, $options, $is_admin);
            }
        }
        ?></div><?php
        if ($options->pagination > 0 && $hr) {
        ?><a class="wp-google-url" href="#" onclick="return rplg_next_reviews.call(this, 'wp-google', <?php echo $options->pagination; ?>);"><?php
            echo __('More reviews', 'widget-google-reviews');
        ?></a><?php
        } else {
            $reviews_link = $options->google_def_rev_link ? $place_url : 'https://search.google.com/local/reviews?placeid=' . $place_id;
            $this->grw_anchor($reviews_link, 'wp-google-url', __('See All Reviews', 'widget-google-reviews'), $options, __('All reviews', 'widget-google-reviews'));
        }
    }

    function grw_place_review($review, $hr, $options, $is_admin = false) {
        ?>
        <div class="wp-google-review<?php if ($hr) { echo ' wp-google-hide'; } if ($is_admin && $review->hide != '') { echo ' wp-review-hidden'; } ?>">
            <?php if (!$options->hide_avatar) { ?>
            <div class="rpi-flx rpi-row12">
                <?php
                $default_avatar = GRW_ASSETS_URL . 'img/guest.png';
                if (strlen($review->author_avatar) > 0) {
                    $author_avatar = $review->author_avatar;
                } else {
                    $author_avatar = $default_avatar;
                }
                if (isset($options->reviewer_avatar_size)) {
                    $author_avatar = str_replace(self::G_AVA_SIZE, 's' . $options->reviewer_avatar_size, $author_avatar);
                    $default_avatar = str_replace(self::G_AVA_SIZE, 's' . $options->reviewer_avatar_size, $default_avatar);
                }
                $author_name = empty($review->author_name) ? __('Google User', 'widget-google-reviews') : $review->author_name;
                $alt = empty($options->aria_label) ? sprintf(__('%s profile picture', 'widget-google-reviews'), $author_name) : '';
                $this->grw_image($author_avatar, $alt, $options->lazy_load_img, $default_avatar);
                ?>
                <div class="rpi-flx rpi-col4">
                    <?php
                    if (strlen($review->author_url) > 0) {
                        $aria_label = sprintf(__('%s user profile', 'widget-google-reviews'), $author_name);
                        $this->grw_anchor($review->author_url, 'wp-google-name', $author_name, $options, $aria_label);
                    } else {
                        ?><div class="wp-google-name"><?php echo esc_html($author_name); ?></div><?php
                    }
                    ?>
                    <div class="wp-google-time" data-time="<?php echo $review->time; ?>"><?php echo gmdate("H:i d M y", $review->time); ?></div>
                    <div class="wp-google-feedback">
                        <span class="rpi-stars" style="--rating:<?php echo $review->rating; ?>"></span>
                        <span class="wp-google-text"><?php echo wp_kses_post($review->text); ?></span>
                    </div>
                    <?php if ($is_admin) {
                        echo '<a href="#" class="wp-review-hide" data-id=' . $review->id . '>' . ($review->hide == '' ? 'Hide' : 'Show') . ' review</a>';
                    } ?>
                </div>
            </div>
            <?php } ?>
        </div>
        <?php
    }

    function grw_slider_review($review, $hr, $options, $is_admin = false) {
        $cls = $hr ? ' grw-hide' : '';
        $cls .= $is_admin && ($review->hide != '' || (isset($options->hidden) && $this->is_hidden($review->id, $options->hidden))) ? ' wp-review-hidden' : '';
        $inr_cls = $options->hide_backgnd ? "" : " grw-backgnd";
        $inr_cls .= $options->show_round ? " grw-round" : "";
        $inr_cls .= $options->show_shadow ? " grw-shadow" : "";
        ?><div class="rpi-slide grw-review<?php echo $cls; ?>">
            <div class="rpi-flx rpi-col12 grw-review-inner<?php echo $inr_cls; ?>">
                <div class="rpi-flx rpi-row12-center"><?php
                    // Google reviewer avatar
                    $default_avatar = GRW_ASSETS_URL . 'img/guest.png';
                    if (strlen($review->author_avatar) > 0) {
                        $author_avatar = $review->author_avatar;
                    } else {
                        $author_avatar = $default_avatar;
                    }
                    if (isset($options->reviewer_avatar_size)) {
                        $author_avatar = str_replace(self::G_AVA_SIZE, 's' . $options->reviewer_avatar_size, $author_avatar);
                        $default_avatar = str_replace(self::G_AVA_SIZE, 's' . $options->reviewer_avatar_size, $default_avatar);
                    }
                    $author_name = empty($review->author_name) ? __('Google User', 'widget-google-reviews') : $review->author_name;
                    $alt = empty($options->aria_label) ? sprintf(__('%s profile picture', 'widget-google-reviews'), $author_name) : '';
                    $this->grw_image($author_avatar, $alt, $options->lazy_load_img, $default_avatar);

                    ?><div class="rpi-flx rpi-col6"><?php
                        // Google reviewer name
                        if (!empty($review->author_url) || !empty($review->url)) {
                            $url = empty($review->author_url) ? $review->url : $review->author_url;
                            $aria_label = sprintf(__('%s user profile', 'widget-google-reviews'), $author_name);
                            $this->grw_anchor($url, 'wp-google-name', $author_name, $options, $aria_label);
                        } else {
                            ?><div class="wp-google-name"><?php echo esc_html($author_name); ?></div><?php
                        }
                        ?><div class="wp-google-time" data-time="<?php echo $review->time; ?>"><?php echo gmdate("H:i d M y", $review->time); ?></div>
                    </div>
                </div>
                <span class="rpi-stars" style="--rating:<?php echo $review->rating; ?>"></span>
                <div class="rpi-flx rpi-col4">
                    <div class="wp-google-feedback grw-scroll" <?php if (strlen($options->slider_text_height) > 0) {?> style="height:<?php echo $options->slider_text_height; ?>!important"<?php } ?>>
                        <?php if (strlen($review->text) > 0) { ?>
                        <span class="wp-google-text"><?php echo wp_kses_post($review->text); ?></span>
                        <?php } ?>
                    </div><?php
                    if (isset($options->media) && $options->media && isset($review->images) && strlen($review->images) > 0) {
                    ?><div class="wp-google-img"><?php
                        $images = explode(';', $review->images);
                        foreach ($images as $img) {
                            $thumb_alt = __('Photo from customer review', 'widget-google-reviews');
                            ?><img class="rpi-thumb" src="<?php echo preg_replace('/(=.*)s\d{2,3}/', '$1s50', esc_url($img)); ?>" alt="<?php echo esc_attr($thumb_alt); ?>" loading="lazy"><?php
                        }
                    ?></div><?php
                    }
                ?></div><?php
                if (isset($options->reply) && $options->reply && isset($review->reply) && strlen($review->reply) > 0) {
                ?><div class="wp-google-reply grw-scroll">
                    <div>
                        <span class="grw-b"><?php echo __('Response from the owner', 'widget-google-reviews'); ?></span>
                        <span class="wp-google-time" data-time="<?php echo $review->reply_time; ?>">
                            <?php echo gmdate("H:i d M y", $review->reply_time); ?>
                        </span>
                    </div><?php
                    echo wp_kses_post($review->reply);
                ?></div><?php
                }
                if ($is_admin) {
                    echo '<a href="#" class="wp-review-hide" data-id=' . $review->id . '>' . ($review->hide == '' ? 'hide' : 'show') . ' review</a>';
                }
                $this->grw_provider($review);
            ?></div>
        </div><?php
    }

    function is_hidden($id, $hidden) {
        $ids = array_map('intval', explode(',', $hidden));
        return in_array((int)$id, $ids, true);
    }

    function grw_provider($review) {
        ?><svg height="16" width="16" role="none"><use href="#grw-<?php echo $review->provider; ?>"/></svg><?php
    }

    function grw_anchor($url, $class, $text, $options, $aria_label = '', $onclick = '', $after_raw = '') {
        $al = $this->grw_aria_label($options, esc_attr($aria_label) . ' - ' . __('opens in a new window', 'widget-google-reviews'));
        echo '<a href="' . esc_url($url) . '"' . ($class ? ' class="' . $class . '"' : '') . ($options->open_link ? ' target="_blank"' : '') . ' rel="' . ($options->nofollow_link ? 'nofollow ' : '') . 'noopener"' . $al . (empty($onclick) ? '' : ' onclick="' . $onclick . '"') . '>' . esc_html($text) . $after_raw . '</a>';
    }

    function grw_aria_label($options, $aria_label, $role = '') {
        return empty($options->aria_label) || empty($aria_label) ? '' : (empty($role) ? '' : ' role="' . $role . '"') . ' aria-label="' . $aria_label . '"';
    }

    function grw_image($src, $alt, $lazy, $def_ava = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', $atts = '') {
        ?><div class="grw-img-wrap"><img class="grw-img" src="<?php echo esc_url($src); ?>"<?php if ($lazy) { ?> loading="lazy"<?php } ?> alt="<?php echo esc_attr($alt); ?>" width="50" height="50" onerror="if(this.src!='<?php echo $def_ava; ?>')this.src='<?php echo $def_ava; ?>';" <?php echo $atts; ?>></div><?php
    }

    function grw_array($params=null) {
        if (!is_array($params)) {
            $params = func_get_args();
            $params = array_slice($params, 0);
        }
        return $params;
    }
}
