/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package model

import (
	"context"
	"fmt"

	"github.com/pkg/errors"
	"github.com/rs/zerolog"

	. "github.com/apache/plc4x/plc4go/spi/codegen/fields"
	. "github.com/apache/plc4x/plc4go/spi/codegen/io"
	"github.com/apache/plc4x/plc4go/spi/utils"
)

// Code generated by code-generation. DO NOT EDIT.

// ParameterChangeReply is the corresponding interface of ParameterChangeReply
type ParameterChangeReply interface {
	fmt.Stringer
	utils.LengthAware
	utils.Serializable
	utils.Copyable
	Reply
	// GetParameterChange returns ParameterChange (property field)
	GetParameterChange() ParameterChange
	// IsParameterChangeReply is a marker method to prevent unintentional type checks (interfaces of same signature)
	IsParameterChangeReply()
	// CreateBuilder creates a ParameterChangeReplyBuilder
	CreateParameterChangeReplyBuilder() ParameterChangeReplyBuilder
}

// _ParameterChangeReply is the data-structure of this message
type _ParameterChangeReply struct {
	ReplyContract
	ParameterChange ParameterChange
}

var _ ParameterChangeReply = (*_ParameterChangeReply)(nil)
var _ ReplyRequirements = (*_ParameterChangeReply)(nil)

// NewParameterChangeReply factory function for _ParameterChangeReply
func NewParameterChangeReply(peekedByte byte, parameterChange ParameterChange, cBusOptions CBusOptions, requestContext RequestContext) *_ParameterChangeReply {
	if parameterChange == nil {
		panic("parameterChange of type ParameterChange for ParameterChangeReply must not be nil")
	}
	_result := &_ParameterChangeReply{
		ReplyContract:   NewReply(peekedByte, cBusOptions, requestContext),
		ParameterChange: parameterChange,
	}
	_result.ReplyContract.(*_Reply)._SubType = _result
	return _result
}

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
/////////////////////// Builder
///////////////////////

// ParameterChangeReplyBuilder is a builder for ParameterChangeReply
type ParameterChangeReplyBuilder interface {
	utils.Copyable
	// WithMandatoryFields adds all mandatory fields (convenience for using multiple builder calls)
	WithMandatoryFields(parameterChange ParameterChange) ParameterChangeReplyBuilder
	// WithParameterChange adds ParameterChange (property field)
	WithParameterChange(ParameterChange) ParameterChangeReplyBuilder
	// WithParameterChangeBuilder adds ParameterChange (property field) which is build by the builder
	WithParameterChangeBuilder(func(ParameterChangeBuilder) ParameterChangeBuilder) ParameterChangeReplyBuilder
	// Done is used to finish work on this child and return (or create one if none) to the parent builder
	Done() ReplyBuilder
	// Build builds the ParameterChangeReply or returns an error if something is wrong
	Build() (ParameterChangeReply, error)
	// MustBuild does the same as Build but panics on error
	MustBuild() ParameterChangeReply
}

// NewParameterChangeReplyBuilder() creates a ParameterChangeReplyBuilder
func NewParameterChangeReplyBuilder() ParameterChangeReplyBuilder {
	return &_ParameterChangeReplyBuilder{_ParameterChangeReply: new(_ParameterChangeReply)}
}

type _ParameterChangeReplyBuilder struct {
	*_ParameterChangeReply

	parentBuilder *_ReplyBuilder

	err *utils.MultiError
}

var _ (ParameterChangeReplyBuilder) = (*_ParameterChangeReplyBuilder)(nil)

func (b *_ParameterChangeReplyBuilder) setParent(contract ReplyContract) {
	b.ReplyContract = contract
	contract.(*_Reply)._SubType = b._ParameterChangeReply
}

func (b *_ParameterChangeReplyBuilder) WithMandatoryFields(parameterChange ParameterChange) ParameterChangeReplyBuilder {
	return b.WithParameterChange(parameterChange)
}

func (b *_ParameterChangeReplyBuilder) WithParameterChange(parameterChange ParameterChange) ParameterChangeReplyBuilder {
	b.ParameterChange = parameterChange
	return b
}

func (b *_ParameterChangeReplyBuilder) WithParameterChangeBuilder(builderSupplier func(ParameterChangeBuilder) ParameterChangeBuilder) ParameterChangeReplyBuilder {
	builder := builderSupplier(b.ParameterChange.CreateParameterChangeBuilder())
	var err error
	b.ParameterChange, err = builder.Build()
	if err != nil {
		if b.err == nil {
			b.err = &utils.MultiError{MainError: errors.New("sub builder failed")}
		}
		b.err.Append(errors.Wrap(err, "ParameterChangeBuilder failed"))
	}
	return b
}

func (b *_ParameterChangeReplyBuilder) Build() (ParameterChangeReply, error) {
	if b.ParameterChange == nil {
		if b.err == nil {
			b.err = new(utils.MultiError)
		}
		b.err.Append(errors.New("mandatory field 'parameterChange' not set"))
	}
	if b.err != nil {
		return nil, errors.Wrap(b.err, "error occurred during build")
	}
	return b._ParameterChangeReply.deepCopy(), nil
}

func (b *_ParameterChangeReplyBuilder) MustBuild() ParameterChangeReply {
	build, err := b.Build()
	if err != nil {
		panic(err)
	}
	return build
}

func (b *_ParameterChangeReplyBuilder) Done() ReplyBuilder {
	if b.parentBuilder == nil {
		b.parentBuilder = NewReplyBuilder().(*_ReplyBuilder)
	}
	return b.parentBuilder
}

func (b *_ParameterChangeReplyBuilder) buildForReply() (Reply, error) {
	return b.Build()
}

func (b *_ParameterChangeReplyBuilder) DeepCopy() any {
	_copy := b.CreateParameterChangeReplyBuilder().(*_ParameterChangeReplyBuilder)
	if b.err != nil {
		_copy.err = b.err.DeepCopy().(*utils.MultiError)
	}
	return _copy
}

// CreateParameterChangeReplyBuilder creates a ParameterChangeReplyBuilder
func (b *_ParameterChangeReply) CreateParameterChangeReplyBuilder() ParameterChangeReplyBuilder {
	if b == nil {
		return NewParameterChangeReplyBuilder()
	}
	return &_ParameterChangeReplyBuilder{_ParameterChangeReply: b.deepCopy()}
}

///////////////////////
///////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
/////////////////////// Accessors for discriminator values.
///////////////////////

///////////////////////
///////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

func (m *_ParameterChangeReply) GetParent() ReplyContract {
	return m.ReplyContract
}

///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
/////////////////////// Accessors for property fields.
///////////////////////

func (m *_ParameterChangeReply) GetParameterChange() ParameterChange {
	return m.ParameterChange
}

///////////////////////
///////////////////////
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////

// Deprecated: use the interface for direct cast
func CastParameterChangeReply(structType any) ParameterChangeReply {
	if casted, ok := structType.(ParameterChangeReply); ok {
		return casted
	}
	if casted, ok := structType.(*ParameterChangeReply); ok {
		return *casted
	}
	return nil
}

func (m *_ParameterChangeReply) GetTypeName() string {
	return "ParameterChangeReply"
}

func (m *_ParameterChangeReply) GetLengthInBits(ctx context.Context) uint16 {
	lengthInBits := uint16(m.ReplyContract.(*_Reply).getLengthInBits(ctx))

	// Simple field (parameterChange)
	lengthInBits += m.ParameterChange.GetLengthInBits(ctx)

	return lengthInBits
}

func (m *_ParameterChangeReply) GetLengthInBytes(ctx context.Context) uint16 {
	return m.GetLengthInBits(ctx) / 8
}

func (m *_ParameterChangeReply) parse(ctx context.Context, readBuffer utils.ReadBuffer, parent *_Reply, cBusOptions CBusOptions, requestContext RequestContext) (__parameterChangeReply ParameterChangeReply, err error) {
	m.ReplyContract = parent
	parent._SubType = m
	positionAware := readBuffer
	_ = positionAware
	if pullErr := readBuffer.PullContext("ParameterChangeReply"); pullErr != nil {
		return nil, errors.Wrap(pullErr, "Error pulling for ParameterChangeReply")
	}
	currentPos := positionAware.GetPos()
	_ = currentPos

	parameterChange, err := ReadSimpleField[ParameterChange](ctx, "parameterChange", ReadComplex[ParameterChange](ParameterChangeParseWithBuffer, readBuffer))
	if err != nil {
		return nil, errors.Wrap(err, fmt.Sprintf("Error parsing 'parameterChange' field"))
	}
	m.ParameterChange = parameterChange

	if closeErr := readBuffer.CloseContext("ParameterChangeReply"); closeErr != nil {
		return nil, errors.Wrap(closeErr, "Error closing for ParameterChangeReply")
	}

	return m, nil
}

func (m *_ParameterChangeReply) Serialize() ([]byte, error) {
	wb := utils.NewWriteBufferByteBased(utils.WithInitialSizeForByteBasedBuffer(int(m.GetLengthInBytes(context.Background()))))
	if err := m.SerializeWithWriteBuffer(context.Background(), wb); err != nil {
		return nil, err
	}
	return wb.GetBytes(), nil
}

func (m *_ParameterChangeReply) SerializeWithWriteBuffer(ctx context.Context, writeBuffer utils.WriteBuffer) error {
	positionAware := writeBuffer
	_ = positionAware
	log := zerolog.Ctx(ctx)
	_ = log
	ser := func() error {
		if pushErr := writeBuffer.PushContext("ParameterChangeReply"); pushErr != nil {
			return errors.Wrap(pushErr, "Error pushing for ParameterChangeReply")
		}

		if err := WriteSimpleField[ParameterChange](ctx, "parameterChange", m.GetParameterChange(), WriteComplex[ParameterChange](writeBuffer)); err != nil {
			return errors.Wrap(err, "Error serializing 'parameterChange' field")
		}

		if popErr := writeBuffer.PopContext("ParameterChangeReply"); popErr != nil {
			return errors.Wrap(popErr, "Error popping for ParameterChangeReply")
		}
		return nil
	}
	return m.ReplyContract.(*_Reply).serializeParent(ctx, writeBuffer, m, ser)
}

func (m *_ParameterChangeReply) IsParameterChangeReply() {}

func (m *_ParameterChangeReply) DeepCopy() any {
	return m.deepCopy()
}

func (m *_ParameterChangeReply) deepCopy() *_ParameterChangeReply {
	if m == nil {
		return nil
	}
	_ParameterChangeReplyCopy := &_ParameterChangeReply{
		m.ReplyContract.(*_Reply).deepCopy(),
		utils.DeepCopy[ParameterChange](m.ParameterChange),
	}
	_ParameterChangeReplyCopy.ReplyContract.(*_Reply)._SubType = m
	return _ParameterChangeReplyCopy
}

func (m *_ParameterChangeReply) String() string {
	if m == nil {
		return "<nil>"
	}
	wb := utils.NewWriteBufferBoxBased(
		utils.WithWriteBufferBoxBasedMergeSingleBoxes(),
		utils.WithWriteBufferBoxBasedOmitEmptyBoxes(),
		utils.WithWriteBufferBoxBasedPrintPosLengthFooter(),
	)
	if err := wb.WriteSerializable(context.Background(), m); err != nil {
		return err.Error()
	}
	return wb.GetBox().String()
}
