package arith

import (
	"context"
	"time"

	"github.com/smallnest/rpcx/client"
	"github.com/smallnest/rpcx/protocol"
	"github.com/smallnest/rpcx/share"
)

type ArithXClient struct {
	xcli client.XClient
}

func (a *ArithXClient) Init(etcdAddrs []string, basePath, servicePath string) {
	opt := client.Option{
		Retries:        1,
		RPCPath:        share.DefaultRPCPath,
		ConnectTimeout: 10 * time.Second,
		SerializeType:  protocol.JSON,
		CompressType:   protocol.None,
		BackupLatency:  10 * time.Millisecond,
	}

	d := client.NewEtcdDiscovery(basePath, servicePath, etcdAddrs, nil)
	a.xcli = client.NewXClient(servicePath, client.Failtry, client.RandomSelect, d, opt)
}

func (a *ArithXClient) Add(ctx context.Context, req *ArithAddReq) (reply ArithAddReply, err error) {
	err = a.xcli.Call(ctx, "Add", req, &reply)
	return
}

func (a *ArithXClient) Mul(ctx context.Context, req *ArithMulReq) (reply ArithMulReply, err error) {
	err = a.xcli.Call(ctx, "Mul", req, &reply)
	return
}
