//
// SrvUserOnline.cs
//
// Copyright (c) 2007 Lukas Lipka.
//

using System;
using System.Collections.Generic;
using System.Text;

using IcqSharp.Base;
using IcqSharp.Util;

namespace IcqSharp.Packets
{
    [SnacPacketType (0x0003, 0x000B)]
    class SrvUserOnline : PacketHandler
    {
        private CliIconRequest icon_req = null;

        byte[] xstatus_values = 
        { 
            0x01, 0xD8, 0xD7, 0xEE, 0xAC, 0x3B, 0x49, 0x2A, 0xA5, 0x8D, 0xD3, 0xD8, 0x77, 0xE6, 0x6B, 0x92, // angry
            0x5A, 0x58, 0x1E, 0xA1, 0xE5, 0x80, 0x43, 0x0C, 0xA0, 0x6F, 0x61, 0x22, 0x98, 0xB7, 0xE4, 0xC7, // taking a bath
            0x83, 0xC9, 0xB7, 0x8E, 0x77, 0xE7, 0x43, 0x78, 0xB2, 0xC5, 0xFB, 0x6C, 0xFC, 0xC3, 0x5B, 0xEC, // tired
            0xE6, 0x01, 0xE4, 0x1C, 0x33, 0x73, 0x4B, 0xD1, 0xBC, 0x06, 0x81, 0x1D, 0x6C, 0x32, 0x3D, 0x81, // party
            0x8C, 0x50, 0xDB, 0xAE, 0x81, 0xED, 0x47, 0x86, 0xAC, 0xCA, 0x16, 0xCC, 0x32, 0x13, 0xC7, 0xB7, // beer
            0x3F, 0xB0, 0xBD, 0x36, 0xAF, 0x3B, 0x4A, 0x60, 0x9E, 0xEF, 0xCF, 0x19, 0x0F, 0x6A, 0x5A, 0x7F, // thinking
            0xF8, 0xE8, 0xD7, 0xB2, 0x82, 0xC4, 0x41, 0x42, 0x90, 0xF8, 0x10, 0xC6, 0xCE, 0x0A, 0x89, 0xA6, // eating
            0x80, 0x53, 0x7D, 0xE2, 0xA4, 0x67, 0x4A, 0x76, 0xB3, 0x54, 0x6D, 0xFD, 0x07, 0x5F, 0x5E, 0xC6, // watching rv
            0xF1, 0x8A, 0xB5, 0x2E, 0xDC, 0x57, 0x49, 0x1D, 0x99, 0xDC, 0x64, 0x44, 0x50, 0x24, 0x57, 0xAF, // meeting
            0x1B, 0x78, 0xAE, 0x31, 0xFA, 0x0B, 0x4D, 0x38, 0x93, 0xD1, 0x99, 0x7E, 0xEE, 0xAF, 0xB2, 0x18, // coffee
            0x61, 0xBE, 0xE0, 0xDD, 0x8B, 0xDD, 0x47, 0x5D, 0x8D, 0xEE, 0x5F, 0x4B, 0xAA, 0xCF, 0x19, 0xA7, // listening to music
            0x48, 0x8E, 0x14, 0x89, 0x8A, 0xCA, 0x4A, 0x08, 0x82, 0xAA, 0x77, 0xCE, 0x7A, 0x16, 0x52, 0x08, // business
            0x10, 0x7A, 0x9A, 0x18, 0x12, 0x32, 0x4D, 0xA4, 0xB6, 0xCD, 0x08, 0x79, 0xDB, 0x78, 0x0F, 0x09, // camera
            0x6F, 0x49, 0x30, 0x98, 0x4F, 0x7C, 0x4A, 0xFF, 0xA2, 0x76, 0x34, 0xA0, 0x3B, 0xCE, 0xAE, 0xA7, // having fun
            0x12, 0x92, 0xE5, 0x50, 0x1B, 0x64, 0x4F, 0x66, 0xB2, 0x06, 0xB2, 0x9A, 0xF3, 0x78, 0xE4, 0x8D, // on the phone
            0xD4, 0xA6, 0x11, 0xD0, 0x8F, 0x01, 0x4E, 0xC0, 0x92, 0x23, 0xC5, 0xB6, 0xBE, 0xC6, 0xCC, 0xF0, // gaming
            0x60, 0x9D, 0x52, 0xF8, 0xA2, 0x9A, 0x49, 0xA6, 0xB2, 0xA0, 0x25, 0x24, 0xC5, 0xE9, 0xD2, 0x60, // studying
            0x63, 0x62, 0x73, 0x37, 0xA0, 0x3F, 0x49, 0xFF, 0x80, 0xE5, 0xF7, 0x09, 0xCD, 0xE0, 0xA4, 0xEE, // shopping
            0x1F, 0x7A, 0x40, 0x71, 0xBF, 0x3B, 0x4E, 0x60, 0xBC, 0x32, 0x4C, 0x57, 0x87, 0xB0, 0x4C, 0xF1, // feeling sick
            0x78, 0x5E, 0x8C, 0x48, 0x40, 0xD3, 0x4C, 0x65, 0x88, 0x6F, 0x04, 0xCF, 0x3F, 0x3F, 0x43, 0xDF, // sleeping
            0xA6, 0xED, 0x55, 0x7E, 0x6B, 0xF7, 0x44, 0xD4, 0xA5, 0xD4, 0xD2, 0xE7, 0xD9, 0x5C, 0xE8, 0x1F, // surfing
            0x12, 0xD0, 0x7E, 0x3E, 0xF8, 0x85, 0x48, 0x9E, 0x8E, 0x97, 0xA7, 0x2A, 0x65, 0x51, 0xE5, 0x8D, // browsing
            0xBA, 0x74, 0xDB, 0x3E, 0x9E, 0x24, 0x43, 0x4B, 0x87, 0xB6, 0x2F, 0x6B, 0x8D, 0xFE, 0xE5, 0x0F, // working
            0x63, 0x4F, 0x6B, 0xD8, 0xAD, 0xD2, 0x4A, 0xA1, 0xAA, 0xB9, 0x11, 0x5B, 0xC2, 0x6D, 0x05, 0xA1, // typing
            0x2C, 0xE0, 0xE4, 0xE5, 0x7C, 0x64, 0x43, 0x70, 0x9C, 0x3A, 0x7A, 0x1C, 0xE8, 0x78, 0xA7, 0xDC, // barbecue
            0x10, 0x11, 0x17, 0xC9, 0xA3, 0xB0, 0x40, 0xF9, 0x81, 0xAC, 0x49, 0xE1, 0x59, 0xFB, 0xD5, 0xD4, // pda
            0x16, 0x0C, 0x60, 0xBB, 0xDD, 0x44, 0x43, 0xF3, 0x91, 0x40, 0x05, 0x0F, 0x00, 0xE6, 0xC0, 0x09, // mobile
            0x64, 0x43, 0xC6, 0xAF, 0x22, 0x60, 0x45, 0x17, 0xB5, 0x8C, 0xD7, 0xDF, 0x8E, 0x29, 0x03, 0x52, // man
            0x16, 0xF5, 0xB7, 0x6F, 0xA9, 0xD2, 0x40, 0x35, 0x8C, 0xC5, 0xC0, 0x84, 0x70, 0x3C, 0x98, 0xFA, // on WC
            0x3F, 0xB0, 0xBD, 0x36, 0xAF, 0x3B, 0x4A, 0x60, 0x9E, 0xEF, 0xCF, 0x19, 0x0F, 0x6A, 0x5A, 0x7E, // smoking
            0xE6, 0x01, 0xE4, 0x1C, 0x33, 0x73, 0x4B, 0xD1, 0xBC, 0x06, 0x81, 0x1D, 0x6C, 0x32, 0x3D, 0x82, // sex
            0x63, 0x14, 0x36, 0xFF, 0x3F, 0x8A, 0x40, 0xD0, 0xA5, 0xCB, 0x7B, 0x66, 0xE0, 0x51, 0xB3, 0x64, // to be or not to be
            0xB7, 0x08, 0x67, 0xF5, 0x38, 0x25, 0x43, 0x27, 0xA1, 0xFF, 0xCF, 0x4C, 0xC1, 0x93, 0x97, 0x97, // 
            0xDD, 0xCF, 0x0E, 0xA9, 0x71, 0x95, 0x40, 0x48, 0xA9, 0xC6, 0x41, 0x32, 0x06, 0xD6, 0xF2, 0x80, // love
        };

        public SrvUserOnline (Session session, SnacPacket packet)
            : base (session, packet)
        {
            int length = (int)packet.Data[0];
            string uin = Encoding.UTF8.GetString (packet.Data, 1, length);

            Log.Debug("{0} status changed!", uin);

            TlvList tlvs = TlvList.Parse (packet.Data, length + 5, packet.Data.Length - (length + 5));           
            Contact contact = session.ContactList[uin];

            if (contact == null) {
                Log.Error ("Received status notification for a user not in contact list!");
                return;
            }

            Status status = Status.Offline;
            Status last_status = contact.Status;
            XStatus xstatus = XStatus.None;

            // Parse capabilities and check X-Status
            if (tlvs[0x0D] != null) {
                for (int i = 0; i < (tlvs[0x0D].Data.Length / 16); i++) {
                    byte[] capability = new byte[16];
                    Array.Copy (tlvs[0x0D].Data, i * 16, capability, 0, 16);

                    XStatus check = CheckXStatus (capability);

                    if (check != XStatus.None) {
                        xstatus = check;
                        continue;
                    }

                    contact.Capabilities.Add (new Clsid (capability));
                }
            }

            // Online time
            if (tlvs[0x0f] != null) {
                uint online_time = tlvs[0x0f].ToUInt32 ();
                contact.OnlineSince = DateTime.Now.AddSeconds (-online_time);
            }

            // Status
            if (tlvs[0x06] != null) {
                //ushort status_flags = LittleEndianBitConverter.Big.ToUInt16(packet.Data, offset + 4);
                status = GetStatus (EndianBitConverter.Big.ToUInt16 (tlvs[0x06].Data, 2));
            }

            // Buddy icon
            if (tlvs[0x1d] != null) {
                ushort id = LittleEndianBitConverter.Big.ToUInt16 (tlvs[0x1d].Data, 0);
                byte flags = tlvs[0x1d].Data[2];
                byte hash_length = tlvs[0x1d].Data[3];

                byte[] hash = new byte[hash_length];
                Array.Copy (tlvs[0x1d].Data, 4, hash, 0, hash_length);

                //icon_req = new CliIconRequest(uin, hash);
                //Log.Debug("Icon:\nID: {0}\nFlags: {1}\nLength: {2}\nHash: {3}", id, flags, hash_length, hash);
            }

            Log.Debug("{0} ({1}) - Status: {2}, XStatus: {3}", contact.Nickname, contact.Uin, status, xstatus);

            contact.SetStatus (status, xstatus);

            // If status was previously offline assume that
            // the contact has just connected
            if (last_status == Status.Offline)
            {
                Log.Debug("{0} ({1}) has come online (online since: {2})!", contact.Nickname, contact.Uin, contact.OnlineSince);

                contact.OnSignedOn(contact);
                session.ContactList.OnContactSignedOn(contact);
            }

            contact.OnStatusChanged (contact);
            session.ContactList.OnContactStatusChanged(contact);
        }

        private XStatus CheckXStatus (byte[] capability)
        {
            for (int i = 0; i < xstatus_values.Length / 16; i++) {
                bool found = true;

                for (int j = 0; j < 16; j++) {
                    if (capability[j] != xstatus_values[(i * 16) + j]) {
                        found = false;
                        break;
                    }
                }

                if (found)
                    return (XStatus)(i + 1);
            }

            return XStatus.None;
        }

        private Status GetStatus (ushort status)
        {
            switch (status) {
                case 0x0001:
                    return Status.Away;

                case 0x0005:
                    return Status.NA;

                case 0x0020:
                    return Status.FreeForChat;

                case 0x0011:
                    return Status.Occupied;

                case 0x0002:
                    return Status.DoNotDisturb;

                case 0x0100:
                    return Status.Invisible;

                case 0xffff:
                    return Status.Offline;

                default:
                case 0x0000:
                    return Status.Online;
            }
        }

        public override List<FlapPacket> Response (FlapPacket packet)
        {
            List<FlapPacket> responses = new List<FlapPacket> ();

            if (icon_req != null)
                responses.Add (icon_req);

            return responses;
        }
    }
}
