package initialize

import (
	"encoding/hex"
	"fmt"

	"git.frostfs.info/TrueCloudLab/frostfs-contract/nns"
	"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/constants"
	"git.frostfs.info/TrueCloudLab/frostfs-node/cmd/frostfs-adm/internal/modules/morph/helper"
	morphClient "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/morph/client"
	"github.com/nspcc-dev/neo-go/pkg/core/state"
	"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
	"github.com/nspcc-dev/neo-go/pkg/encoding/address"
	"github.com/nspcc-dev/neo-go/pkg/io"
	"github.com/nspcc-dev/neo-go/pkg/rpcclient/management"
	"github.com/nspcc-dev/neo-go/pkg/smartcontract/callflag"
	"github.com/nspcc-dev/neo-go/pkg/util"
	"github.com/nspcc-dev/neo-go/pkg/vm/emit"
	"github.com/nspcc-dev/neo-go/pkg/vm/opcode"
)

func setNNS(c *helper.InitializeContext) error {
	r := management.NewReader(c.ReadOnlyInvoker)
	nnsCs, err := r.GetContractByID(1)
	if err != nil {
		return err
	}

	ok, err := c.NNSRootRegistered(nnsCs.Hash, "frostfs")
	if err != nil {
		return err
	} else if !ok {
		bw := io.NewBufBinWriter()
		emit.AppCall(bw.BinWriter, nnsCs.Hash, "register", callflag.All,
			"frostfs", c.CommitteeAcc.Contract.ScriptHash(),
			constants.FrostfsOpsEmail, constants.NNSRefreshDefVal, constants.NNSRetryDefVal,
			int64(constants.DefaultExpirationTime), constants.NNSTtlDefVal)
		emit.Opcodes(bw.BinWriter, opcode.ASSERT)
		if err := c.SendCommitteeTx(bw.Bytes(), true); err != nil {
			return fmt.Errorf("can't add domain root to NNS: %w", err)
		}
		if err := c.AwaitTx(); err != nil {
			return err
		}
	}

	alphaCs := c.GetContract(constants.AlphabetContract)
	for i, acc := range c.Accounts {
		alphaCs.Hash = state.CreateContractHash(acc.Contract.ScriptHash(), alphaCs.NEF.Checksum, alphaCs.Manifest.Name)

		domain := helper.GetAlphabetNNSDomain(i)
		if err := nnsRegisterDomain(c, nnsCs.Hash, alphaCs.Hash, domain); err != nil {
			return err
		}
		c.Command.Printf("NNS: Set %s -> %s\n", domain, alphaCs.Hash.StringLE())
	}

	for _, ctrName := range constants.ContractList {
		cs := c.GetContract(ctrName)

		domain := ctrName + ".frostfs"
		if err := nnsRegisterDomain(c, nnsCs.Hash, cs.Hash, domain); err != nil {
			return err
		}
		c.Command.Printf("NNS: Set %s -> %s\n", domain, cs.Hash.StringLE())
	}

	groupKey := c.ContractWallet.Accounts[0].PrivateKey().PublicKey()
	err = updateNNSGroup(c, nnsCs.Hash, groupKey)
	if err != nil {
		return err
	}
	c.Command.Printf("NNS: Set %s -> %s\n", morphClient.NNSGroupKeyName, hex.EncodeToString(groupKey.Bytes()))

	return c.AwaitTx()
}

func updateNNSGroup(c *helper.InitializeContext, nnsHash util.Uint160, pub *keys.PublicKey) error {
	bw := io.NewBufBinWriter()
	keyAlreadyAdded, domainRegCodeEmitted, err := c.EmitUpdateNNSGroupScript(bw, nnsHash, pub)
	if keyAlreadyAdded || err != nil {
		return err
	}

	script := bw.Bytes()
	if domainRegCodeEmitted {
		w := io.NewBufBinWriter()
		emit.Instruction(w.BinWriter, opcode.INITSSLOT, []byte{1})
		wrapRegisterScriptWithPrice(w, nnsHash, script)
		script = w.Bytes()
	}

	return c.SendCommitteeTx(script, true)
}

// wrapRegisterScriptWithPrice wraps a given script with `getPrice`/`setPrice` calls for NNS.
// It is intended to be used for a single transaction, and not as a part of other scripts.
// It is assumed that script already contains static slot initialization code, the first one
// (with index 0) is used to store the price.
func wrapRegisterScriptWithPrice(w *io.BufBinWriter, nnsHash util.Uint160, s []byte) {
	if len(s) == 0 {
		return
	}

	emit.AppCall(w.BinWriter, nnsHash, "getPrice", callflag.All)
	emit.Opcodes(w.BinWriter, opcode.STSFLD0)
	emit.AppCall(w.BinWriter, nnsHash, "setPrice", callflag.All, 1)

	w.WriteBytes(s)

	emit.Opcodes(w.BinWriter, opcode.LDSFLD0, opcode.PUSH1, opcode.PACK)
	emit.AppCallNoArgs(w.BinWriter, nnsHash, "setPrice", callflag.All)

	if w.Err != nil {
		panic(fmt.Errorf("BUG: can't wrap register script: %w", w.Err))
	}
}

func nnsRegisterDomain(c *helper.InitializeContext, nnsHash, expectedHash util.Uint160, domain string) error {
	script, ok, err := c.NNSRegisterDomainScript(nnsHash, expectedHash, domain)
	if ok || err != nil {
		return err
	}

	w := io.NewBufBinWriter()
	emit.Instruction(w.BinWriter, opcode.INITSSLOT, []byte{1})
	wrapRegisterScriptWithPrice(w, nnsHash, script)

	emit.AppCall(w.BinWriter, nnsHash, "deleteRecords", callflag.All, domain, int64(nns.TXT))
	emit.AppCall(w.BinWriter, nnsHash, "addRecord", callflag.All,
		domain, int64(nns.TXT), expectedHash.StringLE())
	emit.AppCall(w.BinWriter, nnsHash, "addRecord", callflag.All,
		domain, int64(nns.TXT), address.Uint160ToString(expectedHash))
	return c.SendCommitteeTx(w.Bytes(), true)
}