/*
 * 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
 *
 *   http://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 (
	"fmt"
	"github.com/apache/plc4x/plc4go/internal/plc4go/spi/utils"
	"github.com/pkg/errors"
	"github.com/rs/zerolog/log"
)

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

// Constant values.
const S7MessageObjectRequest_VARIABLESPEC uint8 = 0x12
const S7MessageObjectRequest_LENGTH uint8 = 0x08

// The data-structure of this message
type S7MessageObjectRequest struct {
	SyntaxId  SyntaxIdType
	QueryType QueryType
	AlarmType AlarmType
	Parent    *S7DataAlarmMessage
}

// The corresponding interface
type IS7MessageObjectRequest interface {
	LengthInBytes() uint16
	LengthInBits() uint16
	Serialize(writeBuffer utils.WriteBuffer) error
}

///////////////////////////////////////////////////////////
// Accessors for discriminator values.
///////////////////////////////////////////////////////////
func (m *S7MessageObjectRequest) CpuFunctionType() uint8 {
	return 0x04
}

func (m *S7MessageObjectRequest) InitializeParent(parent *S7DataAlarmMessage) {
}

func NewS7MessageObjectRequest(syntaxId SyntaxIdType, queryType QueryType, alarmType AlarmType) *S7DataAlarmMessage {
	child := &S7MessageObjectRequest{
		SyntaxId:  syntaxId,
		QueryType: queryType,
		AlarmType: alarmType,
		Parent:    NewS7DataAlarmMessage(),
	}
	child.Parent.Child = child
	return child.Parent
}

func CastS7MessageObjectRequest(structType interface{}) *S7MessageObjectRequest {
	castFunc := func(typ interface{}) *S7MessageObjectRequest {
		if casted, ok := typ.(S7MessageObjectRequest); ok {
			return &casted
		}
		if casted, ok := typ.(*S7MessageObjectRequest); ok {
			return casted
		}
		if casted, ok := typ.(S7DataAlarmMessage); ok {
			return CastS7MessageObjectRequest(casted.Child)
		}
		if casted, ok := typ.(*S7DataAlarmMessage); ok {
			return CastS7MessageObjectRequest(casted.Child)
		}
		return nil
	}
	return castFunc(structType)
}

func (m *S7MessageObjectRequest) GetTypeName() string {
	return "S7MessageObjectRequest"
}

func (m *S7MessageObjectRequest) LengthInBits() uint16 {
	return m.LengthInBitsConditional(false)
}

func (m *S7MessageObjectRequest) LengthInBitsConditional(lastItem bool) uint16 {
	lengthInBits := uint16(m.Parent.ParentLengthInBits())

	// Const Field (variableSpec)
	lengthInBits += 8

	// Const Field (length)
	lengthInBits += 8

	// Simple field (syntaxId)
	lengthInBits += 8

	// Reserved Field (reserved)
	lengthInBits += 8

	// Simple field (queryType)
	lengthInBits += 8

	// Reserved Field (reserved)
	lengthInBits += 8

	// Simple field (alarmType)
	lengthInBits += 8

	return lengthInBits
}

func (m *S7MessageObjectRequest) LengthInBytes() uint16 {
	return m.LengthInBits() / 8
}

func S7MessageObjectRequestParse(readBuffer utils.ReadBuffer) (*S7DataAlarmMessage, error) {
	if pullErr := readBuffer.PullContext("S7MessageObjectRequest"); pullErr != nil {
		return nil, pullErr
	}

	// Const Field (variableSpec)
	variableSpec, _variableSpecErr := readBuffer.ReadUint8("variableSpec", 8)
	if _variableSpecErr != nil {
		return nil, errors.Wrap(_variableSpecErr, "Error parsing 'variableSpec' field")
	}
	if variableSpec != S7MessageObjectRequest_VARIABLESPEC {
		return nil, errors.New("Expected constant value " + fmt.Sprintf("%d", S7MessageObjectRequest_VARIABLESPEC) + " but got " + fmt.Sprintf("%d", variableSpec))
	}

	// Const Field (length)
	length, _lengthErr := readBuffer.ReadUint8("length", 8)
	if _lengthErr != nil {
		return nil, errors.Wrap(_lengthErr, "Error parsing 'length' field")
	}
	if length != S7MessageObjectRequest_LENGTH {
		return nil, errors.New("Expected constant value " + fmt.Sprintf("%d", S7MessageObjectRequest_LENGTH) + " but got " + fmt.Sprintf("%d", length))
	}

	// Simple Field (syntaxId)
	if pullErr := readBuffer.PullContext("syntaxId"); pullErr != nil {
		return nil, pullErr
	}
	syntaxId, _syntaxIdErr := SyntaxIdTypeParse(readBuffer)
	if _syntaxIdErr != nil {
		return nil, errors.Wrap(_syntaxIdErr, "Error parsing 'syntaxId' field")
	}
	if closeErr := readBuffer.CloseContext("syntaxId"); closeErr != nil {
		return nil, closeErr
	}

	// Reserved Field (Compartmentalized so the "reserved" variable can't leak)
	{
		reserved, _err := readBuffer.ReadUint8("reserved", 8)
		if _err != nil {
			return nil, errors.Wrap(_err, "Error parsing 'reserved' field")
		}
		if reserved != uint8(0x00) {
			log.Info().Fields(map[string]interface{}{
				"expected value": uint8(0x00),
				"got value":      reserved,
			}).Msg("Got unexpected response.")
		}
	}

	// Simple Field (queryType)
	if pullErr := readBuffer.PullContext("queryType"); pullErr != nil {
		return nil, pullErr
	}
	queryType, _queryTypeErr := QueryTypeParse(readBuffer)
	if _queryTypeErr != nil {
		return nil, errors.Wrap(_queryTypeErr, "Error parsing 'queryType' field")
	}
	if closeErr := readBuffer.CloseContext("queryType"); closeErr != nil {
		return nil, closeErr
	}

	// Reserved Field (Compartmentalized so the "reserved" variable can't leak)
	{
		reserved, _err := readBuffer.ReadUint8("reserved", 8)
		if _err != nil {
			return nil, errors.Wrap(_err, "Error parsing 'reserved' field")
		}
		if reserved != uint8(0x34) {
			log.Info().Fields(map[string]interface{}{
				"expected value": uint8(0x34),
				"got value":      reserved,
			}).Msg("Got unexpected response.")
		}
	}

	// Simple Field (alarmType)
	if pullErr := readBuffer.PullContext("alarmType"); pullErr != nil {
		return nil, pullErr
	}
	alarmType, _alarmTypeErr := AlarmTypeParse(readBuffer)
	if _alarmTypeErr != nil {
		return nil, errors.Wrap(_alarmTypeErr, "Error parsing 'alarmType' field")
	}
	if closeErr := readBuffer.CloseContext("alarmType"); closeErr != nil {
		return nil, closeErr
	}

	if closeErr := readBuffer.CloseContext("S7MessageObjectRequest"); closeErr != nil {
		return nil, closeErr
	}

	// Create a partially initialized instance
	_child := &S7MessageObjectRequest{
		SyntaxId:  syntaxId,
		QueryType: queryType,
		AlarmType: alarmType,
		Parent:    &S7DataAlarmMessage{},
	}
	_child.Parent.Child = _child
	return _child.Parent, nil
}

func (m *S7MessageObjectRequest) Serialize(writeBuffer utils.WriteBuffer) error {
	ser := func() error {
		if pushErr := writeBuffer.PushContext("S7MessageObjectRequest"); pushErr != nil {
			return pushErr
		}

		// Const Field (variableSpec)
		_variableSpecErr := writeBuffer.WriteUint8("variableSpec", 8, 0x12)
		if _variableSpecErr != nil {
			return errors.Wrap(_variableSpecErr, "Error serializing 'variableSpec' field")
		}

		// Const Field (length)
		_lengthErr := writeBuffer.WriteUint8("length", 8, 0x08)
		if _lengthErr != nil {
			return errors.Wrap(_lengthErr, "Error serializing 'length' field")
		}

		// Simple Field (syntaxId)
		if pushErr := writeBuffer.PushContext("syntaxId"); pushErr != nil {
			return pushErr
		}
		_syntaxIdErr := m.SyntaxId.Serialize(writeBuffer)
		if popErr := writeBuffer.PopContext("syntaxId"); popErr != nil {
			return popErr
		}
		if _syntaxIdErr != nil {
			return errors.Wrap(_syntaxIdErr, "Error serializing 'syntaxId' field")
		}

		// Reserved Field (reserved)
		{
			_err := writeBuffer.WriteUint8("reserved", 8, uint8(0x00))
			if _err != nil {
				return errors.Wrap(_err, "Error serializing 'reserved' field")
			}
		}

		// Simple Field (queryType)
		if pushErr := writeBuffer.PushContext("queryType"); pushErr != nil {
			return pushErr
		}
		_queryTypeErr := m.QueryType.Serialize(writeBuffer)
		if popErr := writeBuffer.PopContext("queryType"); popErr != nil {
			return popErr
		}
		if _queryTypeErr != nil {
			return errors.Wrap(_queryTypeErr, "Error serializing 'queryType' field")
		}

		// Reserved Field (reserved)
		{
			_err := writeBuffer.WriteUint8("reserved", 8, uint8(0x34))
			if _err != nil {
				return errors.Wrap(_err, "Error serializing 'reserved' field")
			}
		}

		// Simple Field (alarmType)
		if pushErr := writeBuffer.PushContext("alarmType"); pushErr != nil {
			return pushErr
		}
		_alarmTypeErr := m.AlarmType.Serialize(writeBuffer)
		if popErr := writeBuffer.PopContext("alarmType"); popErr != nil {
			return popErr
		}
		if _alarmTypeErr != nil {
			return errors.Wrap(_alarmTypeErr, "Error serializing 'alarmType' field")
		}

		if popErr := writeBuffer.PopContext("S7MessageObjectRequest"); popErr != nil {
			return popErr
		}
		return nil
	}
	return m.Parent.SerializeParent(writeBuffer, m, ser)
}

func (m *S7MessageObjectRequest) String() string {
	if m == nil {
		return "<nil>"
	}
	buffer := utils.NewBoxedWriteBufferWithOptions(true, true)
	m.Serialize(buffer)
	return buffer.GetBox().String()
}
