// @generated Code generated by thrift-gen. Do not modify.

// Package tracetest is generated code used to make or handle TChannel calls using Thrift.
package tracetest

import (
	"fmt"

	athrift "github.com/apache/thrift/lib/go/thrift"
	"github.com/uber/tchannel-go/thrift"
)

// Interfaces for the service and client for the services defined in the IDL.

// TChanTracedService is the interface that defines the server handler and client interface.
type TChanTracedService interface {
	JoinTrace(ctx thrift.Context, request *JoinTraceRequest) (*TraceResponse, error)
	StartTrace(ctx thrift.Context, request *StartTraceRequest) (*TraceResponse, error)
}

// Implementation of a client and service handler.

type tchanTracedServiceClient struct {
	thriftService string
	client        thrift.TChanClient
}

func NewTChanTracedServiceInheritedClient(thriftService string, client thrift.TChanClient) *tchanTracedServiceClient {
	return &tchanTracedServiceClient{
		thriftService,
		client,
	}
}

// NewTChanTracedServiceClient creates a client that can be used to make remote calls.
func NewTChanTracedServiceClient(client thrift.TChanClient) TChanTracedService {
	return NewTChanTracedServiceInheritedClient("TracedService", client)
}

func (c *tchanTracedServiceClient) JoinTrace(ctx thrift.Context, request *JoinTraceRequest) (*TraceResponse, error) {
	var resp TracedServiceJoinTraceResult
	args := TracedServiceJoinTraceArgs{
		Request: request,
	}
	success, err := c.client.Call(ctx, c.thriftService, "joinTrace", &args, &resp)
	if err == nil && !success {
	}

	return resp.GetSuccess(), err
}

func (c *tchanTracedServiceClient) StartTrace(ctx thrift.Context, request *StartTraceRequest) (*TraceResponse, error) {
	var resp TracedServiceStartTraceResult
	args := TracedServiceStartTraceArgs{
		Request: request,
	}
	success, err := c.client.Call(ctx, c.thriftService, "startTrace", &args, &resp)
	if err == nil && !success {
	}

	return resp.GetSuccess(), err
}

type tchanTracedServiceServer struct {
	handler TChanTracedService
}

// NewTChanTracedServiceServer wraps a handler for TChanTracedService so it can be
// registered with a thrift.Server.
func NewTChanTracedServiceServer(handler TChanTracedService) thrift.TChanServer {
	return &tchanTracedServiceServer{
		handler,
	}
}

func (s *tchanTracedServiceServer) Service() string {
	return "TracedService"
}

func (s *tchanTracedServiceServer) Methods() []string {
	return []string{
		"joinTrace",
		"startTrace",
	}
}

func (s *tchanTracedServiceServer) Handle(ctx thrift.Context, methodName string, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
	switch methodName {
	case "joinTrace":
		return s.handleJoinTrace(ctx, protocol)
	case "startTrace":
		return s.handleStartTrace(ctx, protocol)

	default:
		return false, nil, fmt.Errorf("method %v not found in service %v", methodName, s.Service())
	}
}

func (s *tchanTracedServiceServer) handleJoinTrace(ctx thrift.Context, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
	var req TracedServiceJoinTraceArgs
	var res TracedServiceJoinTraceResult

	if err := req.Read(protocol); err != nil {
		return false, nil, err
	}

	r, err :=
		s.handler.JoinTrace(ctx, req.Request)

	if err != nil {
		return false, nil, err
	} else {
		res.Success = r
	}

	return err == nil, &res, nil
}

func (s *tchanTracedServiceServer) handleStartTrace(ctx thrift.Context, protocol athrift.TProtocol) (bool, athrift.TStruct, error) {
	var req TracedServiceStartTraceArgs
	var res TracedServiceStartTraceResult

	if err := req.Read(protocol); err != nil {
		return false, nil, err
	}

	r, err :=
		s.handler.StartTrace(ctx, req.Request)

	if err != nil {
		return false, nil, err
	} else {
		res.Success = r
	}

	return err == nil, &res, nil
}
