package com.mercury.chat.client.impl;

import io.netty.channel.Channel;

import java.util.Date;
import java.util.List;

import com.google.common.collect.Lists;
import com.mercury.chat.client.Session;
import com.mercury.chat.client.protocol.SecureChatClientHandler;
import com.mercury.chat.common.MessageListener;
import com.mercury.chat.common.MessageType;
import com.mercury.chat.common.struct.protocol.Message;

public class SessionImpl implements Session {

	private Channel channel;
	
	public SessionImpl(Channel channel) {
		super();
		this.channel = channel;
	}

	@Override
	public boolean sendMessage(Message message) {
		try {
			channel.writeAndFlush(message).sync();
		} catch (InterruptedException e) {
			return false;
		}
		return true;
	}
	
	@Override
	public boolean logoff() {
		//TODO
		return true;
	}

	@Override
	public List<Message> getHistoricalMessages(String user, Date from, Date to) {
		// TODO
		return Lists.newArrayList();
	}
	
	public void addMessageListener(MessageType messageType, MessageListener messageListener){
		channel.pipeline().get(SecureChatClientHandler.class).addMessageListener(messageListener);
	}

}

@@ -358,24 +358,80 @@
 ssageType;%0D%0A
+import com.mercury.chat.common.struct.protocol.Header;%0D%0A
 import com.m
@@ -883,14 +883,208 @@
 %0D%0A%09%09
-//TODO
+try %7B%0D%0A%09%09%09Message message = new Message().header(new Header().type(MessageType.LOGOFF.value()));%0D%0A%09%09%09channel.writeAndFlush(message).sync();%0D%0A%09%09%7D catch (InterruptedException e) %7B%0D%0A%09%09%09return false;%0D%0A%09%09%7D
 %0D%0A%09%09

implement the logoff logic