using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using MyInformation.BLL;
using MyInformation.Models;
namespace MyInformationQQ
{
    public partial class frm_chatIng : Form
    {
        //实例化userManager用于调用方法
        userManager um = new userManager();
        //定义全局变量用于存储数据
        string nickName = "";
        string loginPwd = "";
        //好友的ID
        int  FriendID;
        //声明全局变量ur
        Users ur = new Users();
        //声明IPEndPoint
        IPEndPoint ipend;
        //声明UdpClient
        UdpClient uc;
        //声明Thread
        Thread get;
        //带参构造
        public frm_chatIng(int friendID,string nickname,string loginpwd)
        {
            //将当前用户的昵称与密码保存在ur实体类中
            ur.NickName = nickname;
            ur.LoginPwd = loginpwd;
            //得到当前用户的昵称与密码与好友的ID
            this.nickName = nickname;
            this.loginPwd = loginpwd;
            this.FriendID = friendID;
            //指示不对错误的线程进行捕获
            CheckForIllegalCrossThreadCalls = false;
            //程序提供的
            InitializeComponent();
        }        //根据nickName和loginPwd得到用户当前的ID
        private void frm_chatIng_Load(object sender, EventArgs e)
        {
            //调用得到消息的方法
            getMsg();
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
        }        //得到当前用户的ID
        public int  getMyID(Users ur)
        {
           int id= um.GetUserId(ur);
           return id;
        }        //程序的线程异常
        public void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            MessageBox.Show("出现未预料到的错误:" + e.Exception.Message);
            Application.Exit();
        }
        
        //得到消息
        private void getMsg()
        {
            get = new Thread(new ThreadStart(getMsgCode)); 
            get.IsBackground=true;
            get.Start();
        }
        
        //得到本机端口
        public int getPort() 
        {
            Users urs = new Users();
            urs = this.GetMyIPandPort(nickName, loginPwd);
            string ip=urs.IP.ToString();
            int port =Convert.ToInt32(urs.Port);
            return port;
        }
       
        //接收消息
        private void getMsgCode()
        {
            Users ur = new Users();
            int port =Convert.ToInt32(ur.Port);
            IPHostEntry iphost = new IPHostEntry();
            iphost = Dns.GetHostEntry(Dns.GetHostName());
            IPEndPoint ipend = new IPEndPoint(iphost.AddressList[0],port);
            UdpClient uc = new UdpClient(ipend); 
            while (true)
            {
                Thread.Sleep(500);
                byte[] recvData = uc.Receive(ref ipend);
                string recvString = System.Text.Encoding.UTF8.GetString(recvData);
                rich_out.AppendText(DateTime.Now.ToString() + "\r");
                rich_out.AppendText(ipend.Address.ToString() + ": " + recvString + "\r");
            }
        }        //根据当前用户的昵称与密码查询出当前用户的IP与端口号
        public Users GetMyIPandPort(string  nickName,string loginPwd) 
        {
            Users urs = new Users();
            urs = um.getMyIPandPort(nickName, loginPwd);
            return urs;
        }        //根据好友的ID查询出该好友的IP地址与端口
        public Users GetFriendsIPandPort(int FriendID) 
        {
            Users usr = new Users();
            usr = um.getFriendsIPandPort(FriendID);
            return usr;
        }        //发送消息
        private void btn_send_Click(object sender, EventArgs e)
        {
            Users usr = new Users();
            usr = this.GetFriendsIPandPort(FriendID);
            int port = Convert.ToInt32(usr.Port);
            string ip = usr.IP.ToString();
            if (ipend == null && uc == null)
            {
                //uc = new UdpClient("192.168.1.100",5665);
                uc = new UdpClient(ip, port);
            }
            //得到用户发送的消息
            byte[] sendData = System.Text.Encoding.UTF8.GetBytes(rich_input.Text);
            uc.Send(sendData, sendData.Length);
            rich_out.SelectionStart = 0;
            //追加文本消息
            rich_out.AppendText(nickName);
            rich_out.AppendText("  "+DateTime.Now.ToString());
            rich_out.AppendText("\r\n");
            //将发送信息添加到接收文本框中
            rich_out.SelectedRtf = rich_input.Rtf;
            rich_input.Clear();
          }        private void tmr_getData_Tick(object sender, EventArgs e)
        {
           //this.getMsg();
        }
     }
 }