public void UpdateGoogleContactWithAvegaContactData(Contact contact, AvegaContact avegaContact) {
			contact.Title = avegaContact.DisplayName;

			contact.Phonenumbers.Clear();

			if (!string.IsNullOrEmpty(avegaContact.MobilePhone)) {
				PhoneNumber phoneNumber = new PhoneNumber(avegaContact.MobilePhone);
				phoneNumber.Primary = true;
				phoneNumber.Rel = ContactsRelationships.IsMobile;
				contact.Phonenumbers.Add(phoneNumber);
			}

            if (!string.IsNullOrEmpty(avegaContact.OfficePhone))
            {
				PhoneNumber workPhone = new PhoneNumber(avegaContact.OfficePhone);
				workPhone.Primary = false;
				workPhone.Rel = ContactsRelationships.IsWork;
				contact.Phonenumbers.Add(workPhone);
			}

            if (!string.IsNullOrEmpty(avegaContact.Email))
            {
				contact.Emails.Clear();
				EMail primaryEmail = new EMail(avegaContact.Email);
				primaryEmail.Primary = true;
				primaryEmail.Rel = ContactsRelationships.IsWork;
				contact.Emails.Add(primaryEmail);
			}
		}
		public Contact GetGoogleContactFor(AvegaContact contact) {
			var allContacts = GetAllContacts();

			Contact found = null;
			var avegaGroup = ensureGroupExists(GoogleContactGroupName);
		
			foreach (var x in allContacts) {
				bool avegaGroupFound = false;
				foreach (GroupMembership g in x.GroupMembership) {
					if (g.HRef == avegaGroup.Id) {
						avegaGroupFound = true;
					}
				}

				bool isNameMatch = x.Title.Equals(contact.DisplayName, StringComparison.InvariantCultureIgnoreCase);
				bool isMatch = isNameMatch && avegaGroupFound;

				if (isMatch) {
					if (found != null) throw new ApplicationException("Duplicate contact found [" + contact.DisplayName + "]");
					found = x;
				}
			}

			return found;
		}
		public void shouldFindExistingContact() {
			GoogleContactService googleContactService = new GoogleContactService(Common.GoogleAuthentication, Common.TestGoogleContactGroupName);
			var avegaContact = new AvegaContact("Existing contact" + DateTime.Now.Ticks);
			googleContactService.Create(avegaContact);

			var contact = googleContactService.GetGoogleContactFor(avegaContact);


			Assert.IsNotNull(contact);
		}
		public SynchronizationStatus SynchronizeWithGoogleContact(AvegaContact avegaContact) {
			var existingContact = GoogleContactService.GetGoogleContactFor(avegaContact);

			if (existingContact != null) { /* Exists in google contact - Do update */
				GoogleContactService.UpdateGoogleContactWithAvegaContactData(existingContact, avegaContact);
				var updatedContact = GoogleContactService.Update(existingContact);
				GoogleContactService.SetImage(updatedContact, avegaContact);

				return new SynchronizationStatus(true, false, true);
			}
			else {
				var createdContact = GoogleContactService.Create(avegaContact);
				GoogleContactService.SetImage(createdContact, avegaContact);

				return new SynchronizationStatus(true, true, false);
			}
		}
		public void shouldUpdateContactWhenContactExistInGoogleContact() {
			GoogleContactService googleContactService = new GoogleContactService(Common.GoogleAuthentication, Common.TestGoogleContactGroupName);
			IAvegaClientRepository avegaClientRepository = new IntranetAvegaClientRepository(Common.AvegaAuthentication);

			AvegaContactService avegaContactService = new AvegaContactService(googleContactService, avegaClientRepository);

			var contact = new AvegaContact("Mikael Test"+DateTime.Now.Ticks);
			var syncResultA = avegaContactService.SynchronizeWithGoogleContact(contact);
			googleContactService.InvalidateCache();

			contact.MobilePhone = "12345678";
			var syncResultB = avegaContactService.SynchronizeWithGoogleContact(contact);
			googleContactService.InvalidateCache();

			var syncResultC = avegaContactService.SynchronizeWithGoogleContact(contact);
			googleContactService.InvalidateCache();


			Assert.IsTrue(syncResultB.ContactWasUpdated);
			Assert.IsTrue(syncResultC.ContactWasUpdated);
		}
		private IList<AvegaContact> getContactsRequest(int start, int contactsToGet) {
			ensureLoggedIn();

			IList<AvegaContact> contacts = new List<AvegaContact>();

			string uri = "https://intranet.avegagroup.se/templates/UserList.aspx?id=105";
			var listGet = WebSession.Get(uri);

			string listResponse = listGet.TextContent;
			HtmlTagReader tagReader = new HtmlTagReader(listResponse, "a");
			
			int totalContactInList = countTotalContact(listResponse);
			int totalContactToFetch = Math.Min(contactsToGet - start, totalContactInList);


			int count = 0;
			OnContactDataFetched(new ContactDataFetchedEventArgs(count, totalContactInList, totalContactToFetch, DataFetchedType.ListIndex));
			while (tagReader.GetNextTag()) {
				var hrefValue = HtmlTagReader.GetAttributeValueInTag(tagReader.CurrentTag.Content, "href");

				if (hrefValue.Contains("templates/vcard.aspx")) {
					var sid = SID.Find(hrefValue);

					if (start <= count && contactsToGet + start > count) {
						AvegaContact contact = new AvegaContact(sid);

						contact.Merge(requestVCard(sid));
						OnContactDataFetched(new ContactDataFetchedEventArgs(count + 1, totalContactInList, totalContactToFetch, DataFetchedType.ContactInformation));

						contact.SetImage(requestImage(sid));
						OnContactDataFetched(new ContactDataFetchedEventArgs(count + 1, totalContactInList, totalContactToFetch, DataFetchedType.Image));

						contacts.Add(contact);

						count++;
					}
				}
			}

			return contacts;
		}
		public Contact Create(AvegaContact avegaContact) {
			Contact newContact = new Contact();

			UpdateGoogleContactWithAvegaContactData(newContact, avegaContact);

			var avegaGroup = ensureGroupExists(GoogleContactGroupName);

			var groupMembership = new GroupMembership();
			groupMembership.HRef = avegaGroup.Id;
			newContact.GroupMembership.Add(groupMembership);

			//Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("default"));
			Feed<Contact> contactFeed = GetGoogleRequest().GetContacts();
			var createdContact = GetGoogleRequest().Insert<Contact>(contactFeed, newContact);

			return createdContact;
		}
		public void SetImage(Contact contact, AvegaContact avegaContact) {
			if (avegaContact.HasImage) {
				using (var stream = new MemoryStream(avegaContact.Image)) {
					try {
						GetGoogleRequest().SetPhoto(contact, stream);
					}
					catch (GDataRequestException ex) {

						/* Seams to be a bug in the Google Contact API - sometimes it reports not found when updateing a contact */
						/* We throw a warning but continue execution*/
						if (((HttpWebResponse)ex.Response).StatusCode == HttpStatusCode.NotFound) {
							this.OnWarning(new WarningEventArgs("Unable to sync image for contact [" + avegaContact.DisplayName + "]. Please try again later."));
						}
						else {
							throw;
						}
					}
				}
			}
		}