public ObservableCollection<UnitOfMeasureViewModel> GetUnitsOfMeasure()
        {
            _unitsOfMeasure = new ObservableCollection<UnitOfMeasureViewModel>();
            using (var db = new SQLite.SQLiteConnection(App.DBPath))
            {
                var query = db.Table<UnitOfMeasure>().OrderBy(c => c.Id);
                foreach (var _unitOfMeasure in query)
                {
                    var unitOfMeasure = new UnitOfMeasureViewModel()
                    {
                        Id = _unitOfMeasure.Id,
                        Abbreviation = _unitOfMeasure.Abbreviation,
                        UnitName = _unitOfMeasure.UnitName
                    };

                    _unitsOfMeasure.Add(unitOfMeasure);
                }
            }

            return _unitsOfMeasure;
        }
        private void IngredientsListView_ItemClick(object sender, ItemClickEventArgs e)
        {
            IngredientViewModel ingredient = (IngredientViewModel)e.ClickedItem;

            if (_selectedIngredientsList.Contains(ingredient))
            {
                _selectedIngredientsList.Remove(ingredient);
                currentSelectedIngredientIndex = -1;
                IngredientAmount.Visibility = Visibility.Collapsed;
                IngredientAmountTextBlock.Visibility = Visibility.Collapsed;
            }
            else
            {
                UnitOfMeasureViewModel uom = new UnitOfMeasureViewModel();
                switch (ingredient.UnitOfMeasure)
                {
                    case 1:
                        {
                            IngredientAmountTextBlock.Text = uom.GetUnitOfMeasure(1).UnitName;
                            
                            break;
                        }
                    case 2:
                        {
                            IngredientAmountTextBlock.Text = uom.GetUnitOfMeasure(2).UnitName;
                            break;
                        }
                    case 3:
                        {
                            IngredientAmountTextBlock.Text = uom.GetUnitOfMeasure(3).UnitName;
                            break;
                        }
                }
                IngredientAmount.Text = ingredient.Weight.ToString();
                IngredientAmount.Visibility = Visibility.Visible;
                IngredientAmountTextBlock.Visibility = Visibility.Visible;
                _selectedIngredientsList.Add(ingredient);
                currentSelectedIngredientIndex = _selectedIngredientsList.IndexOf(ingredient);
            }
        }
        private void MealItemsGridView_ItemClick(object sender, ItemClickEventArgs e)
        {
            currentSelectedMealItem = (MealItemViewModel)e.ClickedItem;
            WeightTextBlock.Visibility = Visibility.Visible;
            WeightTextBox.Visibility = Visibility.Visible;

            UnitOfMeasureViewModel unitOfMeasure = new UnitOfMeasureViewModel();
            string unitOfMeasureName = unitOfMeasure.GetUnitOfMeasure(currentSelectedMealItem.TotalAmountUnitOfMeasure).UnitName;
            WeightTextBlock.Text = "Menge in " + unitOfMeasureName;

            string mealItemWeight = "";
            if (_meal.MealItemIDsWithWeight[currentSelectedMealItem.Id] > 0)
            {
                mealItemWeight = this._meal.MealItemIDsWithWeight[currentSelectedMealItem.Id].ToString();
            }
            else
            {
                float mealItemWeightDefault = currentSelectedMealItem.TotalAmount;
                int numberGuestes = Int32.Parse(NumberOfGuestsTextBox.Text);
                float totalWeight = mealItemWeightDefault * numberGuestes;
                mealItemWeight = totalWeight.ToString();
            }

            WeightTextBox.Text = mealItemWeight.Replace('.', ',');
        }
        public UnitOfMeasureViewModel GetUnitOfMeasure(int uomId)
        {
            var unitOfMeasure = new UnitOfMeasureViewModel();
            using (var db = new SQLite.SQLiteConnection(App.DBPath))
            {
                try
                {
                    var _uom = (db.Table<UnitOfMeasure>().Where(c => c.Id == uomId)).Single();
                    unitOfMeasure.Id = _uom.Id;
                    unitOfMeasure.UnitName = _uom.UnitName;
                    unitOfMeasure.Abbreviation = _uom.Abbreviation;
                }
                catch (Exception e)
                {
                    string errMessage = e.Message;
                }

            }
            return unitOfMeasure;
        }
        private async void ImportMealItemsData(string fileContent, string mealItemCategory)
        {
            string line = "";
            string[] mealItemsRaw = fileContent.Split('|');
            char[] trimChar = {'\n','\r'};
            bool mealItemExists = false;

            foreach(var mealItemRaw in mealItemsRaw)
            {
                if (mealItemRaw.ToString().Length == 0) { continue; }
                string mealItemInfo = mealItemRaw.TrimStart(trimChar);
                using (System.IO.StringReader stringReader = new System.IO.StringReader(mealItemInfo))
                {
                    int lineNo = 0;
                    string mealItemName = "";
                    float grossWeight = 0;
                    float divisionFactor = 0;
                    string grossWeightUnitOfMeasure = "";
                    List<string> rawIngredients = new List<string>();

                    while ((line = await stringReader.ReadLineAsync()) != null)
                    {
                        // At 0 this should be the name
                        if (lineNo == 0)
                        {
                            mealItemName = CleanMealItemName(line);
                            MealItemViewModel mealItemView = new MealItemViewModel();
                            // In case the meal item exists we move on to the next
                            if (mealItemView.MealItemExists(mealItemName) == true) { mealItemExists = true; break; }
                            lineNo++;
                            continue;
                        }

                        if (line.Contains("Personen"))
                        {
                            string _grossWeight;
                            string _divisionFactor;
                            GetGrossWeightAndDivisionFactor(line, out _grossWeight, 
                                out grossWeightUnitOfMeasure, out _divisionFactor);
                            _divisionFactor = _divisionFactor.Replace(',', '.');
                            _grossWeight = _grossWeight.Replace(',', '.');
                            divisionFactor = float.Parse(_divisionFactor, CultureInfo.InvariantCulture.NumberFormat);
                            grossWeight = float.Parse(_grossWeight, CultureInfo.InvariantCulture.NumberFormat) / divisionFactor;
                            
                            lineNo++;
                            continue;
                        }

                        rawIngredients = InterpretIngredientDefinition(line);
                        lineNo++;
                    }

                    // Let's see if there was an inner break
                    if (mealItemExists) { mealItemExists = false; continue; }

                    // if go this far it means a new meal item
                    MealItemViewModel mealItem = new MealItemViewModel();
                    IngredientViewModel ingredient = null;
                    foreach (var rawIngredient in rawIngredients)
                    {
                        string amount;
                        string unitOfMeasure;
                        ingredient = GetIngredientDetails(rawIngredient, out amount, out unitOfMeasure);

                        if (ingredient != null)
                        {
                            UnitOfMeasureViewModel unitOfMeasureModel = new UnitOfMeasureViewModel();
                            unitOfMeasureModel = unitOfMeasureModel.GetUnitOfMeasure(ingredient.UnitOfMeasure);
                            amount = amount.Replace(',', '.');
                            float floatAmount = float.Parse(amount, CultureInfo.InvariantCulture.NumberFormat);

                            // Recalculating kilogramms and liters
                            if ((unitOfMeasure.Equals("kg", StringComparison.CurrentCultureIgnoreCase)) ||
                                    (unitOfMeasure.Equals("Ltr", StringComparison.CurrentCultureIgnoreCase)))
                                floatAmount = floatAmount * 1000;
                            // Devive by the number of people all amounts were calculated for
                            floatAmount = floatAmount / divisionFactor;
                            mealItem.IngredientIDsWithTotalAmount.Add(ingredient.Id, floatAmount);
                        }
                    }

                    mealItem.Name = mealItemName;
                    if (grossWeightUnitOfMeasure.Equals("Ltr", StringComparison.CurrentCultureIgnoreCase) ||
                        grossWeightUnitOfMeasure.Equals("kg", StringComparison.CurrentCultureIgnoreCase))
                        grossWeight = grossWeight * 1000;

                    if (grossWeightUnitOfMeasure.Equals("kg", StringComparison.CurrentCultureIgnoreCase) ||
                        grossWeightUnitOfMeasure.Equals("g", StringComparison.CurrentCultureIgnoreCase))
                        mealItem.TotalAmountUnitOfMeasure = 1;

                    if (grossWeightUnitOfMeasure.Equals("Ltr", StringComparison.CurrentCultureIgnoreCase) ||
                        grossWeightUnitOfMeasure.Equals("ml", StringComparison.CurrentCultureIgnoreCase))
                        mealItem.TotalAmountUnitOfMeasure = 2;

                    mealItem.TotalAmount = grossWeight;
                    FoodCategoryViewModel mealItemCategoryModel = new FoodCategoryViewModel();
                    mealItemCategoryModel = mealItemCategoryModel.GetFoodCategoryByName(mealItemCategory);
                    if (mealItemCategoryModel != null) { mealItem.CategoryId = mealItemCategoryModel.Id; }
                    _mealItems.Add(mealItem);
                     
                }
            }
        }
        private string GetIngredientsAsText(IngredientsViewModel ingredientsView, 
            MealItemViewModel mealItem, Dictionary<float,float> mealItemIDsWithWeight)
        {
            StringBuilder sb = new StringBuilder();
            ObservableCollection<IngredientViewModel> ingredients =
                    ingredientsView.GetIngredients(mealItem.IngredientIDsWithTotalAmount);

            if (ingredients.Count > 0)
            {
                //sb.Append(@"\b ");
                int loopCount = 0;
                UnitOfMeasureViewModel unitOfMeasure = new UnitOfMeasureViewModel();
                MealItemViewModel defaultMealItem = new MealItemViewModel();
                defaultMealItem = defaultMealItem.GetMealItemById(mealItem.Id);

                foreach (var ingredient in ingredients)
                {
                    float ingredientDefaultWeight = mealItem.IngredientIDsWithTotalAmount[ingredient.Id];
                    if (defaultMealItem.TotalAmount == 0) defaultMealItem.TotalAmount = 1;
                    float ingredientWeight = (ingredientDefaultWeight / defaultMealItem.TotalAmount) * mealItemIDsWithWeight[mealItem.Id];
                    decimal roundedIngredientWeight = Math.Round((decimal)ingredientWeight, 0);
                    string uomName = unitOfMeasure.GetUnitOfMeasure(ingredient.UnitOfMeasure).Abbreviation;
                    sb.Append(roundedIngredientWeight.ToString());
                    sb.Append(uomName).Append(" ");
                    sb.Append(ingredient.Name);

                    if ((ingredients.Count >1) && (loopCount < (ingredients.Count-1)))
                    { sb.Append(", "); }

                    loopCount++;
                }

                //sb.Append(@"\line\b0");
                sb.Append(@"\line");
                return sb.ToString();
            }
            else
                return "";
        }
        public string GetTextRepresentation(IMealViewModel meal)
        {
            StringBuilder result = new StringBuilder();
            MealItemsViewModel mealItemsView = new MealItemsViewModel();
            IngredientsViewModel ingredientsView = new IngredientsViewModel();
            ContactViewModel contact = new ContactViewModel();
            contact = contact.GetContact(meal.ContactId);
            string tmp = contact.NameAndAddress;
            tmp = tmp.Replace('\n', '|');
            tmp = tmp.Replace('\r', '|');
            string nameAndAddress = tmp.Replace("||", "\\line ");
            nameAndAddress = contact.Attention + "\\line " + tmp.Replace("||", "\\line ");

            result.Append(@"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang1033{\fonttbl{\f0\fnil\fcharset0 Calibri;}{\f1\fnil\fcharset0 Calibri;}}");
            result.Append(@"{\*\generator Riched20 10.0.10240}\viewkind4\uc1\pard\tx720\cf1\f0\fs22\lang1031\line\line\line\f1\fs26");
            result.Append(" Lieferschein Nr. ").Append(meal.DeliveryNoteId).Append(@"\line \line "); ;
            result.Append(nameAndAddress).Append(@" \line ");
            result.Append("Tel. Nr. : ").Append(contact.PhoneNr).Append(@" \line ");
            result.Append("Handy Nr. : ").Append(contact.CellPhoneNr).Append(@" \line \line ");
            result.Append("Veranstaltungsort:").Append(@" \line ");
            result.Append(meal.DeliveryLocation).Append(@" \line \line ");
            result.Append("Ihr Ansprechpartner: ").Append(@" \line \line ");
            result.Append(_dateTimeConverter.Convert(meal.DeliveryDate, null, null, "")).Append("  ");
            result.Append(_timeSpanConverter.Convert(meal.DeliveryTime,null,null,"")).Append("  Uhr Buffetbeginn").Append(@"\line \line \line");
            result.Append(@" \highlight2 ");
            result.Append("Erwachsene: ").Append(meal.NumberOfGuests).Append(@"\line \line ");
            result.Append(@" \highlight0 ");

            ObservableCollection<MealItemViewModel> _mealItems =
                mealItemsView.GetMealItems(meal.MealItemIDsWithWeight);

            foreach (var mealItem in _mealItems)
            {
                float mealItemWeight = meal.MealItemIDsWithWeight[mealItem.Id];
                result.Append(@"\b ");
                result.Append(mealItem.Name.ToString()).Append("   ");

                UnitOfMeasureViewModel unitOfMeasure = new UnitOfMeasureViewModel();
                string unitOfMeasureName = unitOfMeasure.GetUnitOfMeasure(mealItem.TotalAmountUnitOfMeasure).UnitName;
                string unitOfMeasureAbbreviation = unitOfMeasure.GetUnitOfMeasure(mealItem.TotalAmountUnitOfMeasure).Abbreviation;

                result.Append(mealItemWeight.ToString()).Append("").Append(unitOfMeasureAbbreviation);
                result.Append(@"\line\b0 ");
                result.Append(GetIngredientsAsText(ingredientsView, mealItem, meal.MealItemIDsWithWeight));
                result.Append(@" \line ");
            }

            result.Append(@" \line }");

            return result.ToString();
        }