<?php

/**
 * The single post template file
 */
defined('ABSPATH') || die("Can't access directly");
get_header();

global $post;
$data = json_decode(get_post_meta($post->ID, 'data', true));
$img = arcade_img_src($post, $data);

$pc = explode('<!--more-->', $post->post_content);
$desc = (empty($pc[0])) ? '' : $pc[0];
$ins = (empty($pc[1])) ? '' : $pc[1];
$w = abs($data->w);

// Structure
if (!isset($data->structure) || $data->structure < 1) {
    wp_delete_post($post->ID, true);
}

// TAGS
$terms = get_the_tags();
$tag_links = array();
if (!empty($terms)) {
    foreach ($terms as $key => $t) {
        // Term Link
        $term_link = get_term_link($t->term_id);

        // Term title
        $tag_link_title = sprintf('There is %d more %s game', $t->count, $t->name);
        if ($t->count > 1) {
            $tag_link_title = sprintf('There are %d more %s games', $t->count, $t->name);
        }

        // Term Linked
        $tag_link_title = str_replace(' ', '&nbsp;', $tag_link_title);
        $tag_links[] = sprintf('<a href="%s" title="%s">%s</a>', $term_link, esc_attr($tag_link_title), $t->name);
    }
}

// Admin Notice
$notice = '';
if (adminize() &&  !in_array($data->s, get_theme_mod('signed_up_publisher_platforms', array()))) {
    $notice = sprintf('<div class="btn-green"><i class="fa-solid fa-triangle-exclamation"></i> %s</div>', __('Note to admin: If you have not registered to the game provider, this game may not work correctly. Go to theme settings for details.', 'arcade'));
}

// Embed + %1 more width and height
$width = (empty($data->w)) ? '100%' : ceil(abs($data->w) * 1.01);
$height = (empty($data->h)) ? '100%' : ceil(abs($data->h) * 1.01);

switch ($data->s) {
    case 'gamedistribution':
        $embed = sprintf('%s<iframe style="display:none" id="gamebox" src="about:blank" data-src="//html5.gamedistribution.com/%s/?gd_sdk_referrer_url=%s" width="%d" height="%d" scrolling="none" frameborder="0"></iframe>', $notice, $data->ID, get_permalink(), $width, $height);
        break;
    case 'gamemonetize':
        $embed = sprintf('%s<iframe style="display:none" id="gamebox" src="about:blank" data-src="//html5.gamemonetize.com/%s/" width="%d" height="%d" scrolling="none" frameborder="0"></iframe>', $notice, $data->ID, $width, $height);
        break;
    default:
        $embed = __('Unsupported game provider. Please report it to the administrator.', 'arcade');
        break;
}
// Veriables for Game
?>
<script>
    var game_w = <?php echo $w; ?>;
</script>

<div class="w">

    <!-- TOOLS -->
    <div class="tools w f">
        <header class="fi">
            <h1 class="page-title game"><?php echo get_theme_mod('game_page__before_game_title') ?><?php single_post_title(); ?><?php echo get_theme_mod('game_page__after_game_title') ?></h1>
        </header>

        <div id="fav" class="fi t" title="<?php _e('Add to Favorites', 'arcade'); ?>">
            <span class="btn btn-color btn-red"><i class="fas fa-heart"></i></span>
            <i class="fas fa-check bl"></i>
        </div>

        <div id="share" class="fi t" title="<?php _e('Share', 'arcade'); ?>">
            <span class="btn btn-color btn-yellow"><i class="fas fa-share-square"></i></span>
        </div>

        <div id="FS" class="fi t" title="<?php _e('Full Screen', 'arcade'); ?>">
            <span class="btn btn-color btn-green passive"><i class="fas fa-expand-arrows-alt"></i></span>
        </div>
    </div>

    <!-- EMBED GAME -->
    <div id="game" class="w f">
        <div class="fi ad L"><?php echo get_theme_mod('ads_game_embed_L', arcade_ex_ad('120x600')) ?></div>
        <div class="fi embed" style="flex: 0 1 <?php echo $w; ?>px;">
            <div class="starter w">
                <img src="<?php echo esc_url($img) ?>" alt="<?php the_title() ?>" loading="lazy" />
                <div class="game-title w"><?php the_title() ?></div>
                <div class="start btn btn-color btn-green" id="start"><i class="far fa-play-circle"></i> <?php _e('Start Game', 'arcade') ?></div>
            </div><?php
                    echo $embed;
                    ?>
        </div>
        <div class="fi ad R"><?php echo get_theme_mod('ads_game_embed_R', arcade_ex_ad('160x600')) ?></div>
    </div><!-- #game -->

    <div class="desc w mw">
        <p>
            <img id="gi" src="<?php echo esc_url($img) ?>" alt="<?php echo esc_attr($post->post_title); ?>" loading="lazy" />
            <?php echo apply_filters('the_content', $desc); ?>

            <?php if (!empty($tag_links)) : ?>
                <br /><br />
                <i class="fas fa-tag"></i> <?php _e('Categories and tags of the game', 'arcade'); ?> : <?php
                                                                                                        if (count($tag_links) < 6) {
                                                                                                            echo implode(', ', $tag_links);
                                                                                                        } else {
                                                                                                            echo implode(', ', array_slice($tag_links, 0, 6));

                                                                                                            if (count($tag_links) > 6)
                                                                                                                printf(
                                                                                                                    ' <span class="more-tags" role="button">and more...<span style="display:none">, %s</span></span>',
                                                                                                                    implode(', ', array_slice($tag_links, 6))
                                                                                                                );
                                                                                                        }

                                                                                                        ?><br />
            <?php endif; ?>

            <?php if (!empty(trim(strip_tags($ins)))) : ?>
        <h3><i class="fas fa-mouse-pointer"></i> <?php echo esc_attr($post->post_title); ?> - <?php _e('How to Play', 'arcade'); ?></h3>
        <?php echo apply_filters('the_content', $ins); ?>
    <?php endif; ?>
    </p>
    </div><!-- .desc -->

    <div class="center"><?php echo get_theme_mod('ads_game_desc_after', arcade_ex_ad('728x90')) ?></div>

    <?php
    if (function_exists('yarpp_get_related') && get_theme_mod('game_page__show_related_games', 'on') == true) :
    ?>
        <div class="related w mw">
            <h3><i class="fas fa-gamepad"></i>
                <?php
                if (get_theme_mod('game_page__show_related_games_title')) {
                    echo esc_attr(
                        str_replace('{TITLE}', $post->post_title, get_theme_mod('game_page__show_related_games_title'))
                    );
                } else {
                    printf('%s - %s', esc_attr($post->post_title), __('Related Games', 'arcade') );
                }
                ?>
            </h3>
            <div class="w rg">
                <?php
                $related_posts_array = array();
                $related_posts = array();
                $related_posts = yarpp_get_related(['limit' => abs(get_theme_mod('game_page__show_related_games_count', 12))], $post->ID);

                if (!empty($related_posts)) :
                    foreach ($related_posts as $related) :
                        $r_title = esc_attr($related->post_title);
                        $r_data = json_decode(get_post_meta($related->ID, 'data', true));
                        $r_img_src = arcade_img_src($related, $r_data);  ?>
                        <div class="mt">
                            <a href="<?php the_permalink($related->ID); ?>">
                                <img src="<?php echo $r_img_src; ?>" alt="<?php echo $r_title; ?>" loading="lazy" />
                                <div class="game-name"><?php echo $r_title; ?></div>
                            </a>
                        </div>
                <?php
                    endforeach;
                endif; ?>
            </div>
        </div><!-- .related -->
    <?php endif; ?>

    <div class="center"><?php echo get_theme_mod('ads_game_related_after', arcade_ex_ad('970x90')) ?></div>


</div><!-- .w -->

<?php
get_footer();
