/// <summary>
        /// Creates a new item in the specified category
        /// </summary>
        /// <param name="profileId"></param>
        /// <param name="categoryId"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        public static async Task<SuccessResponse> SaveItemAsync(int categoryId, ApiItem item)
        {
            using (var client = new HttpClient())
            {

                // Authorize the request.
                await AuthorizeRequest(client);
                // Add passphrase to message
                AddPassphraseHeader(client);
                // Create content to send
                var content = JsonContent(item);

                // Execute request
                HttpResponseMessage response;

                if (item.Id == 0)
                {
                    response = await client.PostAsync(GetUrl($"api/profiles/{CurrentProfile.Id}/categories/{categoryId}/items"), content);
                }
                else
                {
                    response = await client.PutAsync(GetUrl($"api/profiles/{CurrentProfile.Id}/categories/{categoryId}/items/{item.Id}"), content);
                }

                // Check if the response is a success code
                if (response.IsSuccessStatusCode)
                {
                    // Get the response content
                    var data = await response.Content.ReadAsStringAsync();
                    // Create SuccessResponse object
                    return new SuccessResponse(Convert.ToInt32(data));
                }
                else
                {
                    throw await CreateException(response);
                }


            }

        }
        private async void AddItemCommandHandler(object p)
        {
            // Prompt to create the new item
            var dialog = new AddItemDialog();

            // Show the dialog and wait for a response
            var result = await dialog.ShowAsync();

            if (result == ContentDialogResult.Primary)
            {

                try
                {
                    // Get the category
                    var category = p as ApiCategory;

                    // Create an item
                    var item = new ApiItem()
                    {
                        Name = dialog.ItemName
                    };

                    // Save the item to the api
                    var r = await KryptPadApi.SaveItemAsync(category.Id, item);

                    // Set the item
                    item.Id = r.Id;

                    // If a template was selected, create a couple of fields to start with
                    if (dialog.SelectedItemTemplate != null)
                    {
                        var templateFields = dialog.SelectedItemTemplate.Fields;

                        // A template was selected, add all the fields from the template
                        foreach (var templateField in templateFields)
                        {
                            // Create field
                            var field = new ApiField()
                            {
                                Name = templateField.Name,
                                FieldType = templateField.FieldType
                            };

                            // Send to api
                            await KryptPadApi.SaveFieldAsync(category.Id, item.Id, field);
                        }
                    }


                    // Navigate to item edit page
                    NavigationHelper.Navigate(typeof(NewItemPage), new EditItemPageParams()
                    {
                        Category = category,
                        Item = item
                    });

                }
                catch (WebException ex)
                {
                    // Something went wrong in the api
                    await DialogHelper.ShowMessageDialogAsync(ex.Message);
                }
                catch (Exception)
                {
                    // Failed
                    await DialogHelper.ShowConnectionErrorMessageDialog();
                }

            }
        }
        /// <summary>
        /// Loads an item into the view model
        /// </summary>
        /// <param name="item"></param>
        public async Task LoadItemAsync(ApiItem selectedItem, ApiCategory category)
        {

            // Prevent change triggers
            _isLoading = true;
            IsBusy = true;

            try
            {
                // Get list of categories for the combobox control
                var categories = await KryptPadApi.GetCategoriesAsync();

                // Set the category view source for the combobox
                CategoriesView.Source = categories.Categories;

                // Update view
                OnPropertyChanged(nameof(CategoriesView));

                // Set the selected category in the list
                Category = (from c in categories.Categories
                            where c.Id == category.Id
                            select c).SingleOrDefault();

                // Check to make sure our parameters are set
                if (Category == null)
                {
                    // Show error
                    throw new WarningException("The item you are trying to edit does not exist in this category.");
                }

                // Get the item
                var itemResp = await KryptPadApi.GetItemAsync(Category.Id, selectedItem.Id);

                // Get the item
                var item = itemResp.Items.FirstOrDefault();

                // Set item
                Item = item;

                // Set properties
                ItemName = item.Name;
                Notes = item.Notes;
                SelectedColor = item.Background;

                // Set fields
                foreach (var field in item.Fields)
                {
                    // Add field to the list
                    AddFieldToCollection(new FieldModel(field));
                }


            }
            catch (WarningException ex)
            {
                // Operation failed
                await DialogHelper.ShowMessageDialogAsync(ex.Message);
            }
            catch (WebException ex)
            {
                // Operation failed
                await DialogHelper.ShowMessageDialogAsync(ex.Message);
                // Navigate back to the items page
                NavigationHelper.Navigate(typeof(ItemsPage), null);
            }
            catch (Exception)
            {
                // Failed
                await DialogHelper.ShowConnectionErrorMessageDialog();
                // Navigate back to the items page
                NavigationHelper.Navigate(typeof(ItemsPage), null);
            }

            _isLoading = false;
            IsBusy = false;
        }