<?php
// Načtení WordPress prostředí z kořenového adresáře
$wordpress_path = dirname(__FILE__, 4) . '/wp-load.php';

if (file_exists($wordpress_path)) {
    require_once $wordpress_path;
} else {
    exit("Chyba: wp-load.php nebyl nalezen.");
}

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

class Role_Image_Widget extends \Elementor\Widget_Base {

    public function get_name() {
        return 'role_image_widget';
    }

    public function get_title() {
        return __('Obrázek s viditelností', 'elementor');
    }

    public function get_icon() {
        return 'eicon-eye';
    }

    public function get_categories() {
        return [ 'basic' ];
    }

    protected function register_controls() {
        $this->start_controls_section(
            'content_section',
            [
                'label' => __('Content', 'elementor'),
                'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
            ]
        );

        $this->add_control(
            'image',
            [
                'label' => __('Choose Image', 'elementor'),
                'type' => \Elementor\Controls_Manager::MEDIA,
                'default' => [
                    'url' => \Elementor\Utils::get_placeholder_image_src(),
                ],
            ]
        );

        $this->add_control(
            'roles',
            [
                'label' => __('Visible for Roles', 'elementor'),
                'type' => \Elementor\Controls_Manager::SELECT2,
                'options' => $this->get_available_roles(),
                'multiple' => true,
                'description' => __('Select roles that can see this image. Leave empty to make it visible for everyone.', 'elementor'),
            ]
        );

        $this->add_control(
            'link_url',
            [
                'label' => __('Link URL', 'elementor'),
                'type' => \Elementor\Controls_Manager::URL,
                'placeholder' => __('https://example.com', 'elementor'),
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Get all available user roles.
     *
     * @return array List of roles in 'slug' => 'label' format.
     */
    private function get_available_roles() {
        global $wp_roles;
        $roles = [];
        foreach ($wp_roles->roles as $role_slug => $role_details) {
            $roles[$role_slug] = $role_details['name'];
        }
        return $roles;
    }

    protected function render() {
        $settings = $this->get_settings_for_display();
        $image_url = $settings['image']['url'];
        $roles = $settings['roles'];
        $link_url = !empty($settings['link_url']['url']) ? $settings['link_url']['url'] : null;

        if (!empty($roles)) {
            $roles_list = implode(',', $roles);
            echo '[roles_content roles="' . esc_attr($roles_list) . '"]';
        }

        if ($link_url) {
            echo '<a href="' . esc_url($link_url) . '" target="_blank">';
        }

        echo '<img src="' . esc_url($image_url) . '" style="display: block; margin-left: auto; margin-right: auto; width: 50%;">';

        if ($link_url) {
            echo '</a>';
        }

        if (!empty($roles)) {
            echo '[/roles_content]';
        }
    }
}
