/*
 * @Author: Alvin Alvin@qq.com
 * @Date: 2024-09-24 18:54:19
 * @LastEditors: Alvin Alvin@qq.com
 * @LastEditTime: 2024-09-27 11:02:04
 * @FilePath: \server\game\game_cluster\nodes\logstash\module\handlelog\actor_handle_log.go
 * @Description:
 * 处理其他服务发送的埋点日志
 */
package handlelog

import (
	"strings"

	jsoniter "github.com/json-iterator/go"
	"github.com/mhaya/game/game_cluster/internal/code"
	"github.com/mhaya/game/game_cluster/internal/mdb"
	"github.com/mhaya/game/game_cluster/internal/mdb/eventmodels"
	"github.com/mhaya/game/game_cluster/internal/param"
	"github.com/mhaya/game/game_cluster/nodes/logstash/module/event"
	mhayaLogger "github.com/mhaya/logger"
	cactor "github.com/mhaya/net/actor"
)

var (
	json = jsoniter.ConfigCompatibleWithStandardLibrary
)

type (
	HandleLogObject struct {
		cactor.Base
	}
)

func (p *HandleLogObject) AliasID() string {
	return "logrecord"
}

// OnInit logstash为后端节点，不直接与客户端通信，所以注册了一些remote函数，供RPC调用
func (p *HandleLogObject) OnInit() {
	p.Remote().Register("handlelog", p.handlelog)

	event.GetEventIdMap().PrintAllEvent()
}

// handlelog 处理各个节点发送的日志记录
func (p *HandleLogObject) handlelog(req *param.HandleLogReq) int32 {
	mhayaLogger.Warnf("handlelog req:%v", req)

	if strings.TrimSpace(req.JsonContent) == "" || strings.TrimSpace(req.ServerId) == "" || strings.TrimSpace(req.EventName) == "" {
		return code.Error
	}

	e := event.GetEventIdMap().GetEvent(req.EventName)
	if e == nil {
		mhayaLogger.Warnf("handlelog unknow event:%s", req.EventName)
		return code.Error
	}

	return p.handlelogContent(req, e)
}

// 根据不同的请求内容，分别处理不通的埋点日志
func (p *HandleLogObject) handlelogContent(req *param.HandleLogReq, e event.Eventer) int32 {
	switch e.(type) {
	case *eventmodels.UserRegisterEventContent:
		content := &eventmodels.UserRegisterEventContent{}
		err := json.Unmarshal([]byte(req.JsonContent), &content)
		if err != nil {
			mhayaLogger.Warnf("handlelog UserRegisterEventContent Unmarshal err:%v", err)
			return code.Error
		}

		err = mdb.LogstashDB.Create(content).Error
		if err != nil {
			mhayaLogger.Warnf("handlelog LogstashDB Create UserRegisterEventContent err:%v", err)
			return code.Error
		}

	case *eventmodels.UserLoginEventContent:
		content := &eventmodels.UserLoginEventContent{}
		err := json.Unmarshal([]byte(req.JsonContent), &content)
		if err != nil {
			mhayaLogger.Warnf("handlelog UserLoginEventContent Unmarshal err:%v", err)
			return code.Error
		}

		err = mdb.LogstashDB.Create(content).Error
		if err != nil {
			mhayaLogger.Warnf("handlelog LogstashDB Create UserLoginEventContent err:%v", err)
			return code.Error
		}

	case *eventmodels.UserUpdateInfoEventContent:
		content := &eventmodels.UserUpdateInfoEventContent{}
		err := json.Unmarshal([]byte(req.JsonContent), &content)
		if err != nil {
			mhayaLogger.Warnf("handlelog UserUpdateInfoEventContent Unmarshal err:%v", err)
			return code.Error
		}

		err = mdb.LogstashDB.Create(content).Error
		if err != nil {
			mhayaLogger.Warnf("handlelog LogstashDB Create UserUpdateInfoEventContent err:%v", err)
			return code.Error
		}

	case *eventmodels.UserWithdrawEventContent:
		content := &eventmodels.UserWithdrawEventContent{}
		err := json.Unmarshal([]byte(req.JsonContent), &content)
		if err != nil {
			mhayaLogger.Warnf("handlelog UserWithdrawEventContent Unmarshal err:%v", err)
			return code.Error
		}

		err = mdb.LogstashDB.Create(content).Error
		if err != nil {
			mhayaLogger.Warnf("handlelog LogstashDB Create UserWithdrawEventContent err:%v", err)
			return code.Error
		}

	case *eventmodels.AssetsEventContent:
		content := &eventmodels.AssetsEventContent{}
		err := json.Unmarshal([]byte(req.JsonContent), &content)
		if err != nil {
			mhayaLogger.Warnf("handlelog AssetsEventContent Unmarshal err:%v", err)
			return code.Error
		}

		err = mdb.LogstashDB.Create(content).Error
		if err != nil {
			mhayaLogger.Warnf("handlelog LogstashDB Create AssetsEventContent err:%v", err)
			return code.Error
		}

	case *eventmodels.InviteEventContent:
		content := &eventmodels.InviteEventContent{}
		err := json.Unmarshal([]byte(req.JsonContent), &content)
		if err != nil {
			mhayaLogger.Warnf("handlelog InviteEventContent Unmarshal err:%v", err)
			return code.Error
		}

		err = mdb.LogstashDB.Create(content).Error
		if err != nil {
			mhayaLogger.Warnf("handlelog LogstashDB Create InviteEventContent err:%v", err)
			return code.Error
		}

	case *eventmodels.DiceEventContent:
		content := &eventmodels.DiceEventContent{}
		err := json.Unmarshal([]byte(req.JsonContent), &content)
		if err != nil {
			mhayaLogger.Warnf("handlelog DiceEventContent Unmarshal err:%v", err)
			return code.Error
		}

		err = mdb.LogstashDB.Create(content).Error
		if err != nil {
			mhayaLogger.Warnf("handlelog LogstashDB Create DiceEventContent err:%v", err)
			return code.Error
		}

	case *eventmodels.TurntableEventContent:
		content := &eventmodels.TurntableEventContent{}
		err := json.Unmarshal([]byte(req.JsonContent), &content)
		if err != nil {
			mhayaLogger.Warnf("handlelog TurntableEventContent Unmarshal err:%v", err)
			return code.Error
		}

		err = mdb.LogstashDB.Create(content).Error
		if err != nil {
			mhayaLogger.Warnf("handlelog LogstashDB Create TurntableEventContent err:%v", err)
			return code.Error
		}

	case *eventmodels.JoinChannelEventContent:
		content := &eventmodels.JoinChannelEventContent{}
		err := json.Unmarshal([]byte(req.JsonContent), &content)
		if err != nil {
			mhayaLogger.Warnf("handlelog JoinChannelEventContent Unmarshal err:%v", err)
			return code.Error
		}

		err = mdb.LogstashDB.Create(content).Error
		if err != nil {
			mhayaLogger.Warnf("handlelog LogstashDB Create JoinChannelEventContent err:%v", err)
			return code.Error
		}

	case *eventmodels.FollowEventContent:
		content := &eventmodels.FollowEventContent{}
		err := json.Unmarshal([]byte(req.JsonContent), &content)
		if err != nil {
			mhayaLogger.Warnf("handlelog FollowEventContent Unmarshal err:%v", err)
			return code.Error
		}

		err = mdb.LogstashDB.Create(content).Error
		if err != nil {
			mhayaLogger.Warnf("handlelog LogstashDB Create FollowEventContent err:%v", err)
			return code.Error
		}

	case *eventmodels.TaskFinishEventContent:
		content := &eventmodels.TaskFinishEventContent{}
		err := json.Unmarshal([]byte(req.JsonContent), &content)
		if err != nil {
			mhayaLogger.Warnf("handlelog TaskFinishEventContent Unmarshal err:%v", err)
			return code.Error
		}

		err = mdb.LogstashDB.Create(content).Error
		if err != nil {
			mhayaLogger.Warnf("handlelog LogstashDB Create TaskFinishEventContent err:%v", err)
			return code.Error
		}

	case *eventmodels.BackendOperationEventContent:
		content := &eventmodels.BackendOperationEventContent{}
		err := json.Unmarshal([]byte(req.JsonContent), &content)
		if err != nil {
			mhayaLogger.Warnf("handlelog BackendOperationEventContent Unmarshal err:%v", err)
			return code.Error
		}

		err = mdb.LogstashDB.Create(content).Error
		if err != nil {
			mhayaLogger.Warnf("handlelog LogstashDB Create BackendOperationEventContent err:%v", err)
			return code.Error
		}
	}

	return code.OK
}
