<?php
    if (isset($block['data']['isExample']) && $block['data']['isExample']) {
        echo '<img src="' . esc_url( $block['data']['preview_image'] ) . '"style="max-width:100%; min-width:100%" alt="Preview" />';
        return;
    }

    global $rdgb;
    $rdgb = (!empty($rdgb)) ? $rdgb : Rdg_Blocks::instance();

    $block_name = BLOCK_RDG_TOP_WINNERS;
    $block_top_winners = $rdgb->settings->getBlock($block_name);
    $block_id = (isset($block['data']['custom_id']) && !empty($block['data']['custom_id'])) ? $block['data']['custom_id'] : $block['id'];

    $cache = new Rdg_Blocks_Cache();
    $connect = $cache->getRedisConnect();
    if (!$connect) {
        error_log(sprintf('%s / %s: Redis connection failed.', RDGBLOCKS_NAME, $block_name));
        return;
    }

    $message = '';
    $link = get_field('link');
    $tops = get_field('tops');
    $source = get_field('source');
    $position_block = $rdgb->helpers->getBlocksSpaces(get_fields());
    list($heading, $title, $styles) = $block_top_winners->getTitle(get_fields());
    $presentation = (!empty(get_field('presentation'))) ? get_field('presentation') : 'small';

    $cache_name = null;
    $api_attributes = null;
    $post_id = get_the_ID();
    $post_type = get_post_type($post_id);

    if ($source == 'api' && $block_top_winners->isCaching() && !empty($block_id)) {
        $cache_name = $rdgb->helpers->getBlocksCacheName($block_name, $post_type, $post_id, $block_id);
        $cache_block = $cache->getCacheBlock($cache_name);
        $cache_block_array = $rdgb->helpers->isJson($cache_block);

        if ($cache_block_array && is_array($cache_block_array['api_response'])) {
            $api_attributes = $cache_block_array['api_attributes'];
            $tops = $cache_block_array['api_response'];
            $items = $cache_block_array['items'];
        }
    }
?>

<?php if (is_array($tops) && count($tops) > 0): ?>
    <div class="rdg-blocks">
        <?php
            $params = ['cache_name' => $cache_name, 'api_request' => $api_attributes];
            $block_top_winners->getLogDebug($params);
        ?>
        <div id="<?php echo $block['id']; ?>" class="rdg-top-winners" style="<?php echo $position_block; ?>">
            <div class="header">
                <?php if(!empty($title)): ?>
                    <div class="title" style="<?php echo $styles; ?>">
                        <<?php echo $heading; ?> class="header-title"><?php echo $title; ?></<?php echo $heading; ?>>
                    </div>
                <?php endif; ?>
            </div>

            <div class="body<?php echo ($presentation == 'small') ? ' small' : ''; ?>">
                <div class="legend">
                    <div class="legend-logo"></div>
                    <div class="legend-win">Win</div>
                    <div class="legend-dra">Dra</div>
                </div>
                <div class="tops">
                    <?php
                        if ($source == 'api') {
                            require 'partials/api.php';
                        } else {
                            require 'partials/manual.php';
                        }
                    ?>
                </div>
            </div>

            <div class="footer">
                <?php if(!empty($link)): ?>
                    <div class="footer-link">
                        <a href="<?php echo $link['url']; ?>" target="<?php echo $link['target']; ?>">
                            <?php echo $link['title']; ?>
                        </a>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
<?php endif; ?>