<?php

// Exit if accessed directly.
if (!defined('ABSPATH')) exit;

class Rdg_Blocks_Picks_Vs extends Rdg_Blocks_Base implements Rdg_Blocks_Api_Request
{
    protected $block_name = BLOCK_RDG_PICKS_VS;
    private $block_folder_name = 'rdg-picks-vs';
    protected $block_title = 'RDG Picks Vs';
    private $block_description = '';
    private $block_icon = 'admin-links';
    private $block_keywords = ['rdg', 'custom', 'sports', 'picks'];
    private $block_mode = 'preview';
    private $block_category = RDGBLOCKS_NAME_SLUG;
    protected $is_caching = true;
    protected $is_config = true;
    protected $is_styles_load = true;
    protected $config_page_title = 'RDG Picks Vs';
    protected $config_menu_title = 'Picks Vs';

    public function init() {
        $this->register_block();
        add_action('acf/init', [$this, 'register_config_menu'], 20);
        add_action('acf/init', [$this, 'register_fields'], 20);
    }

    private function register_block() {
        if (function_exists('acf_register_block_type')) {
            $rdg_helpers = new Rdg_Blocks_Helpers();
            acf_register_block_type([
                'name' => $this->block_name,
                'title' => __($this->block_title),
                'description' => __($this->block_description),
                'category' => $this->block_category,
                'icon' => $this->block_icon,
                'keywords' => $this->block_keywords,
                'mode' => $this->block_mode,
                'render_template' => RDGBLOCKS_PLUGIN_DIR . "/blocks/".$this->block_folder_name."/template.php",
                'enqueue_style' => $rdg_helpers->webpack('rdg_picks_vs.css'),
                'enqueue_assets' => function(){
                    wp_register_script('rdg_picks_vs_script', RDGBLOCKS_PLUGIN_URL.'blocks/rdg-picks-vs/assets/js/rdg-picks-vs.js', array('jquery'), '1', true);
                    wp_enqueue_script('rdg_picks_vs_script');
                    wp_localize_script('rdg_picks_vs_script','rdg_picks_vs_var',['ajaxurl' => admin_url('admin-ajax.php')]);
                },
                'supports' => [
                    'jsx' => true,
			        'color' => [
                        'background' => true,
                        'gradients'  => false,
                        'text' => false,
                    ],
                ],
            ]);
        }
    }

    public function register_fields() {
        $rdg_helpers = new Rdg_Blocks_Helpers();

        if( function_exists('acf_add_local_field_group') ):
            acf_add_local_field_group(array(
                'key' => 'group_rdg_picks_vs_block_sdf4334r',
                'title' => 'Block_rdg_picks_vs',
                'fields' => array(
                    array(
                        'key' => 'field_645c264c1d81d',
                        'label' => 'Show In',
                        'name' => 'show_in',
                        'aria-label' => '',
                        'type' => 'radio',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'choices' => array(
                            'full' => 'Full width',
                            'content' => 'Content width',
                        ),
                        'default_value' => 'full',
                        'return_format' => 'value',
                        'allow_null' => 0,
                        'other_choice' => 0,
                        'layout' => 'horizontal',
                        'save_other_choice' => 0,
                    ),
                    array(
                        'key' => 'rdg_picks_vs_show_title',
                        'label' => 'Show Title',
                        'name' => 'show_title',
                        'aria-label' => '',
                        'type' => 'radio',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'choices' => array(
                            'show' => 'Show',
                            'none' => 'None',
                        ),
                        'default_value' => 'show',
                        'return_format' => 'value',
                        'allow_null' => 0,
                        'other_choice' => 0,
                        'layout' => 'horizontal',
                        'save_other_choice' => 0,
                    ),
                    array(
                        'key' => 'field_63d9b54a73136',
                        'label' => 'Source',
                        'name' => 'source',
                        'aria-label' => '',
                        'type' => 'radio',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'choices' => array(
                            'manual' => 'Manual',
                            'api' => 'API',
                        ),
                        'default_value' => 'manual',
                        'return_format' => 'value',
                        'allow_null' => 0,
                        'other_choice' => 0,
                        'save_other_choice' => 0,
                        'layout' => 'horizontal',
                    ),
                    array(
                        'key' => 'field_642eebcadb45e',
                        'label' => 'Link Options',
                        'name' => 'link_options',
                        'aria-label' => '',
                        'type' => 'radio',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => array(
                            array(
                                array(
                                    'field' => 'field_63d9b54a73136',
                                    'operator' => '==',
                                    'value' => 'api',
                                ),
                            ),
                        ),
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'choices' => array(
                            'none' => 'None',
                            'default' => 'API',
                            'custom' => 'Custom',
                        ),
                        'default_value' => 'hide',
                        'return_format' => 'value',
                        'allow_null' => 0,
                        'other_choice' => 0,
                        'layout' => 'horizontal',
                        'save_other_choice' => 0,
                    ),
                    array(
                        'key' => 'field_642f017adb45f',
                        'label' => 'Link',
                        'name' => 'custom_link',
                        'aria-label' => '',
                        'type' => 'link',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => array(
                            array(
                                array(
                                    'field' => 'field_642eebcadb45e',
                                    'operator' => '==',
                                    'value' => 'custom',
                                ),
                            ),
                        ),
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'return_format' => 'array',
                    ),
                    array(
                        'key' => 'field_63dbbd1ec1393',
                        'label' => 'Filter Category',
                        'name' => 'filter_category',
                        'aria-label' => '',
                        'type' => 'radio',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => array(
                            array(
                                array(
                                    'field' => 'field_63d9b54a73136',
                                    'operator' => '==',
                                    'value' => 'api',
                                ),
                            ),
                        ),
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'choices' => array(
                            'hide' => 'Hide',
                            'show' => 'Show',
                        ),
                        'default_value' => 'hide',
                        'return_format' => 'value',
                        'allow_null' => 0,
                        'other_choice' => 0,
                        'layout' => 'horizontal',
                        'save_other_choice' => 0,
                    ),
                    array(
                        'key' => 'field_63cc527c9d04e',
                        'label' => 'Picks',
                        'name' => 'picks',
                        'aria-label' => '',
                        'type' => 'repeater',
                        'instructions' => '',
                        'required' => 1,
                        'conditional_logic' => array(
                            array(
                                array(
                                    'field' => 'field_63d9b54a73136',
                                    'operator' => '==',
                                    'value' => 'manual',
                                ),
                            ),
                        ),
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'layout' => 'block',
                        'pagination' => 0,
                        'min' => 0,
                        'max' => 0,
                        'collapsed' => '',
                        'button_label' => 'Add Row',
                        'rows_per_page' => 20,
                        'sub_fields' => array(
                            array(
                                'key' => 'field_63cc527cbe269',
                                'label' => 'Category',
                                'name' => 'category',
                                'aria-label' => '',
                                'type' => 'text',
                                'instructions' => '',
                                'required' => 1,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => '',
                                'maxlength' => '',
                                'placeholder' => '',
                                'prepend' => '',
                                'append' => '',
                                'parent_repeater' => 'field_63cc527c9d04e',
                            ),
                            array(
                                'key' => 'field_63cc527cc1bba',
                                'label' => 'Time',
                                'name' => 'time',
                                'aria-label' => '',
                                'type' => 'text',
                                'instructions' => '',
                                'required' => 1,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => '',
                                'maxlength' => '',
                                'placeholder' => '',
                                'prepend' => '',
                                'append' => '',
                                'parent_repeater' => 'field_63cc527c9d04e',
                            ),
                            array(
                                'key' => 'field_63cc527cc584e',
                                'label' => 'Status',
                                'name' => 'status',
                                'aria-label' => '',
                                'type' => 'text',
                                'instructions' => '',
                                'required' => 1,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => '',
                                'maxlength' => '',
                                'placeholder' => '',
                                'prepend' => '',
                                'append' => '',
                                'parent_repeater' => 'field_63cc527c9d04e',
                            ),
                            array(
                                'key' => 'field_63cc527cc92a2',
                                'label' => 'Vs',
                                'name' => 'vs',
                                'aria-label' => '',
                                'type' => 'text',
                                'instructions' => '',
                                'required' => 1,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => '',
                                'maxlength' => '',
                                'placeholder' => '',
                                'prepend' => '',
                                'append' => '',
                                'parent_repeater' => 'field_63cc527c9d04e',
                            ),
                            array(
                                'key' => 'field_63cc527cccd29',
                                'label' => 'Teams',
                                'name' => 'teams',
                                'aria-label' => '',
                                'type' => 'repeater',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'layout' => 'block',
                                'pagination' => 0,
                                'min' => 0,
                                'max' => 2,
                                'collapsed' => '',
                                'button_label' => 'Add Row',
                                'rows_per_page' => 20,
                                'sub_fields' => array(
                                    array(
                                        'key' => 'field_63cc527ce34ba',
                                        'label' => 'Logo',
                                        'name' => 'logo',
                                        'aria-label' => '',
                                        'type' => 'image',
                                        'instructions' => '',
                                        'required' => 1,
                                        'conditional_logic' => 0,
                                        'wrapper' => array(
                                            'width' => '',
                                            'class' => '',
                                            'id' => '',
                                        ),
                                        'return_format' => 'array',
                                        'library' => 'all',
                                        'min_width' => '',
                                        'min_height' => '',
                                        'min_size' => '',
                                        'max_width' => '',
                                        'max_height' => '',
                                        'max_size' => '',
                                        'mime_types' => '',
                                        'preview_size' => 'medium',
                                        'parent_repeater' => 'field_63cc527cccd29',
                                    ),
                                    array(
                                        'key' => 'field_63cc527ce6f33',
                                        'label' => 'Name',
                                        'name' => 'name',
                                        'aria-label' => '',
                                        'type' => 'text',
                                        'instructions' => '',
                                        'required' => 1,
                                        'conditional_logic' => 0,
                                        'wrapper' => array(
                                            'width' => '',
                                            'class' => '',
                                            'id' => '',
                                        ),
                                        'default_value' => '',
                                        'maxlength' => '',
                                        'placeholder' => '',
                                        'prepend' => '',
                                        'append' => '',
                                        'parent_repeater' => 'field_63cc527cccd29',
                                    ),
                                ),
                                'parent_repeater' => 'field_63cc527c9d04e',
                            ),
                        ),
                    ),
                    array(
                        'key' => 'field_63d9b5edd9ce1',
                        'label' => 'Category',
                        'name' => 'category',
                        'aria-label' => '',
                        'type' => 'select',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => array(
                            array(
                                array(
                                    'field' => 'field_63d9b54a73136',
                                    'operator' => '==',
                                    'value' => 'api',
                                ),
                            ),
                        ),
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'choices' => $rdg_helpers->getSporstCategoriesOptions(true),
                        'default_value' => 'NFL',
                        'return_format' => 'value',
                        'multiple' => 0,
                        'allow_null' => 0,
                        'ui' => 0,
                        'ajax' => 0,
                        'placeholder' => '',
                    ),
                    array(
                        'key' => 'field_63d9b735d9ce3',
                        'label' => 'Number of Items',
                        'name' => 'number_of_items',
                        'aria-label' => '',
                        'type' => 'number',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => array(
                            array(
                                array(
                                    'field' => 'field_63d9b54a73136',
                                    'operator' => '==',
                                    'value' => 'api',
                                ),
                            ),
                        ),
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => 3,
                        'min' => 0,
                        'max' => '',
                        'placeholder' => '',
                        'step' => '',
                        'prepend' => '',
                        'append' => '',
                    ),
                    array(
                        'key' => 'field_63dbbff733345',
                        'label' => 'Filter Date',
                        'name' => 'filter_date',
                        'aria-label' => '',
                        'type' => 'radio',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => array(
                            array(
                                array(
                                    'field' => 'field_63d9b54a73136',
                                    'operator' => '==',
                                    'value' => 'api',
                                ),
                            ),
                        ),
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'choices' => array(
                            'hide' => 'Hide',
                            'show' => 'Show',
                        ),
                        'default_value' => 'hide',
                        'return_format' => 'value',
                        'allow_null' => 0,
                        'other_choice' => 0,
                        'layout' => 'horizontal',
                        'save_other_choice' => 0,
                    ),
                    array(
                        'key' => 'field_63e6b06bc7010',
                        'label' => 'Date Type',
                        'name' => 'date_type',
                        'aria-label' => '',
                        'type' => 'select',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => array(
                            array(
                                array(
                                    'field' => 'field_63dbbff733345',
                                    'operator' => '==',
                                    'value' => 'show',
                                ),
                            ),
                        ),
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'choices' => array(
                            'yesterday' => 'Yesterday',
                            'now' => 'Now',
                            'tomorrow' => 'Tomorrow',
                            'range' => 'Range',
                        ),
                        'default_value' => 'now',
                        'return_format' => 'value',
                        'multiple' => 0,
                        'allow_null' => 0,
                        'ui' => 0,
                        'ajax' => 0,
                        'placeholder' => '',
                    ),
                    array(
                        'key' => 'field_63e6af4c50608',
                        'label' => 'Since Date',
                        'name' => 'since_date',
                        'aria-label' => '',
                        'type' => 'date_picker',
                        'instructions' => '',
                        'required' => 1,
                        'conditional_logic' => array(
                            array(
                                array(
                                    'field' => 'field_63e6b06bc7010',
                                    'operator' => '==',
                                    'value' => 'range',
                                ),
                            ),
                        ),
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'display_format' => 'Y-m-d',
                        'return_format' => 'Y-m-d',
                        'first_day' => 1,
                    ),
                    array(
                        'key' => 'field_63e6afb550609',
                        'label' => 'To Date',
                        'name' => 'to_date',
                        'aria-label' => '',
                        'type' => 'date_picker',
                        'instructions' => '',
                        'required' => 1,
                        'conditional_logic' => array(
                            array(
                                array(
                                    'field' => 'field_63e6b06bc7010',
                                    'operator' => '==',
                                    'value' => 'range',
                                ),
                            ),
                        ),
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'display_format' => 'Y-m-d',
                        'return_format' => 'Y-m-d',
                        'first_day' => 1,
                    ),
                    array(
                        'key' => 'field_63dbbed44c4bd',
                        'label' => 'Date Range',
                        'name' => 'date_range',
                        'aria-label' => '',
                        'type' => 'repeater',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => array(
                            array(
                                array(
                                    'field' => 'field_63dbbff733345',
                                    'operator' => '==',
                                    'value' => 'show',
                                ),
                            ),
                        ),
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'layout' => 'block',
                        'pagination' => 0,
                        'min' => 0,
                        'max' => 0,
                        'collapsed' => '',
                        'button_label' => 'Add Row',
                        'rows_per_page' => 20,
                        'sub_fields' => array(
                            array(
                                'key' => 'field_63eb9ff68a727',
                                'label' => 'Status',
                                'name' => 'status',
                                'aria-label' => '',
                                'type' => 'radio',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'choices' => array(
                                    'link-info' => 'Active',
                                    'link-default' => 'Deactive',
                                ),
                                'default_value' => 'link-default',
                                'return_format' => 'value',
                                'allow_null' => 0,
                                'other_choice' => 0,
                                'layout' => 'horizontal',
                                'save_other_choice' => 0,
                                'parent_repeater' => 'field_63dbbed44c4bd',
                            ),
                            array(
                                'key' => 'field_63eb8ed62d5eb',
                                'label' => 'Date Type',
                                'name' => 'date_type',
                                'aria-label' => '',
                                'type' => 'select',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => array(
                                    array(
                                        array(
                                            'field' => 'field_63dbbff733345',
                                            'operator' => '==',
                                            'value' => 'show',
                                        ),
                                    ),
                                ),
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'choices' => array(
                                    'yesterday' => 'Yesterday',
                                    'now' => 'Now',
                                    'tomorrow' => 'Tomorrow',
                                    'range' => 'Range',
                                ),
                                'default_value' => 'now',
                                'return_format' => 'value',
                                'multiple' => 0,
                                'allow_null' => 0,
                                'ui' => 0,
                                'ajax' => 0,
                                'placeholder' => '',
                                'parent_repeater' => 'field_63dbbed44c4bd',
                            ),
                            array(
                                'key' => 'field_63eb8ee12d5ec',
                                'label' => 'Since Date',
                                'name' => 'since_date',
                                'aria-label' => '',
                                'type' => 'date_picker',
                                'instructions' => '',
                                'required' => 1,
                                'conditional_logic' => array(
                                    array(
                                        array(
                                            'field' => 'field_63eb8ed62d5eb',
                                            'operator' => '==',
                                            'value' => 'range',
                                        ),
                                    ),
                                ),
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'display_format' => 'Y-m-d',
                                'return_format' => 'Y-m-d',
                                'first_day' => 1,
                                'parent_repeater' => 'field_63dbbed44c4bd',
                            ),
                            array(
                                'key' => 'field_63eb8eef2d5ed',
                                'label' => 'To Date',
                                'name' => 'to_date',
                                'aria-label' => '',
                                'type' => 'date_picker',
                                'instructions' => '',
                                'required' => 1,
                                'conditional_logic' => array(
                                    array(
                                        array(
                                            'field' => 'field_63eb8ed62d5eb',
                                            'operator' => '==',
                                            'value' => 'range',
                                        ),
                                    ),
                                ),
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'display_format' => 'Y-m-d',
                                'return_format' => 'Y-m-d',
                                'first_day' => 1,
                                'parent_repeater' => 'field_63dbbed44c4bd',
                            ),
                            array(
                                'key' => 'field_63e6b171f782c',
                                'label' => 'Title Date',
                                'name' => 'title_date',
                                'aria-label' => '',
                                'type' => 'text',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => '',
                                'maxlength' => '',
                                'placeholder' => '',
                                'prepend' => '',
                                'append' => '',
                                'parent_repeater' => 'field_63dbbed44c4bd',
                            ),
                            array(
                                'key' => 'field_63dbbde453392',
                                'label' => 'Icon',
                                'name' => 'icon',
                                'aria-label' => '',
                                'type' => 'select',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'choices' => array(
                                    'fa-circle' => 'Circle',
                                    'fa-chevron-circle-up' => 'Circle Up',
                                    'fa-chevron-circle-right' => 'Circle Right',
                                    'fa-chevron-circle-down' => 'Circle Down',
                                    'fa-chevron-circle-left' => 'Circle Left',
                                    'fa-dot-circle' => 'Circle Dot',
                                    'fa-check-circle' => 'Circle Check',
                                    'fa-times-circle' => 'Circle X Mark',
                                    'fa-user-circle' => 'Circle User',
                                    'fa-question-circle' => 'Circle Question',
                                    'fa-info-circle' => 'Circle Info',
                                ),
                                'default_value' => false,
                                'return_format' => 'value',
                                'multiple' => 0,
                                'allow_null' => 1,
                                'ui' => 0,
                                'ajax' => 0,
                                'placeholder' => '',
                                'parent_repeater' => 'field_63dbbed44c4bd',
                            ),
                        ),
                    ),
                ),
                'location' => array(
                    array(
                        array(
                            'param' => 'block',
                            'operator' => '==',
                            'value' => 'acf/rdg-picks-vs',
                        ),
                    ),
                ),
                'menu_order' => 1,
                'position' => 'normal',
                'style' => 'default',
                'label_placement' => 'top',
                'instruction_placement' => 'label',
                'hide_on_screen' => '',
                'active' => true,
                'description' => '',
            ));

        endif;
    }

    public function register_config_menu() {
        if( function_exists('acf_add_local_field_group') ):
            acf_add_local_field_group(array(
                'key' => 'api_configuration_picks_vs_block_sdfw34fs2332',
                'title' => 'API configuration',
                'fields' => array(
                    array(
                        'key' => 'field_63d9b987bd0be',
                        'label' => 'URL',
                        'name' => 'picks_vs_api_url',
                        'aria-label' => '',
                        'type' => 'text',
                        'instructions' => '',
                        'required' => 1,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '70',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '',
                        'maxlength' => '',
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                    ),
                    array(
                        'key' => 'field_63d9b987c0adf',
                        'label' => 'Cache Refresh Frequency (Hours)',
                        'name' => 'picks_vs_interval',
                        'aria-label' => '',
                        'type' => 'number',
                        'instructions' => '',
                        'required' => 1,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '30',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => 1,
                        'min' => '',
                        'max' => '',
                        'placeholder' => '',
                        'step' => '',
                        'prepend' => '',
                        'append' => '',
                    ),
                    array(
                        'key' => 'field_63d9b987c4506',
                        'label' => 'Token',
                        'name' => 'picks_vs_api_token',
                        'aria-label' => '',
                        'type' => 'text',
                        'instructions' => '',
                        'required' => 1,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '100',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '',
                        'maxlength' => '',
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                    ),
                ),
                'location' => array(
                    array(
                        array(
                            'param' => 'options_page',
                            'operator' => '==',
                            'value' => 'acf-options-picks-vs',
                        ),
                    ),
                ),
                'menu_order' => 1,
                'position' => 'normal',
                'style' => 'default',
                'label_placement' => 'top',
                'instruction_placement' => 'label',
                'hide_on_screen' => '',
                'active' => true,
                'description' => '',
            ));

        endif;
    }

    public function getApiAttributes($items = null, $attributes = []) {
        $api_url = get_field('picks_vs_api_url', 'option');
        $api_token = get_field('picks_vs_api_token', 'option');
        $cache_refresh = get_field('picks_vs_interval', 'option');
        $items = (!empty($items)) ? intval($items) : 10;
        
        $api_url = (substr($api_url, -1) == '/') ? substr($api_url, 0, strlen($api_url) -1) : $api_url;
        $api_url .= '?gamesToShow='.$items;

        if (isset($attributes['category']) && strpos($attributes['category'], '-')) {
            list($category_primary, $sub_category) = explode('-', $attributes['category']);
        } else {
            $sub_category = null;

            if (isset($attributes['category']) && !empty($attributes['category'])) {
                $category_primary = $attributes['category'];
            } else {
                $category_primary = 1;
            }
        }

        $api_url .= '&sport='.$category_primary;
        
        if (!empty($sub_category)) {
            $api_url .= '&category='.$sub_category;
        }

        if (isset($attributes['date']) && is_array($attributes['date']) && count($attributes['date']) == 2) {
            if (!empty($attributes['date']['start'])) {
                $api_url .= '&principalDate='.$attributes['date']['start'];
            }
            
            if (!empty($attributes['date']['end'])) {
                $api_url .= '&secondaryDate='.$attributes['date']['end'];
            }
        }

        $api_request = $api_url.'&api_token='.$api_token;

        return [
            'items' => $items,
            'method' => 'GET',
            'cache_refresh' => $cache_refresh,
            'headers' => ['Accept: application/json'],
            'request' => $api_request,
            'category_primary' => $category_primary,
            'sub_category' => $sub_category,
        ];
    }

    public function getApiParams() {
        return [
            'category' => 'category',
            '_to_transform_' => [
                'key' => 'date',
                'function' => 'get_date_filter',
                'params' => ['date_type', 'since_date', 'to_date'],
                'fields_date' => ['since_date', 'to_date'],
            ],
        ];
    }

    public function getResources($request, $cache_file) {
        $data = new StdClass();
        $data->json = '';
        $data->message = '';

        $rdgb_helpers = New Rdg_Blocks_Helpers;
        $picks_vs = $rdgb_helpers->getApiData($this->block_name, $cache_file, $request);

        if (!empty($picks_vs->message)) {
            $data->message = $picks_vs->message;
        
            return $data;
        }

        $data->json = $picks_vs->json;

        return $data;
    }

    public function getResourcesAjax($request) {
        $data = new StdClass();
        $data->json = '';
        $data->message = '';

        $rdgb_helpers = New Rdg_Blocks_Helpers;
        $picks_vs = $rdgb_helpers->getApiDataAjax($this->block_name, $request);

        if (!empty($picks_vs->message)) {
            $data->message = $picks_vs->message;
        
            return $data;
        }

        $data->json = $picks_vs->json;

        return $data;
    }

    public function get_style($type, $fields) {
        $style = [];

        if ('buttons' == $type) {
            $style[] = 'background-color: '.$fields['background_color'];
            $style[] = 'color: '.$fields['text_color'];
    
            return implode('; ', $style);
        }
    
        return '';
    }

    public function get_width($type, $filter_category, $filter_date) {
        if ($type == 'style') {
            if ($filter_category == 'hide' && $filter_date == 'hide') {
                return '';
            }
    
            if ($filter_category == 'show' && $filter_date == 'show') {
                $style[] = '--category-align: flex-end';
                $style[] = '--category-select-width: 70%';
                $style[] = '--sub-category-align: flex-start';
        
                return implode('; ', $style).';';
            }
    
            if ($filter_category == 'show' && $filter_date == 'hide') {
                $style[] = '--category-align: center';
                $style[] = '--category-select-width: 40%';
                $style[] = '--sub-category-align: flex-start';
        
                return implode('; ', $style).';';
            }
        
            $style[] = '--category-align: flex-end';
            $style[] = '--sub-category-align: center';
    
            return implode('; ', $style).';';
        }

        if ($filter_category == 'hide' && $filter_date == 'hide') {
            return '';
        }

        if ($filter_category == 'show' && $filter_date == 'show') {
            return 'w-50';
        }
        
        if ($type == 'class_category') {
            if ($filter_category == 'show' && $filter_date == 'hide') {
                return 'w-100';
            }
        }

        if ($type == 'class_sub_category') {
            if ($filter_category == 'hide' && $filter_date == 'show') {
                return 'w-100';
            }
        }
        
        return '';
    }

    public function get_date_filter($date_type, $since_date, $to_date) {
        $date = [
            'start' => null,
            'end' => null
        ];

        if ($date_type == API_DATE_TYPE_YESTERDAY) {
            $date['start'] = date('Y-m-d', strtotime("-1 days"));

            return $date;
        }

        if ($date_type == API_DATE_TYPE_NOW) {

            return $date;
        }
        
        if ($date_type == API_DATE_TYPE_TOMORROW) {
            $date['start'] = date('Y-m-d',strtotime("+1 days"));
            
            return $date;
        }

        if ($date_type == API_DATE_TYPE_RANGE) {
            if (!empty($since_date) && !empty($to_date)) {
                $date['start'] = $since_date;
                $date['end'] = $to_date;
            
                return $date;
            }

            if (empty($since_date) && empty($to_date)) {
                return $date;
            }

            if (!empty($since_date) && empty($to_date)) {
                $date['start'] = $since_date;
            
                return $date;
            }

            if (empty($since_date) && !empty($to_date)) {
                $date['start'] = $to_date;
            
                return $date;
            }
        }

        return $date;
    }

    public function get_image($url_image, $attributes = []) {
        $rdgb_helpers = New Rdg_Blocks_Helpers;
        $url_image = $rdgb_helpers->getImageSource($url_image, $attributes);

        return $url_image;
    }

    public function get_picks_vs_title_format($title) {
        if (strpos($title, '|') && is_array(explode(' | ', $title)) && count(explode(' | ', $title)) == 2) {
            $title_parts = explode(' | ', $title);
    
            return "<span style='color: var(--color-black); text-shadow: none;'>$title_parts[0] | </span> $title_parts[1]";
        }
        
        return "<span style='color: var(--color-black); text-shadow: none;'>$title</span>";
    }

    public function getFieldsValidate() {
        return [
            //'id_game' => ['required'],
            //'id_sport' => ['required'],
            'sport_external' => ['required', 'notnull'],
            'game_date' => ['required', 'notnull'],
            //'id_visitor_team' => ['required'],
            //'id_master_visitor_team' => ['required'],
            'visitor_name' => ['required', 'notnull'],
            //'id_home_team' => ['required'],
            //'id_master_home_team' => ['required'],
            'home_name' => ['required', 'notnull'],
            'game_stat' => ['required', 'notnull'],
            'num_picks' => ['required', 'notnull'],
            'game_url' => ['required', 'notnull'],
            'visitor_image' => ['required', 'notnull'],
            'home_image' => ['required', 'notnull'],
            //'visitor_image_type' => ['required'],
            //'home_image_type' => ['required'],
        ];
    }

    public function getColorsToloadGeneratePress()
    {
        return [
            [
                'name' => 'color-picks-vs-wait-bg',
                'slug' => 'color-picks-vs-wait-bg',
                'color' => 'rgba(255, 255, 255, .8)',
            ],
            [
                'name' => 'color-picks-vs-filter-select-box-shadow',
                'slug' => 'color-picks-vs-filter-select-box-shadow',
                'color' => 'rgba(0, 0, 0, .15)',
            ],
            [
                'name' => 'color-picks-vs-filter-select',
                'slug' => 'color-picks-vs-filter-select',
                'color' => '#3a96e0',
            ],
            [
                'name' => 'color-picks-vs-filter-select-bg',
                'slug' => 'color-picks-vs-filter-select-bg',
                'color' => '#ffffff',
            ],
            [
                'name' => 'color-picks-vs-filter-button',
                'slug' => 'color-picks-vs-filter-button',
                'color' => '#ffffff',
            ],
            [
                'name' => 'color-picks-vs-filter-button-link-info',
                'slug' => 'color-picks-vs-filter-button-link-info',
                'color' => '#ffffff',
            ],
            [
                'name' => 'color-picks-vs-filter-button-link-info-bg',
                'slug' => 'color-picks-vs-filter-button-link-info-bg',
                'color' => '#0160ac',
            ],
            [
                'name' => 'color-picks-vs-filter-button-link-info-border',
                'slug' => 'color-picks-vs-filter-button-link-info-border',
                'color' => '#3a96e0',
            ],
            [
                'name' => 'color-picks-vs-filter-button-link-default',
                'slug' => 'color-picks-vs-filter-button-link-default',
                'color' => '#ffffff',
            ],
            [
                'name' => 'color-picks-vs-filter-button-link-default-bg',
                'slug' => 'color-picks-vs-filter-button-link-default-bg',
                'color' => '#3a96e0',
            ],
            [
                'name' => 'color-picks-vs-filter-button-link-default-hover',
                'slug' => 'color-picks-vs-filter-button-link-default-hover',
                'color' => '#3a96e0',
            ],
            [
                'name' => 'color-picks-vs-filter-button-link-default-hover-bg',
                'slug' => 'color-picks-vs-filter-button-link-default-hover-bg',
                'color' => '#cccccc',
            ],
            [
                'name' => 'color-picks-vs-picks-message-bg',
                'slug' => 'color-picks-vs-picks-message-bg',
                'color' => '#f79e9e',
            ],
            [
                'name' => 'color-picks-vs-picks-message-border',
                'slug' => 'color-picks-vs-picks-message-border',
                'color' => '#891010',
            ],
            [
                'name' => 'color-picks-vs-picks-message-span',
                'slug' => 'color-picks-vs-picks-message-span',
                'color' => '#891010',
            ],
            [
                'name' => 'color-picks-vs-picks-pick-bg',
                'slug' => 'color-picks-vs-picks-pick-bg',
                'color' => '#ffffff',
            ],
            [
                'name' => 'color-picks-vs-picks-pick-teams-vs-logo-span',
                'slug' => 'color-picks-vs-picks-pick-teams-vs-logo-span',
                'color' => '#ffffff',
            ],
            [
                'name' => 'color-picks-vs-picks-pick-teams-vs-name-bg',
                'slug' => 'color-picks-vs-picks-pick-teams-vs-name-bg',
                'color' => '#0160ac',
            ],
            [
                'name' => 'color-picks-vs-picks-pick-teams-vs-name-span',
                'slug' => 'color-picks-vs-picks-pick-teams-vs-name-span',
                'color' => '#ffffff',
            ],
[
                'name' => 'color-picks-vs-picks',
                'slug' => 'color-picks-vs-picks',
                'color' => '#0160ac',
            ],
        ];
    }

    public function getStylesToloadGeneratePress()
    {
        return [
            [
                'customSelector' => '.rdg-picks-vs .filter .buttons a',
                'fontSize' => '1rem',
                'fontWeight' => '500',
            ],
            [
                'customSelector' => '.rdg-picks-vs .filter .buttons a i',
                'fontSize' => '1.2rem',
            ],
            [
                'customSelector' => '.rdg-picks-vs .message span',
                'fontSize' => '1.125rem',
                'fontSizeTablet' => '1rem',
                'fontWeight' => '600',
            ],
            [
                'customSelector' => '.rdg-picks-vs .pick .pick-header .sport',
                'fontSize' => '.875rem',
                'fontWeight' => '700',
                'textTransform' => 'uppercase'
            ],
            [
                'customSelector' => '.rdg-picks-vs .pick .pick-header .date-time',
                'fontSize' => '.938rem',
                'fontSizeMobile' => '.813rem',
            ],
            [
                'customSelector' => '.rdg-picks-vs .pick .pick-header .status',
                'fontSize' => '.875rem',
                'fontWeight' => '700',
                'textTransform' => 'uppercase'
            ],
            [
                'customSelector' => '.rdg-picks-vs .pick .pick-body .team-vs .vs-logo span',
                'fontSize' => '.9rem',
                'fontWeight' => '500',
            ],
            [
                'customSelector' => '.rdg-picks-vs .pick .pick-body .team .team-name',
                'fontSize' => '.938rem',
                'fontWeight' => '700',
            ],
            [
                'customSelector' => '.rdg-picks-vs .pick .pick-body .team-vs .vs-name span',
                'fontSize' => '.9rem',
                'fontWeight' => '600',
            ],
        ];
    }
}

add_action('wp_ajax_nopriv_load_picks_vs','get_picks_vs_data');
add_action('wp_ajax_load_picks_vs','get_picks_vs_data');

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

    $block_name = BLOCK_RDG_PICKS_VS;
    $block_picks_vs = $rdgb->settings->getBlock($block_name);

    $message = null;
    $items = $_POST['items'];
    $id_sub_category = isset($_POST['id_sub_category']) ? $_POST['id_sub_category'] : null;
    $data_date_type = $_POST['data_date_type'];
    $data_since_date = $_POST['data_since_date'];
    $data_to_date = $_POST['data_to_date'];

    $link_options = $_POST['custom_link_show'];
    $custom_link = [
        'url' => $_POST['custom_link_url'],
        'target' => $_POST['custom_link_target'],
    ];

    $api_url = get_field('picks_vs_api_url', 'option');
    $api_token = get_field('picks_vs_api_token', 'option');

    $date_request = $block_picks_vs->get_date_filter($data_date_type, $data_since_date, $data_to_date);

    if (strpos($id_sub_category, '-')) {
        list($cat_primary, $cat_secondary) = explode('-', $id_sub_category);
        $key = $cat_primary.':'.$cat_secondary.':'.$data_since_date.':'.$data_to_date.':'.$items;
    } else {
        $cat_primary = $id_sub_category;
        $cat_secondary = '';
        $key = $cat_primary.':'.$data_since_date.':'.$data_to_date.':'.$items;
    }

    $api_attributes = [
        'category_primary' => $cat_primary,
        'sub_category' => $cat_secondary,
    ];

    if ($rdgb->helpers->initRedis()) {
        $rdgb->helpers->setRedisGroup(REDIS_GROUP_BLOCKS);
        $data_block = $rdgb->helpers->getRedis($key);

        if (!empty($data_block)) {
            $picks = unserialize($data_block);

            require 'partials/api.php';
            wp_die();
        }
    }

    if (!empty($api_url) && !empty($api_token)) {
        $api_attributes = $block_picks_vs->getApiAttributes($items, ['category' => $id_sub_category, 'date' => $date_request]);
        $response = $block_picks_vs->getResourcesAjax($api_attributes);

        if (empty($response->message)) {
            $picks = $response->json;
        } else {
            $picks = null;
            $message = $response->message;
        }

        if ($rdgb->helpers->initRedis() && $picks !== null) {
            $rdgb->helpers->setRedis($key, serialize($picks), REDIS_TIMEOUT);
        }
    } else {
        $message = 'Configure API connection correctly.';
    }

    if (!empty($message)) {
        $message = 'Picks with these characteristics were not found.';
        require 'partials/message.php';
        wp_die();
    }

    require 'partials/api.php';

    wp_die();
}

add_action('wp_ajax_nopriv_load_picks_vs_title','get_picks_vs_title');
add_action('wp_ajax_load_picks_vs_title','get_picks_vs_title');

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

    $block_name = BLOCK_RDG_PICKS_VS;
    $block_picks_vs = $rdgb->settings->getBlock($block_name);
    $id_sub_category = isset($_POST['id_sub_category']) ? $_POST['id_sub_category'] : '';

    $titles_sports = $rdgb->helpers->getSporstCategoriesTitles(true);
    $title = (is_array($titles_sports) && isset($titles_sports[$id_sub_category])) ? $titles_sports[$id_sub_category] : '';

    echo $block_picks_vs->get_picks_vs_title_format($title);

    wp_die();
}

$rdg_picks_vs_class = new Rdg_Blocks_Picks_Vs();
$rdg_picks_vs_class->init();

return $rdg_picks_vs_class;
