// clsChat.cs
// Copyright (C) 2008 Karel Jordaan karel_jordaan@yahoo.com.au
// 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Diagnostics;
using Crownwood.Magic.Controls;
using FileScope;

namespace ZANZIClient
{
    /// <summary>
    /// Summary description for clsChat.
    /// </summary>
    public class clsChat : IDisposable
    {
//		public delegate void newChat(int chatNum);
//		public delegate void connectedChat(int chatNum);
//		public delegate void newChatData(int chatNum, string msg);
//		public delegate void disconnectedChat(int chatNum);

        #region Delegates

        public delegate void removeChatTabPage();

        #endregion

        private readonly clsMain m_frmMain;


        public clsChat(clsMain frm)
        {
            //
            // TODO: Add constructor logic here
            //
            m_frmMain = frm;

            //regoste evemt handler for the newchat event
            GUIBridge.NewChatEvent += NewChat;
            GUIBridge.NewChatDataEvent += NewChatData;
            GUIBridge.ConnectedChatEvent += ConnectedChat;
            GUIBridge.DisconnectedChatEvent += DisconnectedChat;
        }

        #region IDisposable Members

        void IDisposable.Dispose()
        {
            // TODO:  Add clsChat.System.IDisposable.Dispose implementation
        }

        #endregion

        public void NewChat(int chatNum)
        {
            //creates a new chat ctrl
            frmSearch frm = (frmSearch) m_frmMain.tabControl1.ActiveControl;

            ctrlChat newChat = new ctrlChat(m_frmMain, frm.m_ResultOneObj, frm.m_CurrentResultRecordGUID);

            //add the ctrl to the tabpages
            m_frmMain.AddTabPage(newChat, 1);

            newChat.SetupCrip(chatNum);
            //tabControl1.SelectedIndex = tabControl1.TabCount - 1;
            if (!ChatManager.chats[chatNum].incoming)
                newChat.StartedConnecting();
        }

        public void ConnectedChat(int chatNum)
        {
            foreach (ctrlChat ctp in m_frmMain.tabControl1.TabPages)
                if (ctp.chatNum == chatNum)
                {
                    ctp.JustFinishedHandshake();
                    return;
                }
        }

        public void NewChatData(int chatNum, string msg)
        {
            foreach (ctrlChat ctp in m_frmMain.tabControl1.TabPages)
                if (ctp.chatNum == chatNum)
                {
                    try
                    {
                        if (m_frmMain.tabControl1.SelectedTab.Control != ctp)
                            if (!ctp.Text.EndsWith(" *"))
                                ctp.Text += " *";
                    }
                    catch
                    {
                        Debug.WriteLine("NewChatData 1");
                    }
                    ctp.JustGotMessage(msg);
                    return;
                }
        }

        public void DisconnectedChat(int chatNum)
        {
            foreach (TabPage tpg in m_frmMain.tabControl1.TabPages)
            {
                try
                {
                    ctrlChat ctp = (ctrlChat) tpg.Control;


                    if (ctp.chatNum == chatNum)
                    {
                        ctp.JustDisconnected();
                        return;
                    }
                }
                catch
                {
                }
            }
        }


        public void RemoveChatTabPage()
        {
            for (int tbp = 0; tbp < m_frmMain.tabControl1.TabPages.Count; tbp++)
            {
                if (m_frmMain.tabControl1.TabPages[tbp].Text == "dispose")
                {
                    m_frmMain.tabControl1.TabPages.RemoveAt(tbp);
                    m_frmMain.tabControl1.Focus();
                    return;
                }
            }
        }

        public void ChatIP()
        {
            //will be th ip of the author of the object if online
            string peer = "202.11.0.1";
            ChatManager.Outgoing(ref peer);
        }
    }
}