// ctrlChat.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 System.Windows.Forms;
using FileScope;
using ZANZILib.Protocols.QueryCriteria;
using ZANZILib.Protocols.ResultOne;

namespace ZANZIClient
{
    /// <summary>
    /// Summary description for frmSearch.
    /// </summary>
    public class ctrlChat : UserControl
    {
        #region Variable declaration

        //This is the protocol currently in use
        private Button btnSend;
        //private GUIControlLogic.clsSearch clsManager ;
        //private GUIControlLogic.clsSearch clsManager = new GUIControlLogic.clsSearch ();
        public int chatNum;
        private clsZanziProt objSearchCriteria = new clsZanziProt();
        private Panel panel1;
        private Panel panel2;
        private Panel panel3;
        private Panel panel4;
        private PropertyGrid propertyGrid1;
        private Splitter splitter1;
        private TextBox txtChat;
        private TextBox txtChatMsg;

        #endregion

        #region Functions

        /// <summary>Main entry for frmSearch</summary>
        /// <returns>Returns nothing</returns>
        /// <param name="frm">Used to pass the MDIParent as reference</param>
        /// <remarks>frmSearch is the constructor in the frmSearch class.  
        /// The <paramref name="frm"/> parameter takes a reference to frmMain.
        /// </remarks>
        //public frmChat(ZANZIClient.clsMain frm,ListView lst,ZANZILib.Protocols.ResultOne.clsZanziProtResultOne supplierDetail, long callingGUID  )
        public ctrlChat(clsMain frm, clsZanziProtResultOne supplierDetail, long callingGUID)
        {
            //

            // Required for Windows Form Designer support
            //

            //this.MdiParent =frm;
            InitializeComponent();


            //Load the ResultOne structure from ZANZILib into propertygrid
            propertyGrid1.SelectedObject = supplierDetail;

            //ListViewItem[] myitms  = new ListViewItem[lst.Items.Count];
            //load the characteristics into the char tab
            //lst.Items.CopyTo(myitms,0);
            //this.lstCharacteristics.Items.AddRange(myitms) ;
            //this.lstCharacteristics.Refresh ();


            //update the caption to unique text
            Text = Text + callingGUID;
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        //		protected override void Dispose( bool disposing )
        //		{
        //			if( disposing )
        //			{
        //				if(components != null)
        //				{
        //					components.Dispose();
        //				}
        //			}
        //			base.Dispose( disposing );
        //		}
        private void SendMessage()
        {
            try
            {
                //make sure we have something to send
                if (txtChatMsg.Text == "" || !txtChatMsg.Enabled)
                    return;

                //make sure we have something to send
                if (txtChatMsg.Text == "" || !txtChatMsg.Enabled)
                    return;

                //deal with our message
                string msg = txtChatMsg.Text;
                txtChatMsg.Clear();
                txtChat.Text = txtChat.Text + "\r\n" + "<You> " + msg + "\r\n";

                //send our message
                msg += "\n";
                ChatManager.chats[chatNum].SendString(ref msg);
            }
            catch (Exception e)
            {
                Debug.WriteLine("ChatTabPage SendMessage: " + e.Message);
            }
        }

        public void SetupCrip(int chatNum)
        {
            this.chatNum = chatNum;

            if (!ChatManager.chats[chatNum].incoming)
            {
                btnSend.Enabled = false;
                txtChatMsg.Enabled = false;
                Text = ChatManager.chats[chatNum].address;
                Tag = ChatManager.chats[chatNum].address + ":" + ChatManager.chats[chatNum].port;
            }
            else
            {
                Text = ChatManager.chats[chatNum].RemoteIP();
                Tag = Text;
            }
        }

        public void StartedConnecting()
        {
            try
            {
                //txtChat.SelectionFont = new Font("Arial", 10, FontStyle.Regular);
                //txtChat.SelectionColor = System.Drawing.Color.Green;
                txtChat.AppendText("[-Connecting to " + Text + "-]\r\n");
                //ResizeMe();
            }
            catch
            {
                Debug.WriteLine("ChatTabPage StartedConnecting");
            }
        }

        public void JustFinishedHandshake()
        {
            try
            {
                //t//xtChat.SelectionColor = System.Drawing.Color.Green;
                //txtChat.SelectionFont = new Font("Arial", 10, FontStyle.Regular);
                txtChat.AppendText("[-Connected-]\r\n");
                txtChatMsg.Enabled = true;
                btnSend.Enabled = true;
            }
            catch
            {
                Debug.WriteLine("ChatTabPage JustFinishedHandshake");
            }
        }

        public void JustGotMessage(string msg)
        {
            try
            {
                //txtChat.SelectionColor = Stats.settings.clChatHeader;
                //txtChat.SelectionFont = new Font("Arial", 14, FontStyle.Bold);
                txtChat.AppendText("<Peer> ");
                //txtChat.SelectionColor = Stats.settings.clChatPeer;
                //txtChat.SelectionFont = new Font("Arial", 14, FontStyle.Regular);
                txtChat.AppendText(msg.Substring(0, msg.Length) + "\r\n");
            }
            catch
            {
                Debug.WriteLine("ChatTabPage JustGotMessage");
            }
        }

        public void JustDisconnected()
        {
            try
            {
                //txtChat.SelectionColor = System.Drawing.Color.Green;
                //txtChat.SelectionFont = new Font("Arial", 10, FontStyle.Regular);
                txtChat.AppendText("[-Disconnected-]\r\n");
                txtChatMsg.Enabled = false;
                btnSend.Enabled = false;
                chatNum = -1;
            }
            catch
            {
                Debug.WriteLine("ChatTabPage JustDisconnected");
            }
        }

        #endregion

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.panel1 = new System.Windows.Forms.Panel();
            this.propertyGrid1 = new System.Windows.Forms.PropertyGrid();
            this.splitter1 = new System.Windows.Forms.Splitter();
            this.panel2 = new System.Windows.Forms.Panel();
            this.panel3 = new System.Windows.Forms.Panel();
            this.panel4 = new System.Windows.Forms.Panel();
            this.txtChatMsg = new System.Windows.Forms.TextBox();
            this.btnSend = new System.Windows.Forms.Button();
            this.txtChat = new System.Windows.Forms.TextBox();
            this.panel1.SuspendLayout();
            this.panel3.SuspendLayout();
            this.panel4.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.propertyGrid1);
            this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
            this.panel1.Location = new System.Drawing.Point(0, 0);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(200, 373);
            this.panel1.TabIndex = 2;
            // 
            // propertyGrid1
            // 
            this.propertyGrid1.CommandsVisibleIfAvailable = true;
            this.propertyGrid1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.propertyGrid1.HelpVisible = false;
            this.propertyGrid1.LargeButtons = false;
            this.propertyGrid1.LineColor = System.Drawing.SystemColors.ScrollBar;
            this.propertyGrid1.Location = new System.Drawing.Point(0, 0);
            this.propertyGrid1.Name = "propertyGrid1";
            this.propertyGrid1.PropertySort = System.Windows.Forms.PropertySort.Alphabetical;
            this.propertyGrid1.Size = new System.Drawing.Size(200, 373);
            this.propertyGrid1.TabIndex = 3;
            this.propertyGrid1.Text = "PropertyGrid";
            this.propertyGrid1.ToolbarVisible = false;
            this.propertyGrid1.ViewBackColor = System.Drawing.SystemColors.Window;
            this.propertyGrid1.ViewForeColor = System.Drawing.SystemColors.WindowText;
            // 
            // splitter1
            // 
            this.splitter1.Location = new System.Drawing.Point(200, 0);
            this.splitter1.Name = "splitter1";
            this.splitter1.Size = new System.Drawing.Size(3, 373);
            this.splitter1.TabIndex = 3;
            this.splitter1.TabStop = false;
            // 
            // panel2
            // 
            this.panel2.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel2.Location = new System.Drawing.Point(0, 0);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(192, 299);
            this.panel2.TabIndex = 0;
            // 
            // panel3
            // 
            this.panel3.Controls.Add(this.panel4);
            this.panel3.Controls.Add(this.txtChat);
            this.panel3.Dock = System.Windows.Forms.DockStyle.Fill;
            this.panel3.Location = new System.Drawing.Point(203, 0);
            this.panel3.Name = "panel3";
            this.panel3.Size = new System.Drawing.Size(245, 373);
            this.panel3.TabIndex = 9;
            // 
            // panel4
            // 
            this.panel4.Controls.Add(this.txtChatMsg);
            this.panel4.Controls.Add(this.btnSend);
            this.panel4.Dock = System.Windows.Forms.DockStyle.Bottom;
            this.panel4.Location = new System.Drawing.Point(0, 349);
            this.panel4.Name = "panel4";
            this.panel4.Size = new System.Drawing.Size(245, 24);
            this.panel4.TabIndex = 12;
            // 
            // txtChatMsg
            // 
            this.txtChatMsg.Anchor =
                ((System.Windows.Forms.AnchorStyles)
                 ((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                   | System.Windows.Forms.AnchorStyles.Right)));
            this.txtChatMsg.Location = new System.Drawing.Point(0, 0);
            this.txtChatMsg.Name = "txtChatMsg";
            this.txtChatMsg.Size = new System.Drawing.Size(168, 20);
            this.txtChatMsg.TabIndex = 13;
            this.txtChatMsg.Text = "";
            // 
            // btnSend
            // 
            this.btnSend.Anchor =
                ((System.Windows.Forms.AnchorStyles)
                 ((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.btnSend.Location = new System.Drawing.Point(168, 2);
            this.btnSend.Name = "btnSend";
            this.btnSend.Size = new System.Drawing.Size(75, 20);
            this.btnSend.TabIndex = 12;
            this.btnSend.Text = "&Send";
            this.btnSend.Click += new System.EventHandler(this.btnSend_Click);
            // 
            // txtChat
            // 
            this.txtChat.Dock = System.Windows.Forms.DockStyle.Fill;
            this.txtChat.Location = new System.Drawing.Point(0, 0);
            this.txtChat.Multiline = true;
            this.txtChat.Name = "txtChat";
            this.txtChat.Size = new System.Drawing.Size(245, 373);
            this.txtChat.TabIndex = 9;
            this.txtChat.Text = "";
            // 
            // ctrlChat
            // 
            this.Controls.Add(this.panel3);
            this.Controls.Add(this.splitter1);
            this.Controls.Add(this.panel1);
            this.Name = "ctrlChat";
            this.Size = new System.Drawing.Size(448, 373);
            this.Load += new System.EventHandler(this.frmChat_Load);
            this.panel1.ResumeLayout(false);
            this.panel3.ResumeLayout(false);
            this.panel4.ResumeLayout(false);
            this.ResumeLayout(false);
        }

        #endregion

        #region Events

        private void btnSearch_Click(object sender, EventArgs e)
        {
            //			//Pass the query object to the server
            //
            //			ZANZI.Test.clsResults cls = new ZANZI.Test.clsResults();
            //
            //			grdResultOne.DataSource = cls.GetTestData();
            //			//grdResultOne

            //clsManager.DoSearch(this.grdResultOne);
        }

        #endregion

        //private System.ComponentModel.IContainer components;

        private void frmChat_Load(object sender, EventArgs e)
        {
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            try
            {
                SendMessage();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("ChatTabPage SendMessage: " + ex.Message);
            }
        }
    }
}