package com.qq.client.tools;

import java.io.ObjectInputStream;
import java.net.Socket;
import java.util.Iterator;
import java.util.Map;

import com.qq.client.view.QqChat;
import com.qq.client.view.QqFriendList;
import com.qq.common.Message;
import com.qq.common.MessageType;

public class ClientConServerThread extends Thread{//½«ÓÃ»§-->·þÎñÆ÷µÄÁ´½ÓÐ´³ÉÏß³ÌµÄÐÎÊ½
	private Socket socket;//¸ÃÏß³ÌËùÓµÓÐµÄ¿Í»§¶ËµÄsocket
	public Socket getSocket() {
		return socket;
	}

	public ClientConServerThread(Socket socket) {
		// TODO Auto-generated constructor stub
		this.socket= socket;
	}
	
	public void run()//·¢ËÍ¿ÉÒÔÔÚÈÎºÎÄãÐèÒªµÄµØ·½·¢ËÍ£¬¶ø½ÓÊÕÖ»ÄÜÔÚ¸÷×ÔµÄÏß³ÌÖÐ½ÓÊÜÏûÏ¢
	{
		while(true)
		{
			try {
				ObjectInputStream ois= new ObjectInputStream(socket.getInputStream());
				Message message= (Message) ois.readObject();
				if(message.getMesType().equals(MessageType.common_mess))//ÆÕÍ¨ÏûÏ¢µÄ´¦Àí
				{
				QqChat qqChat= ManageQqChat.getQqChat(message.getReceiver()+" "+message.getSender());
				qqChat.setMessage(message);//ÔÚmessageÖÐ£¬senderÊÇºÃÓÑ£¬receiverÊÇ×Ô¼º£¬¶øqqchatÁÄÌì¿òÊÇÒÔsender+receiverµÄ·½Ê½ÃüÃûµÄ£¬ËùÒÔÎÒÃÇÒª°ÑÊÕµ½µÄÐÅÏ¢ÀïµÄ½ÓÊÕË«·½·´¹ýÀ´²ÅÄÜÐ´µ½×Ô¼ºµÄ¿òÖÐ
				
				}else if(message.getMesType().equals(MessageType.re_onLineFriend))//»ØÊÕÔÚÏßÈËÊýÏûÏ¢µÄ´¦Àí
				{
					QqFriendList qqFriendList= ManageFriendList.getFriendList(message.getReceiver());
					if(qqFriendList!= null)
						qqFriendList.updateFriendList(message);	//¸üÐÂ¸ÃºÃÓÑÁÐ±í	
				}else if(message.getMesType().equals(MessageType.off_line))//ÒòÎªËùÓÐµÄ¶Ô»°¿ò¶¼ÐèÒªÒ»¶Ô·¢¼þÈË--ÊÕ¼þÈË²ÅÄÜÈ·¶¨£¬ÒòÎªÊÇ·þÎñÆ÷µ¥·½Ïò¿Í»§¶Ë·¢ËÍµÄÏûÏ¢£¬ËùÒÔÐÅÏ¢²»È«£¬Ö»ÄÜÒÀ´Î±éÀú
				{
					Map<String , QqChat> qqChats= ManageQqChat.chatPool;
					Iterator it= qqChats.keySet().iterator();
					while(it.hasNext())
					{
						QqChat qqChat= qqChats.get(it.next());
						qqChat.setOffLine();
					}
				}else if(message.getMesType().equals(MessageType.sys_mess))//·¢²¼ÏµÍ³ÐÅÏ¢
				{
					Map<String , QqChat> qqChats= ManageQqChat.chatPool;
					Iterator it= qqChats.keySet().iterator();
					while(it.hasNext())
					{
						QqChat qqChat= qqChats.get(it.next());
						qqChat.setMessage(message);
					}
				}
				
				} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
}
