<?php

namespace WeDevs\Wpuf\Pro\Fields;

/**
 * Secondary Email Field Class
 *
 * Allows collecting an additional email address for users (backup/recovery/contact)
 * Stores in user_meta under wpuf_secondary_email with standard email validation
 *
 * @since 4.2.10
 */
class Field_Secondary_Email extends Pro_Field_Contract {

    /**
     * Constructor
     *
     * @since 4.2.10
     */
    public function __construct() {
        $this->name       = __( 'Secondary Email', 'wpuf-pro' );
        $this->input_type = 'secondary_email';
        $this->icon       = 'mail';
    }

    /**
     * Render the Secondary Email field
     *
     * @since 4.2.10
     *
     * @param array  $field_settings
     * @param int    $form_id
     * @param string $type
     * @param int    $post_id
     *
     * @return void
     */
    public function render( $field_settings, $form_id, $type = 'post', $post_id = null ) {
        $value = '';

        if ( isset( $post_id ) && '0' !== $post_id ) {
            if ( $this->is_meta( $field_settings ) ) {
                $value = $this->get_meta( $post_id, $field_settings['name'], $type );
            }
        } else {
            $value = isset( $field_settings['default'] ) ? $field_settings['default'] : '';
        }

        $read_only = isset( $field_settings['read_only'] ) && 'yes' === $field_settings['read_only'] ? 'readonly' : '';;

        $this->field_print_label( $field_settings, $form_id ); ?>

        <div class="wpuf-fields">
            <input
                id="<?php echo esc_attr( $field_settings['name'] ) . '_' . esc_attr( $form_id ); ?>"
                type="email"
                class="email <?php echo ' wpuf_' . esc_attr( $field_settings['name'] ) . '_' . esc_attr( $form_id ); ?>"
                data-required="<?php echo esc_attr( $field_settings['required'] ); ?>"
                data-type="email"
                name="<?php echo esc_attr( $field_settings['name'] ); ?>"
                placeholder="<?php echo esc_attr( $field_settings['placeholder'] ); ?>"
                value="<?php echo esc_attr( $value ); ?>"
                size="<?php echo esc_attr( $field_settings['size'] ); ?>"
                autocomplete="email"
                <?php echo esc_attr( $read_only ); ?>
            />
            <?php $this->help_text( $field_settings ); ?>
        </div>

        <?php $this->after_field_print_label();
    }

    /**
     * Get field options setting
     *
     * @since 4.2.10
     *
     * @return array
     */
    public function get_options_settings() {
        // Get default options but exclude the name field since we'll provide our own meta_key field
        $filtered_options = $this->get_default_option_settings( true, [ 'name' ] );

        // Get default text options for email fields
        $text_options = $this->get_default_text_option_settings( true );

        $secondary_email_options = [
            [
                'name'      => 'meta_key',
                'title'     => __( 'Meta Key', 'wpuf-pro' ),
                'type'      => 'text',
                'default'   => 'wpuf_secondary_email',
                'section'   => 'basic',
                'priority'  => 11.5,
                'readonly'  => true,
                'help_text' => __( 'The meta key used to store this field data. This is fixed to "wpuf_secondary_email" and cannot be changed.', 'wpuf-pro' ),
            ],
            [
                'name'      => 'read_only',
                'title'     => __( 'Read Only', 'wpuf-pro' ),
                'type'      => 'radio',
                'options'   => [
                    'yes' => __( 'Yes', 'wpuf-pro' ),
                    'no'  => __( 'No', 'wpuf-pro' ),
                ],
                'default'   => 'no',
                'inline'    => true,
                'section'   => 'advanced',
                'priority'  => 14,
                'help_text' => __( 'Make this field read-only', 'wpuf-pro' ),
            ],
        ];

        return array_merge( $filtered_options, $secondary_email_options, $text_options );
    }

    /**
     * Get the field props
     *
     * @since 4.2.10
     *
     * @return array
     */
    public function get_field_props() {
        $defaults = $this->default_attributes();

        $props = [
            'input_type'       => 'email',
            'template'         => 'secondary_email',
            'label'            => __( 'Secondary Email', 'wpuf-pro' ),
            'name'             => 'wpuf_secondary_email',
            'meta_key'         => 'wpuf_secondary_email',
            'is_meta'          => 'yes',
            'help'             => '',
            'css'              => '',
            'placeholder'      => __( 'example@domain.com', 'wpuf-pro' ),
            'default'          => '',
            'size'             => 40,
            'id'               => 0,
            'is_new'           => true,
            'show_in_post'     => 'yes',
            'hide_field_label' => 'no',
            'read_only'        => 'no',
        ];

        return array_merge( $defaults, $props );
    }

    /**
     * Prepare entry
     *
     * @since 4.2.10
     *
     * @param array $field
     *
     * @return string
     */
    public function prepare_entry( $field ) {
        check_ajax_referer( 'wpuf_form_add' );

        $value = ! empty( $_POST[ $field['name'] ] )
            ? sanitize_email( wp_unslash( $_POST[ $field['name'] ] ) )
            : '';

        return $value;
    }

    /**
     * Render field data for display
     *
     * @since 4.2.10
     *
     * @param mixed $data
     * @param array $field
     *
     * @return string
     */
    public function render_field_data( $data, $field ) {
        $data       = is_array( $data ) ? implode( ',', $data ) : $data;
        $hide_label = isset( $field['hide_field_label'] )
            ? wpuf_validate_boolean( $field['hide_field_label'] )
            : false;

        if ( empty( $data ) ) {
            return '';
        }

        $container_classnames = [ 'wpuf-field-data', 'wpuf-field-data-' . $this->input_type ];

        ob_start();
        ?>
        <li class="<?php echo esc_attr( implode( ' ', $container_classnames ) ); ?>">
            <?php if ( ! $hide_label ) : ?>
                <label><?php echo esc_html( $field['label'] ); ?>:</label>
            <?php endif; ?>
            <?php
            echo wp_kses(
                make_clickable( sanitize_email( $data ) ),
                [
                    'a' => [
                        'href' => [],
                    ],
                ]
            );
            ?>
        </li>
        <?php
        return ob_get_clean();
    }
}
