package netmap

import (
	"fmt"

	"git.frostfs.info/TrueCloudLab/frostfs-contract/netmap"
	"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
	"github.com/nspcc-dev/neo-go/pkg/network/payload"
)

type UpdatePeer struct {
	PubKey *keys.PublicKey

	State netmap.NodeState

	// For notary notifications only.
	// Contains raw transactions of notary request.
	Request *payload.P2PNotaryRequest
}

// MorphEvent implements Neo:Morph Event interface.
func (UpdatePeer) MorphEvent() {}

// Online returns true if node's state is requested to be switched
// to "online".
func (s UpdatePeer) Online() bool {
	return s.State == netmap.NodeStateOnline
}

// Maintenance returns true if node's state is requested to be switched
// to "maintenance".
func (s UpdatePeer) Maintenance() bool {
	return s.State == netmap.NodeStateMaintenance
}

func (s UpdatePeer) PublicKey() *keys.PublicKey {
	return s.PubKey
}

// NotaryRequest returns raw notary request if notification
// was received via notary service. Otherwise, returns nil.
func (s UpdatePeer) NotaryRequest() *payload.P2PNotaryRequest {
	return s.Request
}

func (s *UpdatePeer) decodeState(state int64) error {
	switch s.State = netmap.NodeState(state); s.State {
	default:
		return fmt.Errorf("unsupported node state %d", state)
	case
		netmap.NodeStateOffline,
		netmap.NodeStateOnline,
		netmap.NodeStateMaintenance:
		return nil
	}
}

const expectedItemNumUpdatePeer = 2