package channelserver

import (
	"erupe-ce/common/byteframe"
	"erupe-ce/network/mhfpacket"

	"go.uber.org/zap"
)

func handleMsgSysCreateObject(s *Session, p mhfpacket.MHFPacket) {
	pkt := p.(*mhfpacket.MsgSysCreateObject)

	s.stage.Lock()
	newObj := &Object{
		id:          s.getObjectId(),
		ownerCharID: s.charID,
		x:           pkt.X,
		y:           pkt.Y,
		z:           pkt.Z,
	}
	s.stage.objects[s.charID] = newObj
	s.stage.Unlock()

	// Response to our requesting client.
	resp := byteframe.NewByteFrame()
	resp.WriteUint32(newObj.id) // New local obj handle.
	doAckSimpleSucceed(s, pkt.AckHandle, resp.Data())
	// Duplicate the object creation to all sessions in the same stage.
	dupObjUpdate := &mhfpacket.MsgSysDuplicateObject{
		ObjID:       newObj.id,
		X:           newObj.x,
		Y:           newObj.y,
		Z:           newObj.z,
		OwnerCharID: newObj.ownerCharID,
	}

	s.logger.Info("Broadcasting new object", zap.String("name", s.Name), zap.Uint32("objectID", newObj.id))
	s.stage.BroadcastMHF(dupObjUpdate, s)
}

func handleMsgSysDeleteObject(s *Session, p mhfpacket.MHFPacket) {} // stub: unimplemented

func handleMsgSysPositionObject(s *Session, p mhfpacket.MHFPacket) {
	pkt := p.(*mhfpacket.MsgSysPositionObject)
	if s.server.erupeConfig.DebugOptions.LogInboundMessages {
		s.logger.Debug("Object position update",
			zap.String("name", s.Name),
			zap.Uint32("objectID", pkt.ObjID),
			zap.Float32("x", pkt.X),
			zap.Float32("y", pkt.Y),
			zap.Float32("z", pkt.Z),
		)
	}
	s.stage.Lock()
	object, ok := s.stage.objects[s.charID]
	if ok {
		object.x = pkt.X
		object.y = pkt.Y
		object.z = pkt.Z
	}
	s.stage.Unlock()
	// One of the few packets we can just re-broadcast directly.
	s.stage.BroadcastMHF(pkt, s)
}

func handleMsgSysRotateObject(s *Session, p mhfpacket.MHFPacket) {} // stub: unimplemented

func handleMsgSysDuplicateObject(s *Session, p mhfpacket.MHFPacket) {} // stub: unimplemented

func handleMsgSysSetObjectBinary(s *Session, p mhfpacket.MHFPacket) {
	_ = p.(*mhfpacket.MsgSysSetObjectBinary)
	/* This causes issues with PS3 as this actually sends with endiness!
	for _, session := range s.server.sessions {
		if session.charID == s.charID {
			s.server.userBinary.Set(s.charID, 3, pkt.RawDataPayload)
			msg := &mhfpacket.MsgSysNotifyUserBinary{
				CharID:     s.charID,
				BinaryType: 3,
			}
			s.server.BroadcastMHF(msg, s)
		}
	}
	*/
}

func handleMsgSysGetObjectBinary(s *Session, p mhfpacket.MHFPacket) {} // stub: unimplemented

func handleMsgSysGetObjectOwner(s *Session, p mhfpacket.MHFPacket) {} // stub: unimplemented

func handleMsgSysUpdateObjectBinary(s *Session, p mhfpacket.MHFPacket) {} // stub: unimplemented

func handleMsgSysCleanupObject(s *Session, p mhfpacket.MHFPacket) {} // stub: unimplemented

func handleMsgSysAddObject(s *Session, p mhfpacket.MHFPacket) {} // stub: unimplemented

func handleMsgSysDelObject(s *Session, p mhfpacket.MHFPacket) {} // stub: unimplemented

func handleMsgSysDispObject(s *Session, p mhfpacket.MHFPacket) {} // stub: unimplemented

func handleMsgSysHideObject(s *Session, p mhfpacket.MHFPacket) {} // stub: unimplemented
