<?php
/**
 * Define custom fields for widgets.
 *
 * This function adds custom fields to widgets, enhancing their 
 * functionality and allowing for more personalized and dynamic content 
 * management within the theme's widget areas.
 *
 * @package Yaatra
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

/**
 * Show Widget Field in the Widget Form
 *
 * @param array $instance The widget instance.
 * @param array $widget_field The widget field settings.
 * @param mixed $yaatra_widget_field_value The current value of the widget field.
 */
function yaatra_widgets_show_widget_field( $instance = '', $widget_field = '', $yaatra_widget_field_value = '' ) {
    extract( $widget_field );

    $field_id           = esc_attr( $instance->get_field_id( $yaatra_widgets_name ) );
    $field_name         = esc_attr( $instance->get_field_name( $yaatra_widgets_name ) );
    $field_value        = esc_html( $yaatra_widget_field_value );
    $field_label        = esc_html( $yaatra_widgets_title );
    $field_description  = isset( $yaatra_widgets_description ) ? wp_kses_post( $yaatra_widgets_description ) : '';

    switch ( $yaatra_widgets_field_type ) {
        case 'text':
?>
            <p>
                <span class="field-label"><label for="<?php echo $field_id; ?>"><?php echo $field_label; ?></label></span>
                <input class="widefat" id="<?php echo $field_id; ?>" name="<?php echo $field_name; ?>" type="text" value="<?php echo $field_value; ?>" />
                <?php if ( $field_description ) { ?>
                    <br /><em><?php echo $field_description; ?></em>
                <?php } ?>
            </p>
<?php
            break;

        case 'user_dropdown':
            if ( empty( $yaatra_widget_field_value ) ) {
                $yaatra_widget_field_value = $yaatra_widgets_default;
            }
?>
            <p>
                <label for="<?php echo $field_id; ?>"><?php echo $field_label; ?>:</label>
                <?php
                    $dropdown_args = wp_parse_args(
                        array(
                            'show_option_none' => __( '- - Select User - -', 'yaatra' ),
                            'selected' => esc_attr( $yaatra_widget_field_value ),
                            'echo' => false,
                        )
                    );
                    $dropdown = wp_dropdown_users($dropdown_args);
                    $dropdown = str_replace('<select', '<select name="' . $field_name . '" id="' . $field_id . '" class="widefat"', $dropdown);
                    echo $dropdown;
                ?>
            </p>
<?php
            break;

        case 'number':
            if ( empty( $yaatra_widget_field_value ) ) {
                $yaatra_widget_field_value = $yaatra_widgets_default;
            }
?>
            <p>
                <label for="<?php echo $field_id; ?>"><?php echo $field_label; ?></label>
                <input name="<?php echo $field_name; ?>" type="number" step="1" min="1" id="<?php echo $field_id; ?>" value="<?php echo $field_value; ?>" class="small-text" />
                <?php if ( $field_description ) { ?>
                    <br /><em><?php echo $field_description; ?></em>
                <?php } ?>
            </p>
<?php
            break;

        default:
            break;
    }
}

/**
 * Update Widget Field Value
 *
 * @param array $widget_field The widget field settings.
 * @param mixed $new_field_value The new value to be saved.
 * @return mixed The sanitized value to be saved.
 */
function yaatra_widgets_updated_field_value( $widget_field, $new_field_value ) {
    extract( $widget_field );

    if ( $yaatra_widgets_field_type === 'number' ) {
        return absint( $new_field_value );
    } else {
        return sanitize_text_field( $new_field_value );
    }
}