<?php

// add_filter( 'adfoin_form_providers', 'adfoin_weforms_add_provider' );
// function adfoin_weforms_add_provider( $providers ) {
//     if ( is_plugin_active( 'weforms/weforms.php' ) ) {
//         $providers['weforms'] = __( 'weForms', 'advanced-form-integration' );
//     }
//     return $providers;
// }
function adfoin_weforms_get_forms(  $form_provider  ) {
    if ( $form_provider != 'weforms' ) {
        return;
    }
    $forms = weforms()->form->get_forms();
    $filtered = wp_list_pluck( $forms['forms'], 'name', 'id' );
    return $filtered;
}

function adfoin_weforms_get_form_fields(  $form_provider, $form_id  ) {
    if ( $form_provider != 'weforms' ) {
        return;
    }
    $form = weforms()->form->get( $form_id );
    $form_fields = $form->get_fields();
    $fields = array();
    foreach ( $form_fields as $single_field ) {
        if ( adfoin_fs()->is_not_paying() ) {
            if ( 'name_field' == $single_field['template'] ) {
                $fields[$single_field['name']] = $single_field['label'];
                if ( 'first-middle-last' == $single_field['format'] ) {
                    $fields[$single_field['name'] . '_first_name'] = $single_field['name'] . ' First Name';
                    $fields[$single_field['name'] . '_middle_name'] = $single_field['name'] . ' Middle Name';
                    $fields[$single_field['name'] . '_last_name'] = $single_field['name'] . ' Last Name';
                }
                if ( 'first-last' == $single_field['format'] ) {
                    $fields[$single_field['name'] . '_first_name'] = $single_field['name'] . ' First Name';
                    $fields[$single_field['name'] . '_last_name'] = $single_field['name'] . ' Last Name';
                }
            }
            if ( 'email_address' == $single_field['template'] ) {
                $fields[$single_field['name']] = $single_field['label'];
            }
        }
    }
    $fields['form_id'] = __( 'Form ID', 'advanced-form-integration' );
    $fields['entry_id'] = __( 'Entry ID', 'advanced-form-integration' );
    $special_tags = adfoin_get_special_tags();
    if ( is_array( $fields ) && is_array( $special_tags ) ) {
        $fields = $fields + $special_tags;
    }
    return $fields;
}

function adfoin_weforms_get_form_name(  $form_provider, $form_id  ) {
    if ( $form_provider != 'weforms' ) {
        return;
    }
    $form = weforms()->form->get( $form_id );
    $form_name = $form->get_name();
    return $form_name;
}

add_action(
    'weforms_entry_submission',
    'adfoin_weforms_submission',
    10,
    2
);
function adfoin_weforms_submission(  $entry_id, $form_id  ) {
    global $wpdb, $post;
    $integration = new Advanced_Form_Integration_Integration();
    $saved_records = $integration->get_by_trigger( 'weforms', $form_id );
    if ( empty( $saved_records ) ) {
        return;
    }
    $entry = weforms_get_entry_data( $entry_id );
    $posted_data = $entry['data'];
    foreach ( $posted_data as $key => $value ) {
        $field_type = $entry['fields'][$key]['type'];
        if ( 'name_field' == $field_type ) {
            $name_values = explode( '|', $value );
            if ( count( $name_values ) == 2 ) {
                $name_organized = array(
                    $key . '_first_name' => trim( $name_values[0] ),
                    $key . '_last_name'  => trim( $name_values[1] ),
                );
            }
            if ( count( $name_values ) == 3 ) {
                $name_organized = array(
                    $key . '_first_name'  => trim( $name_values[0] ),
                    $key . '_middle_name' => trim( $name_values[1] ),
                    $key . '_last_name'   => trim( $name_values[2] ),
                );
            }
            $posted_data = $posted_data + $name_organized;
        }
    }
    $posted_data['submission_date'] = date( 'Y-m-d H:i:s' );
    $posted_data['user_ip'] = adfoin_get_user_ip();
    if ( !is_object( $post ) ) {
        $post_id = url_to_postid( wp_get_referer() );
        if ( $post_id ) {
            $post = get_post( $post_id, 'OBJECT' );
        }
    }
    $special_tag_values = adfoin_get_special_tags_values( $post );
    if ( is_array( $posted_data ) && is_array( $special_tag_values ) ) {
        $posted_data = $posted_data + $special_tag_values;
    }
    $posted_data['form_id'] = $form_id;
    $posted_data['entry_id'] = ( isset( $entry_id ) ? $entry_id : '' );
    adfoin_dispatch_integrations( $saved_records, $posted_data );
}

if ( adfoin_fs()->is_not_paying() ) {
    add_action( 'adfoin_trigger_extra_fields', 'adfoin_weforms_trigger_fields' );
}
function adfoin_weforms_trigger_fields() {
    ?>
    <div class="afi-upgrade-notice" v-if="trigger.formProviderId == 'weforms' && trigger.formId">
        <span class="dashicons dashicons-info-outline" aria-hidden="true"></span>
        <p><?php 
    esc_html_e( 'The basic AFI plugin supports name and email fields only.', 'advanced-form-integration' );
    ?></p>
    </div>
    <?php 
}
