//V1.0.0
//include aliases notworking if blank
exports.onesignal_create_email_notification = async function (options) {
   // get appid
   const appid = this.parseOptional(options.appid, '*', process.env.ONESIGNAL_APP_ID)
   console.log(options.appid + " => " + appid)

   // get message internal name
   const osname = this.parseOptional(options.osname, '*', 'message_auto')
   console.log('Name: ' + options.osname + " => " + osname)

   // get email subject
   const email_subject = this.parseOptional(options.email_subject, '*', ' ')
   console.log('Subject: ' + options.email_subject + " => " + email_subject)

   const email_body = this.parseRequired(options.email_body, '*', 'No email_body specified ')
   console.log('Body: ' + options.email_body + " => " + email_body)


   const email_from_name = this.parseRequired(options.email_from_name, '*', 'No email_body specified ')
   console.log('From: ' + options.email_from_name + " => " + email_from_name)

   const email_from_address = this.parseRequired(options.email_from_address, '*', 'No email_from_address specified')
   console.log('From Address: ' + options.email_from_address + " => " + email_from_address)

   const email_reply_to_address = this.parseOptional(options.email_reply_to_address, '*', null)
   console.log('Reply to: ' + options.email_reply_to_address + " => " + email_reply_to_address)

   const email_preheader = this.parseOptional(options.email_preheader, '*', null)
   console.log('Preheader: ' + options.email_preheader + " => " + email_preheader)

   const include_email_tokens = this.parseOptional(options.include_email_tokens, '*', null)
   console.log('include_email_tokens: ' + options.include_email_tokens + " => " + include_email_tokens)

   const segment = this.parseOptional(options.segment, '*', 'All Email Subscriptions')
   console.log("Segment: " + options.segment + " => " + segment)

   const filter = this.parseOptional(options.filter, '*', null)
   console.log("Filter: " + options.filter + " => " + filter)

   const include_aliases = this.parseOptional(options.include_aliases, '*', null)
   console.log("include_aliases: " + options.include_aliases + " => " + include_aliases)

   const template_id = this.parseOptional(options.template_id, '*', null)
   console.log("Template ID: " + template_id + " => " + template_id)

   const fetch = require('node-fetch');

   const url = 'https://api.onesignal.com/notifications?c=email';
   const os_options = {
      method: 'POST',
      headers: { accept: 'application/json', 'content-type': 'application/json', Authorization: `Basic ${process.env.ONESIGNAL_REST_API_KEY}` },
      body: JSON.stringify({
         app_id: appid,
         email_subject: email_subject,
         email_body: email_body,
         template_id: template_id,
         name: osname,
         email_from_name: email_from_name,
         email_from_address: email_from_address,
         email_reply_to_address: email_reply_to_address,
         disable_email_click_tracking: options.disable_email_click_tracking,
         include_unsubscribed: options.include_unsubscribed,
         email_preheader: email_preheader,
         //include_aliases: include_aliases,
         included_segments: [segment],
         filters: filter,
         include_email_tokens: [include_email_tokens]
      }
      )
   };

   return fetch(url, os_options)
      .then(res => {

         return res.json().then(json => {
            json.status = res.status; // Add status to json response
            return json;
         });
      })
      .catch(err => console.error('Error:', err));

}
