package service

import (
	"encoding/json"
	"fmt"
	"gadmin/config"
	"gadmin/internal/admin/consts"
	"gadmin/internal/admin/forms"
	"gadmin/internal/admin/gm_rpc/rpc_share"
	"gadmin/package/gmdata"
	"gadmin/utility/character"
	"gadmin/utility/player"
	"gadmin/utility/serializer"
	"github.com/gin-gonic/gin"
	msg2 "leafstalk/covenant/msg"
)

// Gm gm服务
var Gm = new(sGm)

type sGm struct{}

/*// DelRank 删除玩家排行榜
func (s *sGm) DelRank(ctx *gin.Context, req forms.DelRankReq) serializer.Response {
	logrus.Warnf("Gm.DelRank req:%+v", req)
	var (
		err              error
		name, ok         = constants.GmRankNameMap[req.RankType]
		userAccountModel sqlModel.UserAccount
	)

	if req.ServerId <= 0 {
		return serializer.Err(consts.CodeParamErr, "请选择一个有效的服务器", nil)
	}

	if !ok {
		return serializer.Err(consts.CodeParamErr, "删除玩家排行失败，提交的排名类型未加入到服务中", err)
	}

	playerAttr, err := player.GetAttrByUserId(req.UserID)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}
	DB, err := player.GetDBByPlayerAttr(playerAttr)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}
	//获取userAccount
	err = config.LDB.WithContext(ctx).Scopes(sqlModel.UserAccountTable(&userAccountModel, playerAttr.AccID)).Where("accId = ?", playerAttr.AccID).First(&userAccountModel).Error
	if err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}
	openIdArr := strings.Split(userAccountModel.Openid, "@")
	msg := msg2.GmDelRank{
		MsgId:      fmt.Sprintf("delRank:%v", character.GenerateMsgId()),
		PlayerID:   req.UserID,
		RankType:   req.RankType,
		ClientPlat: openIdArr[1],
	}

	//if err = gate.SendToWorld(0, &msg); err != nil {
	//	return serializer.Err(consts.CodeParamErr, "删除玩家排行失败", err)
	//}
	rpc_share.MsgMap[msg.MsgId] = fmt.Sprintf("GM删除玩家排行榜排名 PlayerID: %d, RankType: [%v]", req.UserID, name)

	var resp *msg2.ResponseGmDelRank
	res, err := config.GmNats.GmRequest(DB, "GmDelRank", msg)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}

	if err = json.Unmarshal(res, &resp); err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}

	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: msg.MsgId,
		Data:  resp,
	}

	return serializer.Suc(req)
}*/

// UpdateGuides 更新引导
func (s *sGm) UpdateGuides(ctx *gin.Context, req forms.UpdateGuidesReq) serializer.Response {
	var (
		err error
	)
	DB, err := player.GetDBByUserId(req.Playerid)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}
	userGuides := make(map[int64]int)
	for _, v := range req.Guides {
		userGuides[int64(v)] = 1
	}
	msg := msg2.GmUpdateGuides{
		MsgId:    character.GenerateMsgId(),
		PlayerID: req.Playerid,
		Guides:   userGuides,
	}

	rpc_share.MsgMap[msg.MsgId] = fmt.Sprintf("GM为修改引导 PlayerID: %d, Guides: %v", req.Playerid, req.Guides)

	var resp *msg2.ResponseGmUpdateGuides
	res, err := config.GmNats.GmRequest(DB, "GmUpdateGuides", msg)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}

	if err = json.Unmarshal(res, &resp); err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}

	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: msg.MsgId,
		Data:  resp,
	}
	return serializer.Suc(req)
}

// UpdateChapter 更新关卡
func (s *sGm) UpdateChapter(ctx *gin.Context, req forms.UpdateChapterReq) serializer.Response {
	var err error
	DB, err := player.GetDBByUserId(req.Playerid)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}

	msgId := character.GenerateMsgId()
	var msg msg2.GmUpdateChapter
	if req.OneFull {
		msg = msg2.GmUpdateChapter{
			MsgId:    msgId,
			PlayerID: req.Playerid,
			Chapters: NewFullChapters(true),
		}

		rpc_share.MsgMap[msgId] = fmt.Sprintf("GM为玩家[%d]一键通关", req.Playerid)
	} else if req.OneEmpty {
		msg = msg2.GmUpdateChapter{
			MsgId:    msgId,
			PlayerID: req.Playerid,
			Chapters: NewFullChapters(false),
		}

		rpc_share.MsgMap[msgId] = fmt.Sprintf("GM清除了玩家[%d]的通关数据", req.Playerid)
	} else {
		msg = msg2.GmUpdateChapter{
			MsgId:    msgId,
			PlayerID: req.Playerid,
			Chapters: NewPassChapters(req.PassChapters),
		}
		if len(msg.Chapters) == 0 {
			return serializer.Err(consts.CodeParamErr, "请选择一个有效的关卡", nil)
		}
		rpc_share.MsgMap[msgId] = fmt.Sprintf("GM为玩家[%d]通关了指定关卡：%v", req.Playerid, req.PassChapters)
	}

	var resp *msg2.ResponseGmUpdateChapter
	res, err := config.GmNats.GmRequest(DB, "GmUpdateChapter", msg)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}

	if err = json.Unmarshal(res, &resp); err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}

	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: msg.MsgId,
		Data:  resp,
	}

	return serializer.Suc(req)
}

func NewFullChapters(pass bool) map[int64]int64 {
	chapters := make(map[int64]int64, 3)
	if pass {
		chapters = gmdata.GetMaxChapterForType()
	}
	return chapters
}

func NewPassChapters(chapterStr []string) map[int64]int64 {
	chapters := make(map[int64]int64, 3)
	chapterMap := gmdata.GetChaptersMap()
	for _, chapter := range chapterStr {
		if data, ok := chapterMap[chapter]; ok {
			if _, ok1 := chapters[data.Type]; !ok1 || data.Uid > chapters[data.Type] {
				chapters[data.Type] = data.Uid
			}
		}
	}
	return chapters
}

/*// UpdateTalent 更新天赋
func (s *sGm) UpdateTalent(ctx *gin.Context, req forms.UpdateTalentReq) serializer.Response {

	var (
		data = msg2.GmUpdateTalent{
			MsgId:    character.GenerateMsgId(),
			PlayerID: req.Playerid,
		}
		err error
	)
	data.Talents = req.TalentInfo
	//if err = gate.SendToWorld(0, &msg); err == nil {
	//	rpc_share.MsgMap[msg.MsgId] = fmt.Sprintf("GM为玩家[%d]修改天赋[%v]", req.Playerid, req.TalentInfo)
	//}

	DB, err := player.GetDBByUserId(req.Playerid)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}

	rpc_share.MsgMap[data.MsgId] = fmt.Sprintf("GM为玩家[%d]修改天赋[%v]", req.Playerid, req.TalentInfo)

	var resp *msg2.ResponseGmUpdateTalent
	res, err := config.GmNats.GmRequest(DB, "GmUpdateTalent", data)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}

	if err = json.Unmarshal(res, &resp); err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}

	rpc_share.LogChan <- rpc_share.LogMsg{
		MsgID: data.MsgId,
		Data:  resp,
	}
	return serializer.Suc(req)
}

// UpdatePlayerBase 更新玩家金币、钻石、 经验、体力、精力、天赋点
func (s *sGm) UpdatePlayerBase(ctx *gin.Context, req forms.UpdatePlayerBaseReq) serializer.Response {
	playerAttr, err := player.GetAttrByUserId(req.Playerid)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}

	DB, err := player.GetDBByPlayerAttr(playerAttr)
	if err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}

	//获取userAccount
	var userAccountModel sqlModel.UserAccount
	err = config.LDB.WithContext(ctx).Scopes(sqlModel.UserAccountTable(&userAccountModel, playerAttr.AccID)).Where("accId = ?", playerAttr.AccID).First(&userAccountModel).Error
	if err != nil {
		return serializer.Err(consts.CodeParamErr, err.Error(), err)
	}
	openIdArr := strings.Split(userAccountModel.Openid, "@")

	if req.Exp > 0 || req.TalentPoint != 0 {
		data := msg2.GmAddExp{
			MsgId:       character.GenerateMsgId(),
			PlayerID:    req.Playerid,
			Exp:         req.Exp,
			TalentPoint: req.TalentPoint,
		}

		//if err = gate.SendToWorld(0, &data); err != nil {
		//	return serializer.Err(consts.CodeParamErr, "更新玩家经验或天赋点失败", err)
		//}

		if req.Exp > 0 {
			rpc_share.MsgMap[data.MsgId] = fmt.Sprintf("GM为玩家[%d]添加经验[%d]", req.Playerid, req.Exp)
		}
		if req.TalentPoint > 0 {
			rpc_share.MsgMap[data.MsgId] = fmt.Sprintf("GM为玩家[%d]添加天赋[%d]", req.Playerid, req.TalentPoint)
		}

		var resp *msg2.ResponseGmAddExp
		res, err := config.GmNats.GmRequest(DB, "GmAddExp", data)
		if err != nil {
			return serializer.Err(consts.CodeParamErr, err.Error(), err)
		}

		if err = json.Unmarshal(res, &resp); err != nil {
			return serializer.Err(consts.CodeParamErr, err.Error(), err)
		}

		rpc_share.LogChan <- rpc_share.LogMsg{
			MsgID: data.MsgId,
			Data:  resp,
		}
	}

	if req.Stamina != 0 || req.Energy != 0 {
		data := msg2.GmAddStama{
			MsgId:      character.GenerateMsgId(),
			PlayerID:   req.Playerid,
			Stama:      req.Stamina,
			Energy:     req.Energy,
			ClientPlat: openIdArr[1],
		}
		//if err = gate.SendToWorld(0, &msg); err != nil {
		//	return serializer.Err(consts.CodeParamErr, "更新玩家体力或精力失败", err)
		//}
		rpc_share.MsgMap[data.MsgId] = fmt.Sprintf("GM为玩家[%d]更新体力[%d],精力[%d]", req.Playerid, req.Stamina, req.Energy)

		var resp *msg2.ResponseGmAddStama
		res, err := config.GmNats.GmRequest(DB, "GmAddStama", data)
		if err != nil {
			return serializer.Err(consts.CodeParamErr, err.Error(), err)
		}

		if err = json.Unmarshal(res, &resp); err != nil {
			return serializer.Err(consts.CodeParamErr, err.Error(), err)
		}

		rpc_share.LogChan <- rpc_share.LogMsg{
			MsgID: data.MsgId,
			Data:  resp,
		}
	}

	if req.Diamond != 0 || req.Coin != 0 {
		data := msg2.GmAddMaterial{
			MsgId:      character.GenerateMsgId(),
			PlayerID:   req.Playerid,
			Materials:  []*model.DropMaterial{{constants.MaterilaId_Diamond, req.Diamond}, {constants.MaterilaId_Coin, req.Coin}},
			ClientPlat: openIdArr[1],
		}
		//if err = gate.SendToWorld(0, &msg); err != nil {
		//	return serializer.Err(consts.CodeParamErr, "更新玩家变更钻石或金币失败", err)
		//}
		rpc_share.MsgMap[data.MsgId] = fmt.Sprintf("GM为玩家[%d]变更钻石[%d],金币[%d] ", req.Playerid, req.Diamond, req.Coin)

		var resp *msg2.ResponseGmAddMaterial
		res, err := config.GmNats.GmRequest(DB, "GmAddMaterial", data)
		if err != nil {
			return serializer.Err(consts.CodeParamErr, err.Error(), err)
		}

		if err = json.Unmarshal(res, &resp); err != nil {
			return serializer.Err(consts.CodeParamErr, err.Error(), err)
		}

		rpc_share.LogChan <- rpc_share.LogMsg{
			MsgID: data.MsgId,
			Data:  resp,
		}
	}

	return serializer.Suc(req)
}*/
