private static void Traverse(CategoryClass category, ref ShowLookUpObject lookupObj, ref List<ShowLookUpObject> slo)
        {
            if (category is Show)
            {
                if (category.CategoryId == lookupObj.SubCategoryId)
                {
                    lookupObj.SubCategoryId = lookupObj.MainCategoryId;
                    lookupObj.SubCategory = lookupObj.MainCategory;
                }
                lookupObj.ShowId = category.CategoryId;
                lookupObj.Show = category.CategoryName;
                slo.Add(lookupObj);
            }
            else
            {
                var subcategories = category.CategoryClassSubCategories.Select(s => s.SubCategory).Where(s => s.StatusId == 1);

                foreach (CategoryClass c in subcategories)
                {
                    ShowLookUpObject lookupObj_t = new ShowLookUpObject();
                    lookupObj_t.MainCategoryId = lookupObj.MainCategoryId;
                    lookupObj_t.MainCategory = lookupObj.MainCategory;
                    var sub_category = category.CategoryClassSubCategories.Where(sc => sc.CategoryId == c.CategoryId).Single();
                    lookupObj_t.SubCategoryId = (lookupObj.SubCategoryId == null) ? sub_category.SubCategory.CategoryId : lookupObj.SubCategoryId;
                    lookupObj_t.SubCategory = (lookupObj.SubCategory == null) ? sub_category.SubCategory.CategoryName : lookupObj.SubCategory;
                    Traverse(c, ref lookupObj_t, ref slo);
                }
            }
        }
        public static List<ShowLookUpObject> GetShowsOnCurrentOffering()
        {
            var context = new IPTV2Entities();
            var service = context.Offerings.Find(GlobalConfig.offeringId).Services.Where(p => p.PackageId == GlobalConfig.serviceId && p.StatusId == GlobalConfig.Visible).Single();

            List<ShowLookUpObject> slo = new List<ShowLookUpObject>();
            foreach (PackageCategory pkg_category in service.Categories)
            {
                ShowLookUpObject lookupObj = new ShowLookUpObject();
                CategoryClass categoryClass = context.CategoryClasses.Find(pkg_category.CategoryId);
                lookupObj.MainCategoryId = pkg_category.CategoryId;
                lookupObj.MainCategory = categoryClass.CategoryName;
                Traverse(categoryClass, ref lookupObj, ref slo);
            }

            return slo;

            //HttpContext.Current.Cache.Insert("ShowList", slo, null, DateTime.Now.AddHours(1), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
        }
        public ActionResult GetAllVideosBasedOnEntitlements()
        {
            List<ShowLookUpObject> list = null;

            if (!GlobalConfig.IsSynapseEnabled)
                return Json(list, JsonRequestBehavior.AllowGet);
            var registDt = DateTime.Now;
            if (MyUtility.isUserLoggedIn())
            {
                try
                {
                    //var cache = DataCache.Cache;
                    //string cacheKey = "MOBILEGAV:U:" + User.Identity.Name;
                    //list = (List<ShowLookUpObject>)cache[cacheKey];
                    if (list == null)
                    {
                        list = new List<ShowLookUpObject>();
                        var context = new IPTV2Entities();
                        var UserId = new Guid(User.Identity.Name);
                        var user = context.Users.FirstOrDefault(u => u.UserId == UserId);
                        if (user != null)
                        {
                            SortedSet<Int32> ShowIds = new SortedSet<int>();
                            var service = context.Offerings.Find(GlobalConfig.offeringId).Services.FirstOrDefault(s => s.StatusId == GlobalConfig.Visible);
                            foreach (var entitlement in user.Entitlements.Where(e => e.EndDate > registDt))
                            {
                                if (entitlement is PackageEntitlement)
                                {
                                    var packageEntitlement = (PackageEntitlement)entitlement;
                                    var pkgCat = context.PackageCategories.Where(p => p.PackageId == packageEntitlement.PackageId).Select(p => p.Category);
                                    var pkgCatSubCategories = pkgCat.Select(p => p.SubCategories);
                                    foreach (var categories in pkgCatSubCategories)
                                    {
                                        foreach (var category in categories)
                                        {
                                            var listOfIds = service.GetAllMobileShowIds(MyUtility.GetCurrentCountryCodeOrDefault(), category);
                                            var showList = context.CategoryClasses.Where(c => listOfIds.Contains(c.CategoryId) && c.StatusId == GlobalConfig.Visible);
                                            foreach (var show in showList)
                                            {
                                                if (show != null)
                                                {
                                                    if (!(ShowIds.Contains(show.CategoryId)))
                                                    {
                                                        if (show.StartDate < registDt && show.EndDate > registDt)
                                                        {
                                                            if (show is Show)
                                                            {
                                                                if (!(show is LiveEvent))
                                                                {
                                                                    ShowLookUpObject data = new ShowLookUpObject();
                                                                    data.Show = show.Description;
                                                                    data.ShowId = show.CategoryId;
                                                                    data.MainCategory = category.Description;
                                                                    data.MainCategoryId = category.CategoryId;
                                                                    data.ShowType = (show is Movie) ? "MOVIE" : "SHOW";
                                                                    if (!(show is Movie))
                                                                        list.Add(data);
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                            ShowIds.UnionWith(listOfIds); //For checking
                                        }
                                    }
                                }
                                else if (entitlement is ShowEntitlement)
                                {
                                    var showEntitlement = (ShowEntitlement)entitlement;
                                    if (!(ShowIds.Contains(showEntitlement.CategoryId)))
                                    {
                                        if (!(showEntitlement.Show is LiveEvent))
                                        {
                                            ShowLookUpObject data = new ShowLookUpObject();
                                            var show = showEntitlement.Show;
                                            if (show != null)
                                            {
                                                if (show.StartDate < registDt && show.EndDate > registDt)
                                                {
                                                    var CacheDuration = new TimeSpan(0, GlobalConfig.GetParentCategoriesCacheDuration, 0);
                                                    var parentCategories = show.GetAllParentCategories(CacheDuration);
                                                    var parent = context.CategoryClasses.Where(c => parentCategories.Contains(c.CategoryId) && c.StatusId == GlobalConfig.Visible && c is Category);
                                                    data.Show = show.Description;
                                                    data.ShowId = show.CategoryId;
                                                    data.MainCategory = parent.First().Description;
                                                    data.MainCategoryId = parent.First().CategoryId;
                                                    data.ShowType = (show is Movie) ? "MOVIE" : "SHOW";
                                                    if (!(show is Movie))
                                                        ShowIds.Add(show.CategoryId);
                                                    list.Add(data);
                                                }
                                            }
                                        }
                                    }
                                }
                                else if (entitlement is EpisodeEntitlement)
                                {
                                    var episodeEntitlement = (EpisodeEntitlement)entitlement;
                                    var eCacheDuration = new TimeSpan(0, GlobalConfig.GetParentShowsForEpisodeCacheDuration, 0);
                                    var listOfShows = episodeEntitlement.Episode.GetParentShows(eCacheDuration);
                                    var parentShow = context.CategoryClasses.FirstOrDefault(c => listOfShows.Contains(c.CategoryId) && c.StatusId == GlobalConfig.Visible && c is Show);
                                    if (parentShow != null)
                                    {
                                        if (!(ShowIds.Contains(parentShow.CategoryId)))
                                        {
                                            if (!(parentShow is LiveEvent))
                                            {
                                                if (parentShow.StartDate < registDt && parentShow.EndDate > registDt)
                                                {
                                                    ShowLookUpObject data = new ShowLookUpObject();
                                                    var CacheDuration = new TimeSpan(0, GlobalConfig.GetParentCategoriesCacheDuration, 0);
                                                    var parentCategories = ((Show)parentShow).GetAllParentCategories(CacheDuration);
                                                    var parent = context.CategoryClasses.Where(c => parentCategories.Contains(c.CategoryId) && c.StatusId == GlobalConfig.Visible && c is Category);
                                                    data.EpisodeId = episodeEntitlement.Episode.EpisodeId;
                                                    data.EpisodeName = episodeEntitlement.Episode.Description + ", " + episodeEntitlement.Episode.DateAired.Value.ToString("MMMM d, yyyy");
                                                    data.Show = parentShow.Description;
                                                    data.ShowId = parentShow.CategoryId;
                                                    data.MainCategory = parent.First().Description;
                                                    data.MainCategoryId = parent.First().CategoryId;
                                                    data.ShowType = (parentShow is Movie) ? "MOVIE" : "SHOW";

                                                    if (!(parentShow is Movie))
                                                    {
                                                        ShowIds.Add(parentShow.CategoryId);
                                                        list.Add(data);
                                                    }

                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            list = list.OrderBy(c => c.Show).ToList();
                            //cache.Put(cacheKey, list, DataCache.CacheDuration);
                        }
                    }
                }
                catch (Exception e) { MyUtility.LogException(e); }
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetFreeTrialPackageContent()
        {
            if (!GlobalConfig.IsSynapseEnabled)
                return Json(null, JsonRequestBehavior.AllowGet);

            var context = new IPTV2Entities();
            string countrycode = MyUtility.GetCurrentCountryCodeOrDefault();
            string currencycode = MyUtility.GetCurrencyOrDefault(countrycode);

            int packageid = 0;
            //int freeTrialPackageId = 0;
            var list = MyUtility.StringToIntList(GlobalConfig.FreeTrialPackageIds);

            var registDt = DateTime.Now;

            List<ShowLookUpObject> datalist = new List<ShowLookUpObject>();
            List<ShowLookUpObject> clone_datalist = null;
            List<Int32> packageIds = null;
            IEnumerable<Int32> categoryIds = null;
            try
            {

                if (!User.Identity.IsAuthenticated)
                    return Json(null, JsonRequestBehavior.AllowGet);

                if (User.Identity.IsAuthenticated)
                {
                    var userId = new Guid(User.Identity.Name);
                    var user = context.Users.FirstOrDefault(u => u.UserId == userId);
                    if (user != null)
                    {
                        //var package = user.PackageEntitlements.FirstOrDefault(p => list.Contains(p.PackageId) && p.EndDate > registDt);
                        //if (package != null)
                        //    freeTrialPackageId = package.PackageId;

                        var packages = user.PackageEntitlements.Where(p => p.EndDate > registDt);

                        if (packages != null)
                        {
                            packageIds = new List<int>();
                            foreach (var pkg in packages)
                            {
                                if (this.IsAllowed(pkg.PackageId, countrycode))
                                    packageIds.Add(pkg.PackageId);
                            }
                        }


                        var showPackages = user.ShowEntitlements.Where(s => s.EndDate > registDt);
                        if (showPackages != null)
                        {
                            categoryIds = showPackages.Select(s => s.CategoryId);
                        }
                    }
                }

                if (packageIds == null && categoryIds == null)
                {
                    return Json(null, JsonRequestBehavior.AllowGet);
                }


                var service = context.Offerings.Find(GlobalConfig.offeringId).Services.FirstOrDefault(s => s.StatusId == GlobalConfig.Visible);
                var maincategory = context.PackageCategories.Where(pc => packageIds.Contains(pc.PackageId)).Select(p => p.Category);
                var subcategory = maincategory.Select(p => p.SubCategories);
                int[] category_ids = context.PackageCategories.Where(pc => pc.PackageId == packageid).Select(p => p.CategoryId).ToArray();
                category_ids.Union(categoryIds);

                //for movies
                foreach (var movie in maincategory)
                {
                    if (movie.SubCategories.Count() == 0)
                    {
                        int[] movieIds = service.GetAllMobileShowIds(MyUtility.GetCurrentCountryCodeOrDefault(), movie).ToArray();
                        foreach (var item in context.CategoryClasses
                                    .Where(s => movieIds.Contains(s.CategoryId)
                                        && s.StatusId == GlobalConfig.Visible))
                        {
                            if (item is Show)
                            {
                                var show = (Show)item;
                                if (!(show is LiveEvent))
                                {
                                    ShowLookUpObject data = new ShowLookUpObject();
                                    data.Show = show.Description;
                                    data.ShowId = show.CategoryId;
                                    data.MainCategory = movie.Description;
                                    data.MainCategoryId = movie.CategoryId;
                                    if (show.StartDate < registDt && show.EndDate > registDt)
                                        datalist.Add(data);
                                }
                            }
                        }
                    }
                }

                //for shows
                foreach (var subitems in subcategory)
                {
                    foreach (Category item in subitems)
                    {
                        int[] showIds = service.GetAllMobileShowIds(MyUtility.GetCurrentCountryCodeOrDefault(), item).ToArray();

                        foreach (var cat in context.CategoryClasses
                                    .Where(s => showIds.Contains(s.CategoryId)
                                        && s.StatusId == GlobalConfig.Visible))
                        {
                            if (cat is Show)
                            {
                                var show = (Show)cat;
                                if (!(show is LiveEvent))
                                {
                                    ShowLookUpObject data = new ShowLookUpObject();
                                    data.Show = show.Description;
                                    data.ShowId = show.CategoryId;
                                    data.MainCategory = item.Description;
                                    data.MainCategoryId = item.CategoryId;
                                    if (show.StartDate < registDt && show.EndDate > registDt)
                                        datalist.Add(data);
                                }
                            }
                        }
                    }
                }

                clone_datalist = datalist.Where(p => p.ShowId == 310).ToList();
                if (clone_datalist.Count() > 0)
                {
                    int mmk_main_id = clone_datalist.FirstOrDefault().MainCategoryId;
                    clone_datalist.AddRange(datalist.Where(p => p.MainCategoryId == mmk_main_id && p.ShowId != 310).ToList());
                    clone_datalist.AddRange(datalist.Where(p => p.MainCategoryId != mmk_main_id).OrderBy(p => p.MainCategoryId).ToList());
                }
                else
                {
                    clone_datalist = datalist;
                }

                return Json(clone_datalist, JsonRequestBehavior.AllowGet);
            }
            catch (Exception e) { MyUtility.LogException(e); }
            return Json(clone_datalist, JsonRequestBehavior.AllowGet);
        }
        public ActionResult GetChannelContent(int? id)
        {

            List<ShowLookUpObject> list = new List<ShowLookUpObject>();
            try
            {

                if (!GlobalConfig.IsTVERegistrationEnabled)
                    return Json(list, JsonRequestBehavior.AllowGet);

                DateTime registDt = DateTime.Now;
                var context = new IPTV2Entities();

                var countryCode = MyUtility.GetCurrentCountryCodeOrDefault();

                var service = context.Offerings.Find(GlobalConfig.offeringId).Services.FirstOrDefault(s => s.StatusId == GlobalConfig.Visible);
                var category = context.CategoryClasses.FirstOrDefault(c => c.CategoryId == id && c.StatusId == GlobalConfig.Visible);
                if (category != null)
                {
                    if (category.CategoryClassParentCategories.Count(c => c.ParentCategory.CategoryId == GlobalConfig.TFCEverywhereParentCategoryId) <= 0)
                        return Json(list, JsonRequestBehavior.AllowGet);

                    if (category is Category)
                    {
                        var parent = (Category)category;
                        var subCategoryies = parent.SubCategories;
                        if (subCategoryies.Count() > 0)
                        {
                            foreach (var subCategory in subCategoryies)
                            {
                                int[] showIds = service.GetAllOnlineShowIds(countryCode, subCategory).ToArray();
                                var shows = context.CategoryClasses.Where(c => showIds.Contains(c.CategoryId) && c.StatusId == GlobalConfig.Visible && c is Show);
                                foreach (var item in shows)
                                {
                                    if (item is Show)
                                    {
                                        var show = (Show)item;
                                        ShowLookUpObject data = new ShowLookUpObject();
                                        data.Show = show.Description;
                                        data.ShowId = show.CategoryId;
                                        data.MainCategory = subCategory.Description;
                                        data.MainCategoryId = subCategory.CategoryId;
                                        if (show.StartDate < registDt && show.EndDate > registDt)
                                            list.Add(data);
                                    }
                                    else if (item is Category)
                                    {
                                        // Loop thru the category again
                                        var subCategories2 = ((Category)item).SubCategories;
                                        if (subCategories2.Count() > 0)
                                        {
                                            foreach (var subCategory2 in subCategories2)
                                            {
                                                int[] showIds2 = service.GetAllOnlineShowIds(countryCode, subCategory2).ToArray();
                                                var shows2 = context.CategoryClasses.Where(c => showIds2.Contains(c.CategoryId) && c.StatusId == GlobalConfig.Visible && c is Show);
                                                foreach (var item2 in shows2)
                                                {
                                                    if (item2 is Show)
                                                    {
                                                        var show2 = (Show)item2;
                                                        ShowLookUpObject data = new ShowLookUpObject();
                                                        data.Show = show2.Description;
                                                        data.ShowId = show2.CategoryId;
                                                        data.MainCategory = subCategory2.Description;
                                                        data.MainCategoryId = subCategory2.CategoryId;
                                                        if (show2.StartDate < registDt && show2.EndDate > registDt)
                                                            list.Add(data);
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                else
                    return RedirectToAction("Index", "Home");

            }
            catch (Exception) { }
            return Json(list, JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// Description: Get Shows that belongs to a particular Package
        /// </summary>
        /// <param name="packageid"></param>
        /// <returns></returns>
        public ActionResult GetShows(int packageid)
        {
            List<ShowLookUpObject> datalist = null;
            var cache = DataCache.Cache;
            string cacheKey = "PKGDLGTSHOWS:0;P:" + packageid;
            datalist = (List<ShowLookUpObject>)cache[cacheKey];

            ////No Lite Package on US
            //if (countrycode == "US" && packageid == GlobalConfig.liteId)
            //{
            //    return Json(null, JsonRequestBehavior.AllowGet);
            //}


            if (datalist == null)
            {
                string countrycode = MyUtility.GetCurrentCountryCodeOrDefault();
                DateTime registDt = DateTime.Now;
                datalist = new List<ShowLookUpObject>();

                var context = new IPTV2Entities();
                if (!this.IsAllowed(packageid, countrycode))
                {
                    return Json(null, JsonRequestBehavior.AllowGet);
                }

                var service = context.Offerings.Find(GlobalConfig.offeringId).Services.FirstOrDefault(s => s.StatusId == GlobalConfig.Visible);
                var maincategory = context.PackageCategories.Where(pc => pc.PackageId == packageid).Select(p => p.Category);
                var subcategory = maincategory.Select(p => p.SubCategories);
                int[] category_ids = context.PackageCategories.Where(pc => pc.PackageId == packageid).Select(p => p.CategoryId).ToArray();

                //for movies
                foreach (var movie in maincategory)
                {
                    if (movie.SubCategories.Count() == 0)
                    {
                        int[] movieIds = service.GetAllOnlineShowIds(MyUtility.GetCurrentCountryCodeOrDefault(), movie).ToArray();
                        foreach (var item in context.CategoryClasses
                                    .Where(s => movieIds.Contains(s.CategoryId)
                                        && s.StatusId == GlobalConfig.Visible))
                        {
                            if (item is Show)
                            {
                                var show = (Show)item;
                                ShowLookUpObject data = new ShowLookUpObject();
                                data.Show = show.Description;
                                data.ShowId = show.CategoryId;
                                data.MainCategory = movie.Description;
                                data.MainCategoryId = movie.CategoryId;
                                //data.SubCategory = show.CategoryClassSubCategories.FirstOrDefault().SubCategory.CategoryName;
                                //data.SubCategoryId = show.CategoryClassSubCategories.FirstOrDefault().SubCategory.CategoryId;
                                //datalist.Add(data);
                                if (show.StartDate != null && show.EndDate != null)
                                {
                                    if (show.StartDate < registDt && show.EndDate > registDt)
                                        datalist.Add(data);
                                }
                                else
                                    datalist.Add(data);
                            }
                        }
                    }
                }

                //for shows
                foreach (var subitems in subcategory)
                {
                    foreach (Category item in subitems)
                    {
                        int[] showIds = service.GetAllOnlineShowIds(MyUtility.GetCurrentCountryCodeOrDefault(), item).ToArray();

                        foreach (var cat in context.CategoryClasses
                                    .Where(s => showIds.Contains(s.CategoryId)
                                        && s.StatusId == GlobalConfig.Visible))
                        {
                            if (cat is Show)
                            {
                                var show = (Show)cat;
                                ShowLookUpObject data = new ShowLookUpObject();
                                data.Show = show.Description;
                                data.ShowId = show.CategoryId;
                                data.MainCategory = item.Description;
                                data.MainCategoryId = item.CategoryId;
                                //data.SubCategory = show.CategoryClassSubCategories.FirstOrDefault().SubCategory.CategoryName;
                                //data.SubCategoryId = show.CategoryClassSubCategories.FirstOrDefault().SubCategory.CategoryId;
                                //datalist.Add(data);
                                if (show.StartDate != null && show.EndDate != null)
                                {
                                    if (show.StartDate < registDt && show.EndDate > registDt)
                                        datalist.Add(data);
                                }
                                else
                                    datalist.Add(data);
                            }
                        }
                    }
                }

                var cacheDuration = new TimeSpan(0, 60, 0);
                cache.Put(cacheKey, datalist, cacheDuration);
            }


            //Arrange MMK to be on Top;
            List<ShowLookUpObject> clone_datalist = datalist.Where(p => p.ShowId == 310).ToList();
            if (clone_datalist.Count() > 0)
            {
                int mmk_main_id = clone_datalist.FirstOrDefault().MainCategoryId;
                clone_datalist.AddRange(datalist.Where(p => p.MainCategoryId == mmk_main_id && p.ShowId != 310).ToList());
                clone_datalist.AddRange(datalist.Where(p => p.MainCategoryId != mmk_main_id).OrderBy(p => p.MainCategoryId).ToList());
            }
            else
            {
                clone_datalist = datalist;
            }

            return Json(clone_datalist, JsonRequestBehavior.AllowGet);
        }