package service

import (
	"encoding/hex"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	tronaddr "github.com/fbsobreira/gotron-sdk/pkg/address"
	"github.com/fbsobreira/gotron-sdk/pkg/client"
	"google.golang.org/grpc"
	"key-manager/conf"
	"key-manager/dao"
	"math/big"
	"testing"
)

func TestTronAddress(t *testing.T) {
	inputdata := "a9059cbb000000000000000000000000c106d14c008c4e1eb09433c549beff124ddf067300000000000000000000000000000000000000000000000000000001e1f69180"
	input, err := hex.DecodeString(inputdata)
	if err != nil {
		t.Fatal(err)
	}
	var methodId [4]byte
	copy(methodId[:], input[:4])
	t.Log(hex.EncodeToString(methodId[:]))
	unpack, err := arguments.Unpack(input[4:])
	if err != nil {
		t.Fatal(err)
	}
	t.Log(unpack[0].(common.Address))
	t.Log(unpack[1].(*big.Int))
	addressTron := make([]byte, 0)
	addressTron = append(addressTron, tronaddr.TronBytePrefix)
	addressTron = append(addressTron, unpack[0].(common.Address).Bytes()...)
	t.Log(tronaddr.Address(addressTron).String())

	addr2, err := tronaddr.Base58ToAddress(tronaddr.Address(addressTron).String())
	if err != nil {
		t.Fatal(err)
	}
	t.Log("addr2", addr2.String())
}

func setup() *KeyManager {
	err := conf.InitConfig("../conf/conf.yaml")
	if err != nil {
		panic(err)
	}

	db, err := dao.InitMysqlDB()
	if err != nil {
		panic(err)
	}

	km, err := NewKeyManager(db, "111")
	if err != nil {
		panic(err)
	}

	return km
}

func TestKeyManager_Sign_Eth(t *testing.T) {
	km := setup()

	usdc := common.HexToAddress(ethUsdc)
	to := common.HexToAddress("0x08eF611f69ab48a31fFF7380088eAa106a3BD5A5")
	data, err := arguments.Pack(to, big.NewInt(1000000))
	if err != nil {
		t.Fatal(err)
	}

	input := append(transferId, data...)
	tx := &types.DynamicFeeTx{
		ChainID:   big.NewInt(1),
		Nonce:     0,
		GasTipCap: big.NewInt(10000),
		GasFeeCap: big.NewInt(10000),
		Gas:       10000,
		To:        &usdc,
		Value:     big.NewInt(0),
		Data:      input,
	}

	txStr, err := marshalJEthTx(types.NewTx(tx))
	if err != nil {
		t.Fatal(err)
	}
	t.Log("txStr", txStr)

	signReq := &SignRequest{
		Network: "eth",
		Sender:  "0x313A1F70aCA7dE919ff2104F50f94985308E38A8",
		Tx:      txStr,
	}
	res, err := km.Sign(nil, signReq)
	if err != nil {
		t.Fatal(err)
	}

	t.Log("signedTx", res.SignedTx)
	signedTx, err := unmarshalJEthTx(res.SignedTx)
	if err != nil {
		t.Fatal(err)
	}
	txStr2, err := signedTx.MarshalJSON()
	if err != nil {
		t.Fatal(err)
	}

	t.Log("txStr2", string(txStr2))
}

func TestKeyManager_Sign_Tron(t *testing.T) {
	km := setup()

	conn := client.NewGrpcClient("grpc.trongrid.io:50051")
	err := conn.Start(grpc.WithInsecure())
	if err != nil {
		t.Fatal(err)
	}

	transactionExtention, err := conn.TRC20Send("TW2aoftrhYE9SbRwyVGFPhDWQU9XzivhBt", "TN1HN14scJCuGMPmr4bh8rxknK5AG2zMgx", tronUsdt, big.NewInt(1000000), 50)
	if err != nil {
		t.Fatal(err)
	}

	t.Log("transactionExtention", transactionExtention.String())

	txStr, err := marshalJTronTx(transactionExtention.Transaction)
	if err != nil {
		t.Fatal(err)
	}

	t.Log("txStr", txStr)

	signReq := &SignRequest{
		Network: "tron",
		Sender:  "TLZMwPhCvqGt8w4523L7ciMcEG4EWwxWKP",
		Tx:      txStr,
	}

	res, err := km.Sign(nil, signReq)
	if err != nil {
		t.Fatal(err)
	}

	t.Log("signedTx", res.SignedTx)

	tx, err := unmarshalJTronTx(res.SignedTx)
	if err != nil {
		t.Fatal(err)
	}

	t.Log("tx", tx.String())
}
