public int checkToFreezeAccount(Account acc)
        {
            var folderList = CCFolderRepository.CCFolders.Where(f => f.AccountGUID == acc.AccountGUID).ToList();

            foreach(var fold in folderList)
            {
                int FolderItemCount = CCItemRepository.CCContacts.Where(i => i.FolderID == fold.FolderID).Count();

                LimitationsViewModel limitationsObj = new LimitationsViewModel();
                HelperFunctions HF = new HelperFunctions();
                limitationsObj = HF.updateAccountLimitations(acc);

                if ((FolderItemCount > limitationsObj.maxItemCountPerFolder) | (fold.isOverFlow == true))
                {
                    return 1;
                }
            }
            return 0;
        }
        public ActionResult Index()
        {
            TempData["SelectedMenu"] = "Summary";
            TempData["SelectedSubMenu"] = "";
            TempData["SelectedSubMenuFolder"] = "";
            User userObj = (User)Session["user"];
            Account accountObj = (Account)Session["account"];
            if (userObj != null)
            {
                //if (accountObj.isOverFlow == true)
                //{
                //    return RedirectToAction("BillingOptions", "Admin", new { id = 1 });
                //}

                LimitationsViewModel limitationsObj = new LimitationsViewModel();
                HelperFunctions HF = new HelperFunctions();
                limitationsObj = HF.updateAccountLimitations(accountObj);
                Session["limitations"] = limitationsObj;

                if (userObj.isPasswordChange == false)
                {
                    return RedirectToAction("ChangePassword", "Admin");
                }

                if (Session["trialData"] == null)
                {
                    if (accountObj.HasPurchased == false)
                    {
                        if ((((DateTime)(accountObj.TrialEnds) - (DateTime)(DateTime.Now.Date)).TotalDays > (CCGlobalValues.trialPeriod - 2)) | (((DateTime)(accountObj.TrialEnds) - (DateTime)(DateTime.Now.Date)).TotalDays < 2))
                        {
                            TrialDataModel trialObj = new TrialDataModel();
                            trialObj.hasPurchased = accountObj.HasPurchased;
                            trialObj.createdDate = accountObj.CreatedDate;
                            trialObj.trialEndDate = accountObj.TrialEnds;
                            trialObj.showPurchaseOptions = true;
                            Session["trialData"] = trialObj;

                            return RedirectToAction("Index", "Admin");
                        }
                        else if (((DateTime)(accountObj.TrialEnds) - (DateTime)(DateTime.Now.Date)).TotalDays < CCGlobalValues.trialPeriod)
                        {
                            TrialDataModel trialObj = new TrialDataModel();
                            trialObj.hasPurchased = accountObj.HasPurchased;
                            trialObj.createdDate = accountObj.CreatedDate;
                            trialObj.trialEndDate = accountObj.TrialEnds;
                            trialObj.showPurchaseOptions = true;
                            Session["trialData"] = trialObj;
                        }
                    }
                }

                if (accountObj.HasPurchased == true & Session["trialData"] != null)
                {
                    Session["trialData"] = null;
                }

                AdminViewModel ObjModel = new AdminViewModel();
                List<AdminDashboardViewModel> objDash = new List<AdminDashboardViewModel>();
                List<List<ConnectionInforViewModel>> connectionInforByConnction = new List<List<ConnectionInforViewModel>>();

                if (CCFolderRepository.CCFolders != null) { }
                var allFolders = CCFolderRepository.CCFolders.Where(guid => guid.AccountGUID == accountObj.AccountGUID).ToList();

                ObjModel.noOfFolders = CCFolderRepository.CCFolders.Where(F => F.AccountGUID == accountObj.AccountGUID).Count();
                ObjModel.noOfConnections = CCConnectionRepository.CCSubscriptions.Where(C => C.AccountGUID == accountObj.AccountGUID).Count();
                ObjModel.noOfUsers = CCUserRepository.Users.Where(U => U.AccountID == userObj.AccountID).Count();
                //ObjModel.noOfSubscriptionsPurchased = 0;
                ObjModel.noOfTotalItems = CCItemRepository.CCContacts.Where(I => I.AccountGUID == accountObj.AccountGUID & I.isDeleted == false).Count();

                // get saved quantity
                var accDetails = accountRepo.Accounts.Where(aguid => aguid.AccountGUID == accountObj.AccountGUID).FirstOrDefault();
                var planLeval = planRepository.Plans.Where(pid => pid.ID == accDetails.PlanID).FirstOrDefault().PlanLevel;

                    var featureQuality = featureRepository.Features.Where(pid => pid.PlanLevel == planLeval & pid.Type == "Max Items per Folder").FirstOrDefault();
                    var savedQuality = purchRepository.Purchases.Where(fid => fid.FeatureID == featureQuality.ID && fid.AccountGUID == accountObj.AccountGUID).FirstOrDefault();

                    if (savedQuality != null)
                    {
                        var quantitySaved = (savedQuality.Quantity) / (featureQuality.Quantity);
                        ObjModel.NoOfConnection = quantitySaved * 5;
                        ObjModel.noOfSubscriptionsPurchased = quantitySaved;
                        //limitationsObj.availableCconnectionCount = (int)ObjModel.NoOfConnection - (int)ObjModel.noOfConnections;
                        //limitationsObj.maxItemCountPerFolder = featureQuality.Quantity;
                        //if (featureRepository.Features.Where(pid => pid.PlanLevel == planLeval & pid.Type == "Sync Calendar").FirstOrDefault().Quantity == 0)
                        //    limitationsObj.isCalendarSyncAvailable = false;
                        //else
                        //    limitationsObj.isCalendarSyncAvailable = true;
                    }
                    else
                    {
                        ObjModel.NoOfConnection = 0;
                        ObjModel.noOfSubscriptionsPurchased = 0;
                    }

                foreach (var folder in allFolders)
                {
                    AdminDashboardViewModel dash = new AdminDashboardViewModel();
                    List<ConnectionInforViewModel> connectionInforByFolder = new List<ConnectionInforViewModel>();

                    dash.FolderName = folder.Name;
                    dash.FolderID = folder.FolderID;
                    dash.FolderType = folder.Type;
                    dash.NumberOfItems = CCContactRepository.CCContacts.Where(fid => fid.FolderID == folder.FolderID & fid.AccountGUID == accountObj.AccountGUID & fid.isDeleted == false).Count();
                    dash.NumberOfConnections = CCConnectionRepository.CCSubscriptions.Where(fid => fid.FolderID == folder.FolderID & fid.AccountGUID == accountObj.AccountGUID).Count();

                    if (folder.IsPaused == false)
                    {
                        dash.IsPaused = false;
                    }
                    else
                    {
                        dash.IsPaused = true;
                    }
                    objDash.Add(dash);

                    var connections = CCConnectionRepository.CCSubscriptions.Where(fid => fid.FolderID == folder.FolderID & fid.AccountGUID == accountObj.AccountGUID);
                    foreach (var conn in connections)
                    {
                        ConnectionInforViewModel connectionInfor = new ConnectionInforViewModel();
                        connectionInfor.FolderName = folder.Name;
                        connectionInfor.ConnctionFolderName = conn.FolderName;
                        connectionInfor.ConnectionID = conn.ConnectionID;

                        if (DateTime.Parse(conn.LastSyncTime) < DateTime.Parse("1902-01-01 00:00"))
                        {
                            connectionInfor.LastSyncTime = "Never";
                        }
                        else
                        {

                            DateTime timeUtc = DateTime.Parse(conn.LastSyncTime);
                            DateTime cstTime = new DateTime();
                            try
                            {
                                TimeZoneInfo cstZone = TimeZoneInfo.FindSystemTimeZoneById(accountObj.TimeZone.ToString());
                                cstTime = TimeZoneInfo.ConvertTimeFromUtc(timeUtc, cstZone);
                                Console.WriteLine("The date and time are {0} {1}.",
                                                  cstTime,
                                                  cstZone.IsDaylightSavingTime(cstTime) ?
                                                          cstZone.DaylightName : cstZone.StandardName);
                            }
                            catch (TimeZoneNotFoundException)
                            {
                                Console.WriteLine("Timezone conversion error");
                            }
                            catch (InvalidTimeZoneException)
                            {
                                Console.WriteLine("conn.LastSyncTime");
                            }

                            connectionInfor.LastSyncTime = cstTime.ToString();

                            //connectionInfor.LastSyncTime = conn.LastSyncTime;
                        }
                        connectionInfor.Type = conn.Type;
                        connectionInfor.FolderID = folder.FolderID;
                        if (conn.IsRunning == null) { connectionInfor.IsRunning = false; }
                        else if (conn.IsRunning == false) { connectionInfor.IsRunning = false; }
                        else if (conn.IsRunning == true) { connectionInfor.IsRunning = true; }
                        if (conn.IsPaused == false)
                        {
                            connectionInfor.IsPaused = false;
                            connectionInfor.rowClass = "notPaused ";
                        }
                        else
                        {
                            connectionInfor.IsPaused = true;
                            connectionInfor.rowClass = "Paused ";
                        }
                        connectionInforByFolder.Add(connectionInfor);

                    }

                    connectionInforByConnction.Add(connectionInforByFolder);

                }
                var assemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
                ViewBag.version = assemblyVersion;
                Session["version"] = assemblyVersion;
                ObjModel.FoldersInfor = objDash;
                ObjModel.ConnectionsInfor = connectionInforByConnction;
                return View(ObjModel);
            }
            return View();
        }
        public ActionResult ImpersonatedAccount(long AccID)
        {
            if (Session["SysAdminDetails"] != null)
            {
                TempData["SelectedMenu"] = "AccImpersontation";

                var userObj = CCUserRepository.GetUserByEmailAddress(CCUserRepository.Users.First(U => U.AccountID == AccID).Email);
                Session["user"] = userObj;
                var accountObj = CCAccRepository.Accounts.FirstOrDefault(x => x.ID == userObj.AccountID);
                Session["account"] = accountObj;
                var timeZone = CCAccRepository.Accounts.Where(aid => aid.ID == userObj.AccountID).FirstOrDefault().TimeZone;
                Session["timeZone"] = timeZone;
                var folders = CCFolderRepository.CCFolders.Where(guid => guid.AccountGUID == accountObj.AccountGUID).ToList();
                Session["folderss"] = folders;

                LimitationsViewModel limitationsObj = new LimitationsViewModel();
                HelperFunctions HF1 = new HelperFunctions();
                limitationsObj = HF1.updateAccountLimitations(accountObj);
                Session["limitations"] = limitationsObj;

                List<NotificationListViewModel> notificationList = new List<NotificationListViewModel>();

                CronJobController CJC = new CronJobController();
                Session["account"] = CJC.checkAccountTrialExpiryForAccount(accountObj);

                HelperFunctions HF = new HelperFunctions();
                notificationList = HF.generateNotificationList(accountObj);

                HF.CheckAcccountStatus(accountObj);

                if (notificationList.Count > 0)
                    Session["notifications"] = notificationList;
                else
                    Session["notifications"] = null;

                if (((DateTime)(accountObj.TrialEnds) - (DateTime)(DateTime.Now.Date)).TotalDays < CCGlobalValues.trialPeriod)
                {
                    TrialDataModel trialObj = new TrialDataModel();
                    trialObj.hasPurchased = accountObj.HasPurchased;
                    trialObj.createdDate = accountObj.CreatedDate;
                    trialObj.trialEndDate = accountObj.TrialEnds;
                    trialObj.showPurchaseOptions = true;
                    Session["trialData"] = trialObj;
                }

                ImpersonateAccountsSysAdminModel model = new ImpersonateAccountsSysAdminModel();

                var AccountsInfo = CCAccRepository.Accounts.Where(A => A.ID == AccID).FirstOrDefault();

                model.SelectedAccountInfo = AccountsInfo;
                model.FolderCount = CCFolderRepository.CCFolders.Where(F => F.AccountGUID == AccountsInfo.AccountGUID).Count();
                model.ConnectionCount = CCConnectionRepository.CCSubscriptions.Where(C => C.AccountGUID == AccountsInfo.AccountGUID).Count();
                model.UserCount = CCUserRepository.Users.Where(U => U.AccountID == AccountsInfo.ID).Count();
                model.SubscriptionCount = 0;
                model.ItemsCount = CCItemRepository.CCContacts.Where(I => I.AccountGUID == AccountsInfo.AccountGUID).Count();
                model.SelectedAccountOwner = CCUserRepository.Users.First(U => U.AccountID == AccountsInfo.ID).FullName;

                //Sync Graph Data

                List<string> SyncDates = new List<string>();
                List<double> SyncDateUsage = new List<double>();

                DateTime startDate = DateTime.Now.Date.AddDays(-1);

                for (int i = 6; i > -1; i--)
                {
                    SyncDates.Add((startDate.AddDays(-i)).ToShortDateString());
                }

                for (int i = 0; i < SyncDates.Count(); i++)
                {
                    DateTime syncDateStart = DateTime.Parse(SyncDates[i]);
                    DateTime syncDateEnd = syncDateStart.AddHours(23).AddMinutes(59);

                    SyncDateUsage.Add(CCSyncItemsRepository.CCSyncItems.Where(SI => SI.LastUpdated >= syncDateStart && SI.LastUpdated <= syncDateEnd).Count());
                }

                model.SyncDates = SyncDates;
                model.SyncDateUsage = SyncDateUsage;

                return View(model);
            }
            else
            {
                return RedirectToAction("Login", "Login");
            }
        }