package frostfs

import (
	"fmt"

	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
	"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/event"
	"github.com/nspcc-dev/neo-go/pkg/core/state"
	"github.com/nspcc-dev/neo-go/pkg/util"
	"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
)

type Bind struct {
	BindCommon
}

type BindCommon struct {
	UserValue []byte
	KeysValue [][]byte

	// TxHashValue is used in notary environmental
	// for calculating unique but same for
	// all notification receivers values.
	TxHashValue util.Uint256
}

// TxHash returns hash of the TX with new epoch
// notification.
func (b BindCommon) TxHash() util.Uint256 {
	return b.TxHashValue
}

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

func (b BindCommon) Keys() [][]byte { return b.KeysValue }

func (b BindCommon) User() []byte { return b.UserValue }

func ParseBind(e *state.ContainedNotificationEvent) (event.Event, error) {
	var (
		ev  Bind
		err error
	)

	params, err := event.ParseStackArray(e)
	if err != nil {
		return nil, fmt.Errorf("could not parse stack items from notify event: %w", err)
	}

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

	ev.TxHashValue = e.Container

	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.UserValue, 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.KeysValue = 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.KeysValue = append(dst.KeysValue, rawKey)
	}

	return nil
}