var oscar = require('./oscar');

var USER_STATUSES = { // 1st two bytes of user status field
    ONLINE: 0x0000,
    AWAY: 0x0001,
    DND: 0x0002,
    NA: 0x0004,
    OCCUPIED: 0x0010,
    FREE4CHAT: 0x0020,
    INVISIBLE: 0x0100,
    EVIL: 0x3000,
    DEPRESSION: 0x4000,
    ATHOME: 0x5000,
    ATWORK: 0x6000,
    LUNCH: 0x2001,
    OFFLINE: 0xFFFF
};


var aim = new oscar.OscarConnection({
    connection: {
        // username: '580221473', //'gifer@nsoft-s.com', // 580221473
        // password: 'Qwer1234'
        username: '329332113', //'gifer@nsoft-s.com', // 580221473
        password: 'A141592a'
    },
    other: {
        initialStatus: USER_STATUSES.ONLINE
    }

});

aim.on('typing', function(who, type) {
    if (type === oscar.TYPING_NOTIFY.START)
        type = 'started typing';
    else if (type === oscar.TYPING_NOTIFY.FINISH)
        type = 'finished typing';
    else if (type === oscar.TYPING_NOTIFY.TEXT_ENTERED)
        type = 'entered text';
    else
        type = 'closed the IM';
    console.log('test.js :: typing notification: ' + who + ' ' + type);
});

aim.on('im', function(text, sender, flags, when) {
    console.log('test.js :: received ' + (when ? 'offline ' : '')
        + 'IM from ' + sender.name + (when ? ' (on ' + when + ')' : '')
        + ': ' + text);
    if (when)
        return;
    aim.sendIM(sender.name, 'Preved Medved');
});

aim.on('missed', function(sender, numMissed, reason, channel) {
    console.log('test.js :: missed ' + numMissed + ' messages from ' + sender.name
        + '. Reason: ' + reason + '. Channel: ' + channel);
});

aim.on('contactonline', function(user) {
    var status = 'other';

    if (user.idleMins)
        status = 'idle (' + user.idleMins + ' mins)';
    else if (user.status === oscar.USER_STATUSES.ONLINE)
        status = 'available';
    else if (user.status === oscar.USER_STATUSES.AWAY)
        status = 'away';

    console.log('test.js :: ' + user.name + ' is now online and ' + status
        + (user.statusMsg ? ': ' + user.statusMsg : ''));

    // console.log('\nFull info:' + JSON.stringify(user));
});

aim.on('contactupdate', function(user) {
    var status = 'other';

    if (user.idleMins)
        status = 'idle (' + user.idleMins + ' mins)';
    else if (user.status === oscar.USER_STATUSES.ONLINE)
        status = 'available';
    else if (user.status === oscar.USER_STATUSES.AWAY)
        status = 'away';

    // console.log('test.js :: ' + user.name + ' is now ' + status + (user.statusMsg ? ': ' + user.statusMsg : ''));
});

aim.on('contactoffline', function(user) {
    console.log('test.js :: ' + user.name + ' is now offline');
});

aim.on('icon', function(who, icon, size) {
    console.log('test.js :: Got ' + size + ' buddy icon for ' + who);
});

aim.connect(function(err) {
    if (err)
        console.log('test.js :: Encountered error: ' + err);
    else {
        console.log('test.js :: ready!');
        // automatically check for offline messages
        aim.getOfflineMsgs();
    }

    // aim.sendIM('329332113', 'Preved!!!!!!!!!!!!!!! Medved');
    console.log('Profile: ' + aim.me);

    aim.getInfo('329332113', function (user) {
        console.log(user);
    });
});

//===========================================



/*var tt =
{
    type : 'icq',
    id   : '123456789',
    name : 'My ICQ Contacts',
    contacts : {
        'My Group' : [
            {
                name  : "Hobit",
                id    : "123456789",
                online: 1,
                state : "",
                mood  : ""
            }
        ]
    }
} */