package base

import (
	"reflect"

	"mongo.games.com/game/model"
)

// LogChannelSingleton 日志记录器
var LogChannelSingleton = &LogChannel{
	cName: make(map[reflect.Type]string),
}

type LogChannel struct {
	cName map[reflect.Type]string
}

func (c *LogChannel) RegisterLogCName(cname string, log interface{}) {
	t := c.getLogType(log)
	c.cName[t] = cname
}

func (c *LogChannel) getLogType(log interface{}) reflect.Type {
	return reflect.Indirect(reflect.ValueOf(log)).Type()
}

func (c *LogChannel) getLogCName(log interface{}) string {
	t := c.getLogType(log)
	if name, exist := c.cName[t]; exist {
		return name
	}
	return ""
}

func (c *LogChannel) WriteLog(log interface{}) {
	cname := c.getLogCName(log)
	if cname == "" {
		cname = "_null_"
	}
	RabbitMQPublisher.Send(cname, log)
}

func (c *LogChannel) WriteMQData(data *model.RabbitMQData) {
	RabbitMQPublisher.Send(data.MQName, data.Data)
}

func init() {
	LogChannelSingleton.RegisterLogCName(model.LoginLogCollName, &model.LoginLog{})
	LogChannelSingleton.RegisterLogCName(model.CoinGiveLogCollName, &model.CoinGiveLog{})
	LogChannelSingleton.RegisterLogCName(model.CoinLogCollName, &model.CoinLog{})
	LogChannelSingleton.RegisterLogCName(model.SceneCoinLogCollName, &model.SceneCoinLog{})
	LogChannelSingleton.RegisterLogCName(model.GameDetailedLogCollName, &model.GameDetailedLog{})
	LogChannelSingleton.RegisterLogCName(model.GamePlayerListLogCollName, &model.GamePlayerListLog{})
	LogChannelSingleton.RegisterLogCName(model.FriendRecordLogCollName, &model.FriendRecord{})
	LogChannelSingleton.RegisterLogCName(model.ItemLogCollName, &model.ItemLog{})
}
