public async System.Threading.Tasks.Task <ActionResult> Add(Models.ScenarioDetailVM model)
        {
            IScenarioItemService scenarioItemService = DependencyUtils.Resolve <IScenarioItemService>();

            //TrinhNNP
            if (ModelState.IsValid)
            {
                /*Add scenario*/
                var user     = Helper.GetCurrentUser();
                var scenario = new Data.Models.Entities.Scenario
                {
                    Title          = model.Title,
                    Description    = model.Description,
                    LayoutID       = model.LayoutId,
                    BrandID        = user.BrandID,
                    isPublic       = model.IsPublic,
                    AudioArea      = model.AudioArea ?? -1,
                    UpdateDateTime = DateTime.Now,
                };
                await this.scenarioService.CreateAsync(scenario);

                /*Add scenario items*/
                if (model.PlaylistAreaArr != null)
                {
                    foreach (var item in model.PlaylistAreaArr)
                    {
                        var i = 0;
                        if (item.PlaylistIds != null)
                        {
                            foreach (var playlist in item.PlaylistIds)
                            {
                                var scenarioItem = new Data.Models.Entities.ScenarioItem
                                {
                                    AreaID       = item.AreaId,
                                    PlaylistID   = playlist,
                                    DisplayOrder = i++,
                                    ScenarioID   = scenario.ScenarioID,
                                    LayoutID     = scenario.LayoutID,
                                };
                                await scenarioItemService.CreateAsync(scenarioItem);

                                Session.Clear();
                                Session["ADD_RESULT"] = true;
                            }
                        }
                    }
                }
                return(Json(new
                {
                    success = true,
                    url = "/Scenario/Index",
                }, JsonRequestBehavior.AllowGet));
            }
            return(Json(new
            {
                success = false,
            }, JsonRequestBehavior.AllowGet));
        }
 public JsonResult CheckPlaylistIdIsUsed(int id)
 {
     try
     {
         IScenarioItemService scenarioItemService = DependencyUtils.Resolve <IScenarioItemService>();
         IScenarioService     scenarioService     = DependencyUtils.Resolve <IScenarioService>();
         var scenarioItem    = scenarioItemService.Get().ToList();
         var scenario        = scenarioService.Get().ToList();
         var scenarioItemVMs = new List <Models.ScenarioItemVM>();
         var scenarioVMs     = new List <Models.ScenarioDetailVM>();
         //check playlistId have in playlistItem
         foreach (var item in scenarioItem)
         {
             if (item.PlaylistID == id)
             {
                 var b = new Models.ScenarioItemVM
                 {
                     ScenarioId = item.ScenarioID,
                 };
                 scenarioItemVMs.Add(b);
             }
         }
         // if scenarioItemVMs != null, get Scenario Title by ScenarioId
         if (scenarioItemVMs.Count != 0)
         {
             foreach (var item in scenarioItemVMs)
             {
                 foreach (var itemScenario in scenario)
                 {
                     if (item.ScenarioId == itemScenario.ScenarioID)
                     {
                         var b = new Models.ScenarioDetailVM
                         {
                             Title = itemScenario.Title,
                         };
                         scenarioVMs.Add(b);
                     }
                 }
             }
         }
         return(Json(new
         {
             isUsing = scenarioItemVMs.Count != 0,
             scenarioVMlist = scenarioVMs,
         }, JsonRequestBehavior.AllowGet));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
        // GET: Scenario/Delete/:id
        public ActionResult Delete(int id)
        {
            IScenarioItemService scerarioItemService = DependencyUtils.Resolve <IScenarioItemService>();
            var scenario     = this.scenarioService.FirstOrDefault(a => a.ScenarioID == id);
            var user         = Helper.GetCurrentUser();
            var scenarioItem = scerarioItemService.GetItemListByScenarioId(id);

            if (scenarioItem != null && scenario != null && scenario.BrandID == user.BrandID)
            {
                foreach (var i in scenarioItem)
                {
                    scerarioItemService.Delete(i);
                }
                this.scenarioService.Delete(scenario);
            }
            return(this.RedirectToAction("Index"));
        }
        public JsonResult LoadAreaPlaylist(int areaId, int scenarioId)
        {
            IScenarioItemService scenarioItemService = DependencyUtils.Resolve <IScenarioItemService>();
            IPlaylistService     playlistService     = DependencyUtils.Resolve <IPlaylistService>();
            var ScenarioItems   = scenarioItemService.GetItemListByAreaScenarioId(areaId, scenarioId);
            var ScenarioItemVMs = new List <Models.ScenarioItemVM>();
            var ScenarioItemIds = new List <int>();

            if (scenarioItemService != null)
            {
                foreach (var item in ScenarioItems)
                {
                    ScenarioItemIds.Add(item.PlaylistID);
                }
            }
            return(Json(new
            {
                ScenarioItemIds = ScenarioItemIds,
            }, JsonRequestBehavior.AllowGet));
        }
        public async System.Threading.Tasks.Task <ActionResult> Update(Models.ScenarioUpdateDetailVM model)
        {
            if (ModelState.IsValid)
            {
                var scenario = this.scenarioService.FirstOrDefault(a => a.ScenarioID == model.ScenarioId);
                if (scenario != null)
                {
                    scenario.LayoutID       = model.LayoutId;
                    scenario.Description    = model.Description;
                    scenario.Title          = model.Title;
                    scenario.isPublic       = model.IsPublic;
                    scenario.UpdateDateTime = DateTime.Now;
                    scenario.AudioArea      = model.AudioArea ?? -1;
                }
                await this.scenarioService.UpdateAsync(scenario);

                /*Delete items of updated areas of scenario*/
                if (model.PlaylistAreaArr != null)
                {
                    IScenarioItemService scenarioItemService = DependencyUtils.Resolve <IScenarioItemService>();
                    for (int i = 0; i < model.PlaylistAreaArr.Length; i++)
                    {
                        /*Delete items of updated areas of scenario*/
                        var ScenarioItems = scenarioItemService.GetItemListByAreaScenarioId(model.PlaylistAreaArr[i].AreaId, model.ScenarioId);
                        if (ScenarioItems != null)
                        {
                            foreach (var item in ScenarioItems)
                            {
                                await scenarioItemService.DeleteAsync(item);
                            }
                        }
                    }
                    /*Add updated items to scenario*/
                    foreach (var item in model.PlaylistAreaArr)
                    {
                        var i = 0;
                        if (item.PlaylistIds != null)
                        {
                            foreach (var playlist in item.PlaylistIds)
                            {
                                var scenarioItem = new Data.Models.Entities.ScenarioItem
                                {
                                    AreaID       = item.AreaId,
                                    PlaylistID   = playlist,
                                    DisplayOrder = i++,
                                    ScenarioID   = model.ScenarioId,
                                    LayoutID     = model.LayoutId,
                                };
                                await scenarioItemService.CreateAsync(scenarioItem);

                                Session.Clear();
                                Session["UPDATE_RESULT"] = true;
                            }
                        }
                    }
                }
                return(Json(new
                {
                    success = true,
                    url = "/Scenario/Index",
                }, JsonRequestBehavior.AllowGet));
            }
            return(Json(new
            {
                success = false,
            }, JsonRequestBehavior.AllowGet));
        }