package early_comm

import (
	"testing"

	"github.com/andeya/erpc/v7"
	"github.com/andeya/goutil"
)

//go:generate go test -v -c -o "${GOPACKAGE}_client" $GOFILE

func TestClient(t *testing.T) {
	if goutil.IsGoTest() {
		t.Log("skip test in go test")
		return
	}
	defer erpc.FlushLogger()
	cli := erpc.NewPeer(
		erpc.PeerConfig{
			PrintDetail: false,
		},
		new(earlyCall),
	)
	defer cli.Close()
	_, stat := cli.Dial(":9090")
	if !stat.OK() {
		erpc.Fatalf("%v", stat)
	}
}

type earlyCall struct{}

func (e *earlyCall) Name() string {
	return "early_call"
}

func (e *earlyCall) PostDial(sess erpc.PreSession, isRedial bool) *erpc.Status {
	stat := sess.PreSend(
		erpc.TypeCall,
		"/early/ping",
		map[string]string{
			"author": "andeya",
		},
		nil,
	)
	if !stat.OK() {
		return stat
	}

	input := sess.PreReceive(func(header erpc.Header) interface{} {
		if header.ServiceMethod() == "/early/pong" {
			return new(string)
		}
		erpc.Panicf("Received an unexpected response: %s", header.ServiceMethod())
		return nil
	})
	stat = input.Status()
	if !stat.OK() {
		return stat
	}
	erpc.Infof("result: %v", input.String())
	return nil
}
