// Code generated by neo-go contract generate-rpcwrapper --manifest <file.json> --out <file.go> [--hash <hash>] [--config <config>]; DO NOT EDIT.

// Package nft contains RPC wrappers for HASHY NFT contract.
package nft

import (
	"errors"
	"fmt"
	"git.marketally.com/tutus-one/tutus-chain/pkg/core/transaction"
	"git.marketally.com/tutus-one/tutus-chain/pkg/rpcclient/nep11"
	"git.marketally.com/tutus-one/tutus-chain/pkg/rpcclient/nep22"
	"git.marketally.com/tutus-one/tutus-chain/pkg/rpcclient/nep24"
	"git.marketally.com/tutus-one/tutus-chain/pkg/rpcclient/nep31"
	"git.marketally.com/tutus-one/tutus-chain/pkg/smartcontract"
	"git.marketally.com/tutus-one/tutus-chain/pkg/util"
	"git.marketally.com/tutus-one/tutus-chain/pkg/vm/stackitem"
	"math/big"
)

// Hash contains contract hash.
var Hash = util.Uint160{0x33, 0x22, 0x11, 0x0, 0xff, 0xee, 0xdd, 0xcc, 0xbb, 0xaa, 0x99, 0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x0}

// NEP22Contract is an alias for nep22.Contract.
type NEP22Contract = nep22.Contract

// NEP31Contract is an alias for nep31.Contract.
type NEP31Contract = nep31.Contract

// NftRoyaltyRecipientShare is a contract-specific nft.RoyaltyRecipientShare type used by its methods.
type NftRoyaltyRecipientShare struct {
	Address util.Uint160
	Share   *big.Int
}

// Invoker is used by ContractReader to call various safe methods.
type Invoker interface {
	nep11.Invoker
}

// Actor is used by Contract to call state-changing methods.
type Actor interface {
	Invoker

	nep11.Actor

	MakeCall(contract util.Uint160, method string, params ...any) (*transaction.Transaction, error)
	MakeRun(script []byte) (*transaction.Transaction, error)
	MakeUnsignedCall(contract util.Uint160, method string, attrs []transaction.Attribute, params ...any) (*transaction.Transaction, error)
	MakeUnsignedRun(script []byte, attrs []transaction.Attribute) (*transaction.Transaction, error)
	SendCall(contract util.Uint160, method string, params ...any) (util.Uint256, uint32, error)
	SendRun(script []byte) (util.Uint256, uint32, error)
}

// ContractReader implements safe contract methods.
type ContractReader struct {
	nep11.NonDivisibleReader
	nep24.RoyaltyReader
	invoker Invoker
	hash    util.Uint160
}

// Contract implements all contract methods.
type Contract struct {
	ContractReader
	nep11.BaseWriter
	NEP22Contract
	NEP31Contract
	actor Actor
	hash  util.Uint160
}

// NewReader creates an instance of ContractReader using Hash and the given Invoker.
func NewReader(invoker Invoker) *ContractReader {
	var hash = Hash
	return &ContractReader{*nep11.NewNonDivisibleReader(invoker, hash), *nep24.NewRoyaltyReader(invoker, hash), invoker, hash}
}

// New creates an instance of Contract using Hash and the given Actor.
func New(actor Actor) *Contract {
	var hash = Hash
	var nep11ndt = nep11.NewNonDivisible(actor, hash)
	var nep24t = nep24.NewRoyaltyReader(actor, hash)
	return &Contract{ContractReader{nep11ndt.NonDivisibleReader, *nep24t, actor, hash}, nep11ndt.BaseWriter, NEP22Contract(*nep22.NewContract(actor, hash)), NEP31Contract(*nep31.NewContract(actor, hash)), actor, hash}
}

func (c *Contract) scriptForSetRoyaltyInfo(ctx any, tokenID []byte, recipients []*NftRoyaltyRecipientShare) ([]byte, error) {
	return smartcontract.CreateCallWithAssertScript(c.hash, "setRoyaltyInfo", ctx, tokenID, recipients)
}

// SetRoyaltyInfo creates a transaction invoking `setRoyaltyInfo` method of the contract.
// This transaction is signed and immediately sent to the network.
// The values returned are its hash, ValidUntilBlock value and error if any.
func (c *Contract) SetRoyaltyInfo(ctx any, tokenID []byte, recipients []*NftRoyaltyRecipientShare) (util.Uint256, uint32, error) {
	script, err := c.scriptForSetRoyaltyInfo(ctx, tokenID, recipients)
	if err != nil {
		return util.Uint256{}, 0, err
	}
	return c.actor.SendRun(script)
}

// SetRoyaltyInfoTransaction creates a transaction invoking `setRoyaltyInfo` method of the contract.
// This transaction is signed, but not sent to the network, instead it's
// returned to the caller.
func (c *Contract) SetRoyaltyInfoTransaction(ctx any, tokenID []byte, recipients []*NftRoyaltyRecipientShare) (*transaction.Transaction, error) {
	script, err := c.scriptForSetRoyaltyInfo(ctx, tokenID, recipients)
	if err != nil {
		return nil, err
	}
	return c.actor.MakeRun(script)
}

// SetRoyaltyInfoUnsigned creates a transaction invoking `setRoyaltyInfo` method of the contract.
// This transaction is not signed, it's simply returned to the caller.
// Any fields of it that do not affect fees can be changed (ValidUntilBlock,
// Nonce), fee values (NetworkFee, SystemFee) can be increased as well.
func (c *Contract) SetRoyaltyInfoUnsigned(ctx any, tokenID []byte, recipients []*NftRoyaltyRecipientShare) (*transaction.Transaction, error) {
	script, err := c.scriptForSetRoyaltyInfo(ctx, tokenID, recipients)
	if err != nil {
		return nil, err
	}
	return c.actor.MakeUnsignedRun(script, nil)
}

// itemToNftRoyaltyRecipientShare converts stack item into *NftRoyaltyRecipientShare.
// NULL item is returned as nil pointer without error.
func itemToNftRoyaltyRecipientShare(item stackitem.Item, err error) (*NftRoyaltyRecipientShare, error) {
	if err != nil {
		return nil, err
	}
	_, null := item.(stackitem.Null)
	if null {
		return nil, nil
	}
	var res = new(NftRoyaltyRecipientShare)
	err = res.FromStackItem(item)
	return res, err
}

// Ensure *NftRoyaltyRecipientShare is a proper [stackitem.Convertible].
var _ = stackitem.Convertible(&NftRoyaltyRecipientShare{})

// Ensure *NftRoyaltyRecipientShare is a proper [smartcontract.Convertible].
var _ = smartcontract.Convertible(&NftRoyaltyRecipientShare{})

// FromStackItem retrieves fields of NftRoyaltyRecipientShare from the given
// [stackitem.Item] or returns an error if it's not possible to do to so.
// It implements [stackitem.Convertible] interface.
func (res *NftRoyaltyRecipientShare) FromStackItem(item stackitem.Item) error {
	arr, ok := item.Value().([]stackitem.Item)
	if !ok {
		return errors.New("not an array")
	}
	if len(arr) != 2 {
		return errors.New("wrong number of structure elements")
	}

	var (
		index = -1
		err   error
	)
	index++
	res.Address, err = func(item stackitem.Item) (util.Uint160, error) {
		b, err := item.TryBytes()
		if err != nil {
			return util.Uint160{}, err
		}
		u, err := util.Uint160DecodeBytesBE(b)
		if err != nil {
			return util.Uint160{}, err
		}
		return u, nil
	}(arr[index])
	if err != nil {
		return fmt.Errorf("field Address: %w", err)
	}

	index++
	res.Share, err = arr[index].TryInteger()
	if err != nil {
		return fmt.Errorf("field Share: %w", err)
	}

	return nil
}

// ToStackItem creates [stackitem.Item] representing NftRoyaltyRecipientShare.
// It implements [stackitem.Convertible] interface.
func (res *NftRoyaltyRecipientShare) ToStackItem() (stackitem.Item, error) {
	if res == nil {
		return stackitem.Null{}, nil
	}

	var (
		err   error
		itm   stackitem.Item
		items = make([]stackitem.Item, 0, 2)
	)
	itm, err = stackitem.NewByteArray(res.Address.BytesBE()), error(nil)
	if err != nil {
		return nil, fmt.Errorf("field Address: %w", err)
	}
	items = append(items, itm)

	itm, err = (*stackitem.BigInteger)(res.Share), error(nil)
	if err != nil {
		return nil, fmt.Errorf("field Share: %w", err)
	}
	items = append(items, itm)

	return stackitem.NewStruct(items), nil
}

// ToSCParameter creates [smartcontract.Parameter] representing NftRoyaltyRecipientShare.
// It implements [smartcontract.Convertible] interface so that NftRoyaltyRecipientShare
// could be used with invokers.
func (res *NftRoyaltyRecipientShare) ToSCParameter() (smartcontract.Parameter, error) {
	if res == nil {
		return smartcontract.Parameter{Type: smartcontract.AnyType}, nil
	}

	var (
		err  error
		prm  smartcontract.Parameter
		prms = make([]smartcontract.Parameter, 0, 2)
	)
	prm, err = smartcontract.NewParameterFromValue(res.Address)
	if err != nil {
		return smartcontract.Parameter{}, fmt.Errorf("field Address: %w", err)
	}
	prms = append(prms, prm)

	prm, err = smartcontract.NewParameterFromValue(res.Share)
	if err != nil {
		return smartcontract.Parameter{}, fmt.Errorf("field Share: %w", err)
	}
	prms = append(prms, prm)

	return smartcontract.Parameter{Type: smartcontract.ArrayType, Value: prms}, nil
}
