const axios = require('axios')

const accountInfo = {
  Username: 'asanservice.sms@gmail.com',
  Password: '9u&amp;8[Ve9',
  Number: '5000467341',
  url: 'http://www.afe.ir/WebService/V4/BoxService.asmx'
}

/// /////// http post soap method
module.exports.SMS = (mobileNumbersArray, message) => {
  return new Promise((resolve, reject) => {
    function renderMobileNumbers(array) {
      return array.map(item => `<string>${item}</string>`).join('')
    }

    const xml = `<?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <SendMessage xmlns="http://www.afe.ir/">
        <Username>${accountInfo.Username}</Username>
        <Password>${accountInfo.Password}</Password>
        <Number>${accountInfo.Number}</Number>
          <Mobile>
            ${renderMobileNumbers(mobileNumbersArray)}
          </Mobile>
          <Message>${message}</Message>
          <Type>1</Type>
        </SendMessage>
      </soap:Body>
    </soap:Envelope>`

    axios
      .post(accountInfo.url, xml, {
        headers: {
          'Content-Type': 'text/xml; charset=utf-8',
          SOAPAction: 'http://www.afe.ir/SendMessage'
        }
      })
      .then(res => {
        resolve(res)
      })
      .catch(err => {
        console.log(
          'this is sms module error ------------------ status code: ',
          err.response.status,
          'error message: ',
          err.response.data
        )
        resolve(err)
      })
  })
}
