package consensus

import (
	"Walnut-HS/pkg/myecdsa"
	"Walnut-HS/pkg/protobuf"
	"Walnut-HS/pkg/vrf"
	"fmt"
	"testing"
)

func PrintTestMsg(m *protobuf.Message) {
	fmt.Print("Type: ", m.Type, ", ")
	fmt.Print("View: ", m.View, ", ")
	fmt.Print("Com: ", m.Com, ", ")
	fmt.Println("Sender: ", m.Sender, ", ")
}

func TestHonestParty(t *testing.T) {
	ipList := []string{"127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1",
		"127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1",
		"127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1", "127.0.0.1",
		"127.0.0.1"}
	portList := []string{"8880", "8881", "8882", "8883", "8884", "8885", "8886", "8887", "8888", "8889",
		"8870", "8871", "8872", "8873", "8874", "8875", "8876", "8877", "8878", "8879",
		"8860", "8861", "8862", "8863", "8864", "8865", "8866", "8867", "8868", "8869", "8859"}

	N := uint32(4)
	lambda := uint32(4)
	tt := uint32(1)
	QCsize := uint32(3)
	CurCom := make([]uint32, N)
	CurComBool := make([]bool, N)
	for i := uint32(0); i < N; i++ {
		CurCom[i] = i
		CurComBool[i] = true
	}
	privks, pubks := myecdsa.SigKeysGen(int(N))
	vrfprivks, vrfpubks := vrf.VrfKeysGen(int(N))

	var p []*HonestParty = make([]*HonestParty, N)
	for i := uint32(0); i < N; i++ {
		p[i] = NewHonestParty(i, ipList, portList, pubks, privks[i], vrfpubks, vrfprivks[i], N, lambda, tt, QCsize)
		p[i].CurCom = CurCom
		p[i].CurComBool = CurComBool
	}

	// Initialize receive channel and then send channel, the order cannot be changed
	for i := uint32(0); i < N; i++ {
		p[i].InitReceiveChannel()
	}

	for i := uint32(0); i < N; i++ {
		p[i].InitSendChannel()
	}

	// each replica broadcasts a message
	for i := uint32(0); i < N; i++ {
		m := &protobuf.Message{
			Type:   "Test",
			View:   0,
			Com:    0,
			Sender: uint32(i),
			Data:   make([]byte, 10000000),
		}
		p[i].SendCurCom(m)
	}

	// each replica prints the message received
	for i := uint32(0); i < N; i++ {
		fmt.Println("--------------------------------")
		fmt.Println("PID ", p[i].PID, ": ")
		for j := uint32(0); j < N; j++ {
			m := <-p[i].GetMessage("Test", 0)
			PrintTestMsg(m)
		}
	}

}