// Autogenerated by Thrift Compiler (0.13.0)
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING

package cmd

import (
	"bytes"
	"context"
	"fmt"
	"github.com/apache/thrift/lib/go/thrift"
	"reflect"
)

// (needed to ensure safety because of naive import list construction.)
var _ = thrift.ZERO
var _ = fmt.Printf
var _ = context.Background
var _ = reflect.DeepEqual
var _ = bytes.Equal

// Attributes:
//  - Cmd
//  - Arguments
type Command struct {
	Cmd       string   `thrift:"cmd,1" db:"cmd" json:"cmd"`
	Arguments []string `thrift:"arguments,2" db:"arguments" json:"arguments"`
}

func NewCommand() *Command {
	return &Command{}
}

func (p *Command) GetCmd() string {
	return p.Cmd
}

func (p *Command) GetArguments() []string {
	return p.Arguments
}
func (p *Command) Read(iprot thrift.TProtocol) error {
	if _, err := iprot.ReadStructBegin(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err)
	}

	for {
		_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin()
		if err != nil {
			return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err)
		}
		if fieldTypeId == thrift.STOP {
			break
		}
		switch fieldId {
		case 1:
			if fieldTypeId == thrift.STRING {
				if err := p.ReadField1(iprot); err != nil {
					return err
				}
			} else {
				if err := iprot.Skip(fieldTypeId); err != nil {
					return err
				}
			}
		case 2:
			if fieldTypeId == thrift.LIST {
				if err := p.ReadField2(iprot); err != nil {
					return err
				}
			} else {
				if err := iprot.Skip(fieldTypeId); err != nil {
					return err
				}
			}
		default:
			if err := iprot.Skip(fieldTypeId); err != nil {
				return err
			}
		}
		if err := iprot.ReadFieldEnd(); err != nil {
			return err
		}
	}
	if err := iprot.ReadStructEnd(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
	}
	return nil
}

func (p *Command) ReadField1(iprot thrift.TProtocol) error {
	if v, err := iprot.ReadString(); err != nil {
		return thrift.PrependError("error reading field 1: ", err)
	} else {
		p.Cmd = v
	}
	return nil
}

func (p *Command) ReadField2(iprot thrift.TProtocol) error {
	_, size, err := iprot.ReadListBegin()
	if err != nil {
		return thrift.PrependError("error reading list begin: ", err)
	}
	tSlice := make([]string, 0, size)
	p.Arguments = tSlice
	for i := 0; i < size; i++ {
		var _elem0 string
		if v, err := iprot.ReadString(); err != nil {
			return thrift.PrependError("error reading field 0: ", err)
		} else {
			_elem0 = v
		}
		p.Arguments = append(p.Arguments, _elem0)
	}
	if err := iprot.ReadListEnd(); err != nil {
		return thrift.PrependError("error reading list end: ", err)
	}
	return nil
}

func (p *Command) Write(oprot thrift.TProtocol) error {
	if err := oprot.WriteStructBegin("command"); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
	}
	if p != nil {
		if err := p.writeField1(oprot); err != nil {
			return err
		}
		if err := p.writeField2(oprot); err != nil {
			return err
		}
	}
	if err := oprot.WriteFieldStop(); err != nil {
		return thrift.PrependError("write field stop error: ", err)
	}
	if err := oprot.WriteStructEnd(); err != nil {
		return thrift.PrependError("write struct stop error: ", err)
	}
	return nil
}

func (p *Command) writeField1(oprot thrift.TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("cmd", thrift.STRING, 1); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field begin error 1:cmd: ", p), err)
	}
	if err := oprot.WriteString(string(p.Cmd)); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T.cmd (1) field write error: ", p), err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field end error 1:cmd: ", p), err)
	}
	return err
}

func (p *Command) writeField2(oprot thrift.TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("arguments", thrift.LIST, 2); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field begin error 2:arguments: ", p), err)
	}
	if err := oprot.WriteListBegin(thrift.STRING, len(p.Arguments)); err != nil {
		return thrift.PrependError("error writing list begin: ", err)
	}
	for _, v := range p.Arguments {
		if err := oprot.WriteString(string(v)); err != nil {
			return thrift.PrependError(fmt.Sprintf("%T. (0) field write error: ", p), err)
		}
	}
	if err := oprot.WriteListEnd(); err != nil {
		return thrift.PrependError("error writing list end: ", err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field end error 2:arguments: ", p), err)
	}
	return err
}

func (p *Command) String() string {
	if p == nil {
		return "<nil>"
	}
	return fmt.Sprintf("Command(%+v)", *p)
}

type RemoteCmdService interface {
	// Parameters:
	//  - Cmd
	CallCommand(ctx context.Context, cmd *Command) (r string, err error)
}

type RemoteCmdServiceClient struct {
	c thrift.TClient
}

func NewRemoteCmdServiceClientFactory(t thrift.TTransport, f thrift.TProtocolFactory) *RemoteCmdServiceClient {
	return &RemoteCmdServiceClient{
		c: thrift.NewTStandardClient(f.GetProtocol(t), f.GetProtocol(t)),
	}
}

func NewRemoteCmdServiceClientProtocol(t thrift.TTransport, iprot thrift.TProtocol, oprot thrift.TProtocol) *RemoteCmdServiceClient {
	return &RemoteCmdServiceClient{
		c: thrift.NewTStandardClient(iprot, oprot),
	}
}

func NewRemoteCmdServiceClient(c thrift.TClient) *RemoteCmdServiceClient {
	return &RemoteCmdServiceClient{
		c: c,
	}
}

func (p *RemoteCmdServiceClient) Client_() thrift.TClient {
	return p.c
}

// Parameters:
//  - Cmd
func (p *RemoteCmdServiceClient) CallCommand(ctx context.Context, cmd *Command) (r string, err error) {
	var _args1 RemoteCmdServiceCallCommandArgs
	_args1.Cmd = cmd
	var _result2 RemoteCmdServiceCallCommandResult
	if err = p.Client_().Call(ctx, "callCommand", &_args1, &_result2); err != nil {
		return
	}
	return _result2.GetSuccess(), nil
}

type RemoteCmdServiceProcessor struct {
	processorMap map[string]thrift.TProcessorFunction
	handler      RemoteCmdService
}

func (p *RemoteCmdServiceProcessor) AddToProcessorMap(key string, processor thrift.TProcessorFunction) {
	p.processorMap[key] = processor
}

func (p *RemoteCmdServiceProcessor) GetProcessorFunction(key string) (processor thrift.TProcessorFunction, ok bool) {
	processor, ok = p.processorMap[key]
	return processor, ok
}

func (p *RemoteCmdServiceProcessor) ProcessorMap() map[string]thrift.TProcessorFunction {
	return p.processorMap
}

func NewRemoteCmdServiceProcessor(handler RemoteCmdService) *RemoteCmdServiceProcessor {

	self3 := &RemoteCmdServiceProcessor{handler: handler, processorMap: make(map[string]thrift.TProcessorFunction)}
	self3.processorMap["callCommand"] = &remoteCmdServiceProcessorCallCommand{handler: handler}
	return self3
}

func (p *RemoteCmdServiceProcessor) Process(ctx context.Context, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) {
	name, _, seqId, err := iprot.ReadMessageBegin()
	if err != nil {
		return false, err
	}
	if processor, ok := p.GetProcessorFunction(name); ok {
		return processor.Process(ctx, seqId, iprot, oprot)
	}
	iprot.Skip(thrift.STRUCT)
	iprot.ReadMessageEnd()
	x4 := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, "Unknown function "+name)
	oprot.WriteMessageBegin(name, thrift.EXCEPTION, seqId)
	x4.Write(oprot)
	oprot.WriteMessageEnd()
	oprot.Flush(ctx)
	return false, x4

}

type remoteCmdServiceProcessorCallCommand struct {
	handler RemoteCmdService
}

func (p *remoteCmdServiceProcessorCallCommand) Process(ctx context.Context, seqId int32, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) {
	args := RemoteCmdServiceCallCommandArgs{}
	if err = args.Read(iprot); err != nil {
		iprot.ReadMessageEnd()
		x := thrift.NewTApplicationException(thrift.PROTOCOL_ERROR, err.Error())
		oprot.WriteMessageBegin("callCommand", thrift.EXCEPTION, seqId)
		x.Write(oprot)
		oprot.WriteMessageEnd()
		oprot.Flush(ctx)
		return false, err
	}

	iprot.ReadMessageEnd()
	result := RemoteCmdServiceCallCommandResult{}
	var retval string
	var err2 error
	if retval, err2 = p.handler.CallCommand(ctx, args.Cmd); err2 != nil {
		x := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, "Internal error processing callCommand: "+err2.Error())
		oprot.WriteMessageBegin("callCommand", thrift.EXCEPTION, seqId)
		x.Write(oprot)
		oprot.WriteMessageEnd()
		oprot.Flush(ctx)
		return true, err2
	} else {
		result.Success = &retval
	}
	if err2 = oprot.WriteMessageBegin("callCommand", thrift.REPLY, seqId); err2 != nil {
		err = err2
	}
	if err2 = result.Write(oprot); err == nil && err2 != nil {
		err = err2
	}
	if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil {
		err = err2
	}
	if err2 = oprot.Flush(ctx); err == nil && err2 != nil {
		err = err2
	}
	if err != nil {
		return
	}
	return true, err
}

// HELPER FUNCTIONS AND STRUCTURES

// Attributes:
//  - Cmd
type RemoteCmdServiceCallCommandArgs struct {
	Cmd *Command `thrift:"cmd,1" db:"cmd" json:"cmd"`
}

func NewRemoteCmdServiceCallCommandArgs() *RemoteCmdServiceCallCommandArgs {
	return &RemoteCmdServiceCallCommandArgs{}
}

var RemoteCmdServiceCallCommandArgs_Cmd_DEFAULT *Command

func (p *RemoteCmdServiceCallCommandArgs) GetCmd() *Command {
	if !p.IsSetCmd() {
		return RemoteCmdServiceCallCommandArgs_Cmd_DEFAULT
	}
	return p.Cmd
}
func (p *RemoteCmdServiceCallCommandArgs) IsSetCmd() bool {
	return p.Cmd != nil
}

func (p *RemoteCmdServiceCallCommandArgs) Read(iprot thrift.TProtocol) error {
	if _, err := iprot.ReadStructBegin(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err)
	}

	for {
		_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin()
		if err != nil {
			return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err)
		}
		if fieldTypeId == thrift.STOP {
			break
		}
		switch fieldId {
		case 1:
			if fieldTypeId == thrift.STRUCT {
				if err := p.ReadField1(iprot); err != nil {
					return err
				}
			} else {
				if err := iprot.Skip(fieldTypeId); err != nil {
					return err
				}
			}
		default:
			if err := iprot.Skip(fieldTypeId); err != nil {
				return err
			}
		}
		if err := iprot.ReadFieldEnd(); err != nil {
			return err
		}
	}
	if err := iprot.ReadStructEnd(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
	}
	return nil
}

func (p *RemoteCmdServiceCallCommandArgs) ReadField1(iprot thrift.TProtocol) error {
	p.Cmd = &Command{}
	if err := p.Cmd.Read(iprot); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", p.Cmd), err)
	}
	return nil
}

func (p *RemoteCmdServiceCallCommandArgs) Write(oprot thrift.TProtocol) error {
	if err := oprot.WriteStructBegin("callCommand_args"); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
	}
	if p != nil {
		if err := p.writeField1(oprot); err != nil {
			return err
		}
	}
	if err := oprot.WriteFieldStop(); err != nil {
		return thrift.PrependError("write field stop error: ", err)
	}
	if err := oprot.WriteStructEnd(); err != nil {
		return thrift.PrependError("write struct stop error: ", err)
	}
	return nil
}

func (p *RemoteCmdServiceCallCommandArgs) writeField1(oprot thrift.TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("cmd", thrift.STRUCT, 1); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field begin error 1:cmd: ", p), err)
	}
	if err := p.Cmd.Write(oprot); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T error writing struct: ", p.Cmd), err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field end error 1:cmd: ", p), err)
	}
	return err
}

func (p *RemoteCmdServiceCallCommandArgs) String() string {
	if p == nil {
		return "<nil>"
	}
	return fmt.Sprintf("RemoteCmdServiceCallCommandArgs(%+v)", *p)
}

// Attributes:
//  - Success
type RemoteCmdServiceCallCommandResult struct {
	Success *string `thrift:"success,0" db:"success" json:"success,omitempty"`
}

func NewRemoteCmdServiceCallCommandResult() *RemoteCmdServiceCallCommandResult {
	return &RemoteCmdServiceCallCommandResult{}
}

var RemoteCmdServiceCallCommandResult_Success_DEFAULT string

func (p *RemoteCmdServiceCallCommandResult) GetSuccess() string {
	if !p.IsSetSuccess() {
		return RemoteCmdServiceCallCommandResult_Success_DEFAULT
	}
	return *p.Success
}
func (p *RemoteCmdServiceCallCommandResult) IsSetSuccess() bool {
	return p.Success != nil
}

func (p *RemoteCmdServiceCallCommandResult) Read(iprot thrift.TProtocol) error {
	if _, err := iprot.ReadStructBegin(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err)
	}

	for {
		_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin()
		if err != nil {
			return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err)
		}
		if fieldTypeId == thrift.STOP {
			break
		}
		switch fieldId {
		case 0:
			if fieldTypeId == thrift.STRING {
				if err := p.ReadField0(iprot); err != nil {
					return err
				}
			} else {
				if err := iprot.Skip(fieldTypeId); err != nil {
					return err
				}
			}
		default:
			if err := iprot.Skip(fieldTypeId); err != nil {
				return err
			}
		}
		if err := iprot.ReadFieldEnd(); err != nil {
			return err
		}
	}
	if err := iprot.ReadStructEnd(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
	}
	return nil
}

func (p *RemoteCmdServiceCallCommandResult) ReadField0(iprot thrift.TProtocol) error {
	if v, err := iprot.ReadString(); err != nil {
		return thrift.PrependError("error reading field 0: ", err)
	} else {
		p.Success = &v
	}
	return nil
}

func (p *RemoteCmdServiceCallCommandResult) Write(oprot thrift.TProtocol) error {
	if err := oprot.WriteStructBegin("callCommand_result"); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
	}
	if p != nil {
		if err := p.writeField0(oprot); err != nil {
			return err
		}
	}
	if err := oprot.WriteFieldStop(); err != nil {
		return thrift.PrependError("write field stop error: ", err)
	}
	if err := oprot.WriteStructEnd(); err != nil {
		return thrift.PrependError("write struct stop error: ", err)
	}
	return nil
}

func (p *RemoteCmdServiceCallCommandResult) writeField0(oprot thrift.TProtocol) (err error) {
	if p.IsSetSuccess() {
		if err := oprot.WriteFieldBegin("success", thrift.STRING, 0); err != nil {
			return thrift.PrependError(fmt.Sprintf("%T write field begin error 0:success: ", p), err)
		}
		if err := oprot.WriteString(string(*p.Success)); err != nil {
			return thrift.PrependError(fmt.Sprintf("%T.success (0) field write error: ", p), err)
		}
		if err := oprot.WriteFieldEnd(); err != nil {
			return thrift.PrependError(fmt.Sprintf("%T write field end error 0:success: ", p), err)
		}
	}
	return err
}

func (p *RemoteCmdServiceCallCommandResult) String() string {
	if p == nil {
		return "<nil>"
	}
	return fmt.Sprintf("RemoteCmdServiceCallCommandResult(%+v)", *p)
}
