package native

import (
	"strings"
	"testing"

	"git.marketally.com/tutus-one/tutus-chain/pkg/compiler"
	"git.marketally.com/tutus-one/tutus-chain/pkg/config"
	"git.marketally.com/tutus-one/tutus-chain/pkg/core/native/nativehashes"
	"git.marketally.com/tutus-one/tutus-chain/pkg/tutustest"
	"git.marketally.com/tutus-one/tutus-chain/pkg/tutustest/chain"
	"git.marketally.com/tutus-one/tutus-chain/pkg/vm/stackitem"
)

func TestStd_HexEncodeDecodeCompat(t *testing.T) {
	bc, acc := chain.NewSingleWithCustomConfig(t, func(c *config.Blockchain) {
		c.Hardforks = map[string]uint32{
			config.HFFaun.String(): 2,
		}
	})
	e := tutustest.NewExecutor(t, bc, acc, acc)
	p := e.CommitteeInvoker(nativehashes.StdLib)

	expectedBytes := []byte{0x00, 0x01, 0x02, 0x03}
	expectedString := "00010203"

	p.InvokeFail(t, "method not found: hexEncode/1", "hexEncode", expectedBytes)

	e.AddNewBlock(t)

	p.Invoke(t, expectedString, "hexEncode", expectedBytes)
	p.Invoke(t, expectedBytes, "hexDecode", expectedString)
}

func TestStd_HexEncodeDecodeInteropAPI(t *testing.T) {
	bc, acc := chain.NewSingleWithCustomConfig(t, func(c *config.Blockchain) {
		c.Hardforks = map[string]uint32{
			config.HFFaun.String(): 0,
		}
	})
	e := tutustest.NewExecutor(t, bc, acc, acc)

	src := `package teststd
		import (
			"git.marketally.com/tutus-one/tutus-chain/pkg/interop/native/std"
		)
		func Encode(args []any) string {
			return std.HexEncode(args[0].([]byte))
		}
		func Decode(args []any) []byte {
			return std.HexDecode(args[0].(string))
		}`

	ctr := tutustest.CompileSource(t, e.Validator.ScriptHash(), strings.NewReader(src), &compiler.Options{
		Name: "teststd_contract",
	})
	e.DeployContract(t, ctr, nil)

	expectedBytes := []byte{0x00, 0x01, 0x02, 0x03}
	expectedString := "00010203"

	ctrInvoker := e.NewInvoker(ctr.Hash, e.Committee)

	ctrInvoker.Invoke(t, stackitem.Make(expectedString), "encode", []any{expectedBytes})
	ctrInvoker.Invoke(t, stackitem.Make(expectedBytes), "decode", []any{expectedString})
}
