package main

import (
	"reflect"

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

// 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 ""
}

// WriteLog 记录日志，需要提前注册
func (c *LogChannel) WriteLog(log interface{}) {
	cname := c.getLogCName(log)
	if cname == "" {
		cname = "_null_"
	}
	RabbitMQPublisher.Send(cname, log)
}

// WriteMQData rabbitMQ消息
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.ItemLogCollName, &model.ItemLog{})
	LogChannelSingleton.RegisterLogCName(model.WelfareLogCollName, &model.WelfareLog{})
	LogChannelSingleton.RegisterLogCName(model.OnlineLogCollName, &model.OnlineLog{})
	LogChannelSingleton.RegisterLogCName(model.MQRankSeason, &model.PlayerRankScore{})
	LogChannelSingleton.RegisterLogCName(model.MQRankPlayerCoin, &model.RankPlayerCoin{})
	LogChannelSingleton.RegisterLogCName(mq.BackBankrupt, &model.BankruptLog{})
	LogChannelSingleton.RegisterLogCName(mq.BackReliefund, &model.ReliefFundLog{})
	LogChannelSingleton.RegisterLogCName(model.EvtBindInvite, &model.BindInvite{})
	LogChannelSingleton.RegisterLogCName(mq.BackInviteScore, &model.InviteScore{})
}
