package oracle

import (
	"errors"
	"math/big"
	"testing"

	"git.marketally.com/tutus-one/tutus-chain/pkg/core/transaction"
	"git.marketally.com/tutus-one/tutus-chain/pkg/tutusrpc/result"
	"git.marketally.com/tutus-one/tutus-chain/pkg/util"
	"git.marketally.com/tutus-one/tutus-chain/pkg/vm/stackitem"
	"github.com/stretchr/testify/require"
)

type testAct struct {
	err error
	res *result.Invoke
	tx  *transaction.Transaction
	txh util.Uint256
	vub uint32
}

func (t *testAct) Call(contract util.Uint160, operation string, params ...any) (*result.Invoke, error) {
	return t.res, t.err
}
func (t *testAct) MakeCall(contract util.Uint160, method string, params ...any) (*transaction.Transaction, error) {
	return t.tx, t.err
}
func (t *testAct) MakeUnsignedCall(contract util.Uint160, method string, attrs []transaction.Attribute, params ...any) (*transaction.Transaction, error) {
	return t.tx, t.err
}
func (t *testAct) SendCall(contract util.Uint160, method string, params ...any) (util.Uint256, uint32, error) {
	return t.txh, t.vub, t.err
}

func TestReader(t *testing.T) {
	ta := new(testAct)
	ora := NewReader(ta)

	ta.err = errors.New("")
	_, err := ora.GetPrice()
	require.Error(t, err)

	ta.err = nil
	ta.res = &result.Invoke{
		State: "HALT",
		Stack: []stackitem.Item{
			stackitem.Make(42),
		},
	}
	price, err := ora.GetPrice()
	require.NoError(t, err)
	require.Equal(t, big.NewInt(42), price)
}

func TestPriceSetter(t *testing.T) {
	ta := new(testAct)
	ora := New(ta)

	big42 := big.NewInt(42)

	ta.err = errors.New("")
	_, _, err := ora.SetPrice(big42)
	require.Error(t, err)
	_, err = ora.SetPriceTransaction(big42)
	require.Error(t, err)
	_, err = ora.SetPriceUnsigned(big42)
	require.Error(t, err)

	ta.err = nil
	ta.txh = util.Uint256{1, 2, 3}
	ta.vub = 42
	ta.tx = transaction.New([]byte{1, 2, 3}, 100500)

	h, vub, err := ora.SetPrice(big42)
	require.NoError(t, err)
	require.Equal(t, ta.txh, h)
	require.Equal(t, ta.vub, vub)

	tx, err := ora.SetPriceTransaction(big42)
	require.NoError(t, err)
	require.Equal(t, ta.tx, tx)
	tx, err = ora.SetPriceUnsigned(big42)
	require.NoError(t, err)
	require.Equal(t, ta.tx, tx)
}
