package neofs

import (
	"fmt"

	"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
	"github.com/nspcc-dev/neofs-node/pkg/morph/client"
	"github.com/nspcc-dev/neofs-node/pkg/morph/event"
)

type Bind struct {
	bindCommon
}

type bindCommon struct {
	user []byte
	keys [][]byte
}

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

func (b bindCommon) Keys() [][]byte { return b.keys }

func (b bindCommon) User() []byte { return b.user }

func ParseBind(params []stackitem.Item) (event.Event, error) {
	var (
		ev  Bind
		err error
	)

	err = parseBind(&ev.bindCommon, params)
	if err != nil {
		return nil, err
	}

	return ev, nil
}

func parseBind(dst *bindCommon, params []stackitem.Item) error {
	if ln := len(params); ln != 2 {
		return event.WrongNumberOfParameters(2, ln)
	}

	var err error

	// parse user
	dst.user, err = client.BytesFromStackItem(params[0])
	if err != nil {
		return fmt.Errorf("could not get bind user: %w", err)
	}

	// parse keys
	bindKeys, err := client.ArrayFromStackItem(params[1])
	if err != nil {
		return fmt.Errorf("could not get bind keys: %w", err)
	}

	dst.keys = make([][]byte, 0, len(bindKeys))

	for i := range bindKeys {
		rawKey, err := client.BytesFromStackItem(bindKeys[i])
		if err != nil {
			return fmt.Errorf("could not get bind public key: %w", err)
		}

		dst.keys = append(dst.keys, rawKey)
	}

	return nil
}