using System;
using System.Text;
using System.Configuration;
using System.Collections.Generic;
using MarketData.Utils;
using Microsoft.Extensions.Configuration;

namespace MarketData.DataAccess
{
  [Serializable]
  public sealed class DataSourceEx
  {
    private string key;
    private string database;
    private string datasource;
    private string username;
    private string password;
    private string port;
    
    public DataSourceEx(IConfiguration configuration,String configurationKey)
    {
      Dictionary<String, String> dictionary = CreateConfigurationSettings(configuration, configurationKey);
      Database = dictionary["Database"];
      Datasource = dictionary["Datasource"];
      Username = dictionary["Username"];
      Password = dictionary["Password"];
    }

    private Dictionary<String, String> CreateConfigurationSettings(IConfiguration configuration, String configurationKey)
    {
      String marketData = configuration[configurationKey];

      String[] elements = marketData.Split(';');
      Dictionary<String, String> dictionary = new Dictionary<string, string>();
      foreach (String element in elements)
      {
        String[] valuePairs = element.Split('=');
        dictionary.Add(valuePairs[0], valuePairs[1]);
      }
      return dictionary;
    }

    public string Key
    {
      get { return key; }
      set { key = value; }
    }

    public string Database
    {
      get { return database; }
      set { database = value; }
    }

    public string Datasource
    {
      get { return datasource; }
      set { datasource = value; }
    }

    public string Port
    {
      get { return port; }
      set { port = value; }
    }

    public string Password
    {
      get { return password; }
      set { password = value; }
    }

    public string Username
    {
      get { return username; }
      set { username = value; }
    }

    public override String ToString()
    {
      StringBuilder sb = new StringBuilder();
      sb.Append("Key='").Append(key).Append("', ");
      sb.Append("Database='").Append(database).Append("', ");
      sb.Append("DataSource='").Append(datasource).Append("', ");
      sb.Append("Port='").Append(port).Append("', ");
      sb.Append("UserName='").Append(username).Append("', ");
      sb.Append("Password='").Append(Utility.AsteriskForString(password)).Append("'");
      return sb.ToString();
    }
  }
}