<?php 
    global $shr;
    $shr = (!empty($shr)) ? $shr : Sportshub_Recaps::instance();

    $block_styles = [];
    $block_classes = [BLOCK_SH_RECAP_TOP_HANDICAPPER];
    $block_game_handicapper = $shr->settings->get_block(BLOCK_SH_RECAP_TOP_HANDICAPPER);

    if (!empty($block['backgroundColor'])) {
        $block_classes[] = 'has-background';
        $block_classes[] = 'has-'.$block['backgroundColor'].'-background-color';
    }

    if (!empty($block['style']['color']['background'])) {
        $block_styles[] = 'background-color: '.$block['style']['color']['background'].';';
    }

    $position_block = $shr->helpers->get_blocks_spaces(get_fields());

    $post = get_post();
    $fields = $block_game_handicapper->get_block_fields($post, $shr);
    $type = (isset($fields['type']) && !empty($fields['type'])) ? $fields['type'] : null;
    $example = (isset($fields['example']) && !empty($fields['example'])) ? $fields['example'] : null;
    $invalid_fields = (isset($fields['invalid_fields']) && !empty($fields['invalid_fields'])) ? $fields['invalid_fields'] : null;

    $block_top_handicapper = 'top-handicapper';
    $block_top_handicapper_picks = 'picks';
    $is_config = $shr->helpers->isConfigBlock($block_top_handicapper);
    $picks = (is_array(get_field('shr_picks', $post->ID)) && count(get_field('shr_picks', $post->ID)) > 0) ? get_field('shr_picks', $post->ID) : [];
    $types = $shr->helpers->getContentTypeBlock(['top-handicapper', 'picks']);
?>

<?php if ($is_config): ?>
    <?php if ($type != 'example' && $type != ''): ?>
        <?php
            $params = ['type_game' => $type, 'data' => $example, 'invalid_fields' => $invalid_fields];
            $block_game_handicapper->getLogDebug($params);
        ?>
    <?php else: ?>
        <?php if (empty(get_option('shr_source')) || get_option('shr_source') == 'members'): ?>
            <div class="shr-blocks">
                <div class="<?php echo esc_attr(join(' ', $block_classes)); ?>" style="<?php echo $position_block . esc_attr(join(' ', $block_styles)); ?>">
                    <div class="container content grid-container">
                        <div class="inner-content">
                            <div class="top-header">
                                <?php if ($type == 'example'): ?>
                                    <h2 id="block-recap-links-top-handicapper" class="top-title">EXAMPLE Top Handicapper</h2>
                                <?php else: ?>
                                    <h2 id="block-recap-links-top-handicapper" class="top-title"><?php echo $block_game_handicapper->getTitle($block_top_handicapper, $types, $fields); ?></h2>
                                <?php endif; ?>
                            </div>
                            <div class="top-content">
                                <div class="top-capper">
                                    <?php if (isset($fields['shr_game_top_handicapper_local_image']) && !empty($fields['shr_game_top_handicapper_local_image'])): ?>
                                        <img src="<?php echo $fields['shr_game_top_handicapper_local_image']; ?>" alt="<?php echo $fields['shr_game_top_handicapper_name']; ?>">
                                    <?php endif; ?>
                                </div>
                                <div class="top-body">
                                    <div class="body-header">
                                        <div class="header">
                                            <div class="body-header-capper">
                                                <span class="name"><?php echo $fields['shr_game_top_handicapper_name']; ?></span>
                                                <span class="separator">|</span>
                                                <span class="cat">Handicapper</span>
                                            </div>
                                        </div>
                                        <div class="body-description"><?php echo $fields['shr_game_top_handicapper_description']; ?></div>
                                    </div>
                                    <div class="games">
                                        <div class="game">
                                            <div class="game-title">
                                                #<?php echo $fields['shr_game_results_rank']; ?>
                                                <?php echo $fields['shr_sport_name']; ?>
                                            </div>
                                            <div class="game-body">
                                                <div class="list-games">
                                                    <div class="list-game">
                                                        <div class="time">Yesterday</div>
                                                        <div class="amount"><?php echo $fields['shr_game_results_yesterday']; ?></div>
                                                    </div>
                                                    <div class="list-game">
                                                        <div class="time">Last 7 days</div>
                                                        <div class="amount"><?php echo $fields['shr_game_results_lastthreedays']; ?></div>
                                                    </div>
                                                    <div class="list-game">
                                                        <div class="time">Last 3 days</div>
                                                        <div class="amount"><?php echo $fields['shr_game_results_lastsevendays']; ?></div>
                                                    </div>
                                                    <div class="list-game">
                                                        <div class="time">Last 30 days</div>
                                                        <div class="amount"><?php echo $fields['shr_game_results_lastthirtydays']; ?></div>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="game-footer"></div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <?php $is_config_picks = $shr->helpers->isConfigBlock($block_top_handicapper_picks); ?>
                            <?php if ($is_config_picks && is_array($picks) && count($picks) > 0): ?>
                                <div class="top-header">
                                    <?php if ($type == 'example'): ?>
                                        <h2 id="block-recap-links-picks" class="top-title title-picks">EXAMPLE Sports Picks</h2>
                                    <?php else: ?>
                                        <h2 id="block-recap-links-picks" class="top-title title-picks"><?php echo $block_game_handicapper->getTitle($block_top_handicapper_picks, $types, $fields); ?></h2>
                                    <?php endif; ?>
                                </div>
                                <div class="picks">
                                    <?php if (is_array($picks) && count($picks) > 0): ?>
                                        <?php foreach ($picks as $pick): ?>
                                            <div class="pick">
                                                <div class="pick-header">
                                                    <div class="content">
                                                        <div class="sport"><span></span></div>
                                                        <div class="date-time">
                                                            <span>
                                                                <?php
                                                                    try {
                                                                        $d = DateTime::createFromFormat('Y-m-d H:i:s', $pick['shr_pick_date']);
                                                                        $date_status = $d && $d->format('Y-m-d H:i:s') == $pick['shr_pick_date'];

                                                                        if ($date_status) {
                                                                            echo $d->format('Y-m-d H:i');
                                                                        }
                                                                    } catch (\Exception $exception) {
                                                                        //
                                                                    }
                                                                ?>
                                                            </span>
                                                        </div>
                                                        <div class="status"><span></span></div>
                                                    </div>
                                                </div>
                                                <div class="pick-body">
                                                    <div class="teams">
                                                        <div class="team team-a">
                                                            <div class="team-logo">
                                                                <?php if (isset($pick['shr_pick_visitor_image_local']) && !empty($pick['shr_pick_visitor_image_local'])): ?>
                                                                    <?php
                                                                        $team_visitor_image = $shr->helpers->getNameFile($pick['shr_pick_visitor_image_local']);
                                                                        $team_visitor_alt = (isset($team_visitor_image['alt']) && !empty($team_visitor_image['alt'])) ? $team_visitor_image['alt'] : '';
                                                                    ?>
                                                                    <img src="<?php echo $pick['shr_pick_visitor_image_local']; ?>" alt="<?php echo $team_visitor_alt; ?>">
                                                                <?php endif; ?>
                                                            </div>
                                                            <div class="team-name"><span><?php echo $pick['shr_pick_visitor_name']; ?></span></div>
                                                        </div>
                                                        <div class="team team-vs">
                                                            <div class="vs-logo"><span>vs</span></div>
                                                            <?php
                                                                $url = (!empty($pick['shr_pick_game_url'])) ? $pick['shr_pick_game_url'] : '#';
                                                                $target = '_blank';
                                                            ?>
                                                        </div>
                                                        <div class="team team-b">
                                                            <div class="team-logo">
                                                                <?php if (isset($pick['shr_pick_home_image_local']) && !empty($pick['shr_pick_home_image_local'])): ?>
                                                                    <?php
                                                                        $team_home_image = $shr->helpers->getNameFile($pick['shr_pick_home_image_local']);
                                                                        $team_home_alt = (isset($team_home_image['alt']) && !empty($team_home_image['alt'])) ? $team_home_image['alt'] : '';
                                                                    ?>
                                                                    <img src="<?php echo $pick['shr_pick_home_image_local']; ?>" alt="<?php echo $team_home_alt; ?>">
                                                                <?php endif; ?>
                                                            </div>
                                                            <div class="team-name"><span><?php echo $pick['shr_pick_home_name']; ?></span></div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="pick-link">
                                                    <a href="<?php echo $url; ?>" target="<?php echo $target; ?>" rel="noopener nofollow">
                                                        <div class="link">
                                                            <span>Get this pick</span>
                                                        </div>
                                                    </a>
                                                </div>
                                            </div>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </div>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        <?php endif; ?>
    <?php endif; ?>
<?php endif; ?>
