Inheritance: ICalDavDataAccess
 private static async Task<CalendarProperties> GetCalendarProperties (CalDavDataAccess calDavDataAccess)
 {
   return
       (await calDavDataAccess.IsCalendarAccessSupported() ? CalendarProperties.CalendarAccessSupported : CalendarProperties.None) |
       (await calDavDataAccess.IsWriteable() ? CalendarProperties.IsWriteable : CalendarProperties.None) |
       (await calDavDataAccess.DoesSupportCalendarQuery() ? CalendarProperties.SupportsCalendarQuery : CalendarProperties.None);
 }
    public static async Task<TestResult> TestConnection (Uri url, IWebDavClient webDavClient)
    {
      var calDavDataAccess = new CalDavDataAccess (url, webDavClient);
      var cardDavDataAccess = new CardDavDataAccess (url, webDavClient);

      var ressourceType =
          (await calDavDataAccess.IsResourceCalender() ? ResourceType.Calendar : ResourceType.None) |
          (await cardDavDataAccess.IsResourceAddressBook() ? ResourceType.AddressBook : ResourceType.None);


      return new TestResult (
          ressourceType,
          ressourceType.HasFlag (ResourceType.Calendar) ? await GetCalendarProperties (calDavDataAccess) : CalendarProperties.None,
          ressourceType.HasFlag (ResourceType.AddressBook) ? await GetAddressBookProperties (cardDavDataAccess) : AddressBookProperties.None);
    }
    public static async Task<TestResult> TestConnection (Uri url, IWebDavClient webDavClient, ResourceType supposedRessourceType)
    {
      var calDavDataAccess = new CalDavDataAccess (url, webDavClient);
      var cardDavDataAccess = new CardDavDataAccess (url, webDavClient);

      TestResult result;


      switch (supposedRessourceType)
      {
        case ResourceType.None:
          var ressourceType =
              (await calDavDataAccess.IsResourceCalender() ? ResourceType.Calendar : ResourceType.None) |
              (await cardDavDataAccess.IsResourceAddressBook() ? ResourceType.AddressBook : ResourceType.None);

          result = new TestResult (
              ressourceType,
              ressourceType.HasFlag (ResourceType.Calendar) ? await GetCalendarProperties (calDavDataAccess) : CalendarProperties.None,
              ressourceType.HasFlag (ResourceType.AddressBook) ? await GetAddressBookProperties (cardDavDataAccess) : AddressBookProperties.None);
              break;

        case ResourceType.Calendar:
          result = new TestResult (
              supposedRessourceType,
              CalendarProperties.CalendarAccessSupported |
              (await calDavDataAccess.IsWriteable() ? CalendarProperties.IsWriteable : CalendarProperties.None) |
              (await calDavDataAccess.DoesSupportCalendarQuery() ? CalendarProperties.SupportsCalendarQuery : CalendarProperties.None),
              AddressBookProperties.None);
          break;
        
        case ResourceType.AddressBook:
          result = new TestResult (
              supposedRessourceType,
              CalendarProperties.None,
              AddressBookProperties.AddressBookAccessSupported |
              (await cardDavDataAccess.IsWriteable() ? AddressBookProperties.IsWriteable : AddressBookProperties.None));
          break;
        
        default:
          throw new ArgumentOutOfRangeException ("supposedRessourceType");
      }

      return result;
    }
    public static async Task<TestResult> TestConnection (Uri url, IWebDavClient webDavClient, ResourceType autoRessourceType)
    {
      var calDavDataAccess = new CalDavDataAccess (url, webDavClient);
      var cardDavDataAccess = new CardDavDataAccess (url, webDavClient);

      TestResult result;

      if (autoRessourceType == ResourceType.None)
      {
        var ressourceType =
          (await calDavDataAccess.IsResourceCalender() ? ResourceType.Calendar : ResourceType.None) |
          (await cardDavDataAccess.IsResourceAddressBook() ? ResourceType.AddressBook : ResourceType.None);

        result = new TestResult(
          ressourceType,
          ressourceType.HasFlag(ResourceType.Calendar) ? await GetCalendarProperties(calDavDataAccess) : CalendarProperties.None,
          ressourceType.HasFlag(ResourceType.AddressBook) ? await GetAddressBookProperties(cardDavDataAccess) : AddressBookProperties.None);
      }
      else if (autoRessourceType == ResourceType.Calendar)
      {
        result = new TestResult(
          autoRessourceType,
          CalendarProperties.CalendarAccessSupported |
          (await calDavDataAccess.IsWriteable() ? CalendarProperties.IsWriteable : CalendarProperties.None) |
          (await calDavDataAccess.DoesSupportCalendarQuery() ? CalendarProperties.SupportsCalendarQuery : CalendarProperties.None),
          AddressBookProperties.None);
      }
      else
      {
        result = new TestResult(
          autoRessourceType,
          CalendarProperties.None,
          AddressBookProperties.AddressBookAccessSupported |
          (await cardDavDataAccess.IsWriteable() ? AddressBookProperties.IsWriteable : AddressBookProperties.None));
      }

      return result;
    }
    private async Task<AutoDiscoveryResult> DoAutoDiscovery (Uri autoDiscoveryUri, IWebDavClient webDavClient, bool useWellKnownUrl)
    {
      var calDavDataAccess = new CalDavDataAccess (autoDiscoveryUri, webDavClient);
      IReadOnlyList<Tuple<Uri, string>> foundCaldendars = await calDavDataAccess.GetUserCalendarsNoThrow (useWellKnownUrl);

      var cardDavDataAccess = new CardDavDataAccess (autoDiscoveryUri, webDavClient);
      IReadOnlyList<Tuple<Uri, string>> foundAddressBooks = await cardDavDataAccess.GetUserAddressBooksNoThrow (useWellKnownUrl);

      if (foundCaldendars.Count > 0 || foundAddressBooks.Count > 0)
      {
        using (SelectResourceForm listCalendarsForm = new SelectResourceForm (foundCaldendars, foundAddressBooks, _folderType == OlItemType.olContactItem))
        {
          if (listCalendarsForm.ShowDialog() == DialogResult.OK)
            return new AutoDiscoveryResult (new Uri (autoDiscoveryUri.GetLeftPart (UriPartial.Authority) + listCalendarsForm.SelectedUrl), false, listCalendarsForm.ResourceType);
          else
            return new AutoDiscoveryResult (null, true, ResourceType.None);
        }
      }
      else
      {
        MessageBox.Show ("No resources were found via autodiscovery!", c_connectionTestCaption);
        return new AutoDiscoveryResult (null, false, ResourceType.None);
      }
    }
 private static async Task<ServerResources> GetUserResources (CalDavDataAccess calDavDataAccess, CardDavDataAccess cardDavDataAccess)
 {
   var calDavResources = await calDavDataAccess.GetUserResourcesNoThrow (true);
   if (calDavResources.CalendarResources.Count == 0 && calDavResources.TaskListResources.Count == 0)
     calDavResources = await calDavDataAccess.GetUserResourcesNoThrow (false);
   var foundAddressBooks = await cardDavDataAccess.GetUserAddressBooksNoThrow (true);
   if (foundAddressBooks.Count == 0)
     foundAddressBooks = await cardDavDataAccess.GetUserAddressBooksNoThrow (false);
   return new ServerResources (calDavResources.CalendarResources, foundAddressBooks, calDavResources.TaskListResources);
 }
    public async Task<ServerResources> GetServerResources (NetworkSettingsViewModel networkSettings, GeneralOptions generalOptions)
    {
      string caldavUrlString ;
      string carddavUrlString;

      if (string.IsNullOrEmpty (CalenderUrl) && !string.IsNullOrEmpty (EmailAddress))
      {
        bool success;
        caldavUrlString = OptionTasks.DoSrvLookup (EmailAddress, OlItemType.olAppointmentItem, out success);
        carddavUrlString = OptionTasks.DoSrvLookup (EmailAddress, OlItemType.olContactItem, out success);
      }
      else
      {
        caldavUrlString = CalenderUrl;
        carddavUrlString = CalenderUrl;
      }

      var trimmedCaldavUrl = caldavUrlString.Trim();
      var caldavUrl = new Uri (trimmedCaldavUrl.EndsWith ("/") ? trimmedCaldavUrl : trimmedCaldavUrl + "/");

      var trimmedCarddavUrl = carddavUrlString.Trim();
      var carddavUrl = new Uri (trimmedCarddavUrl.EndsWith("/") ? trimmedCarddavUrl : trimmedCarddavUrl + "/");

      var webDavClientCaldav = CreateWebDavClient (networkSettings, generalOptions, trimmedCaldavUrl);
      var webDavClientCarddav = CreateWebDavClient (networkSettings, generalOptions, trimmedCarddavUrl);
      var calDavDataAccess = new CalDavDataAccess (caldavUrl, webDavClientCaldav);
      var cardDavDataAccess = new CardDavDataAccess (carddavUrl, webDavClientCarddav);

      return await GetUserResources (calDavDataAccess, cardDavDataAccess);
    }
        private void TestServerConnection()
        {
            const string connectionTestCaption = "Test settings";

              try
              {
            StringBuilder errorMessageBuilder = new StringBuilder();
            if (!ValidateCalendarUrl (errorMessageBuilder))
            {
              MessageBox.Show (errorMessageBuilder.ToString(), "The calendar Url is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
              return;
            }

            var dataAccess = new CalDavDataAccess (
            new Uri (_calenderUrlTextBox.Text),
            new CalDavWebClient (
                _userNameTextBox.Text,
                _passwordTextBox.Text,
                TimeSpan.Parse (ConfigurationManager.AppSettings["calDavConnectTimeout"]),
                TimeSpan.Parse (ConfigurationManager.AppSettings["calDavReadWriteTimeout"]),
                Boolean.Parse (ConfigurationManager.AppSettings["disableCertificateValidation"]),
                Boolean.Parse (ConfigurationManager.AppSettings["enableSsl3"]),
                Boolean.Parse (ConfigurationManager.AppSettings["enableTls12"]))
            );

            if (!dataAccess.IsCalendarAccessSupported())
              MessageBox.Show ("The specified Url does not support calendar access!", connectionTestCaption);

            if (!dataAccess.IsResourceCalender())
              MessageBox.Show ("The specified Url is not a calendar!", connectionTestCaption);

            if (!dataAccess.IsWriteableCalender())
              MessageBox.Show ("The specified Url is a read-only calendar!", connectionTestCaption);

            if (!dataAccess.DoesSupportCalendarQuery ())
              MessageBox.Show ("The specified Url does not support Calendar Queries!", connectionTestCaption);

            MessageBox.Show ("Connection test successful.", connectionTestCaption);
              }
              catch (Exception x)
              {
            MessageBox.Show (x.Message, connectionTestCaption);
              }
        }
    private OutlookSynchronizer CreateEventSynchronizer (Options options)
    {
      var calDavDataAccess = new CalDavDataAccess (
          new Uri (options.CalenderUrl),
          CreateWebDavClient (options, _calDavConnectTimeout));

      var storageDataDirectory = _profileDataDirectoryFactory (options.Id);

      var entityRelationDataAccess = new EntityRelationDataAccess<string, DateTime, OutlookEventRelationData, Uri, string> (storageDataDirectory);

      return CreateEventSynchronizer (options, calDavDataAccess, entityRelationDataAccess);
    }
    private async Task<Uri> DoAutoDiscovery (Uri autoDiscoveryUri, IWebDavClient webDavClient)
    {
      var calDavDataAccess = new CalDavDataAccess (autoDiscoveryUri, webDavClient);

      var foundCalendars = await calDavDataAccess.GetUserCalendars();

      if (foundCalendars.Count > 0)
      {
        using (ListCalendarsForm listCalendarsForm = new ListCalendarsForm (foundCalendars))
        {
          if (listCalendarsForm.ShowDialog() == DialogResult.OK)
          {
            return new Uri (autoDiscoveryUri.GetLeftPart (UriPartial.Authority) + listCalendarsForm.getCalendarUri());
          }
        }
      }
      else
      {
        MessageBox.Show ("No calendars were found via autodiscovery!", c_connectionTestCaption);
      }
      return null;
    }
    private async Task TestGoogleConnection ()
    {
      StringBuilder errorMessageBuilder = new StringBuilder ();

      if (!OptionTasks.ValidateGoogleEmailAddress (errorMessageBuilder, _emailAddressTextBox.Text)) 
      {
        MessageBox.Show (errorMessageBuilder.ToString(), "The Email Address is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
      }
      if (UsedServerAdapterType == ServerAdapterType.GoogleTaskApi &&
          !string.IsNullOrWhiteSpace (_calenderUrlTextBox.Text) &&
          _calenderUrlTextBox.Text != c_googleDavBaseUrl)
      {
        var service = await OAuth.Google.GoogleHttpClientFactory.LoginToGoogleTasksService (_emailAddressTextBox.Text);

        try
        {
          TaskList task = await service.Tasklists.Get (_calenderUrlTextBox.Text).ExecuteAsync();
        }
        catch (Exception)
        {
          errorMessageBuilder.AppendFormat ("The tasklist with id '{0}' is invalid.", _calenderUrlTextBox.Text);
          MessageBox.Show(errorMessageBuilder.ToString(), "The tasklist is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
          return;
        }
        TestResult result = new TestResult (ResourceType.TaskList, CalendarProperties.None, AddressBookProperties.None);

        OptionTasks.DisplayTestReport (result, false, _dependencies.SelectedSynchronizationModeDisplayName, _dependencies.OutlookFolderType, UsedServerAdapterType);
        return;
      }

      if (!OptionTasks.ValidateWebDavUrl (_calenderUrlTextBox.Text, errorMessageBuilder, false))
      {
        MessageBox.Show (errorMessageBuilder.ToString(), "The CalDav/CardDav Url is invalid", MessageBoxButtons.OK, MessageBoxIcon.Error);
        return;
      }

      var enteredUri = new Uri (_calenderUrlTextBox.Text);
      var webDavClient = CreateWebDavClient ();

      Uri autoDiscoveredUrl;
      ResourceType autoDiscoveredResourceType;

      if (ConnectionTester.RequiresAutoDiscovery (enteredUri))
      {
        var calDavDataAccess = new CalDavDataAccess(enteredUri, webDavClient);
        IReadOnlyList<Tuple<Uri, string, string>> foundCaldendars = await calDavDataAccess.GetUserCalendarsNoThrow(false);

        var cardDavDataAccess = new CardDavDataAccess(enteredUri, webDavClient);
        IReadOnlyList<Tuple<Uri, string>> foundAddressBooks = await cardDavDataAccess.GetUserAddressBooksNoThrow(true);

        var service = await OAuth.Google.GoogleHttpClientFactory.LoginToGoogleTasksService(_emailAddressTextBox.Text);

        TaskLists taskLists = await service.Tasklists.List().ExecuteAsync();

        if (foundCaldendars.Count > 0 || foundAddressBooks.Count > 0 || taskLists.Items.Any())
        {
          ResourceType initalResourceType;
          if (_dependencies.OutlookFolderType == OlItemType.olContactItem)
          {
            initalResourceType = ResourceType.AddressBook;
          }
          else if (_dependencies.OutlookFolderType == OlItemType.olTaskItem)
          {
            initalResourceType = ResourceType.TaskList;
          }
          else
          {
            initalResourceType = ResourceType.Calendar;
          }

          using (SelectResourceForm listCalendarsForm =
              new SelectResourceForm(
                  foundCaldendars,
                  foundAddressBooks,
                  taskLists.Items.Select(i => Tuple.Create(i.Id, i.Title)).ToArray(),
                  initalResourceType))
          {
            if (listCalendarsForm.ShowDialog() == DialogResult.OK)
            {
              if (listCalendarsForm.ResourceType == ResourceType.TaskList)
              {
                autoDiscoveredUrl = null;
                _calenderUrlTextBox.Text = listCalendarsForm.SelectedUrl;
                UsedServerAdapterType = ServerAdapterType.GoogleTaskApi;
              }
              else
              {
                autoDiscoveredUrl = new Uri(enteredUri.GetLeftPart(UriPartial.Authority) + listCalendarsForm.SelectedUrl);
                UsedServerAdapterType = ServerAdapterType.WebDavHttpClientBasedWithGoogleOAuth;
              }
              autoDiscoveredResourceType = listCalendarsForm.ResourceType;
            }
            else
            {
              autoDiscoveredUrl = null;
              autoDiscoveredResourceType = ResourceType.None;
            }
          }
        }
        else
        {
          MessageBox.Show("No resources were found via autodiscovery!", OptionTasks.ConnectionTestCaption);
          autoDiscoveredUrl = null;
          autoDiscoveredResourceType = ResourceType.None;
        }
      }
      else
      {
        var result = await ConnectionTester.TestConnection (enteredUri, webDavClient, ResourceType.None);
        if (result.ResourceType != ResourceType.None)
        {
          _settingsFaultFinder.FixSynchronizationMode (result);
        }
        OptionTasks.DisplayTestReport (
            result,
            _dependencies.SelectedSynchronizationModeRequiresWriteableServerResource,
            _dependencies.SelectedSynchronizationModeDisplayName,
            _dependencies.OutlookFolderType,
            UsedServerAdapterType);
        return;
      }

      if (autoDiscoveredUrl != null)
      {
        _calenderUrlTextBox.Text = autoDiscoveredUrl.ToString();
        var finalResult =
          await ConnectionTester.TestConnection(autoDiscoveredUrl, webDavClient, autoDiscoveredResourceType);
        _settingsFaultFinder.FixSynchronizationMode(finalResult);

        OptionTasks.DisplayTestReport(
          finalResult,
          _dependencies.SelectedSynchronizationModeRequiresWriteableServerResource,
          _dependencies.SelectedSynchronizationModeDisplayName,
          _dependencies.OutlookFolderType,
          UsedServerAdapterType);
      }
      else if (UsedServerAdapterType == ServerAdapterType.GoogleTaskApi)
      {
        TestResult result = new TestResult(ResourceType.TaskList, CalendarProperties.None, AddressBookProperties.None);

        OptionTasks.DisplayTestReport (result,false,_dependencies.SelectedSynchronizationModeDisplayName, _dependencies.OutlookFolderType, UsedServerAdapterType);
      }
    }
    public async Task<ServerResources> GetServerResources (NetworkSettingsViewModel networkSettings, GeneralOptions generalOptions)
    {
      var trimmedUrl = CalenderUrl.Trim();
      var url = new Uri (trimmedUrl.EndsWith ("/") ? trimmedUrl : trimmedUrl + "/");

      var webDavClient = CreateWebDavClient (networkSettings, generalOptions);
      var calDavDataAccess = new CalDavDataAccess (url, webDavClient);
      var foundResources = await calDavDataAccess.GetUserResourcesNoThrow (false);

      var foundAddressBooks = new[] { new AddressBookData (new Uri ("googleApi://defaultAddressBook"), "Default AddressBook") };

      var service = await GoogleHttpClientFactory.LoginToGoogleTasksService (EmailAddress, SynchronizerFactory.CreateProxy (networkSettings.CreateProxyOptions()));
      var taskLists = await service.Tasklists.List().ExecuteAsync();
      var taskListsData = taskLists?.Items.Select (i => new TaskListData (i.Id, i.Title)).ToArray() ?? new TaskListData[] { };

      return new ServerResources (foundResources.CalendarResources, foundAddressBooks, taskListsData);
    }
    public static CalDavDataAccess CreateCalDavDataAccess (
        string calenderUrl,
        string username,
        string password,
        TimeSpan timeout,
        ServerAdapterType serverAdapterType)
    {
      var productAndVersion = GetProductAndVersion();

      var calDavDataAccess = new CalDavDataAccess (
          new Uri (calenderUrl),
          new CalDavClient (
              () => CreateHttpClient (username, password, timeout, serverAdapterType),
              productAndVersion.Item1,
              productAndVersion.Item2));
      return calDavDataAccess;
    }
    private async Task<bool> TestCalendar (CalDavDataAccess calDavDataAccess, StringBuilder errorMessageBuilder)
    {
      bool hasError = false;

      if (!await calDavDataAccess.IsCalendarAccessSupported())
      {
        errorMessageBuilder.AppendLine ("- The specified Url does not support calendar access.");
        hasError = true;
      }

      if (!await calDavDataAccess.IsWriteable())
      {
        errorMessageBuilder.AppendLine ("- The specified Url is a read-only calendar.");
        hasError = true;
      }

      if (!await calDavDataAccess.DoesSupportCalendarQuery())
      {
        errorMessageBuilder.AppendLine ("- The specified Url does not support Calendar Queries.");
        hasError = true;
      }

      if (_folderType != OlItemType.olAppointmentItem && _folderType != OlItemType.olTaskItem)
      {
        errorMessageBuilder.AppendLine ("- The outlook folder is not a calendar or task folder, or there is no folder selected.");
        hasError = true;
      }

      return hasError;
    }
    private ISynchronizer CreateEventSynchronizer (Options options)
    {
      var calDavDataAccess = new CalDavDataAccess (
          new Uri (options.CalenderUrl),
          CreateWebDavClient (options, _calDavConnectTimeout));

      var storageDataDirectory = Path.Combine (
          _applicationDataDirectory,
          options.Id.ToString()
          );

      var entityRelationDataAccess = new EntityRelationDataAccess<string, DateTime, OutlookEventRelationData, Uri, string> (storageDataDirectory);

      return CreateEventSynchronizer (options, calDavDataAccess, entityRelationDataAccess);
    }
    private async Task<AutoDiscoveryResult> DoAutoDiscovery (Uri autoDiscoveryUri, IWebDavClient webDavClient, bool useWellKnownUrl)
    {
      IReadOnlyList<Tuple<Uri, string>> foundResources;

      var calDavDataAccess = new CalDavDataAccess (autoDiscoveryUri, webDavClient);
      foundResources = await calDavDataAccess.GetUserCalendars (useWellKnownUrl);

      if (foundResources.Count > 0)
      {
        using (ListCalendarsForm listCalendarsForm = new ListCalendarsForm (foundResources))
        {
          if (listCalendarsForm.ShowDialog() == DialogResult.OK)
            return new AutoDiscoveryResult (new Uri (autoDiscoveryUri.GetLeftPart (UriPartial.Authority) + listCalendarsForm.getCalendarUri()), false);
          else
            return new AutoDiscoveryResult (null, true);
        }
      }
      else
      {
        MessageBox.Show ("No resources were found via autodiscovery!", c_connectionTestCaption);
      }
      return new AutoDiscoveryResult (null, false);
    }