var options;
var vertoHandle;
var currentCall;
var foo; 

new QWebChannel(qt.webChannelTransport, function (channel) {
    foo = channel.objects.foo;
    
    foo.makeCall.connect(function (makeCallOptions) {
        
        var makeCallVideoParams = {
            // 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,
        };
        
        vertoHandle.videoParams(makeCallVideoParams);
        /*
        makeCallOptions = {
            // Extension to dial.
            destination_number: targetNumber,
            caller_id_name: 'Test Guy',
            caller_id_number: "999",
            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',
        };
        */
       
        currentCall = vertoHandle.newCall(makeCallOptions);
    });
    
    foo.hangupCall.connect(function () {
        currentCall.hangup();
    });
    
    foo.answerCall.connect(function (){
        
         var makeCallVideoParams = {
            // 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,
        };
        
        vertoHandle.videoParams(makeCallVideoParams);
        
        currentCall.answer({useVideo:true});
    });
    
    foo.requestDevices.connect(function () {
        
        var devices = {
            audioInput:$.verto.audioInDevices,
            audioOutput:$.verto.audioInDevices,
            videoDevices: $.verto.videoDevices
        };
        
        foo.onRequestDevices(devices);
    });
    
    
    options = {
        authName:foo.authName,
        domain:foo.domain,
        password:foo.password,
        socketUrl:foo.socketUrl,
        ringFile:foo.ringFile,
    };

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

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

    function onWSLogin(verto, success) {
        console.log('onWSLogin', success);
        foo.onLogin(success);
    };

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

    function onDialogState(d) {
        
        foo.onDialogState(d.state.name);
        
        if (!currentCall) {
            currentCall = d;
        } else {
            console.log("currentCall allready is set at state : "+ d.state.name);
        }
        
        
        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...
                currentCall = null;
                break;
            default:
                console.log("undefined state case: " + d.state.name);
        }
        
    }



});

  /*
    
    // Handler for .ready() called.
    console.error("TEST!!!");
    new QWebChannel(qt.webChannelTransport, function(channel) {

      // Connect to a signal:
      channel.objects.foo.newCall.connect(function() {
          // This callback will be invoked whenever the signal is emitted on the C++/QML side.
          console.log(arguments);
      });

      // To make the object known globally, assign it to the window object, i.e.:
      window.foo = channel.objects.foo;

      // Invoke a method:
      window.foo.onNewCall("arg1", "arg2", function(returnValue) {
            // This callback will be invoked when myMethod has a return value. Keep in mind that
            // the communication is asynchronous, hence the need for this callback.
            console.log(returnValue);
      });

      // Read a property value, which is cached on the client side:
      console.log(foo.authName);

      // Writing a property will instantly update the client side cache.
      // The remote end will be notified about the change asynchronously
      //foo.myProperty = "Hello World!";

      // To get notified about remote property changes,
      // simply connect to the corresponding notify signal:
      /*foo.onMyPropertyChanged.connect(function(newValue) {
          console.log(newValue);
      });*//*

      // One can also access enums that are marked with Q_ENUM:
      //console.log(foo.MyEnum.MyEnumerator);
      
  });
*/
