using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;


namespace ChtiCanon
{
    public partial class chat : UserControl
    {
        private static  ConnectionManager Connexion;
        private static string pseudo;
        //private int ID_couleur;
        private delegate void NoArgDelegate();
        private Color[] tabCouleur = { Color.Black, Color.Blue, Color.Red, Color.Green, Color.Violet, Color.Yellow };

        public chat()
        {
            InitializeComponent();
            
           
        }

        private void RafraichirChat( object o, Shared.Message m)
        {
            this.Invoke((NoArgDelegate)delegate()
            {

               

                //rtbox_SendMessage.ForeColor = Color.Red;
                // Ajoute la commande à l'historique
                //rtbChat.ForeColor = tabCouleur[ID_couleur];
                rtbChat.Text += m.ToString()+"\n";

            });
        }

        public void init(ConnectionManager myConnexion, string pseudoChat)
        {
            Connexion = myConnexion;
            pseudo = pseudoChat;
            Connexion.message += RafraichirChat;

        }


        private void btnSend_Click(object sender, EventArgs e)
        {
            sendText();
            tbxChat.Text = "";
        }

        private void chat_Load(object sender, EventArgs e)
        {

        }

        private void chat_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue.ToString()=="Enter")
            {
                sendText();

            }
        
        }
        private void sendText()
        {
        
            if ((tbxChat.Text).Trim() != "")
            {
                Shared.Message texte = new Shared.Message(pseudo, tbxChat.Text);
                Connexion.send(texte);
                btnSend.Enabled = false;
            }
        }

        private void tbxChat_TextChanged(object sender, EventArgs e)
        {
            if ((tbxChat.Text).Trim() != "")
            {
                btnSend.Enabled = true;
            }
            else
            {
                btnSend.Enabled = false;
            }
        }

        private void rtbChat_TextChanged(object sender, EventArgs e)
        {

        }


    }
}
