package types

import (
	"testing"

	"github.com/stretchr/testify/require"
)

// TestEncodeResult is used for evm test to show how to encode result
func TestEncodeResult(t *testing.T) {
	result := NewResult(
		"test",
		1,
		mustDecodeString("0000000342544300000000000003e8"),
		1,
		1,
		2,
		1,
		1591622616,
		1591622618,
		RESOLVE_STATUS_SUCCESS,
		mustDecodeString("00000000009443ee"),
	)
	expectedEncodedResult := mustDecodeString(
		"0a047465737410011a0f0000000342544300000000000003e8200128013002380140d8f7f8f60548daf7f8f60550015a0800000000009443ee",
	)
	require.Equal(t, expectedEncodedResult, ModuleCdc.MustMarshal(&result))
}

func TestEncodeResultOfEmptyClientID(t *testing.T) {
	result := NewResult(
		"",
		1,
		mustDecodeString("0000000342544300000000000003e8"),
		1,
		1,
		1,
		1,
		1591622426,
		1591622429,
		RESOLVE_STATUS_SUCCESS,
		mustDecodeString("0000000000944387"),
	)
	expectedEncodedResult := mustDecodeString(
		"10011a0f0000000342544300000000000003e82001280130013801409af6f8f605489df6f8f60550015a080000000000944387",
	)
	require.Equal(t, expectedEncodedResult, ModuleCdc.MustMarshal(&result))
}

func TestEncodeFailResult(t *testing.T) {
	result := NewResult(
		"client_id",
		1,
		mustDecodeString("0000000342544300000000000003e8"),
		1,
		1,
		1,
		1,
		1591622426,
		1591622429,
		RESOLVE_STATUS_FAILURE,
		[]byte{},
	)
	expectedEncodedResult := mustDecodeString(
		"0a09636c69656e745f696410011a0f0000000342544300000000000003e82001280130013801409af6f8f605489df6f8f6055002",
	)
	require.Equal(t, expectedEncodedResult, ModuleCdc.MustMarshal(&result))
}

func TestPackFullABI(t *testing.T) {
	result := NewResult(
		"client_id",
		1,
		mustDecodeString(
			"00000009000000034254430000000344414900000003444f5400000003455448000000044c494e4b00000005535553484900000003554e490000000455534443000000045553445403",
		),
		1,
		1,
		1,
		1,
		1591622426,
		1591622429,
		RESOLVE_STATUS_SUCCESS,
		mustDecodeString(
			"0000000900000003425443000000192aa82026000000000344414900000000003b97bcc000000003444f540000000000e4b3f940000000034554480000000171f262e660000000044c494e4b0000000001b6068d500000000553555348490000000000208eee3000000003554e490000000000f71f5228000000045553444300000000003b994360000000045553445400000000003b9be5c8",
		),
	)

	abiResult, err := result.PackFullABI()
	require.NoError(t, err)
	require.Equal(
		t,
		mustDecodeString(
			"00000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000001a00000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000005ede3b1a000000000000000000000000000000000000000000000000000000005ede3b1d000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000009636c69656e745f69640000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004900000009000000034254430000000344414900000003444f5400000003455448000000044c494e4b00000005535553484900000003554e490000000455534443000000045553445403000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000990000000900000003425443000000192aa82026000000000344414900000000003b97bcc000000003444f540000000000e4b3f940000000034554480000000171f262e660000000044c494e4b0000000001b6068d500000000553555348490000000000208eee3000000003554e490000000000f71f5228000000045553444300000000003b994360000000045553445400000000003b9be5c800000000000000",
		),
		abiResult,
	)
}

func TestPackPartialABI(t *testing.T) {
	result := NewResult(
		"client_id",
		1,
		mustDecodeString(
			"00000009000000034254430000000344414900000003444f5400000003455448000000044c494e4b00000005535553484900000003554e490000000455534443000000045553445403",
		),
		1,
		1,
		1,
		1,
		1591622426,
		1591622429,
		RESOLVE_STATUS_SUCCESS,
		mustDecodeString(
			"0000000900000003425443000000192aa82026000000000344414900000000003b97bcc000000003444f540000000000e4b3f940000000034554480000000171f262e660000000044c494e4b0000000001b6068d500000000553555348490000000000208eee3000000003554e490000000000f71f5228000000045553444300000000003b994360000000045553445400000000003b9be5c8",
		),
	)

	abiResult, err := result.PackPartialABI()
	require.NoError(t, err)
	require.Equal(
		t,
		mustDecodeString(
			"000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000e0000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000005ede3b1d00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000160000000000000000000000000000000000000000000000000000000000000004900000009000000034254430000000344414900000003444f5400000003455448000000044c494e4b00000005535553484900000003554e490000000455534443000000045553445403000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000990000000900000003425443000000192aa82026000000000344414900000000003b97bcc000000003444f540000000000e4b3f940000000034554480000000171f262e660000000044c494e4b0000000001b6068d500000000553555348490000000000208eee3000000003554e490000000000f71f5228000000045553444300000000003b994360000000045553445400000000003b9be5c800000000000000",
		),
		abiResult,
	)
}
