// GET: User/Lists
        public ActionResult Lists()
        {
            if (Session["UserId"] != null)
            {
                ViewBag.Message = TempData["SuccessMessage"];
                ViewBag.Error   = TempData["ErrorMessage"];

                //TODO: Logic to show all lists of a user
                int userId = int.Parse(Session["UserId"].ToString());

                ShoppingListService listService = new ShoppingListService();
                var listOfLists = listService.GetListsByUserId(userId).OrderByDescending(x => x.TimeStamp).ToList();

                if (listOfLists.Count() == 0)
                {
                    ViewBag.Message = "You don't have any lists yet. Start creating your lists now!";
                }

                ListsVM lists = new ListsVM
                {
                    AllUserLists = listOfLists
                };

                return(View(lists));
            }
            else
            {
                return(RedirectToAction("Login", "User"));
            }
        }
        // GET: User/SingleList
        public ActionResult SingleList(int?id, int?templateId, int?sortingId)
        {
            if (Session["UserId"] != null && id != null)
            {
                ViewBag.Message = TempData["SuccessMessage"];
                ViewBag.Error   = TempData["ErrorMessage"];

                ShoppingListService listService = new ShoppingListService();
                var userLists     = listService.GetListsByUserId(int.Parse(Session["UserId"].ToString()));
                var requestedList = listService.Get((int)id);

                //checking whether the list the user tries to access is his or if he has access rights
                foreach (ShoppingListDto x in userLists)
                {
                    if (x.Id == requestedList.Id)
                    {
                        SingleListVM list = new SingleListVM
                        {
                            ShoppingList_Id = requestedList.Id
                        };

                        var listObj = listService.Get(list.ShoppingList_Id, int.Parse(Session["UserId"].ToString()));

                        list.ListName         = listObj.Name;
                        list.ListAccessTypeId = listObj.ListAccessTypeId;

                        LanguageService languageService = new LanguageService();
                        var             langId          = languageService.GetByCode(Session["LanguageCode"].ToString()).Id;

                        ProductService productService = new ProductService();
                        list.ListEntries = productService.GetEntriesAsProducts(list.ShoppingList_Id, langId);

                        if (list.ListEntries.Count() == 0)
                        {
                            ViewBag.Message = "You don't have any entries yet. Start creating your entries now!";
                        }

                        TemplateSortingService templateService    = new TemplateSortingService();
                        UserListSortingService listSortingService = new UserListSortingService();
                        UserListSortingDto     sorting            = new UserListSortingDto();
                        if (listObj.ChosenSortingId != null)
                        {
                            sorting.Id = (int)listObj.ChosenSortingId;
                        }
                        sorting.ShoppingList_Id = list.ShoppingList_Id;

                        if (listObj.ChosenSortingId != null)
                        {
                            list.ListEntries = listSortingService.ApplyUserSorting((int)listObj.ChosenSortingId, list.ListEntries);
                        }

                        if (templateId != null)
                        {
                            list.ListEntries = templateService.SortByTemplate((int)templateId, list.ListEntries);
                            listSortingService.SaveSorting(sorting, list.ListEntries);
                            ViewBag.Message = "Your list has been sorted according to the template. This sorting has been saved permanently for you.";
                        }
                        else if (sortingId != null)
                        {
                            switch ((int)sortingId)
                            {
                            case 1:     // A - Z
                            {
                                list.ListEntries = list.ListEntries.OrderBy(z => z.Name).ToList();
                                listSortingService.SaveSorting(sorting, list.ListEntries);
                                break;
                            }

                            case 2:     // Z - A
                            {
                                list.ListEntries = list.ListEntries.OrderByDescending(z => z.Name).ToList();
                                listSortingService.SaveSorting(sorting, list.ListEntries);
                                break;
                            }

                            case 3:     // lowest price
                            {
                                list.ListEntries = list.ListEntries.OrderBy(z => z.Price).ToList();
                                listSortingService.SaveSorting(sorting, list.ListEntries);
                                break;
                            }

                            case 4:     // highest price
                            {
                                list.ListEntries = list.ListEntries.OrderByDescending(z => z.Price).ToList();
                                listSortingService.SaveSorting(sorting, list.ListEntries);
                                break;
                            }

                            default: break;
                            }
                        }

                        list.ChosenProductId    = 0; //By default no defaultProduct should be selected
                        list.ChooseProductsList = (from item in productService.GetDefaultAndReusableProductsByLanguage(langId)
                                                   select new SelectListItem()
                        {
                            Text = item.Name,
                            Value = item.ProductId.ToString()
                        }).ToList();

                        UnitTypeService unitTypeService = new UnitTypeService();
                        list.UnitTypesListId = 8; //default value: pc.
                        list.UnitTypesList   = (from item in unitTypeService.GetUnitTypesByLanguage(langId)
                                                select new SelectListItem()
                        {
                            Text = item.Name,
                            Value = item.Id.ToString()
                        }).ToList();

                        CurrencyService currencyService = new CurrencyService();
                        list.CurrencyListId = 5; //default DKK
                        list.CurrencyList   = (from item in currencyService.GetAll()
                                               select new SelectListItem()
                        {
                            Text = item.Code,
                            Value = item.Id.ToString()
                        }).ToList();

                        CategoryService categoryService = new CategoryService();
                        list.CategoryListId = 20; //default category: others
                        list.CategoryList   = (from item in categoryService.GetAllCategories(langId, int.Parse(Session["UserId"].ToString()))
                                               select new SelectListItem()
                        {
                            Text = item.Name,
                            Value = item.Id.ToString()
                        }).ToList();

                        list.ChosenTemplateId = 0;
                        list.Templates        = (from item in templateService.GetTemplates()
                                                 select new SelectListItem()
                        {
                            Text = item.TemplateName,
                            Value = item.Id.ToString()
                        }).ToList();

                        return(View(list));
                    }
                }

                TempData["ErrorMessage"] = "The list you tried to access isn't yours!!";
                return(RedirectToAction("Lists", "List"));
            }
            else
            {
                return(RedirectToAction("Login", "User"));
            }
        }