/**
 * Prepare all the common parameters for creating the vCita settings.
 *
 * It also initializes the widget for the first time and stores the form data after the user saves the changes
 *
 * @param widget_type - The type of widget to be stored for next usage
 */
function vcita_prepare_widget_settings($type)
{
    $form_uid = rand();
    $uninitialized = false;
    if (empty($_POST)) {
        //Normal page display
        $vcita_widget = (array) get_option(VCITA_WIDGET_KEY);
        $update_made = false;
        // Create a initial parameters - This form wasn't created yet - set to default
        if (!isset($vcita_widget['created']) || empty($vcita_widget['created'])) {
            $vcita_widget = create_initial_parameters();
            $uninitialized = true;
        }
    } else {
        //Form data sent
        $update_made = true;
        if ($_POST["form_type"] == "page_control") {
            $vcita_widget = (array) get_option(VCITA_WIDGET_KEY);
        } else {
            $vcita_widget = (array) save_user_params();
        }
    }
    if ($type == "widget") {
        $config_floating = "";
    } else {
        $config_floating = "float:left;";
    }
    // In case not empty user
    // Generate the user if he isn't available or update to the latest user data from vCita server
    if (!$uninitialized) {
        if (empty($vcita_widget["uid"])) {
            $vcita_widget = (array) generate_or_validate_user($vcita_widget);
        } else {
            //$vcita_widget = (array) vcita_get_user($vcita_widget);
        }
        update_option(VCITA_WIDGET_KEY, $vcita_widget);
    }
    $config_html = "<div style='clear:both;text-align:left;display:block;padding-top:5px;'></div>";
    if (empty($vcita_widget["uid"])) {
        $disabled = "";
        $first_time = true;
    } else {
        $first_time = false;
        $disabled = "disabled=true title='To change your details, ";
        if ($vcita_widget['confirmed']) {
            $customize_link = vcita_create_link('Customize', 'widget_implementations', 'key=' . $vcita_widget['implementation_key'] . '&widget=widget');
            $set_meeting_pref_link = vcita_create_link('Meeting Preferences', 'settings', 'section=meetings');
            $set_profile_link = vcita_create_link('Edit Email/Profile', 'settings', 'section=profile');
            $disabled .= "please use the \"Customize\" link below.'";
            $config_html = "<div style='clear:both;text-align:left;display:block;padding:5px 0 10px 0;overflow:hidden;'>\r\n                            <div style='margin-right:5px;" . $config_floating . "'>" . $set_meeting_pref_link . "</div>\r\n\t\t\t\t\t\t\t<div style='margin-right:5px;" . $config_floating . "'>" . $set_profile_link . "</div>";
            if ($type == "widget") {
                $config_html .= "<div style='margin-right:5px;" . $config_floating . "'>" . $customize_link . "</div>";
            }
            $config_html .= "</div>";
        } else {
            $disabled .= "please follow the instructions emailed to " . $vcita_widget["email"] . "'";
        }
    }
    vcita_embed_clear_names(array("vcita_first-name_" . $form_uid, "vcita_last-name_" . $form_uid));
    return compact('vcita_widget', 'disabled', 'config_html', 'form_uid', 'update_made', 'first_time');
}
/**
 * Create the message which will be displayed to the user after performing an update to the widget settings.
 * The message is created according to if an error had happen and if the user had finished the registration or not.
 */
function vcita_create_user_message($vcita_widget, $update_made)
{
    if (!empty($vcita_widget['uid'])) {
        // If update wasn't made, keep the message without info about the last change
        if ($update_made) {
            if ($_POST['Submit'] == "Save Settings") {
                $message .= "<div>Account <b>" . $vcita_widget['email'] . "</b> Saved.</div><br> ";
            } else {
                $message = "<b>Changes saved</b>";
            }
        } else {
            $message = "";
        }
        $message_type = "updated below-h2";
        // Wordpress classes for showing a notification box
        if (!$vcita_widget['confirmed']) {
            if ($update_made) {
                $message .= "<br>";
            }
            $message .= "<div style='overflow:hidden'>";
            $prefix = "";
            if (!empty($vcita_widget['confirmation_token'])) {
                $message .= "<div style='float:left;'>Please <b>" . vcita_create_link('configure your contact and meeting preferences', 'users/confirmation', 'confirmation_token=' . $vcita_widget['confirmation_token'], array('style' => 'text-decoration:underline;')) . "</b> or </div>";
            } else {
                $prefix = "Please";
            }
            $message .= "<div style='float:left;display:block;'>" . $prefix . "&nbsp;follow instructions sent to your email.</div>";
            if (empty($vcita_widget['confirmation_token'])) {
                $message .= "&nbsp;" . vcita_create_link("Send email again", 'user/send_confirmation', 'email=' . $vcita_widget['email'], array('style' => 'font-weight:bold;'));
            }
            $message .= "</div>";
        }
    } elseif (!empty($vcita_widget['last_error'])) {
        $message = "<b>" . $vcita_widget['last_error'] . "</b>";
        $message_type = "error below-h2";
    }
    if (empty($message)) {
        return "";
    } else {
        return "<div class='" . $message_type . "' style='padding:5px;text-align:left;'>" . $message . "</div>";
    }
}