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

class FTM_Blocks_Table_CTA extends Ftm_Blocks_Base
{
    private $block_name = 'table-cta';
    private $block_folder_name = 'table-cta';
    private $block_css_manifest_key = 'ftm_table_cta';
    private $block_title = 'Table CTA';
    private $block_description = '';
    private $block_icon = 'editor-table';
    private $block_keywords = ['ftm', 'custom', 'cta', 'call to action', 'table'];
    private $block_mode = 'preview';
    private $block_category = "ftm-blocks";
    protected $block_id             = '';

    /* The property below stores all the necessary customizations for this block and this is what
    *  will be consider to generate the CSS based on the block id
    */

    private $css_generate_fields = [
        'ftm-table-h-bg-color'          => ['field_name' => 'headings_background_color', 'output_type' => 'variable'],
        'ftm-table-h-text-color'        => ['field_name' => 'headings_text_color', 'output_type' => 'variable'],
        'ftm-table-btn-rad'             => ['field_name' => 'btn_border_radius', 'output_type' => 'variable'],
        'ftm-table-btn-bg-color'        => ['field_name' => 'btn_background_color', 'output_type' => 'variable'],
        'ftm-table-btn-bg-color-hv'     => ['field_name' => 'btn_background_color_hover', 'output_type' => 'variable'],
        'ftm-table-btn-txt-color'       => ['field_name' => 'btn_text_color', 'output_type' => 'variable'],
        'ftm-table-btn-txt-color-hv'    => ['field_name' => 'btn_text_color_hover', 'output_type' => 'variable'],
        'ftm-table-row-gradient-1'      => ['field_name' => 'row_gradient_color_1', 'output_type' => 'variable'],
        'ftm-table-row-gradient-2'      => ['field_name' => 'row_gradient_color_2', 'output_type' => 'variable'],
    ];

    public function init()
    {
        $this->register_block();
        $this->register_fields();
    }

    public function get_block_name()
    {
        return $this->block_name;
    }

    public function get_css_fields()
    {
        return $this->css_generate_fields;
    }

    public function load_assets()
    {
        $ftm_blocks_helper = new Ftm_Blocks_Helpers();
        wp_enqueue_style('ftm_table-cta', $ftm_blocks_helper->webpack($this->block_css_manifest_key . '.css'));
    }

    private function register_block()
    {
        if (function_exists('acf_register_block_type')) {
            $ftm_blocks_helper = new Ftm_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' => FTMBLOCKS_PLUGIN_DIR . "blocks/" . $this->block_folder_name . "/render.php",
                'enqueue_assets' => [$this, 'load_assets'],
                'supports' => [
                    'jsx' => true,
                    'color' => [
                        'background' => true,
                        'gradients'  => false,
                        'text' => false,
                    ],
                ],
                'example'  => array(
                    'attributes' => array(
                        'mode' => 'preview',
                        'data' => array(
                            'preview_image_help' => FTMBLOCKS_PLUGIN_URL . "blocks/" . $this->block_folder_name . "/preview.jpg",
                        )
                    )
                ),
                'class' => $this,
                'ftm_id' => 'block-' . uniqid()
            ]);
        }
    }

    private function register_fields()
    {
        add_action('acf/include_fields', function () {
            if (!function_exists('acf_add_local_field_group')) {
                return;
            }

            acf_add_local_field_group(array(
                'key' => 'group_637fdd2367dec',
                'title' => 'Table-CTA',
                'fields' => array(
                    array(
                        'key' => 'field_65cccc33f67d6',
                        'label' => 'General',
                        'name' => '',
                        'aria-label' => '',
                        'type' => 'tab',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'placement' => 'top',
                        'endpoint' => 0,
                    ),
                    array(
                        'key' => 'field_640b639755cca',
                        'label' => 'Headings Background Color',
                        'name' => 'headings_background_color',
                        'aria-label' => '',
                        'type' => 'color_picker',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '#388ACC',
                        'enable_opacity' => 0,
                        'return_format' => 'string',
                    ),
                    array(
                        'key' => 'field_640b63d755ccb',
                        'label' => 'Headings Text Color',
                        'name' => 'headings_text_color',
                        'aria-label' => '',
                        'type' => 'color_picker',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '#FFFFFF',
                        'enable_opacity' => 0,
                        'return_format' => 'string',
                    ),
                    array(
                        'key' => 'field_63d98e0df7c8d',
                        'label' => 'App Header',
                        'name' => 'table_cta_header1',
                        'aria-label' => '',
                        'type' => 'text',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => 'Dating App',
                        'maxlength' => '',
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                    ),
                    array(
                        'key' => 'field_63d98e5ef7c8e',
                        'label' => 'Facts Header',
                        'name' => 'table_cta_header2',
                        'aria-label' => '',
                        'type' => 'text',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => 'Key Facts',
                        'maxlength' => '',
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                    ),
                    array(
                        'key' => 'field_63d98f45f7c8f',
                        'label' => 'Free Trial',
                        'name' => 'table_cta_header3',
                        'aria-label' => '',
                        'type' => 'text',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => 'Free Trial',
                        'maxlength' => '',
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                    ),
                    array(
                        'key' => 'field_637fdddf086b9',
                        'label' => 'Sites',
                        'name' => 'table_cta_sites',
                        'aria-label' => '',
                        'type' => 'repeater',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        '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_638119c49db34',
                                'label' => 'Site Name',
                                'name' => 'table_cta_site_name',
                                'aria-label' => '',
                                'type' => 'text',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => '',
                                'maxlength' => '',
                                'placeholder' => '',
                                'prepend' => '',
                                'append' => '',
                                'parent_repeater' => 'field_637fdddf086b9',
                            ),
                            array(
                                'key' => 'field_637fdd23086b8',
                                'label' => 'Site Logo',
                                'name' => 'table_cta_site_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_637fdddf086b9',
                            ),
                            array(
                                'key' => 'field_637fde91086ba',
                                'label' => 'Facts',
                                'name' => 'table_cta_facts',
                                'aria-label' => '',
                                'type' => 'wysiwyg',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => '',
                                'delay' => 1,
                                'tabs' => 'all',
                                'toolbar' => 'full',
                                'media_upload' => 1,
                                'parent_repeater' => 'field_637fdddf086b9',
                            ),
                            array(
                                'key' => 'field_637fdee8086bb',
                                'label' => 'Link Text',
                                'name' => 'table_cta_link_text',
                                'aria-label' => '',
                                'type' => 'text',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => '',
                                'maxlength' => '',
                                'placeholder' => '',
                                'prepend' => '',
                                'append' => '',
                                'parent_repeater' => 'field_637fdddf086b9',
                            ),
                            array(
                                'key' => 'field_637fdf0b086bc',
                                'label' => 'Link Target',
                                'name' => 'table_cta_link_target',
                                'aria-label' => '',
                                'type' => 'text',
                                'instructions' => '',
                                'required' => 0,
                                'conditional_logic' => 0,
                                'wrapper' => array(
                                    'width' => '',
                                    'class' => '',
                                    'id' => '',
                                ),
                                'default_value' => '',
                                'maxlength' => '',
                                'placeholder' => '',
                                'prepend' => '',
                                'append' => '',
                                'parent_repeater' => 'field_637fdddf086b9',
                            ),
                        ),
                    ),
                    array(
                        'key' => 'field_63811a2b777af',
                        'label' => 'Link Attributes',
                        'name' => 'table_cta_link_attributes',
                        'aria-label' => '',
                        'type' => 'text',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '',
                        'maxlength' => '',
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                    ),
                    array(
                        'key' => 'field_65ccc9049f22e',
                        'label' => 'Button',
                        'name' => '',
                        'aria-label' => '',
                        'type' => 'tab',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'placement' => 'top',
                        'endpoint' => 0,
                    ),
                    array(
                        'key' => 'field_65ccc92e9f22f',
                        'label' => 'Border Radius',
                        'name' => 'btn_border_radius',
                        'aria-label' => '',
                        'type' => 'text',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '',
                        'maxlength' => '',
                        'placeholder' => '',
                        'prepend' => '',
                        'append' => '',
                    ),
                    array(
                        'key' => 'field_65ccc9639f230',
                        'label' => 'Background Color',
                        'name' => 'btn_background_color',
                        'aria-label' => '',
                        'type' => 'color_picker',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '',
                        'enable_opacity' => 0,
                        'return_format' => 'string',
                    ),
                    array(
                        'key' => 'field_65ccc9d18fc0d',
                        'label' => 'Background Color Hover',
                        'name' => 'btn_background_color_hover',
                        'aria-label' => '',
                        'type' => 'color_picker',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '',
                        'enable_opacity' => 0,
                        'return_format' => 'string',
                    ),
                    array(
                        'key' => 'field_65ccc9a19f231',
                        'label' => 'Text Color',
                        'name' => 'btn_text_color',
                        'aria-label' => '',
                        'type' => 'color_picker',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '',
                        'enable_opacity' => 0,
                        'return_format' => 'string',
                    ),
                    array(
                        'key' => 'field_65ccc9df8fc0e',
                        'label' => 'Text Color Hover',
                        'name' => 'btn_text_color_hover',
                        'aria-label' => '',
                        'type' => 'color_picker',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => '',
                        'enable_opacity' => 0,
                        'return_format' => 'string',
                    ),
                    array(
                        'key' => 'field_65d12d0b0e1cc',
                        'label' => 'Row Gradient Color 1',
                        'name' => 'row_gradient_color_1',
                        'aria-label' => '',
                        'type' => 'color_picker',
                        'instructions' => 'This is the first color will be used in the gradient when the user hovers over the table row',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => 'rgba(255, 201, 56, 0)',
                        'enable_opacity' => 1,
                        'return_format' => 'string',
                    ),
                    array(
                        'key' => 'field_65d12e1d82cea',
                        'label' => 'Row Gradient Color 2',
                        'name' => 'row_gradient_color_2',
                        'aria-label' => '',
                        'type' => 'color_picker',
                        'instructions' => 'This is the second and last color will be used in the gradient when the user hovers over the table row',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'default_value' => 'rgba(255, 210, 54, 0.2763699229691877)',
                        'enable_opacity' => 1,
                        'return_format' => 'string',
                    ),
                    array(
                        'key' => 'field_65cf5e74d4f15',
                        'label' => 'Clickable Row',
                        'name' => 'btn_whole_link_clickable',
                        'aria-label' => '',
                        'type' => 'select',
                        'instructions' => '',
                        'required' => 0,
                        'conditional_logic' => 0,
                        'wrapper' => array(
                            'width' => '',
                            'class' => '',
                            'id' => '',
                        ),
                        'choices' => array(
                            'yes' => 'Yes',
                            'no' => 'No',
                        ),
                        'default_value' => 'yes',
                        'return_format' => 'value',
                        'multiple' => 0,
                        'allow_null' => 0,
                        'ui' => 0,
                        'ajax' => 0,
                        'placeholder' => '',
                    ),
                ),
                'location' => array(
                    array(
                        array(
                            'param' => 'block',
                            'operator' => '==',
                            'value' => 'acf/table-cta',
                        ),
                    ),
                ),
                'menu_order' => 0,
                'position' => 'normal',
                'style' => 'default',
                'label_placement' => 'top',
                'instruction_placement' => 'label',
                'hide_on_screen' => '',
                'active' => true,
                'description' => '',
                'show_in_rest' => 0,
            ));
        });
    }
}

$ftm_blocks_table_cta_class = new FTM_Blocks_Table_CTA();
$ftm_blocks_table_cta_class->init();

return $ftm_blocks_table_cta_class;
