package types

import (
	"testing"

	"github.com/ethereum/go-ethereum/params"

	paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
	"github.com/stretchr/testify/require"
)

func TestParamKeyTable(t *testing.T) {
	require.IsType(t, paramtypes.KeyTable{}, ParamKeyTable())
}

func TestParamsValidate(t *testing.T) {
	testCases := []struct {
		name     string
		params   Params
		expError bool
	}{
		{"default", DefaultParams(), false},
		{
			"valid",
			NewParams("ara", true, true, DefaultChainConfig(), 2929, 1884, 1344),
			false,
		},
		{
			"empty",
			Params{},
			true,
		},
		{
			"invalid evm denom",
			Params{
				EvmDenom: "@!#!@$!@5^32",
			},
			true,
		},
		{
			"invalid eip",
			Params{
				EvmDenom:  "stake",
				ExtraEIPs: []int64{1},
			},
			true,
		},
		{
			"invalid chain config",
			NewParams("ara", true, true, ChainConfig{
				HomesteadBlock: newIntPtr(-1),
			}, 2929, 1884, 1344),
			true,
		},
		{
			"invalid eip712 allowed msgs",
			Params{
				EvmDenom:     "ara",
				EnableCreate: true,
				EnableCall:   true,
				ChainConfig:  DefaultChainConfig(),
				ExtraEIPs:    []int64{2929, 1884, 1344},
				EIP712AllowedMsgs: []EIP712AllowedMsg{
					{MsgTypeUrl: "/cosmos.bank.v1beta1.MsgSend", MsgValueTypeName: "MsgValueSend"},
					{MsgTypeUrl: "/cosmos.bank.v1beta1.MsgSend", MsgValueTypeName: "MsgValueSend"},
				},
			},
			true,
		},
	}

	for _, tc := range testCases {
		err := tc.params.Validate()

		if tc.expError {
			require.Error(t, err, tc.name)
		} else {
			require.NoError(t, err, tc.name)
		}
	}
}

func TestParamsEIPs(t *testing.T) {
	params := NewParams("ara", true, true, DefaultChainConfig(), 2929, 1884, 1344)
	actual := params.EIPs()

	require.Equal(t, []int([]int{2929, 1884, 1344}), actual)
}

func TestParamsValidatePriv(t *testing.T) {
	require.Error(t, validateEVMDenom(false))
	require.NoError(t, validateEVMDenom("inj"))
	require.Error(t, validateBool(""))
	require.NoError(t, validateBool(true))
	require.Error(t, validateEIPs(""))
	require.NoError(t, validateEIPs([]int64{1884}))
}

func TestValidateChainConfig(t *testing.T) {
	testCases := []struct {
		name     string
		i        interface{}
		expError bool
	}{
		{
			"invalid chain config type",
			"string",
			true,
		},
		{
			"valid chain config type",
			DefaultChainConfig(),
			false,
		},
	}
	for _, tc := range testCases {
		err := validateChainConfig(tc.i)

		if tc.expError {
			require.Error(t, err, tc.name)
		} else {
			require.NoError(t, err, tc.name)
		}
	}
}

func TestIsLondon(t *testing.T) {
	testCases := []struct {
		name   string
		height int64
		result bool
	}{
		{
			"Before london block",
			5,
			false,
		},
		{
			"After london block",
			12_965_001,
			true,
		},
		{
			"london block",
			12_965_000,
			true,
		},
	}

	for _, tc := range testCases {
		ethConfig := params.MainnetChainConfig
		require.Equal(t, IsLondon(ethConfig, tc.height), tc.result)
	}
}
