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

    $block_styles = [];
    $block_name = BLOCK_RDG_PICKS_VS_CATEGORIES;
    $block_classes = [$block_name];
    $block_picks_vs_categories = $rdgb->settings->get_block($block_name);
    $block_id = (isset($block['data']['custom_id']) && !empty($block['data']['custom_id'])) ? 
        $block['data']['custom_id'] : $block['id'];

    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'].';';
    }

    $sports_picks = [];
    $titles = get_field('titles');
    $sports = (!empty(get_field('sports'))) ? get_field('sports') : null;
    $sports_array = $block_picks_vs_categories->get_categories_sports($sports);

    $link_options = (!empty(get_field('link_options'))) ? get_field('link_options') : 'none';
    $custom_link = (!empty(get_field('custom_link'))) ? get_field('custom_link') : null;
    $date_type = (!empty(get_field('date_type'))) ? get_field('date_type') : null;
    $since_date = (!empty(get_field('since_date'))) ? get_field('since_date') : null;
    $to_date = (!empty(get_field('to_date'))) ? get_field('to_date') : null;

    $position_block = $rdgb->helpers->get_blocks_spaces(get_fields());
    $date_request = $block_picks_vs_categories->get_date_filter($date_type, $since_date, $to_date);
    $show = (!empty(get_field('show_in'))) ? get_field('show_in') : 'column';

    if (is_array($sports) && count($sports) > 0 && $block_picks_vs_categories->is_caching()) {
        $api_url = get_field('picks_vs_api_url', 'option');
        $api_token = get_field('picks_vs_api_token', 'option');

        if (!empty($api_url) && !empty($api_token)) {
            $api_attributes = $block_picks_vs_categories->get_api_attributes(null, ['sports' => $sports, 'date' => $date_request]);
            $block_custom_id = (!empty($block['data']['custom_id'])) ? $block['data']['custom_id'] : '';
            $post_id_custom = (!empty($block['data']['post_id'])) ? $block['data']['post_id'] : $post_id;
            $cache_file = $rdgb->helpers->get_cache_name($block_name, $post_id_custom, $block_custom_id);
            $response = $block_picks_vs_categories->get_resources($api_attributes, $cache_file);
            $titles_sports = $rdgb->helpers->get_sporst_categories_titles(true);

            if (empty($response->message)) {
                $sports_picks = $response->json;
            } else {
                $message = $response->message;
            }
        } else {
            $message = __('Configure API connection correctly.');
        }
    }
?>

<div class="rdg-blocks">
    <div <?php echo (!empty($block_id)) ? 'id="'.$block_id.'"' : ''; ?> class="<?php echo esc_attr(join(' ', $block_classes)); ?> <?php echo $show; ?>" style="<?php echo $position_block . esc_attr(join(' ', $block_styles)); ?>">
        <div class="container content grid-container">
            <?php if (is_array($sports_picks) && count($sports_picks) > 0): ?>
                <div class="sports">
                    <?php foreach($sports_picks as $sport_pick_key => $sport_picks): ?>
                        <?php
                            if (is_array($sport_picks) && count($sport_picks) == 0) continue;
                            $id_sport_category = (isset($sports_array[$sport_pick_key]) && $sports_array[$sport_pick_key] == 'NULL') ? $sport_pick_key : $sport_pick_key.'-'.$sports_array[$sport_pick_key];

                            $custom_title = 1;
                            $api_title = (is_array($titles_sports) && isset($titles_sports[$id_sport_category])) ? $titles_sports[$id_sport_category] : '';
                            $format_title = (is_array($titles) && count($titles) > 0) ? $titles[0] : [];

                            if (!isset($format_title['title']) || $format_title['title'] == '') {
                                $format_title['title'] = $api_title;
                                $custom_title = 0;
                            }

                            list($heading, $title, $styles) = $rdgb->helpers->get_title($format_title);
                        ?>
                        <div id="rdg-picks-vs-categories-title" class="title" style="<?php echo $styles; ?>" data-heading="<?php echo $heading; ?>" data-custom-title="<?php echo $custom_title; ?>">
                            <<?php echo $heading; ?>><?php echo $block_picks_vs_categories->get_picks_vs_title_format($title); ?></<?php echo $heading; ?>>
                        </div>

                        <div class="picks">
                            <?php if (!empty($sport_picks)): ?>
                                <?php
                                    require 'partials/api.php';
                                ?>
                            <?php else: ?>
                                <?php if (!empty($message)): ?>
                                    <span class="message"><?php echo $message; ?></span>
                                <?php endif; ?>
                            <?php endif; ?>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>
