const axios = require('axios');
const qs = require('qs');

const SMS_API_HOST = "https://smsc.ru/sys/send.php";

class SmsCenter
{
	static send(phoneNumber, code)
	{
		console.log("SmsCenter", "propagateUser");

		return new Promise((resolve, reject) => 
		{
			const packet = qs.stringify({ 
				login: process.env.SMSC_LOGIN, 
				psw: process.env.SMSC_PASSWORD, 
				sender: "Evoleasing", 
				phones: `+${ phoneNumber }`, 
				mes: `Код верификации ЛК: ${ code }`, 
				charset: 'utf-8', 
				fmt: 3, 
			});

			console.log(packet);

			axios.post(SMS_API_HOST, packet, {
				"Content-Type": "application/x-www-form-urlencoded"
			})
			.then((smsSendResponse) => 
			{
				console.log(smsSendResponse.data);
				resolve();
			})
			.catch((smsSendError) => 
			{
				console.error(smsSendError);
				reject();
			});
		})
	}
}

module.exports = SmsCenter;