//Get group details with contact count
 public static GroupDTO GetByIdWithContactCount(int Id)
 {
     try
     {
         UnitOfWork uow      = new UnitOfWork();
         Group      Group    = uow.GroupRepo.GetById(Id, true);
         GroupDTO   GroupDTO = Transform.GroupToDTO(Group);
         GroupDTO.ContactCount = GroupContactService.GetGroupIdWiseContactsCount(Id, "");// Group.Contacts.Count();
         return(GroupDTO);
     }
     catch
     {
         throw;
     }
 }
        //get group list by client id
        public static List <GroupDTO> GetGroupListByClientIdForCampaign(int ClientId)
        {
            List <GroupDTO> GroupDTOList = new List <GroupDTO>();

            try
            {
                using (var uow = new UnitOfWork())
                {
                    IEnumerable <Group> Group = uow.GroupRepo.GetAll().Where(e => e.ClientID == ClientId).OrderBy(e => e.Name).ToList();
                    if (Group != null)
                    {
                        GroupDTO GroupDTOAllContact = new GroupDTO();
                        GroupDTOAllContact.Id   = 0;
                        GroupDTOAllContact.Name = "All Contacts";
                        string ReceipentsNumber  = ContactService.GetAllReceipentNumberByClientId(ClientId);
                        int    TotalContactCount = CommonService.GetRecipientsCount(ReceipentsNumber);
                        GroupDTOAllContact.ContactCount = TotalContactCount;
                        GroupDTOList.Add(GroupDTOAllContact);

                        foreach (var item in Group)
                        {
                            GroupDTO GroupDTO = new GroupDTO();
                            GroupDTO = Transform.GroupToDTO(item);
                            Group objGroup = uow.GroupRepo.GetById(item.Id);//, true
                            //GroupContactDTO GroupContactDTO = new GroupContactDTO();
                            //GroupContactDTO = GetGroupContactById(GroupDTO.Id);
                            List <ContactDTO> ContactDTO = new List <ContactDTO>();
                            int ContactCount             = GroupContactService.GetGroupIdWiseContactsCount(objGroup.Id, "");

                            GroupDTO.ContactCount = ContactCount;// objGroup.Contacts.Count();//GroupContactDTO.Contacts.Count(); //
                            GroupDTOList.Add(GroupDTO);
                        }
                    }
                }



                return(GroupDTOList);
            }
            //catch (LoggedInUserException)
            //{
            //    throw new System.TimeoutException();
            //}
            catch (Exception ex)
            {
                throw;
            }
        }
        //Delete contact from group using contact id and group id
        public static void RemoveContactFromGroup(int ContactId, int GroupId)
        {
            try
            {
                //UnitOfWork uow = new UnitOfWork();
                //uow.GroupRepo.RemoveContact(GroupId, ContactId);
                //uow.SaveChanges();

                GroupContactDTO GroupContactDTO = new GroupContactDTO();
                GroupContactDTO.ContactId = ContactId;
                GroupContactDTO.GroupId   = GroupId;
                GroupContactService.DeleteByContactId(GroupContactDTO.ContactId, GroupContactDTO.GroupId);
            }
            catch
            {
                throw;
            }
        }
        //Add contact to group contact by contact id and group id
        public static void AddContactToGroup(int ContactId, int GroupId)
        {
            try
            {
                //UnitOfWork uow = new UnitOfWork();
                //uow.GroupRepo.AddContact(GroupId, ContactId );
                //uow.SaveChanges();

                GroupContactDTO GroupContactDTO = new GroupContactDTO();
                GroupContactDTO.ContactId = ContactId;
                GroupContactDTO.GroupId   = GroupId;
                GroupContactService.Create(GroupContactDTO);
            }
            catch
            {
                throw;
            }
        }
        // Returns group list which have clients
        public static List <GroupContactDTO> GetGroupListWithContactPresentByClientId(int ClientId)
        {
            List <GroupContactDTO> GroupDTOList = new List <GroupContactDTO>();

            try
            {
                using (var uow = new UnitOfWork())
                {
                    IEnumerable <Group> Group = uow.GroupRepo.GetAll().Where(e => e.ClientID == ClientId).OrderBy(e => e.Name).ToList();
                    if (Group != null)
                    {
                        foreach (var item in Group)
                        {
                            GroupDTO GroupDTO = new GroupDTO();
                            GroupDTO = GetById(item.Id);
                            GroupContactDTO GroupContactDTO = new GroupContactDTO();
                            //GroupContactDTO = GetGroupContactById(GroupDTO.Id);
                            List <ContactDTO> ContactDTOList = new List <ContactDTO>();
                            ContactDTOList = GroupContactService.GetGroupIdWiseContacts(GroupDTO.Id); // GroupDTOTemp.Id

                            GroupContactDTO.ContactCount = ContactDTOList.Count();                    // GroupContactDTO.Contacts.Count();
                            if (ContactDTOList.Count() > 0)                                           // GroupContactDTO.Contacts.Count
                            {
                                GroupDTOList.Add(GroupContactDTO);                                    // (Transform.GroupContactToDTO(item));
                            }
                        }
                    }
                }

                return(GroupDTOList);
            }
            //catch (LoggedInUserException)
            //{
            //    throw new System.TimeoutException();
            //}
            catch (Exception)
            {
                throw;
            }
        }
        //Edit Group Contact
        public static void EditGroupContact(GroupContactDTO GroupContactDTO)
        {
            try
            {
                bool            IsDeleted;
                GroupContactDTO GroupDTORemoveContact = new GroupContactDTO();
                //GroupDTORemoveContact = GetGroupContactById(GroupContactDTO.Id);


                if (GroupContactDTO.UnwantedContacts != null)
                {
                    foreach (var item in GroupContactDTO.UnwantedContacts)
                    {
                        try
                        {
                            IsDeleted = GroupContactService.DeleteByGroupId(GroupContactDTO.Id, item.Id);
                            //RemoveContactFromGroup(item.Id, GroupContactDTO.Id);
                        }
                        catch
                        {
                            continue;
                        }
                    }
                }

                //if (GroupDTORemoveContact.Contacts.Count() > 0)
                //{
                //    foreach (var item in GroupDTORemoveContact.Contacts)
                //    {

                //        RemoveContactFromGroup(item.Id, GroupContactDTO.Id);
                //    }
                //}


                if (GroupContactDTO.Contacts != null)
                {
                    foreach (var item in GroupContactDTO.Contacts)
                    {
                        int contactCount = 0;

                        if (item.Groups.Count != 0)
                        {
                            foreach (var grp in item.Groups)
                            {
                                if (grp.Id == GroupContactDTO.GroupId)
                                {
                                    contactCount++;
                                }
                            }
                        }

                        if (contactCount == 0)
                        {
                            GroupContactDTO GroupContactDTOCreate = new GroupContactDTO();
                            GroupContactDTOCreate.GroupId   = GroupContactDTO.Id;
                            GroupContactDTOCreate.ContactId = item.Id;
                            GroupContactService.Create(GroupContactDTOCreate);
                        }
                    }
                }


                //uow.SaveChanges();
            }
            catch
            {
                throw;
            }
        }