using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using TINK.Model.Services.CopriApi.ServerUris;
namespace TINK.Model.Connector
{
/// View model managing active uri and sets of uris.
public class CopriServerUriListViewModel : INotifyPropertyChanged
{
///
/// Object holding active uris.
///
private CopriServerUriList m_oUris;
///
/// Fired whenever a property changes.
///
public event PropertyChangedEventHandler PropertyChanged;
/// Maps uris to user fiendly descriptions.
private Dictionary uriToServerText;
/// Maps user fiendly descriptions to uris.
private Dictionary serverTextToUri;
public CopriServerUriListViewModel(CopriServerUriList p_oSource)
{
uriToServerText = new Dictionary {
{ CopriServerUriList.TINK_DEVEL, "TINK-Konrad-devellopment" },
{ CopriServerUriList.TINK_LIVE, "TINK-Konrad-live" },
{ CopriServerUriList.SHAREE_DEVEL, "Sharee-fr01-devellopment" },
{ CopriServerUriList.SHAREE_LIVE, "Sharee-fr01-live" }
};
serverTextToUri = uriToServerText.ToDictionary(x => x.Value, x => x.Key);
m_oUris = new CopriServerUriList(p_oSource);
NextActiveUri = m_oUris.ActiveUri;
}
///
/// Sets the active uri.
///
private Uri ActiveUri
{
set
{
if (m_oUris.ActiveUri.AbsoluteUri == value.AbsoluteUri)
{
/// Nothing to do.
return;
}
m_oUris = new CopriServerUriList(m_oUris.Uris.ToArray(), value);
}
}
/// Gets the known uris.
public IList ServerTextList
{
get
{
return m_oUris.Uris.Select(x => (uriToServerText.ContainsKey(x.AbsoluteUri) ? uriToServerText[x.AbsoluteUri] : x.AbsoluteUri)).OrderBy(x => x).ToList();
}
}
/// Holds the uri which will be applied after restart of app.
public Uri NextActiveUri { get; private set; }
/// Holds the uri which will be applied after restart.
public string NextActiveServerText
{
get
{
return uriToServerText.ContainsKey(NextActiveUri.AbsoluteUri) ? uriToServerText[NextActiveUri.AbsoluteUri] : NextActiveUri.AbsoluteUri;
}
set
{
NextActiveUri = new Uri(serverTextToUri.ContainsKey(value) ? serverTextToUri[value] : value);
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(CorpiServerUriDescription)));
}
}
/// Holds the description of the picker.
public string CorpiServerUriDescription
{
get
{
return m_oUris.ActiveUri.AbsoluteUri == NextActiveUri.AbsoluteUri
? "Aktiver Copri- Server"
: "Copri- Server.\r\nNeustart erforderlich für Wechsel!";
}
}
}
}