package servers

import (
	sjson "encoding/json"
	"errors"
	"leafstalk/covenant/pbfmsg"
	"leafstalk/log"
	"leafstalk/network/cluster"
	"reflect"
)

// type PlayerGate struct {
// 	PlayerId int64
// 	GateId   int32
// }

type PlayerAgent struct {
	*cluster.ServerAgent
	PlayerId int64
	GateId   int64
}

// var (
// 	allPlayerGates *cach2.Cache
// )

// func InitPlayerAgentList() {
// 	allPlayerGates = cach2.New(0, 10*time.Minute)
// }

func NewPlayerAgent(serverAgent *cluster.ServerAgent, msg *pbfmsg.RouteMsg) *PlayerAgent {
	pa := new(PlayerAgent)
	pa.ServerAgent = serverAgent
	pa.PlayerId = msg.PlayerId
	pa.GateId = msg.GateId
	return pa
}

func GetUserData(serverAgent *cluster.ServerAgent, request *pbfmsg.RouteMsg) func() interface{} {
	return func() interface{} {
		return NewPlayerAgent(serverAgent, request)
	}
}

// func GetPlayerAgent(msg *pbfmsg.RouteMsg, serverAgent *cluster.ServerAgent) interface{} {
// 	if msg.PlayerId == 0 { // || msg.GateId == 0
// 		return serverAgent
// 	}

// 	strId := strconv.FormatInt(msg.PlayerId, 10)
// 	if v, ok := allPlayerGates.Get(strId); ok {
// 		old := v.(*PlayerGate)
// 		if old.PlayerId == msg.PlayerId && old.GateId == msg.GateId {
// 			return NewPlayerAgent(old, serverAgent)
// 		}
// 	}

// 	npg := newPlayerGate(msg)
// 	return NewPlayerAgent(npg, serverAgent)
// }

// func newPlayerGate(msg *pbfmsg.RouteMsg) *PlayerGate {
// 	p := new(PlayerGate)
// 	p.PlayerId = msg.PlayerId
// 	p.GateId = msg.GateId
// 	return p
// }

// func NewPlayerGate(playerId int64, gateId int32) *PlayerGate {
// 	p := new(PlayerGate)
// 	p.PlayerId = playerId
// 	p.GateId = gateId
// 	return p
// }

// // func (p *PlayerGate) GetGateId() int32 {
// // 	return atomic.LoadInt32(&p.GateId)
// // }

// // func (p *PlayerGate) SetGateId(gateId int32) {
// // 	atomic.StoreInt32(&p.GateId, gateId)
// // }

// // 客户端登录，记录玩家网关
// func AddPlayerGate(playerId int64, gateId int32) {

// 	// 确保信息不会被删除
// 	// gateId 不等时，不能修改只能添加，因为信息有可能被使用
// 	strId := strconv.FormatInt(playerId, 10)
// 	if v, ok := allPlayerGates.Get(strId); ok {
// 		old := v.(*PlayerGate)
// 		if old.GateId == gateId {
// 			allPlayerGates.SetDefault(strId, v)
// 			return
// 		}
// 	}

// 	//
// 	pg := NewPlayerGate(playerId, gateId)
// 	allPlayerGates.SetDefault(strId, pg)
// }

// // 客户端退出，延迟删除玩家网关
// func RemovePlayerGate(playerId int64) {
// 	strId := strconv.FormatInt(playerId, 10)
// 	if v, ok := allPlayerGates.Get(strId); ok {
// 		allPlayerGates.Set(strId, v, 3*time.Minute)
// 	}
// }

func CopyPlayerAgent(agent *PlayerAgent) *PlayerAgent {
	p := new(PlayerAgent)
	p.ServerAgent = agent.ServerAgent
	p.PlayerId = agent.PlayerId
	p.GateId = agent.GateId
	return p
}

func (p *PlayerAgent) SendToPlayer(Msg interface{}) error {

	data, _ := sjson.Marshal(Msg)
	// if len(data) > 2048 {
	// 	data = data[:2048]
	// }

	msgType := reflect.TypeOf(Msg)
	var msgName string
	if msgType != nil && msgType.Kind() == reflect.Ptr {
		msgName = msgType.Elem().Name()
	}
	log.Infof("SendToPlayer playerId: %v %v %v", p.PlayerId, msgName, string(data))

	s := GetServerById(p.GateId)
	if s != nil && s.Agent != nil {
		_, err := s.Agent.WriteClientRouteMsg(Msg, p.PlayerId)
		return err
	}
	log.Warn("no found gate.", p.GateId)
	return errors.New("no found gate")
}

// rpc回应消息，需要传递rpcMsgId;
// rpc请求消息和非rpc消息，不用传递rpcMsgIds
func (p *PlayerAgent) SendToServer(Msg interface{}) error {
	if p.ServerAgent != nil {
		return p.ServerAgent.WriteServerRouteMsg(Msg, p.PlayerId, p.GateId, "")
	}
	log.Warn("no found server agent")
	return errors.New("no server agent")
}

func (p *PlayerAgent) SendRpcResponse(Msg interface{}, toMoudel string) error {
	if p.ServerAgent != nil {
		return p.ServerAgent.WriteServerRouteMsg(Msg, p.PlayerId, p.GateId, toMoudel)
	}
	log.Warn("no found server agent")
	return errors.New("no server agent")
}
