//
        // GET: /Navigation/Create

        public ActionResult Create()
        {
            ViewData["ExtraData"] = new NavigationDataView()
            { 
                Categories = CustomSelectList.CreateListCategories(true),
                NavigationPositions = CustomSelectList.CreateMenuPosition(),
                RootNavigations = CustomSelectList.CreateListNavigations(true),
                SiteModules = CustomSelectList.CreateModuleList(),
                OrphanArticles = CustomSelectList.CreateListOphanArticles(),
                Departments = CustomSelectList.CreateDepartments()
            };
            return View(new Models.Navigation());
        }
        //
        // GET: /Navigation/Edit/5
 
        public ActionResult Edit(int id)
        {
            Navigation nav = new NavigationService().GetItem(id);
            ViewData["ExtraData"] = new NavigationDataView()
            {
                Categories = CustomSelectList.CreateListCategories(true, nav.CategoryId),
                NavigationPositions = CustomSelectList.CreateMenuPosition(nav.Position),
                RootNavigations = CustomSelectList.CreateListNavigations(true, nav.ParentId),
                SiteModules = CustomSelectList.CreateModuleList(nav.Component),
                OrphanArticles = CustomSelectList.CreateListOphanArticles(),
                Departments = CustomSelectList.CreateDepartments()
                
            };
            
            return View(nav);
        }
        public ActionResult Edit(int id, Navigation navigation, FormCollection collection)
        {
            try
            {

                if (!ModelState.IsValid)
                {
                    ViewData["ExtraData"] = new NavigationDataView()
                    {
                        Categories = CustomSelectList.CreateListCategories(true, navigation.CategoryId),
                        NavigationPositions = CustomSelectList.CreateMenuPosition(navigation.Position),
                        RootNavigations = CustomSelectList.CreateListNavigations(true, navigation.ParentId),
                        SiteModules = CustomSelectList.CreateModuleList(navigation.Component)
                    };
                    return View(navigation);
                }
                if (collection["Component"] == SiteModules.Post.ToString())
                {
                    if (navigation.ContentId.HasValue)
                    {
                        navigation.Controller = DBNL.App.Models.Statics.Controllers.Article.ToString();
                        navigation.Action = DBNL.App.Models.Statics.Actions.View.ToString();
                        navigation.Area = "";

                    }
                }

                if (collection["Component"] == SiteModules.Article.ToString())
                {
                    if (navigation.CategoryId.HasValue)
                    {
                        navigation.Controller = DBNL.App.Models.Statics.Controllers.Article.ToString();
                        navigation.Action = DBNL.App.Models.Statics.Actions.Category.ToString();
                        navigation.Area = "";

                    }
                }
                if (collection["Component"] == SiteModules.Home.ToString())
                {
                    navigation.Controller = DBNL.App.Models.Statics.Controllers.Home.ToString();
                    navigation.Action = DBNL.App.Models.Statics.Actions.Index.ToString();
                    //navigation.Area = "";
                }
                if (collection["Component"] == SiteModules.WebLink.ToString())
                {
                    navigation.Controller = DBNL.App.Models.Statics.Controllers.WebLink.ToString();
                    navigation.Action = DBNL.App.Models.Statics.Actions.Index.ToString();
                    //navigation.Area = "";
                }


                if (collection["Component"] == SiteModules.WebContact.ToString())
                {
                    navigation.Controller = DBNL.App.Models.Statics.Controllers.WebContact.ToString();
                    navigation.Action = DBNL.App.Models.Statics.Actions.Index.ToString();
                    //navigation.Area = "";
                }

                

                new NavigationService().UpdateMenuItem(navigation);
 
                return RedirectToAction("Index");
            }
            catch
            {
                ViewData["ExtraData"] = new NavigationDataView()
                {
                    Categories = CustomSelectList.CreateListCategories(true, navigation.CategoryId),
                    NavigationPositions = CustomSelectList.CreateMenuPosition(navigation.Position),
                    RootNavigations = CustomSelectList.CreateListNavigations(true, navigation.ParentId),
                    SiteModules = CustomSelectList.CreateModuleList(navigation.Component),
                    OrphanArticles = CustomSelectList.CreateListOphanArticles()
                };
                return View();
            }
        }
        public ActionResult Create(FormCollection collection, Navigation navigation)
        {
            try
            {
                navigation.Status= EntityStatuses.Actived.ToString();

                if (!ModelState.IsValid)
                {
                    ViewData["ExtraData"] = new NavigationDataView()
                    {
                        Categories = CustomSelectList.CreateListCategories(true),
                        NavigationPositions = CustomSelectList.CreateMenuPosition(),
                        RootNavigations = CustomSelectList.CreateListNavigations(true),
                        SiteModules = CustomSelectList.CreateModuleList(),
                        OrphanArticles = CustomSelectList.CreateListOphanArticles(),
                        Departments = CustomSelectList.CreateDepartments()
                    };

                    return View(navigation);
                }
                if (collection["Component"] == SiteModules.Post.ToString())
                {
                    if (navigation.ContentId.HasValue)
                    {
                        navigation.Controller = DBNL.App.Models.Statics.Controllers.Article.ToString();
                        navigation.Action = DBNL.App.Models.Statics.Actions.View.ToString();
                        navigation.Area = "";

                    }
                }

                if(collection["Component"] == SiteModules.Article.ToString()) 
                {
                    if (navigation.CategoryId.HasValue)
                    {
                        navigation.ContentId = null;
                        navigation.Controller = DBNL.App.Models.Statics.Controllers.Article.ToString();
                        navigation.Action = DBNL.App.Models.Statics.Actions.Category.ToString();
                        navigation.Area = "";

                    }
                }

                if (collection["Component"] == SiteModules.WebLink.ToString())
                {
                        navigation.Controller = DBNL.App.Models.Statics.Controllers.WebLink.ToString();
                        navigation.Action = DBNL.App.Models.Statics.Actions.Index.ToString();
                        //navigation.Area = "";
                }
                if (collection["Component"] == SiteModules.Home.ToString())
                {
                    navigation.Controller = DBNL.App.Models.Statics.Controllers.Home.ToString();
                    navigation.Action = DBNL.App.Models.Statics.Actions.Index.ToString();
                    //navigation.Area = "";
                }

                if (collection["Component"] == SiteModules.WebContact.ToString())
                {
                    navigation.Controller = DBNL.App.Models.Statics.Controllers.WebContact.ToString();
                    navigation.Action = DBNL.App.Models.Statics.Actions.Index.ToString();
                    navigation.CustomData = collection["DepartmentId"];
                    //navigation.Area = "";
                }

                if (collection["Component"] == SiteModules.Url.ToString())
                {
                    //navigation.Controller = DBNL.App.Models.Statics.Controllers.WebContact.ToString();
                    //navigation.Action = DBNL.App.Models.Statics.Actions.Index.ToString();
                    //navigation.Area = "";
                    navigation.ExternalUrl = collection["ExternalUrl"];
                }
                
                new NavigationService().Create(navigation);
                return RedirectToAction("Index");
            }
            catch
            {
                throw;
                ViewData.Model = new NavigationDataView()
                {
                    Categories = CustomSelectList.CreateListCategories(true),
                    NavigationPositions = CustomSelectList.CreateMenuPosition(),
                    RootNavigations = CustomSelectList.CreateListNavigations(true),
                    SiteModules = CustomSelectList.CreateModuleList(),
                    OrphanArticles = CustomSelectList.CreateListOphanArticles(),
                    Departments = CustomSelectList.CreateDepartments()
                };

                return View();
            }
        }