/*
 * 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.

// BACnetEventParameterChangeOfTimerAlarmValue is the corresponding interface of BACnetEventParameterChangeOfTimerAlarmValue
type BACnetEventParameterChangeOfTimerAlarmValue interface {
	fmt.Stringer
	utils.LengthAware
	utils.Serializable
	utils.Copyable
	// GetOpeningTag returns OpeningTag (property field)
	GetOpeningTag() BACnetOpeningTag
	// GetAlarmValues returns AlarmValues (property field)
	GetAlarmValues() []BACnetTimerStateTagged
	// GetClosingTag returns ClosingTag (property field)
	GetClosingTag() BACnetClosingTag
	// IsBACnetEventParameterChangeOfTimerAlarmValue is a marker method to prevent unintentional type checks (interfaces of same signature)
	IsBACnetEventParameterChangeOfTimerAlarmValue()
	// CreateBuilder creates a BACnetEventParameterChangeOfTimerAlarmValueBuilder
	CreateBACnetEventParameterChangeOfTimerAlarmValueBuilder() BACnetEventParameterChangeOfTimerAlarmValueBuilder
}

// _BACnetEventParameterChangeOfTimerAlarmValue is the data-structure of this message
type _BACnetEventParameterChangeOfTimerAlarmValue struct {
	OpeningTag  BACnetOpeningTag
	AlarmValues []BACnetTimerStateTagged
	ClosingTag  BACnetClosingTag

	// Arguments.
	TagNumber uint8
}

var _ BACnetEventParameterChangeOfTimerAlarmValue = (*_BACnetEventParameterChangeOfTimerAlarmValue)(nil)

// NewBACnetEventParameterChangeOfTimerAlarmValue factory function for _BACnetEventParameterChangeOfTimerAlarmValue
func NewBACnetEventParameterChangeOfTimerAlarmValue(openingTag BACnetOpeningTag, alarmValues []BACnetTimerStateTagged, closingTag BACnetClosingTag, tagNumber uint8) *_BACnetEventParameterChangeOfTimerAlarmValue {
	if openingTag == nil {
		panic("openingTag of type BACnetOpeningTag for BACnetEventParameterChangeOfTimerAlarmValue must not be nil")
	}
	if closingTag == nil {
		panic("closingTag of type BACnetClosingTag for BACnetEventParameterChangeOfTimerAlarmValue must not be nil")
	}
	return &_BACnetEventParameterChangeOfTimerAlarmValue{OpeningTag: openingTag, AlarmValues: alarmValues, ClosingTag: closingTag, TagNumber: tagNumber}
}

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

// BACnetEventParameterChangeOfTimerAlarmValueBuilder is a builder for BACnetEventParameterChangeOfTimerAlarmValue
type BACnetEventParameterChangeOfTimerAlarmValueBuilder interface {
	utils.Copyable
	// WithMandatoryFields adds all mandatory fields (convenience for using multiple builder calls)
	WithMandatoryFields(openingTag BACnetOpeningTag, alarmValues []BACnetTimerStateTagged, closingTag BACnetClosingTag) BACnetEventParameterChangeOfTimerAlarmValueBuilder
	// WithOpeningTag adds OpeningTag (property field)
	WithOpeningTag(BACnetOpeningTag) BACnetEventParameterChangeOfTimerAlarmValueBuilder
	// WithOpeningTagBuilder adds OpeningTag (property field) which is build by the builder
	WithOpeningTagBuilder(func(BACnetOpeningTagBuilder) BACnetOpeningTagBuilder) BACnetEventParameterChangeOfTimerAlarmValueBuilder
	// WithAlarmValues adds AlarmValues (property field)
	WithAlarmValues(...BACnetTimerStateTagged) BACnetEventParameterChangeOfTimerAlarmValueBuilder
	// WithClosingTag adds ClosingTag (property field)
	WithClosingTag(BACnetClosingTag) BACnetEventParameterChangeOfTimerAlarmValueBuilder
	// WithClosingTagBuilder adds ClosingTag (property field) which is build by the builder
	WithClosingTagBuilder(func(BACnetClosingTagBuilder) BACnetClosingTagBuilder) BACnetEventParameterChangeOfTimerAlarmValueBuilder
	// WithArgTagNumber sets a parser argument
	WithArgTagNumber(uint8) BACnetEventParameterChangeOfTimerAlarmValueBuilder
	// Build builds the BACnetEventParameterChangeOfTimerAlarmValue or returns an error if something is wrong
	Build() (BACnetEventParameterChangeOfTimerAlarmValue, error)
	// MustBuild does the same as Build but panics on error
	MustBuild() BACnetEventParameterChangeOfTimerAlarmValue
}

// NewBACnetEventParameterChangeOfTimerAlarmValueBuilder() creates a BACnetEventParameterChangeOfTimerAlarmValueBuilder
func NewBACnetEventParameterChangeOfTimerAlarmValueBuilder() BACnetEventParameterChangeOfTimerAlarmValueBuilder {
	return &_BACnetEventParameterChangeOfTimerAlarmValueBuilder{_BACnetEventParameterChangeOfTimerAlarmValue: new(_BACnetEventParameterChangeOfTimerAlarmValue)}
}

type _BACnetEventParameterChangeOfTimerAlarmValueBuilder struct {
	*_BACnetEventParameterChangeOfTimerAlarmValue

	err *utils.MultiError
}

var _ (BACnetEventParameterChangeOfTimerAlarmValueBuilder) = (*_BACnetEventParameterChangeOfTimerAlarmValueBuilder)(nil)

func (b *_BACnetEventParameterChangeOfTimerAlarmValueBuilder) WithMandatoryFields(openingTag BACnetOpeningTag, alarmValues []BACnetTimerStateTagged, closingTag BACnetClosingTag) BACnetEventParameterChangeOfTimerAlarmValueBuilder {
	return b.WithOpeningTag(openingTag).WithAlarmValues(alarmValues...).WithClosingTag(closingTag)
}

func (b *_BACnetEventParameterChangeOfTimerAlarmValueBuilder) WithOpeningTag(openingTag BACnetOpeningTag) BACnetEventParameterChangeOfTimerAlarmValueBuilder {
	b.OpeningTag = openingTag
	return b
}

func (b *_BACnetEventParameterChangeOfTimerAlarmValueBuilder) WithOpeningTagBuilder(builderSupplier func(BACnetOpeningTagBuilder) BACnetOpeningTagBuilder) BACnetEventParameterChangeOfTimerAlarmValueBuilder {
	builder := builderSupplier(b.OpeningTag.CreateBACnetOpeningTagBuilder())
	var err error
	b.OpeningTag, 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, "BACnetOpeningTagBuilder failed"))
	}
	return b
}

func (b *_BACnetEventParameterChangeOfTimerAlarmValueBuilder) WithAlarmValues(alarmValues ...BACnetTimerStateTagged) BACnetEventParameterChangeOfTimerAlarmValueBuilder {
	b.AlarmValues = alarmValues
	return b
}

func (b *_BACnetEventParameterChangeOfTimerAlarmValueBuilder) WithClosingTag(closingTag BACnetClosingTag) BACnetEventParameterChangeOfTimerAlarmValueBuilder {
	b.ClosingTag = closingTag
	return b
}

func (b *_BACnetEventParameterChangeOfTimerAlarmValueBuilder) WithClosingTagBuilder(builderSupplier func(BACnetClosingTagBuilder) BACnetClosingTagBuilder) BACnetEventParameterChangeOfTimerAlarmValueBuilder {
	builder := builderSupplier(b.ClosingTag.CreateBACnetClosingTagBuilder())
	var err error
	b.ClosingTag, 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, "BACnetClosingTagBuilder failed"))
	}
	return b
}

func (b *_BACnetEventParameterChangeOfTimerAlarmValueBuilder) WithArgTagNumber(tagNumber uint8) BACnetEventParameterChangeOfTimerAlarmValueBuilder {
	b.TagNumber = tagNumber
	return b
}

func (b *_BACnetEventParameterChangeOfTimerAlarmValueBuilder) Build() (BACnetEventParameterChangeOfTimerAlarmValue, error) {
	if b.OpeningTag == nil {
		if b.err == nil {
			b.err = new(utils.MultiError)
		}
		b.err.Append(errors.New("mandatory field 'openingTag' not set"))
	}
	if b.ClosingTag == nil {
		if b.err == nil {
			b.err = new(utils.MultiError)
		}
		b.err.Append(errors.New("mandatory field 'closingTag' not set"))
	}
	if b.err != nil {
		return nil, errors.Wrap(b.err, "error occurred during build")
	}
	return b._BACnetEventParameterChangeOfTimerAlarmValue.deepCopy(), nil
}

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

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

// CreateBACnetEventParameterChangeOfTimerAlarmValueBuilder creates a BACnetEventParameterChangeOfTimerAlarmValueBuilder
func (b *_BACnetEventParameterChangeOfTimerAlarmValue) CreateBACnetEventParameterChangeOfTimerAlarmValueBuilder() BACnetEventParameterChangeOfTimerAlarmValueBuilder {
	if b == nil {
		return NewBACnetEventParameterChangeOfTimerAlarmValueBuilder()
	}
	return &_BACnetEventParameterChangeOfTimerAlarmValueBuilder{_BACnetEventParameterChangeOfTimerAlarmValue: b.deepCopy()}
}

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

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

func (m *_BACnetEventParameterChangeOfTimerAlarmValue) GetOpeningTag() BACnetOpeningTag {
	return m.OpeningTag
}

func (m *_BACnetEventParameterChangeOfTimerAlarmValue) GetAlarmValues() []BACnetTimerStateTagged {
	return m.AlarmValues
}

func (m *_BACnetEventParameterChangeOfTimerAlarmValue) GetClosingTag() BACnetClosingTag {
	return m.ClosingTag
}

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

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

func (m *_BACnetEventParameterChangeOfTimerAlarmValue) GetTypeName() string {
	return "BACnetEventParameterChangeOfTimerAlarmValue"
}

func (m *_BACnetEventParameterChangeOfTimerAlarmValue) GetLengthInBits(ctx context.Context) uint16 {
	lengthInBits := uint16(0)

	// Simple field (openingTag)
	lengthInBits += m.OpeningTag.GetLengthInBits(ctx)

	// Array field
	if len(m.AlarmValues) > 0 {
		for _, element := range m.AlarmValues {
			lengthInBits += element.GetLengthInBits(ctx)
		}
	}

	// Simple field (closingTag)
	lengthInBits += m.ClosingTag.GetLengthInBits(ctx)

	return lengthInBits
}

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

func BACnetEventParameterChangeOfTimerAlarmValueParse(ctx context.Context, theBytes []byte, tagNumber uint8) (BACnetEventParameterChangeOfTimerAlarmValue, error) {
	return BACnetEventParameterChangeOfTimerAlarmValueParseWithBuffer(ctx, utils.NewReadBufferByteBased(theBytes), tagNumber)
}

func BACnetEventParameterChangeOfTimerAlarmValueParseWithBufferProducer(tagNumber uint8) func(ctx context.Context, readBuffer utils.ReadBuffer) (BACnetEventParameterChangeOfTimerAlarmValue, error) {
	return func(ctx context.Context, readBuffer utils.ReadBuffer) (BACnetEventParameterChangeOfTimerAlarmValue, error) {
		return BACnetEventParameterChangeOfTimerAlarmValueParseWithBuffer(ctx, readBuffer, tagNumber)
	}
}

func BACnetEventParameterChangeOfTimerAlarmValueParseWithBuffer(ctx context.Context, readBuffer utils.ReadBuffer, tagNumber uint8) (BACnetEventParameterChangeOfTimerAlarmValue, error) {
	v, err := (&_BACnetEventParameterChangeOfTimerAlarmValue{TagNumber: tagNumber}).parse(ctx, readBuffer, tagNumber)
	if err != nil {
		return nil, err
	}
	return v, nil
}

func (m *_BACnetEventParameterChangeOfTimerAlarmValue) parse(ctx context.Context, readBuffer utils.ReadBuffer, tagNumber uint8) (__bACnetEventParameterChangeOfTimerAlarmValue BACnetEventParameterChangeOfTimerAlarmValue, err error) {
	positionAware := readBuffer
	_ = positionAware
	if pullErr := readBuffer.PullContext("BACnetEventParameterChangeOfTimerAlarmValue"); pullErr != nil {
		return nil, errors.Wrap(pullErr, "Error pulling for BACnetEventParameterChangeOfTimerAlarmValue")
	}
	currentPos := positionAware.GetPos()
	_ = currentPos

	openingTag, err := ReadSimpleField[BACnetOpeningTag](ctx, "openingTag", ReadComplex[BACnetOpeningTag](BACnetOpeningTagParseWithBufferProducer((uint8)(tagNumber)), readBuffer))
	if err != nil {
		return nil, errors.Wrap(err, fmt.Sprintf("Error parsing 'openingTag' field"))
	}
	m.OpeningTag = openingTag

	alarmValues, err := ReadTerminatedArrayField[BACnetTimerStateTagged](ctx, "alarmValues", ReadComplex[BACnetTimerStateTagged](BACnetTimerStateTaggedParseWithBufferProducer((uint8)(uint8(0)), (TagClass)(TagClass_APPLICATION_TAGS)), readBuffer), IsBACnetConstructedDataClosingTag(ctx, readBuffer, false, tagNumber))
	if err != nil {
		return nil, errors.Wrap(err, fmt.Sprintf("Error parsing 'alarmValues' field"))
	}
	m.AlarmValues = alarmValues

	closingTag, err := ReadSimpleField[BACnetClosingTag](ctx, "closingTag", ReadComplex[BACnetClosingTag](BACnetClosingTagParseWithBufferProducer((uint8)(tagNumber)), readBuffer))
	if err != nil {
		return nil, errors.Wrap(err, fmt.Sprintf("Error parsing 'closingTag' field"))
	}
	m.ClosingTag = closingTag

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

	return m, nil
}

func (m *_BACnetEventParameterChangeOfTimerAlarmValue) 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 *_BACnetEventParameterChangeOfTimerAlarmValue) SerializeWithWriteBuffer(ctx context.Context, writeBuffer utils.WriteBuffer) error {
	positionAware := writeBuffer
	_ = positionAware
	log := zerolog.Ctx(ctx)
	_ = log
	if pushErr := writeBuffer.PushContext("BACnetEventParameterChangeOfTimerAlarmValue"); pushErr != nil {
		return errors.Wrap(pushErr, "Error pushing for BACnetEventParameterChangeOfTimerAlarmValue")
	}

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

	if err := WriteComplexTypeArrayField(ctx, "alarmValues", m.GetAlarmValues(), writeBuffer); err != nil {
		return errors.Wrap(err, "Error serializing 'alarmValues' field")
	}

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

	if popErr := writeBuffer.PopContext("BACnetEventParameterChangeOfTimerAlarmValue"); popErr != nil {
		return errors.Wrap(popErr, "Error popping for BACnetEventParameterChangeOfTimerAlarmValue")
	}
	return nil
}

////
// Arguments Getter

func (m *_BACnetEventParameterChangeOfTimerAlarmValue) GetTagNumber() uint8 {
	return m.TagNumber
}

//
////

func (m *_BACnetEventParameterChangeOfTimerAlarmValue) IsBACnetEventParameterChangeOfTimerAlarmValue() {
}

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

func (m *_BACnetEventParameterChangeOfTimerAlarmValue) deepCopy() *_BACnetEventParameterChangeOfTimerAlarmValue {
	if m == nil {
		return nil
	}
	_BACnetEventParameterChangeOfTimerAlarmValueCopy := &_BACnetEventParameterChangeOfTimerAlarmValue{
		utils.DeepCopy[BACnetOpeningTag](m.OpeningTag),
		utils.DeepCopySlice[BACnetTimerStateTagged, BACnetTimerStateTagged](m.AlarmValues),
		utils.DeepCopy[BACnetClosingTag](m.ClosingTag),
		m.TagNumber,
	}
	return _BACnetEventParameterChangeOfTimerAlarmValueCopy
}

func (m *_BACnetEventParameterChangeOfTimerAlarmValue) 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()
}
