'use strict';

var logger = require('../utils/Logger').loggers.get('main');
var utilString = require('../../shared/utils/UtilString');

var PROVIDERS_DIR = "./providers/";
var errMsg = "Please configure 'phone' on serverConfiguration";

var phoneConfig, provider;

function setupPhone () {
	if (!phoneConfig) {
		if (config.get("phone")) {
			phoneConfig = config.get("phone");
		} else {
			logger.error(errMsg);
		}
	}
};

module.exports = {
	sendSMS: function (phoneNum, message, callback) {
		setupPhone();
		if (phoneConfig) {
			var provider = require(PROVIDERS_DIR + utilString.capitalizeFirstLetter(phoneConfig.provider));

			provider.sendSMS(phoneNum, message, function (err, result) {
				callback(err, result);
			});
		} else {
            if (typeof callback === 'function') {
                callback(errMsg);
            }
		}
	},
	validateNumber: function (phoneNum, callback) {
		setupPhone();
		if (phoneConfig) {
			var provider = require(PROVIDERS_DIR + utilString.capitalizeFirstLetter(phoneConfig.provider));

			provider.validateNumber(phoneNum, function (err, result) {
				callback(err, result);
			});
		} else {
            if (typeof callback === 'function') {
                callback(errMsg);
            }
		}
	},
	serviceName: function () {
		setupPhone();
		if (phoneConfig) {
			return phoneConfig.provider;
		} else {
			logger.error("Configuration value not found: 'phone'");
		}
	}
};