//------------------------------------------------------------------------------
// Automatically generated by the Fast Binary Encoding compiler, do not modify!
// https://github.com/chronoxor/FastBinaryEncoding
// Source: proto.fbe
// FBE version: 1.15.0.0
//------------------------------------------------------------------------------

package proto

import "errors"
import "../fbe"

// Workaround for Go unused imports issue
var _ = errors.New
var _ = fbe.Version

// Fast Binary Encoding proto final receiver
type FinalReceiver struct {
    *fbe.Receiver
    orderMessageValue *OrderMessage
    orderMessageModel *OrderMessageFinalModel
    balanceMessageValue *BalanceMessage
    balanceMessageModel *BalanceMessageFinalModel
    accountMessageValue *AccountMessage
    accountMessageModel *AccountMessageFinalModel

    // Receive OrderMessage handler
    HandlerOnReceiveOrderMessage OnReceiveOrderMessage
    // Receive BalanceMessage handler
    HandlerOnReceiveBalanceMessage OnReceiveBalanceMessage
    // Receive AccountMessage handler
    HandlerOnReceiveAccountMessage OnReceiveAccountMessage
}

// Create a new proto final receiver with an empty buffer
func NewFinalReceiver() *FinalReceiver {
    return NewFinalReceiverWithBuffer(fbe.NewEmptyBuffer())
}

// Create a new proto final receiver with the given buffer
func NewFinalReceiverWithBuffer(buffer *fbe.Buffer) *FinalReceiver {
    receiver := &FinalReceiver{
        fbe.NewReceiver(buffer, true),
        NewOrderMessage(),
        NewOrderMessageFinalModel(buffer),
        NewBalanceMessage(),
        NewBalanceMessageFinalModel(buffer),
        NewAccountMessage(),
        NewAccountMessageFinalModel(buffer),
        nil,
        nil,
        nil,
    }
    receiver.SetupHandlerOnReceive(receiver)
    receiver.SetupHandlerOnReceiveOrderMessageFunc(func(value *OrderMessage) {})
    receiver.SetupHandlerOnReceiveBalanceMessageFunc(func(value *BalanceMessage) {})
    receiver.SetupHandlerOnReceiveAccountMessageFunc(func(value *AccountMessage) {})
    return receiver
}

// Setup handlers
func (r *FinalReceiver) SetupHandlers(handlers interface{}) {
    r.Receiver.SetupHandlers(handlers)
    if handler, ok := handlers.(OnReceiveOrderMessage); ok {
        r.SetupHandlerOnReceiveOrderMessage(handler)
    }
    if handler, ok := handlers.(OnReceiveBalanceMessage); ok {
        r.SetupHandlerOnReceiveBalanceMessage(handler)
    }
    if handler, ok := handlers.(OnReceiveAccountMessage); ok {
        r.SetupHandlerOnReceiveAccountMessage(handler)
    }
}

// Setup receive OrderMessage handler
func (r *FinalReceiver) SetupHandlerOnReceiveOrderMessage(handler OnReceiveOrderMessage) { r.HandlerOnReceiveOrderMessage = handler }
// Setup receive OrderMessage handler function
func (r *FinalReceiver) SetupHandlerOnReceiveOrderMessageFunc(function func(value *OrderMessage)) { r.HandlerOnReceiveOrderMessage = OnReceiveOrderMessageFunc(function) }
// Setup receive BalanceMessage handler
func (r *FinalReceiver) SetupHandlerOnReceiveBalanceMessage(handler OnReceiveBalanceMessage) { r.HandlerOnReceiveBalanceMessage = handler }
// Setup receive BalanceMessage handler function
func (r *FinalReceiver) SetupHandlerOnReceiveBalanceMessageFunc(function func(value *BalanceMessage)) { r.HandlerOnReceiveBalanceMessage = OnReceiveBalanceMessageFunc(function) }
// Setup receive AccountMessage handler
func (r *FinalReceiver) SetupHandlerOnReceiveAccountMessage(handler OnReceiveAccountMessage) { r.HandlerOnReceiveAccountMessage = handler }
// Setup receive AccountMessage handler function
func (r *FinalReceiver) SetupHandlerOnReceiveAccountMessageFunc(function func(value *AccountMessage)) { r.HandlerOnReceiveAccountMessage = OnReceiveAccountMessageFunc(function) }

// Receive message handler
func (r *FinalReceiver) OnReceive(fbeType int, buffer []byte) (bool, error) {
    switch fbeType {
    case r.orderMessageModel.FBEType():
        // Deserialize the value from the FBE stream
        r.orderMessageModel.Buffer().Attach(buffer)
        if !r.orderMessageModel.Verify() {
            return false, errors.New("proto.OrderMessage validation failed")
        }
        deserialized, err := r.orderMessageModel.DeserializeValue(r.orderMessageValue)
        if deserialized <= 0 {
            return false, errors.New("proto.OrderMessage deserialization failed")
        }
        if err != nil {
            return false, err
        }

        // Log the value
        if r.Logging() {
            message := r.orderMessageValue.String()
            r.HandlerOnReceiveLog.OnReceiveLog(message)
        }

        // Call receive handler with deserialized value
        r.HandlerOnReceiveOrderMessage.OnReceiveOrderMessage(r.orderMessageValue)
        return true, nil
    case r.balanceMessageModel.FBEType():
        // Deserialize the value from the FBE stream
        r.balanceMessageModel.Buffer().Attach(buffer)
        if !r.balanceMessageModel.Verify() {
            return false, errors.New("proto.BalanceMessage validation failed")
        }
        deserialized, err := r.balanceMessageModel.DeserializeValue(r.balanceMessageValue)
        if deserialized <= 0 {
            return false, errors.New("proto.BalanceMessage deserialization failed")
        }
        if err != nil {
            return false, err
        }

        // Log the value
        if r.Logging() {
            message := r.balanceMessageValue.String()
            r.HandlerOnReceiveLog.OnReceiveLog(message)
        }

        // Call receive handler with deserialized value
        r.HandlerOnReceiveBalanceMessage.OnReceiveBalanceMessage(r.balanceMessageValue)
        return true, nil
    case r.accountMessageModel.FBEType():
        // Deserialize the value from the FBE stream
        r.accountMessageModel.Buffer().Attach(buffer)
        if !r.accountMessageModel.Verify() {
            return false, errors.New("proto.AccountMessage validation failed")
        }
        deserialized, err := r.accountMessageModel.DeserializeValue(r.accountMessageValue)
        if deserialized <= 0 {
            return false, errors.New("proto.AccountMessage deserialization failed")
        }
        if err != nil {
            return false, err
        }

        // Log the value
        if r.Logging() {
            message := r.accountMessageValue.String()
            r.HandlerOnReceiveLog.OnReceiveLog(message)
        }

        // Call receive handler with deserialized value
        r.HandlerOnReceiveAccountMessage.OnReceiveAccountMessage(r.accountMessageValue)
        return true, nil
    default:
        _ = fbeType
        break
    }

    return false, nil
}
