'use strict';

import {Service, ServiceSettings} from 'blow-service';
import {DataConnector} from './connectors/DataConnector';
import * as connectors from './connectors/connectors';
import {Collection} from './Collection';

export class DataService extends Service<DataConnector> {

  protected _getConnector(connectorTypeId: string): any {
    connectorTypeId = connectorTypeId.toLowerCase();

    if (connectorTypeId === 'mongodb') {
      return connectors.MongoDBConnector;
    } else if (connectorTypeId === 'memory') {
      return connectors.MemoryConnector;
    }
    throw new Error(`Unrecognized connector type: ${connectorTypeId}`);
  }

  protected _getConnection(name?: string): DataConnector {
    if (name) {
      return this.getConnection(name);
    } else {
      return this.getDefaultConnection();
    }
  }

  collection<T>(name: string, connectionName?: string): Collection<T> {
    return new Collection<T>(name, this._getConnection(connectionName));
  }

  static create(settings: ServiceSettings): DataService {
    return new this(settings);
  }
}
