/// <summary>
        /// Creates a copy of last archived inspection schedule and save in database
        /// </summary>
        /// <param name="inspectionScheduleId"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public InspectionSchedule CloneInspectionSchedule(int inspectionScheduleId, string email)
        {
            var inspectionScheduleManager       = this.container.GetInstance <InspectionScheduleManager>();
            var inspectionScheduleDetailManager = this.container.GetInstance <InspectionScheduleDetailManager>();
            var inspectionScheduleSiteManager   = this.container.GetInstance <InspectionScheduleSiteManager>();

            var inspectionScheduleOld             = inspectionScheduleManager.GetById(inspectionScheduleId);
            InspectionSchedule inspectionSchedule = new InspectionSchedule();

            inspectionSchedule.ApplicationId  = inspectionScheduleOld.ApplicationId;
            inspectionSchedule.IsActive       = true;
            inspectionSchedule.IsArchive      = false;
            inspectionSchedule.OrganizationId = inspectionScheduleOld.OrganizationId;
            inspectionSchedule.InspectionDate = inspectionScheduleOld.InspectionDate;
            inspectionSchedule.StartDate      = inspectionScheduleOld.StartDate;
            inspectionSchedule.EndDate        = inspectionScheduleOld.EndDate;
            inspectionSchedule.CompletionDate = inspectionScheduleOld.CompletionDate;
            inspectionSchedule.CreatedDate    = DateTime.Now;
            inspectionSchedule.CreatedBy      = email;
            inspectionSchedule.UpdatedDate    = DateTime.Now;
            inspectionSchedule.IsCompleted    = false;

            inspectionScheduleManager.Add(inspectionSchedule);

            var inspectionScheduleDetailListOld = GetActiveByInspectionScheduleId(inspectionScheduleOld.Id);

            foreach (var inspectionScheduleDetailOld in inspectionScheduleOld.InspectionScheduleDetails)
            {
                InspectionScheduleDetail inspectionScheduleDetail = new InspectionScheduleDetail();

                inspectionScheduleDetail.InspectionScheduleId = inspectionSchedule.Id;
                inspectionScheduleDetail.AccreditationRoleId  = inspectionScheduleDetailOld.AccreditationRoleId;
                inspectionScheduleDetail.UserId = inspectionScheduleDetailOld.UserId;
                inspectionScheduleDetail.InspectionCategoryId = inspectionScheduleDetailOld.InspectionCategoryId;
                inspectionScheduleDetail.SiteId      = inspectionScheduleDetailOld.SiteId;
                inspectionScheduleDetail.IsLead      = inspectionScheduleDetailOld.IsLead;
                inspectionScheduleDetail.IsMentor    = inspectionScheduleDetailOld.IsMentor;
                inspectionScheduleDetail.IsActive    = true;
                inspectionScheduleDetail.IsArchive   = false;
                inspectionScheduleDetail.CreatedDate = DateTime.Now;
                inspectionScheduleDetail.CreatedBy   = email;

                inspectionScheduleDetailManager.BatchAdd(inspectionScheduleDetail);
            }

            inspectionScheduleDetailManager.SaveChanges();

            var existingInspectionScheduleSites = inspectionScheduleSiteManager.GetSiteByInspectionScheduleId(inspectionScheduleOld.Id);

            foreach (var site in existingInspectionScheduleSites)
            {
                InspectionScheduleSite inspectionScheduleSite = new InspectionScheduleSite();
                inspectionScheduleSite.SiteID = site.SiteID;
                inspectionScheduleSite.InspectionScheduleId = inspectionSchedule.Id;
                inspectionScheduleSite.InspectionDate       = site.InspectionDate;
                inspectionScheduleSite.CreatedBy            = email;
                inspectionScheduleSite.CreatedDate          = DateTime.Now;

                inspectionScheduleSiteManager.BatchAdd(inspectionScheduleSite);
            }

            inspectionScheduleSiteManager.SaveChanges();

            return(inspectionSchedule);
        }
        /// <summary>
        /// Adds new inspection schedule and inspection schedule details record
        /// </summary>
        /// <param name="inspectionScheduleId"></param>
        /// <param name="inspectionScheduleDetailId"></param>
        /// <param name="organizationId"></param>
        /// <param name="applicationId"></param>
        /// <param name="selectedUserId"></param>
        /// <param name="selectedRoleId"></param>
        /// <param name="selectedCategoryId"></param>
        /// <param name="lead"></param>
        /// <param name="mentor"></param>
        /// <param name="inspDate"></param>
        /// <param name="selectedFacilityList"></param>
        /// <param name="userEmail"></param>
        public int SaveInspectionSchedule(string inspectionScheduleId, string inspectionScheduleDetailId, string organizationId, string applicationId, string selectedUserId, string selectedRoleId, string selectedCategoryId, bool lead, bool mentor, string startDate, string endDate, List <FacilitySiteItems> selectedSitesList, string userEmail, string selectedSiteId)
        {
            var inspectionScheduleManager       = this.container.GetInstance <InspectionScheduleManager>();
            var inspectionScheduleDetailManager = this.container.GetInstance <InspectionScheduleDetailManager>();
            var orgManager       = this.container.GetInstance <OrganizationManager>();
            var userManager      = this.container.GetInstance <UserManager>();
            var trueVaultManager = this.container.GetInstance <TrueVaultManager>();

            var inspectionScheduleIdLocal = 0;

            if (inspectionScheduleId == "0") // New Schedule
            {
                var inspectionSchedule = new InspectionSchedule
                {
                    ApplicationId  = Convert.ToInt32(applicationId),
                    Id             = Convert.ToInt32(inspectionScheduleId),
                    IsActive       = true,
                    IsArchive      = false,
                    OrganizationId = Convert.ToInt32(organizationId),
                    InspectionDate = DateTime.Now,
                    StartDate      = Convert.ToDateTime(startDate),
                    EndDate        = Convert.ToDateTime(endDate),
                    CompletionDate = DateTime.Now,
                    CreatedDate    = DateTime.Now,
                    CreatedBy      = userEmail,
                    UpdatedDate    = DateTime.Now,
                    IsCompleted    = false
                };
                //Convert.ToDateTime(inspDate);

                inspectionScheduleManager.Add(inspectionSchedule);
                inspectionScheduleIdLocal = inspectionSchedule.Id;
            }
            else // Update Schedule
            {
                inspectionScheduleIdLocal = Convert.ToInt32(inspectionScheduleId);
                var inspectionSchedule = inspectionScheduleManager.GetById(inspectionScheduleIdLocal);
                inspectionSchedule.InspectionDate = DateTime.Now;//Convert.ToDateTime(inspDate);
                inspectionSchedule.StartDate      = Convert.ToDateTime(startDate);
                inspectionSchedule.EndDate        = Convert.ToDateTime(endDate);
                inspectionSchedule.UpdatedDate    = DateTime.Now;
                inspectionSchedule.UpdatedBy      = userEmail;
                inspectionScheduleManager.Save(inspectionSchedule);
            }


            if (!string.IsNullOrEmpty(inspectionScheduleDetailId)) // Just Save schedule not schedule detail
            {
                var userId = Guid.Parse(selectedUserId);
                var groups = trueVaultManager.GetAllGroups();

                if (inspectionScheduleDetailId == "0") // new inspection schedule detail
                {
                    var inspectionScheduleDetail = new InspectionScheduleDetail
                    {
                        AccreditationRoleId = Convert.ToInt32(selectedRoleId),
                        UserId = userId,
                        InspectionCategoryId = Convert.ToInt32(selectedCategoryId),
                        InspectionScheduleId = inspectionScheduleIdLocal,
                        SiteId      = Convert.ToInt32(selectedSiteId),
                        IsLead      = lead,
                        IsMentor    = mentor,
                        CreatedDate = DateTime.Now,
                        UpdatedDate = DateTime.Now,
                        CreatedBy   = userEmail,
                        IsActive    = true,
                        IsArchive   = false
                    };

                    inspectionScheduleDetailManager.Add(inspectionScheduleDetail);
                }
                else // edit inspection schedule detail
                {
                    var inspectionScheduleDetail = inspectionScheduleDetailManager.GetById(Convert.ToInt32(inspectionScheduleDetailId));
                    inspectionScheduleDetail.AccreditationRoleId = Convert.ToInt32(selectedRoleId);
                    inspectionScheduleDetail.UserId = userId;
                    inspectionScheduleDetail.InspectionCategoryId = Convert.ToInt32(selectedCategoryId);
                    inspectionScheduleDetail.SiteId      = Convert.ToInt32(selectedSiteId);
                    inspectionScheduleDetail.IsLead      = lead;
                    inspectionScheduleDetail.IsMentor    = mentor;
                    inspectionScheduleDetail.UpdatedDate = DateTime.Now;
                    inspectionScheduleDetail.UpdatedBy   = userEmail;
                    inspectionScheduleDetailManager.Save(inspectionScheduleDetail);
                }

                var user = userManager.GetById(userId);
                var org  = orgManager.GetById(Convert.ToInt32(organizationId));

                var userOrgs = new List <UserOrganizationItem>
                {
                    new UserOrganizationItem
                    {
                        Organization = new OrganizationItem
                        {
                            OrganizationName       = org.Name,
                            DocumentLibraryGroupId = org.DocumentLibraryGroupId
                        }
                    }
                };

                trueVaultManager.AddUserToGroups(userOrgs, user.DocumentLibraryUserId, groups);
            }

            UpdateInspectionScheduleSite(selectedSitesList, inspectionScheduleIdLocal, userEmail);

            return(inspectionScheduleIdLocal);
        }