using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;

using SmartFoxClientAPI;
using SmartFoxClientAPI.Data;

namespace SFSSilverlightDemo
{
    /*
     * Test project that runs through the most common methods and callbacks + the easy tested ones
     * 
     * The execution plan:
     * - Connect
     * - Login and wait for roomlist
     * - Try to join unknown room
     * - Join room
     * - Get random key
     * - Roundtrip time
     * - Create room
     * - Send message
     * - Send object
     * - Logout
     * 
     * The things that wont be tested in general terms:
     * - Buddy
     * - Extension
     * - Interaction between multiple clients
     */

    public partial class Page : UserControl
    {
        SmartFoxClient smartFox;

        public Page()
        {
            InitializeComponent();
        }

        void SFSConnect_Click(object sender, RoutedEventArgs e)
        {
            SFSEvent.onConnection += HandleConnection;
            SFSEvent.onDebugMessage += OnDebugMessage;
            SFSEvent.onLogin += HandleLogin;
            SFSEvent.onRoomListUpdate += HandleRoomList;
            SFSEvent.onJoinRoomError += HandleJoinRoomError;
            SFSEvent.onJoinRoom += HandleJoinRoom;
            SFSEvent.onUserCountChange += HandleUserCountChange;
            SFSEvent.onRandomKey += HandleRandomKey;
            SFSEvent.onRoundTripResponse += HandleRoundTripResponse;
            SFSEvent.onRoomAdded += HandleRoomAdded;
            SFSEvent.onPublicMessage += HandlePublicMessage;
            SFSEvent.onObjectReceived += HandleObjectReceived;
            SFSEvent.onLogout += HandleLogout;
            
            smartFox = new SmartFoxClient(true);
            smartFox.Connect("127.0.0.1", 4502);
            smartFox.SetQueuePollSpeed(1000);
        }

        void HandleConnection(bool success, string error) {
            LogWindow.Text += "\nConnected " + error;

            LogWindow.Text += "\nTrying to login ";
            smartFox.Login("simpleChat", "", "");

        }

        void HandleLogin(bool success, string name, string error)
        {
            LogWindow.Text += "On Login callback got: " + success + " : " + error + " : " + name;
        }

        void OnDebugMessage(string message)
        {
            LogWindow.Text += "\n[SFS DEBUG] " + message;
        }

        void HandleRoomList(Dictionary<int, Room> roomList)
        {
            LogWindow.Text += "\nRoom list updated with rooms:";
            foreach (Room room in roomList.Values)
            {
                LogWindow.Text += "\n - " + room.GetName();
            }

            smartFox.JoinRoom("The Hall");
            //smartFox.JoinRoom("The Kitchen");
        }

        void HandleUserCountChange(Room room) {
            LogWindow.Text += "\nUser count changed for room " + room.GetName() + " count: " + room.GetUserCount();
        }

        void HandleJoinRoomError(string error) {
            LogWindow.Text += "\nJoined room error " + error;
        }

        private bool firstJoinRoom = true;
        void HandleJoinRoom(Room room) {
            LogWindow.Text += "\nJoined room " + room.GetName();

            if (firstJoinRoom)
            {
                smartFox.GetRandomKey();
                firstJoinRoom = false;
            }
            else {
                smartFox.SendPublicMessage("I'm alive!!!");
            }
        }

        void HandleRandomKey(string key) { 
            LogWindow.Text += "\nRandom key: " + key;

            smartFox.RoundTripBench();
        }

        void HandleRoundTripResponse(int elapsed) { 
            LogWindow.Text += "\nRound trip time: " + elapsed + " seconds";

            NewRoomDescriptor room = new NewRoomDescriptor("New room", 10, false);
            smartFox.CreateRoom(room);
        }

        void HandleRoomAdded(Room room) {
            LogWindow.Text += "\nRoom created: " + room.GetName();

            smartFox.JoinRoom("New room");
        }

        void HandlePublicMessage(string message, User sender, int roomId) {
            LogWindow.Text += "\nUser " + sender.GetName() + " in room " + smartFox.GetRoom(roomId).GetName() + " said: " + message;

            SFSObject newObj = new SFSObject();
            newObj.Put("name", "New object");
            newObj.PutNumber("num", 10.3f);
            newObj.PutBool("new", true);
            smartFox.SendObject(newObj);
            smartFox.Logout();
        }

        void HandleObjectReceived(SFSObject obj, User sender) {
            LogWindow.Text += "\nUser " + sender.GetName() + " sent object " + obj.GetString("name") + " with num " + obj.GetNumber("num");
        }

        void HandleLogout() {
            LogWindow.Text += "\nLogged out";
        }

    }
}
