<?php
class WDCL_LogoCarouselChild extends Divi_Carousel_Free_Builder_Module
{

    public $slug            = 'wdcl_logo_carousel_child';
    public $vb_support      = 'on';
    public $type            = 'child';
    public $child_title_var = 'admin_title';

    public function init()
    {
        $this->name = esc_html__('Logo', 'divi-carousel-free');

        $this->settings_modal_toggles = [
            'general'  => [
                'toggles' => [
                    'main_content' => [
                        'title' => esc_html__('Content', 'divi-carousel-free'),
                    ],
                ],
            ],
            'advanced' => [
                'toggles' => [
                    'overlay' => esc_html__('Overlay', 'divi-carousel-free'),
                    'borders' => esc_html__('Borders', 'divi-carousel-free'),
                ],
            ],
        ];
    }

    public function get_fields()
    {

        $fields = [
            'logo'         => [
                'label'              => esc_html__('Upload Logo', 'divi-carousel-free'),
                'type'               => 'upload',
                'option_category'    => 'basic_option',
                'upload_button_text' => esc_attr__('Upload a Logo', 'divi-carousel-free'),
                'choose_text'        => esc_attr__('Choose a Logo', 'divi-carousel-free'),
                'update_text'        => esc_attr__('Set As Logo', 'divi-carousel-free'),
                'toggle_slug'        => 'main_content',
            ],

            'logo_alt'     => [
                'label'       => esc_html__('Logo Alt Text', 'divi-carousel-free'),
                'type'        => 'text',
                'toggle_slug' => 'main_content',
            ],

            'is_link'      => [
                'label'           => esc_html__('Use Link', 'divi-carousel-free'),
                'type'            => 'yes_no_button',
                'option_category' => 'configuration',
                'options'         => [
                    'on'  => esc_html__('Yes', 'divi-carousel-free'),
                    'off' => esc_html__('No', 'divi-carousel-free'),
                ],
                'default'         => 'off',
                'toggle_slug'     => 'main_content',
            ],

            'link_url'     => [
                'label'       => esc_html__('Link Url', 'divi-carousel-free'),
                'type'        => 'text',
                'default'     => '',
                'show_if'     => [
                    'is_link' => 'on',
                ],
                'toggle_slug' => 'main_content',
            ],

            'link_options' => [
                'type'        => 'multiple_checkboxes',
                'default'     => 'off|off',
                'toggle_slug' => 'main_content',
                'options'     => [
                    'link_target' => 'Open in new window',
                    'link_rel'    => 'Add nofollow',
                ],
                'show_if'     => [
                    'is_link' => 'on',
                ],
            ],
        ];

        $label = [
            'admin_title' => [
                'label'       => esc_html__('Admin Label', 'divi-carousel-free'),
                'type'        => 'text',
                'description' => esc_html__('This will change the label of the item', 'divi-carousel-free'),
                'toggle_slug' => 'admin_label',
            ],
        ];

        $overlay = $this->_get_overlay_option_fields('overlay', []);

        return array_merge($label, $fields, $overlay);
    }

    public function get_advanced_fields_config()
    {

        $advanced_fields                = [];
        $advanced_fields['text']        = false;
        $advanced_fields['fonts']       = false;
        $advanced_fields['text_shadow'] = false;
        $advanced_fields['max_width']   = false;

        $advanced_fields['margin_padding'] = [
            'css' => [
                'main'      => '%%order_class%% .dcm-logo-carousel-item',
                'important' => 'all',
            ],
        ];

        $advanced_fields['borders']['item'] = [
            'css'          => [
                'main'      => [
                    'border_radii'  => '%%order_class%%',
                    'border_styles' => '%%order_class%%',
                ],
                'important' => 'all',
            ],
            'label_prefix' => esc_html__('Iten', 'divi-carousel-free'),
            'defaults'     => [
                'border_radii'  => 'on|0px|0px|0px|0px',
                'border_styles' => [
                    'width' => '0px',
                    'color' => '#333',
                    'style' => 'solid',
                ],
            ],
            'tab_slug'     => 'advanced',
            'toggle_slug'  => 'borders',
        ];

        return $advanced_fields;
    }

    public function _render_ref_attr()
    {

        if ($this->props['is_link'] === 'on') {

            $link_options = explode('|', $this->props['link_options']);

            if ($link_options[1] === 'on') {
                return sprintf('ref="nofollow"');
            }
        }
    }

    public function _render_logo()
    {

        $placeholder = 'data:image/png;base64, ';
        $logo        = !empty($this->props['logo']) ? $this->props['logo'] : $placeholder;
        $logo_alt    = $this->props['logo_alt'];

        if ($this->props['is_link'] === 'on') {

            $link_options = explode('|', $this->props['link_options']);
            $target       = $link_options[0] === 'on' ? '_blank' : '_self';
            $link_url     = $this->props['link_url'];

            return sprintf(
                '<a target="%1$s" href="%2$s" %3$s><img class="dcm-swapped-img" data-mfp-src="%4$s" src="%4$s" alt="%5$s" /></a>',
                esc_attr($target),
                esc_url($link_url),
                $this->_render_ref_attr(),
                esc_url($logo),
                esc_attr($logo_alt)
            );
        }

        return sprintf('<div><img class="dcm-swapped-img" data-mfp-src="%1$s" src="%1$s" alt="%2$s"/></div>', $logo, $logo_alt);
    }

    public function render($attrs, $content, $render_slug)
    {

        $this->remove_classname('et_pb_module');
        $this->add_classname('wdc_et_pb_module');
        $this->_get_overlay_style($render_slug);

        $processed_overlay_icon = esc_attr(et_pb_process_font_icon($this->props['overlay_icon']));
        $overlay_icon           = !empty($processed_overlay_icon) ? $processed_overlay_icon : '';

        return sprintf(
            '<div class="dcm-carousel-item dcm-logo-carousel-item dcm-image-swap">
			<div class="dcm-overlay" data-icon="%2$s"></div>
				%1$s
			</div>',
            $this->_render_logo(),
            $overlay_icon
        );
    }
}

new WDCL_LogoCarouselChild();
