/**
 * 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');
}
function vcita_add_calendar($atts)
{
    $vcita_widget = (array) get_option(VCITA_WIDGET_KEY);
    extract(shortcode_atts(array('type' => 'scheduler', 'email' => '', 'first_name' => '', 'last_name' => '', 'uid' => $vcita_widget['uid'], 'id' => '', 'title' => '', 'width' => '500px', 'height' => '450px'), $atts));
    // If user isn't available - try and create one.
    if (!empty($email)) {
        $vcita_widget['email'] = $email;
        $vcita_widget['first_name'] = $first_name;
        $vcita_widget['last_name'] = $last_name;
        $vcita_widget['uid'] = '';
        $vcita_widget = generate_or_validate_user($vcita_widget);
        // Don't save the user as the widget user - just use it
        $id = $vcita_widget["uid"];
    } else {
        if (empty($id)) {
            if (empty($uid)) {
                $id = vcita_get_uid();
            } else {
                $id = $uid;
            }
        }
    }
    return vcita_create_embed_code($type, $id, $width, $height);
}