<?php

if (!defined('ABSPATH')) { exit; }

class AIEO_SSO_Block {

    const BLOCK_NAME = 'aieo/sso-buttons-block';

    public static function register() {
        add_action('init', array(__CLASS__, 'register_block'));
    }

    public static function register_block() {
        if (!function_exists('register_block_type')) { return; }
        register_block_type(self::BLOCK_NAME, array(
            'api_version'     => 3,
            'title'           => __('AIEO Social Login Buttons', 'ai-eshop-optimizer'),
            'category'        => 'widgets',
            'icon'            => 'admin-users',
            'render_callback' => array(__CLASS__, 'render'),
            'attributes'      => array(
                'intent' => array(
                    'type'    => 'string',
                    'default' => 'checkout',
                ),
                'layout' => array(
                    'type'    => 'string',
                    'default' => 'inline',
                ),
                'intro'  => array(
                    'type'    => 'string',
                    'default' => '',
                ),
            ),
        ));
    }

    
    public static function render($attrs = array()) {
        if (is_user_logged_in()) { return ''; }
        if (!class_exists('AIEO_SSO_Buttons')) { return ''; }

        $intent = isset($attrs['intent']) ? (string) $attrs['intent'] : 'checkout';
        $layout = isset($attrs['layout']) ? (string) $attrs['layout'] : 'inline';
        $intro  = isset($attrs['intro'])  ? (string) $attrs['intro']  : '';
        if ($intro === '') {
            $intro = __('Συνδεθείτε γρήγορα και ολοκληρώστε την παραγγελία:', 'ai-eshop-optimizer');
        }

        $buttons = AIEO_SSO_Buttons::render($intent, array(
            'layout' => $layout,
            'intro'  => $intro,
        ));
        if ($buttons === '') { return ''; }

        return '<div class="aieo-sso-block-checkout-wrap" style="margin:0 auto 24px;max-width:960px;">'
            . $buttons
            . '</div>';
    }
}
