
var vertoHandle;
var currentCall;

options = {
    user: "1231",
    domain: "10.22.83.159",
    password:"nXGcxBpf7bt8WHC",
    socketUrl:"wss://pbx.elster.formular/rtc",
    ringFile:"sounds/bell_ring2.wav",
    stunServers: true
};

/*
 
  [
      {
        url: 'stun:stun.1und1.de',
      },
    ]
 */

(function () {
    var vertoCallbacks;
    $.verto.init({}, bootstrap);

    function bootstrap(status) {
        vertoHandle = new jQuery.verto({
            login: options.user+'@'+options.domain,
            passwd: options.password,
            socketUrl: options.socketUrl,
            ringFile: options.ringFile,
            //iceServers: options.stunServers,
            deviceParams: {
                // Asking for camera permissions and devices.
                useCamera: 'any',
                useMic: 'any',
                useSpeak: 'any',
            },
            tag: "video-container",
        }, vertoCallbacks);

        document.getElementById("make-call").addEventListener("click", makeCall);
        document.getElementById("hang-up-call").addEventListener("click", hangupCall);
        document.getElementById("answer-call").addEventListener("click", answerCall);
    };

    vertoCallbacks = {
        onWSLogin: onWSLogin,
        onWSClose: onWSClose,
        onDialogState: onDialogState
    };


    function onWSLogin(verto, success) {
        console.log('onWSLogin', success);
        if (success) {
            //onLoginSuccess code ... 
        } else {
            //onLoginfailed code ... 
        }
    };

    function onWSClose(verto, success) {
        console.log('onWSClose', success);
    };

    function onDialogState(d) {

        if (!currentCall) {
            currentCall = d;
        }

        switch (d.state.name) {
            case "ringing":
                console.log("onDialogState: " +d.state.name);
                break;
            case "trying":
                console.log("onDialogState: " +d.state.name);
                break;
            case "answering":
                console.log("onDialogState: " +d.state.name);
                break;
            case "active":
                console.log("onDialogState: " +d.state.name);
                break;
            case "hangup":
                console.log("onDialogState: " +d.state.name);
                console.log("Call ended with cause: " + d.cause);
                break;
            case "destroy":
                console.log("onDialogState: " +d.state.name);
                // Some kind of client side cleanup...
                break;
            default:
                console.log("undefined state case: " +d.state.name);
        }
    }

    function makeCall() {
                
        vertoHandle.videoParams({
            // Dimensions of the video feed to send.
            minWidth: 320,
            minHeight: 240,
            maxWidth: 640,
            maxHeight: 480,
            // The minimum frame rate of the client camera, Verto will fail if it's
            // less than this.
            minFrameRate: 15,
            // The maximum frame rate to send from the camera.
            vertoBestFrameRate: 30,
        });
                
        currentCall = vertoHandle.newCall({
            // Extension to dial.
            destination_number: '999',
            caller_id_name: 'Test Guy',
            caller_id_number: options.user,
            outgoingBandwidth: 'default',
            incomingBandwidth: 'default',
            useVideo:true,
            mirrorInput:true,
            // Enable stereo audio.
            useStereo: true,
            // You can pass any application/call specific variables here, and they will
            // be available as a dialplan variable, prefixed with 'verto_dvar_'.
            userVariables: {
                // Shows up as a 'verto_dvar_email' dialplan variable.
                email: 'test@test.com'
            },
            // Use a dedicated outbound encoder for this user's video.
            // NOTE: This is generally only needed if the user has some kind of
            // non-standard video setup, and is not recommended to use, as it
            // dramatically increases the CPU usage for the conference.
            dedEnc: false,
            // Example of setting the devices per-call.
            //useMic: 'any',
            //useSpeak: 'any',
        });
    };

    function hangupCall() {
        currentCall.hangup();
    };

    function answerCall() {
        currentCall.answer();
    };

})();