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

    public function __construct() {
        // Widget initialization with ID, name, and description
        parent::__construct('retheme_social', __("Theme - Social Media", 'jelajah-text'), [
            'description' => __('Widget for social media links', 'jelajah-text'), // Translated and fixed 'sosial' typo.
            'customize_selective_refresh' => true,
        ]);
    }

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

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

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

        // Output the social media links
        echo rt_get_social_media([
            'type' => esc_attr($style), // Escaping attribute value
        ]);

        // 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'] : '';
        $style = !empty($instance['style']) ? $instance['style'] : 'default';

        // Available styles and sizes
        $style_lists = array(
            'simple' => __('Simple', 'jelajah-text'),
            'logos-only' => __('Logos Only', 'jelajah-text'),
            'default' => __('Default', 'jelajah-text'),
        );
?>

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

        <!-- Style selection field -->
        <p>
            <label for="<?php echo esc_attr($this->get_field_id('style')); ?>"><?php _e('Style:', 'jelajah-text'); ?></label>
            <select class="widefat" id="<?php echo esc_attr($this->get_field_id('style')); ?>" name="<?php echo esc_attr($this->get_field_name('style')); ?>">
                <?php foreach ($style_lists as $key => $style_list) : ?>
                    <option value="<?php echo esc_attr($key); ?>" <?php selected($style, $key); ?>>
                        <?php echo esc_html(ucwords($style_list)); ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </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['style'] = (!empty($new_instance['style'])) ? strip_tags($new_instance['style']) : 'default';

        return $instance;
    }
}
?>