﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

using Uber.Messages;
using Uber.HabboHotel.GameClients;
using Uber.Storage;

namespace Uber.HabboHotel.Users.Messenger
{
    class MessengerBuddy
    {
        private uint UserId;

        public Boolean UpdateNeeded;

        public uint Id
        {
            get
            {
                return UserId;
            }
        }

        public string Username
        {
            get
            {
                GameClient Client = UberEnvironment.GetGame().GetClientManager().GetClientByHabbo(UserId);

                if (Client != null)
                {
                    return Client.GetHabbo().Username;
                }
                else
                {
                    using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                    {
                        return dbClient.ReadString("SELECT username FROM users WHERE id = '" + UserId + "' LIMIT 1");
                    }
                }
            }
        }

        public string RealName
        {
            get
            {
                GameClient Client = UberEnvironment.GetGame().GetClientManager().GetClientByHabbo(UserId);

                if (Client != null)
                {
                    return Client.GetHabbo().RealName;
                }
                else
                {
                    using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                    {
                        return dbClient.ReadString("SELECT real_name FROM users WHERE id = '" + UserId + "' LIMIT 1");
                    }
                }
            }
        }

        public string Look
        {
            get
            {
                GameClient Client = UberEnvironment.GetGame().GetClientManager().GetClientByHabbo(UserId);

                if (Client != null)
                {
                    return Client.GetHabbo().Look;
                }

                return "";
            }
        }

        public string Motto
        {
            get
            {
                GameClient Client = UberEnvironment.GetGame().GetClientManager().GetClientByHabbo(UserId);

                if (Client != null)
                {
                    return Client.GetHabbo().Motto;
                }

                return "";
            }
        }

        public string LastOnline
        {
            get
            {
                GameClient Client = UberEnvironment.GetGame().GetClientManager().GetClientByHabbo(UserId);

                if (Client == null)
                {
                    using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
                    {
                        return dbClient.ReadString("SELECT last_online FROM users WHERE id = '" + UserId + "' LIMIT 1");
                    }
                }

                return "";
            }
        }

        public Boolean IsOnline
        {
            get
            {
                GameClient Client = UberEnvironment.GetGame().GetClientManager().GetClientByHabbo(UserId);

                if (Client != null && !Client.GetHabbo().GetMessenger().AppearOffline)
                {
                    return true;
                }

                return false;
            }
        }

        public Boolean InRoom
        {
            get
            {
                if (!IsOnline)
                {
                    return false;
                }

                GameClient Client = UberEnvironment.GetGame().GetClientManager().GetClientByHabbo(UserId);

                if (Client == null)
                {
                    return false;
                }

                if (Client.GetHabbo().InRoom)
                {
                    return true;
                }

                return false;
            }
        }

        public MessengerBuddy(uint UserId)
        {
            this.UserId = UserId;
            this.UpdateNeeded = false;
        }

        public void Serialize(ServerMessage Message, Boolean Search)
        {
            /*Fs
             * H
             * RG
             * 
             * hCNZ
             * name
             * motto
             * H
             * H
             * blank
             * I
             * look
             * laston
             * hBDCRbobbya12HHIkP`wUbobbya12174HHIjiu@Pbobbya123HHIhmT\Xbobbya1234HHIhkLLUbobbya12345HHIkDaXPbobbyaanlandHHIj|yHQbobbyabcHHIiSklTbobbyabcdefHHIhGkIQBobbyAbreuHHIjIo`Ybobbyace123HHHkVemUbobbyacebestHHIibWTFbobbyackda cool 1!!!!!!!!HHIiuQ^SBobbyage13HHIjZTsPBobbyaid1HHIkyVOUbobbyakabobHHIjQoUTBOBBYANDGREGHHIiHqJFbobbyandkurtMutant:Snow.(Blizzard).MMHHIhe\yObobbyandro1HHIhA[qXbobbyandsamHHIiWAjQBobbyAndyHHIjcKwWbobbyangleHHHhsvOXBobbyanna[RAF] Leading AircraftmanHHHiyoWYbobbyannaDevilHHHieZAUBobbyapebang baby bangHHIjIRMYbobbyape001HHIkBEiUbobbyape1HHIiQWzWBOBBYAUSTIN50HHHi~LHZBobbyAvacardoHHIhupsTBobbyAwsomeN:Bobby A:13HHI
             */

            /*
             * @LXVBPrXVBXaCHQAkP\USGammer12345IHHH07-04-2010 18:59:45jMI_SHensedIHHH09-04-2010 14:02:47kFzYZOskrarIHHH12-12-2009 17:28:07kk}[Z0skr4rIHHH10-04-2010 09:32:11kTDyZguy72IHHH23-02-2010 10:58:35PYH
             * 
             * 
             * 
             */

            if (Search)
            {
                Message.AppendUInt(UserId);
                Message.AppendStringWithBreak(Username);
                Message.AppendStringWithBreak(Motto);
                Message.AppendBoolean(IsOnline);
                Message.AppendBoolean(InRoom);
                Message.AppendStringWithBreak("");
                Message.AppendBoolean(false);
                Message.AppendStringWithBreak(Look);
                Message.AppendStringWithBreak(LastOnline);
                Message.AppendStringWithBreak(RealName); // ????????????
            }
            else
            {
                Message.AppendUInt(UserId);
                Message.AppendStringWithBreak(Username);
                Message.AppendBoolean(true);
                Message.AppendBoolean(IsOnline);
                Message.AppendBoolean(InRoom);
                Message.AppendStringWithBreak(Look);
                Message.AppendBoolean(false);
                Message.AppendStringWithBreak(Motto);
                Message.AppendStringWithBreak(LastOnline);
                Message.AppendStringWithBreak(RealName);
            }
        }
    }
}
