package enterprise

import (
	"os"
	"testing"

	"github.com/aergoio/aergo-lib/db"
	"github.com/aergoio/aergo/v2/state"
	"github.com/aergoio/aergo/v2/state/statedb"
	"github.com/aergoio/aergo/v2/types"
	"github.com/stretchr/testify/assert"
)

var cdb *state.ChainStateDB
var sdb *statedb.StateDB

func initTest(t *testing.T) (*statedb.ContractState, *state.AccountState, *state.AccountState) {
	cdb = state.NewChainStateDB()
	cdb.Init(string(db.BadgerImpl), "test", nil, false, nil)
	genesis := types.GetTestGenesis()
	sdb = cdb.OpenNewStateDB(cdb.GetRoot())
	err := cdb.SetGenesis(genesis, nil)
	if err != nil {
		t.Fatalf("failed init : %s", err.Error())
	}
	const testSender = "AmPNYHyzyh9zweLwDyuoiUuTVCdrdksxkRWDjVJS76WQLExa2Jr4"

	scs, err := statedb.GetSystemAccountState(cdb.GetStateDB())
	assert.NoError(t, err, "could not open contract state")

	account, err := types.DecodeAddress(testSender)
	assert.NoError(t, err, "could not decode test address")
	sender, err := state.GetAccountState(account, sdb)
	assert.NoError(t, err, "could not get test address state")
	receiver, err := state.GetAccountState([]byte(types.AergoEnterprise), sdb)
	assert.NoError(t, err, "could not get test address state")
	return scs, sender, receiver
}

func deinitTest() {
	cdb.Close()
	os.RemoveAll("test")
}

func TestSetGetConf(t *testing.T) {
	scs, _, _ := initTest(t)
	defer deinitTest()
	testConf := &Conf{On: true, Values: []string{"abc:w", "def:r", "ghi:s"}}
	retConf, err := getConf(scs, []byte(RPCPermissions))
	assert.NoError(t, err, "could not get test conf")
	assert.Nil(t, retConf, "not set yet")
	err = setConf(scs, []byte(RPCPermissions), testConf)
	assert.NoError(t, err, "could not set test conf")
	retConf, err = getConf(scs, []byte(RPCPermissions))
	assert.NoError(t, err, "could not get test conf")
	assert.Equal(t, testConf.Values, retConf.Values, "check values")
	assert.Equal(t, testConf.On, retConf.On, "check on")

	testConf2 := &Conf{On: false, Values: []string{"1", "22", "333"}}
	err = setConf(scs, []byte("p2pwhite"), testConf2)
	assert.NoError(t, err, "could not set test conf")
	retConf2, err := getConf(scs, []byte("p2pwhite"))
	assert.NoError(t, err, "could not get test conf")
	assert.Equal(t, testConf2.Values, retConf2.Values, "check values")
	assert.Equal(t, testConf2.On, retConf2.On, "check on")
}

const testCert = `-----BEGIN CERTIFICATE-----
MIIE4DCCAsigAwIBAgIBATANBgkqhkiG9w0BAQsFADAQMQ4wDAYDVQQDEwVhZXJn
bzAeFw0xOTA1MjIwMTU3MzJaFw0yMDExMjIwMTU3MzJaMBAxDjAMBgNVBAMTBWFl
cmdvMIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEA6y/dza6BPg6l//Hv
+NR5bgtEVNG68W38vfn/M2WDA9PfIaf2nlH3RiawMpw4G6cT/+qGoESCypDLEASS
NUo1Xkl5Ms78rus3PM6wVLuA6qgtyBEFKOu6ChMcmNh2RtybA10hgyPxakDilSZs
ZnONPBMGEnBFYGPNZm/VZgsavuIcJNnTDbPFjXyiet3+xEflNDA8PPdEHZXkYRCS
Z1bDH+iMm37KbFyJ2i7UTVmP21OFYnoNHcZTuYmbgCm/w9Fvf2vkY5fl3OidFKse
MqCB4VwWbEXlCGarrFrhjZ2Vd6M6YCwcq3lX+wC3f7mFwRa39BWZs+VFW2KPhuvr
jKR1lWjtM6PIl0mhqhJjShzrb2wgX6RaSVrOs1E+dQL9vNB9hQieShUVyd4m433Y
+gpJHeNhVZ+yBuNzm8wB3BB9e4mf4pKB9WFiLO9Gdi35hnldbUne9F78IJXSeIsv
MmZIu3hHkHeHcsTXrK9AQuP2XTAxgZ6OjdlmvZ9kGNHxikAKH0reMSOU5IBn6FwX
+7uKoU/Gyj5jTruMbwfbkr5SsEg0cG+xIT8l+ml3V7kOm3xYFfPRBhFTJIMcqEf5
QD8OlLmOMvDQ/3+w+G+ZngvS/bos8vzMxkpDM/Dr4UY1OFYS0LLXppKUJEC3XJlj
ReGCs4H07gtZiVEW8hc1xJ9VuS0CAwEAAaNFMEMwDgYDVR0PAQH/BAQDAg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