package main

import (
	"mongo.games.com/goserver/core/logger"
	"mongo.games.com/goserver/core/netlib"

	"mongo.games.com/game/common"
	"mongo.games.com/game/protocol/message"
)

// 读取邮件
type CSReadMessagePacketFactory struct {
}
type CSReadMessageHandler struct {
}

func (this *CSReadMessagePacketFactory) CreatePacket() interface{} {
	pack := &message.CSMessageRead{}
	return pack
}

func (this *CSReadMessageHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSReadMessageHandler Process recv ", data)
	if csMessageRead, ok := data.(*message.CSMessageRead); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warn("CSReadMessageHandler p == nil")
			return nil
		}

		p.ReadMessage(csMessageRead.GetId())
	}
	return nil
}

// 删除邮件
type CSDelMessagePacketFactory struct {
}
type CSDelMessageHandler struct {
}

func (this *CSDelMessagePacketFactory) CreatePacket() interface{} {
	pack := &message.CSMessageDel{}
	return pack
}

func (this *CSDelMessageHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSDelMessageHandler Process recv ", data)
	if csMessageDel, ok := data.(*message.CSMessageDel); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warn("CSDelMessageHandler p == nil")
			return nil
		}

		p.DelMessage(csMessageDel.GetId(), 1)
	}
	return nil
}

// 提取邮件附件
type CSGetMessageAttachPacketFactory struct {
}
type CSGetMessageAttachHandler struct {
}

func (this *CSGetMessageAttachPacketFactory) CreatePacket() interface{} {
	pack := &message.CSGetMessageAttach{}
	return pack
}

func (this *CSGetMessageAttachHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Trace("CSGetMessageAttachHandler Process recv ", data)
	if csGetMessageAttach, ok := data.(*message.CSGetMessageAttach); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		if p == nil {
			logger.Logger.Warn("CSGetMessageAttachHandler p == nil")
			return nil
		}

		p.GetMessageAttach(csGetMessageAttach.GetId())
	}
	return nil
}

// 查看邮件
type SCMessageListPacketFactory struct {
}

type SCMessageListHandler struct {
}

func (this *SCMessageListPacketFactory) CreatePacket() interface{} {
	pack := &message.CSMessageList{}
	return pack
}

func (this *SCMessageListHandler) Process(s *netlib.Session, packetid int, data interface{}, sid int64) error {
	logger.Logger.Tracef("(this *SCMessageListHandler) Process [%v].", s.GetSessionConfig().Id)
	if msg, ok := data.(*message.CSMessageList); ok {
		p := PlayerMgrSington.GetPlayer(sid)
		p.SendMessage(msg.GetShowId())
	}
	return nil
}

func init() {
	common.RegisterHandler(int(message.MSGPacketID_PACKET_CS_MESSAGEREAD), &CSReadMessageHandler{})
	netlib.RegisterFactory(int(message.MSGPacketID_PACKET_CS_MESSAGEREAD), &CSReadMessagePacketFactory{})

	common.RegisterHandler(int(message.MSGPacketID_PACKET_CS_MESSAGEDEL), &CSDelMessageHandler{})
	netlib.RegisterFactory(int(message.MSGPacketID_PACKET_CS_MESSAGEDEL), &CSDelMessagePacketFactory{})

	common.RegisterHandler(int(message.MSGPacketID_PACKET_CS_GETMESSAGEATTACH), &CSGetMessageAttachHandler{})
	netlib.RegisterFactory(int(message.MSGPacketID_PACKET_CS_GETMESSAGEATTACH), &CSGetMessageAttachPacketFactory{})

	// 获取邮件列表
	common.RegisterHandler(int(message.MSGPacketID_PACKET_CS_MESSAGELIST), &SCMessageListHandler{})
	netlib.RegisterFactory(int(message.MSGPacketID_PACKET_CS_MESSAGELIST), &SCMessageListPacketFactory{})
}
