<?php
// Creating the widget
class Retheme_Logo_Widget extends WP_Widget {

    public function __construct() {
        // Widget initialization with ID, name, and description
        parent::__construct('retheme_logo', __("Theme - Logo", 'jelajah-text'), [
            'description' => __('Widget for logo'),
            'customize_selective_refresh' => true,
        ]);
    }

    // Widget front-end display
    public function widget($args, $instance) {
        // Retrieve the widget settings
        $title = !empty($instance['title']) ? $instance['title'] : '';
        $width = !empty($instance['width']) ? $instance['width'] : '180';
        $logo_light = !empty($instance['logo_light']) ? true : false;

        // Display the widget before and after content
        echo $args['before_widget'];

        if (!empty($title)) {
            echo $args['before_title'] . $title . $args['after_title'];
        }

        // Output the logo
        $logo = wp_parse_args(rt_get_option('brand_logo_primary'), array(
            'url' => '',
            'width' => '',
            'height' => '',
        ));

        $logo_alternative = wp_parse_args(rt_get_option('brand_logo_alternative',  $logo), array(
            'url' => '',
            'width' => '',
            'height' => '',
        ));

        $site_name = get_bloginfo('name');
        $site_url = site_url();

        $html = "<a class='rt-logo' href='{$site_url}' rel='home' style='width: {$width}px;'>";

        if (!empty($logo)) {
            if ($logo_light) {
                $html .= wp_sprintf("<span class='rt-logo__primary'><img src='%s' width='%d' height='%d' alt='%s'></span>", $logo_alternative['url'], $logo_alternative['width'], $logo_alternative['height'], $site_name);
            } else {
                $html .= wp_sprintf("<span class='rt-logo__primary'><img src='%s' width='%d' height='%d' alt='%s'></span>", $logo['url'], $logo['width'], $logo['height'], $site_name);
            }
        }

        if (!empty($logo_alternative)) {
            $html .= wp_sprintf("<span class='rt-logo__alternative'><img src='%s' width='%d' height='%d' alt='%s'></span>", $logo_alternative['url'], $logo_alternative['width'], $logo_alternative['height'], $site_name);
        }

        $html .= "</a>";

        echo $html;


        // Display the widget after content
        echo $args['after_widget'];
    }

    // Widget backend form
    public function form($instance) {
        // Retrieve or set default values for the form inputs
        $title = !empty($instance['title']) ? $instance['title'] : '';
        $width = !empty($instance['width']) ? $instance['width'] : '180';
        $logo_light = !empty($instance['logo_light']) ? true : false;

?>

        <!-- Title input field -->
        <p>
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php echo __('Title:', 'jelajah-text'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
        </p>

        <!-- Width input field -->
        <p>
            <label for="<?php echo $this->get_field_id('width'); ?>"><?php echo __('Width (default 180px):', 'jelajah-text'); ?></label>
            <input class="widefat" id="<?php echo $this->get_field_id('width'); ?>" name="<?php echo $this->get_field_name('width'); ?>" type="number" value="<?php echo esc_attr($width); ?>" />
        </p>

        <p>
            <input class="checkbox" type="checkbox" <?php checked($logo_light); ?> id="<?php echo $this->get_field_id('logo_light'); ?>" name="<?php echo $this->get_field_name('logo_light'); ?>" />
            <label for="<?php echo $this->get_field_id('logo_light'); ?>"><?php _e('Always show logo alternative', 'jelajah-text'); ?></label>
        </p>

<?php
    }

    // Updating widget instance with new values
    public function update($new_instance, $old_instance) {
        $instance = [];
        $instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
        $instance['width'] = (!empty($new_instance['width'])) ? strip_tags($new_instance['width']) : '';
        $instance['logo_light'] = isset($new_instance['logo_light']) ? true : false;

        return $instance;
    }
}
