require('../fm.js');
require('../fm.websync.js');
fm.xhr.setDisableBinary(true);

var proxyOptions = {
    user: 'smg',
    pass: 'smg',
    port: 808,
    host: 'localhost',
    enabled: false
}


var rr = require('../XmlHttpRequest');
rr.setProxy(proxyOptions)
XMLHttpRequest = rr.XMLHttpRequest;


//smg encryptor module
var Encryptor = require('../../Encrypter/Encrypt.js');
var enc = new Encryptor();


//smg compressor module
var Compressor = require('../../compressor/compressor');
var compressor = new Compressor();

var UserType = {
    Windows: 1, Linux: 2, LinuxConsole: 3, WindowsService: 4, Silverlight: 5, Javascript: 6, defualt: 7
}

//var url = 'http://manager.smg.com.tr/smgpush.ashx';
var url = 'http://newmanager.smg.com.tr/smgpush';
var client;
var retry = true;
var self;
var channel = '/smg';


var user = {
    IP: "1.1.1.1",
    ClientId: "00000000-0000-0000-0000-000000000000",
    username: "smg",
    password: "smg123",
    deviceID: 7786,
    version: "1.1.1.1",
    isAdmin: false,
    deviceName: "dev",
    userType: UserType.Linux
};


function onNotify(p) {
    console.log("notify:" + p.getTag());
}


var data=JSON.stringify(enc.Encrypt(JSON.stringify(user)));
console.log(data);
client = new fm.websync.client(url);
client.addOnNotify(onNotify);
client.connect({
    MetaJson: data,
    onSuccess: function (e) {
        console.log("connected");
    },
    onFailure: function (e) {
        console.log("failure")
        e.setRetry(retry);
    }, RetryBackoff: function (e) {
        console.log("retrying..")

        if (e.Index == 0) {
            return 1000;
        }
        //return e.LastTimeout * 3;
        return 2000
    }
});

