package repo

import (
	"testing"

	"github.com/multiformats/go-multiaddr"
	"github.com/stretchr/testify/assert"
	"golang.org/x/xerrors"

	"github.com/filecoin-project/lotus/chain/types"
	"github.com/filecoin-project/lotus/node/config"

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

func basicTest(t *testing.T, repo Repo) {
	apima, err := repo.APIEndpoint()
	if assert.Error(t, err) {
		assert.Equal(t, ErrNoAPIEndpoint, err)
	}
	assert.Nil(t, apima, "with no api endpoint, return should be nil")

	lrepo, err := repo.Lock(FullNode)
	assert.NoError(t, err, "should be able to lock once")
	assert.NotNil(t, lrepo, "locked repo shouldn't be nil")

	{
		lrepo2, err := repo.Lock(FullNode)
		if assert.Error(t, err) {
			assert.Equal(t, ErrRepoAlreadyLocked, err)
		}
		assert.Nil(t, lrepo2, "with locked repo errors, nil should be returned")
	}

	err = lrepo.Close()
	assert.NoError(t, err, "should be able to unlock")

	lrepo, err = repo.Lock(FullNode)
	assert.NoError(t, err, "should be able to relock")
	assert.NotNil(t, lrepo, "locked repo shouldn't be nil")

	ma, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/43244")
	assert.NoError(t, err, "creating multiaddr shouldn't error")

	err = lrepo.SetAPIEndpoint(ma)
	assert.NoError(t, err, "setting multiaddr shouldn't error")

	apima, err = repo.APIEndpoint()
	assert.NoError(t, err, "setting multiaddr shouldn't error")
	assert.Equal(t, ma, apima, "returned API multiaddr should be the same")

	c1, err := lrepo.Config()
	assert.Equal(t, config.DefaultFullNode(), c1, "there should be a default config")
	assert.NoError(t, err, "config should not error")

	// mutate config and persist back to repo
	err = lrepo.SetConfig(func(c interface{}) {
		cfg := c.(*config.FullNode)
		cfg.Client.IpfsMAddr = "duvall"
	})
	assert.NoError(t, err)

	// load config and verify changes
	c2, err := lrepo.Config()
	require.NoError(t, err)
	cfg2 := c2.(*config.FullNode)
	require.Equal(t, cfg2.Client.IpfsMAddr, "duvall")

	err = lrepo.Close()
	assert.NoError(t, err, "should be able to close")

	apima, err = repo.APIEndpoint()

	if assert.Error(t, err) {
		assert.Equal(t, ErrNoAPIEndpoint, err, "after closing repo, api should be nil")
	}
	assert.Nil(t, apima, "with closed repo, apima should be set back to nil")

	k1 := types.KeyInfo{Type: "foo"}
	k2 := types.KeyInfo{Type: "bar"}

	lrepo, err = repo.Lock(FullNode)
	assert.NoError(t, err, "should be able to relock")
	assert.NotNil(t, lrepo, "locked repo shouldn't be nil")

	kstr, err := lrepo.KeyStore()
	assert.NoError(t, err, "should be able to get keystore")
	assert.NotNil(t, lrepo, "keystore shouldn't be nil")

	list, err := kstr.List()
	assert.NoError(t, err, "should be able to list key")
	assert.Empty(t, list, "there should be no keys")

	err = kstr.Put("k1", k1)
	assert.NoError(t, err, "should be able to put k1")

	err = kstr.Put("k1", k1)
	if assert.Error(t, err, "putting key under the same name should error") {
		assert.True(t, xerrors.Is(err, types.ErrKeyExists), "returned error is ErrKeyExists")
	}

	k1prim, err := kstr.Get("k1")
	assert.NoError(t, err, "should be able to get k1")
	assert.Equal(t, k1, k1prim, "returned key should be the same")

	k2prim, err := kstr.Get("k2")
	if assert.Error(t, err, "should not be able to get k2") {
		assert.True(t, xerrors.Is(err, types.ErrKeyInfoNotFound), "returned error is ErrKeyNotFound")
	}
	assert.Empty(t, k2prim, "there should be no output for k2")

	err = kstr.Put("k2", k2)
	assert.NoError(t, err, "should be able to put k2")

	list, err = kstr.List()
	assert.NoError(t, err, "should be able to list keys")
	assert.ElementsMatch(t, []string{"k1", "k2"}, list, "returned elements match")

	err = kstr.Delete("k2")
	assert.NoError(t, err, "should be able to delete key")

	list, err = kstr.List()
	assert.NoError(t, err, "should be able to list keys")
	assert.ElementsMatch(t, []string{"k1"}, list, "returned elements match")

	err = kstr.Delete("k2")
	if assert.Error(t, err) {
		assert.True(t, xerrors.Is(err, types.ErrKeyInfoNotFound), "returned errror is ErrKeyNotFound")
	}
}
