const xapi = require('xapi');
const MYSITE = '<yourWebexSite>.';
const domain = "webex.com";
var meetingID = '';
const REGEXP_WEBEX = /(webex.com)$/;
const REGEXP_ALPHA = /^[a-zA-Z]+$/;
const REGEX_WEBEXID = /^\d{9}$/;
const REGEXP_AT =/^.+@.+$/;

function onlineMeeting() {
    xapi.event.on('UserInterface Extensions Panel Clicked', (event) => {
        console.log('event', event.PanelId);
        if (event.PanelId == 'online_conference') {
            //Intial press of conference button
            xapi.command('UserInterface Message TextInput Display', {
                FeedbackId: 'MeetingID',
                KeyboardState: 'Open',
                InputType: 'Numeric',
                Title: 'Join an online Meeting',
                Text: 'Enter just the Webex Meeting ID or Entire URI',
                Placeholder: 'Powered by VoIPNorm',
                SubmitText: 'Join'
            });
        }
    })
    xapi.event.on('UserInterface Message TextInput Response', (event) => {
        if (event.FeedbackId === 'MeetingID') {
            const meetingID = event.Text;
            //User enters full URI
            if(REGEXP_AT.exec(meetingID)){
              xapi.command('Dial', { Number: meetingID});
            }
            //User enters PMR address
            if (REGEXP_ALPHA.exec(meetingID)) {
                var uri = meetingID + '@' + MYSITE + domain;
                xapi.command('Dial', {Number: uri, DisplayName: "Webex PMR"});
            //User enter webex numeric ID
            } 
            if(REGEX_WEBEXID.exec(meetingID)) {
                var URI = meetingID+'@'+domain;
                xapi.command('Dial', {Number: URI, DisplayName: "Webex"});
            }else{
              xapi.command('UserInterface Message TextInput Display', {
                    FeedbackId: 'entireUrl',
                    Title: `Invalid Webex Meeting ID`,
                    Text: 'Please enter the entire meeting URI e.g 123456789@sitename.com.',
                    KeyboardState: 'Open',
                    Placeholder: 'Powered by VoIPNorm',
                    SubmitText: 'Join',
                  })
            }
        }
    })
}

function afterCall() {
    xapi.event.on('CallDisconnect', (event) => {
        if (REGEXP_WEBEX.exec(event.RequestedURI)){
          xapi.command("UserInterface Message Prompt Display", {
            FeedbackId: 'webex_feedback',
            Title: "How was the meeting experience", 
            Text: 'How would you rate this call', 
            'Option.1':'It was Great', 
            'Option.2':'Meeting ID was rejected.',
            Duration: 8
            
          });
          xapi.event.on('UserInterface Message Prompt Response', (event) => {
            console.log(event);
            if(event.FeedbackId === 'webex_feedback'){
              var displaytitle = '';
              var displaytext = '';
              switch(event.OptionId){
                case '1':
                  displaytitle = ':-)';
                  displaytext = 'Thank you, yet another satisfied customer!!!';
                  xapi.command("UserInterface Message Alert Display", {Title: displaytitle, Text: displaytext, Duration: 8});
                  break;
                case '2':
                  xapi.command('UserInterface Message TextInput Display', {
                    FeedbackId: 'entireUrl',
                    Title: `Lets Try Joining that Meeting Again`,
                    Text: 'Please enter the entire meeting URI e.g 12345678@sitename.com',
                    //InputType: 'Numeric',
                    Placeholder: 'Powered by VoIPNorm',
                    SubmitText: 'Join',
                    Duration: 30,
                  });
                  break;
              }
            }
          });
        }
    });
}
function dialWebexURL(){
  xapi.event.on('UserInterface Message TextInput Response', (event) => {
        if(event.FeedbackId === 'entireUrl'){
          var uri = event.Text;
          if(REGEXP_AT.exec(uri)){
            xapi.command('Dial', { Number: uri })
            .catch(e => {
              console.log("Dialing error:"+e)
            })
          }else{
            xapi.command('UserInterface Message TextInput Display', {
                    FeedbackId: 'entireUrl',
                    Title: `Invalid Meeting Id`,
                    Text: 'Please enter the entire meeting URI e.g 12345678@sitename.com',
                    //InputType: 'Numeric',
                    Placeholder: 'Powered by VoIPNorm',
                    SubmitText: 'Join',
                    Duration: 30,
                  })
            .catch(e => {
              console.log("Command error:"+e)
            });
          }
        }
    })
}

function onError() {
    xapi.on('error', (err) => {
        console.error(`Macro failed: ${err}`);
    });
}

function onReady(cb) {
    onlineMeeting();
    afterCall();
    dialWebexURL();
    onError();
    cb();
}

onReady(function () {
    console.log("Loading complete");
});