/*
 *
 *    Copyright (c) 2022 Project CHIP Authors
 *
 *    Licensed 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.
 */

// THIS FILE IS GENERATED BY ZAP
// This file is generated from clusters-Structs.ipp.zapt

#include <clusters/OperationalCredentials/Structs.h>

#include <app/data-model/StructDecodeIterator.h>
#include <app/data-model/WrappedStructEncoder.h>

namespace chip {
namespace app {
namespace Clusters {
namespace OperationalCredentials {
namespace Structs {

namespace FabricDescriptorStruct {
CHIP_ERROR Type::EncodeForWrite(TLV::TLVWriter &aWriter, TLV::Tag aTag) const {
  return DoEncode(aWriter, aTag, NullOptional);
}

CHIP_ERROR Type::EncodeForRead(TLV::TLVWriter &aWriter, TLV::Tag aTag,
                               FabricIndex aAccessingFabricIndex) const {
  return DoEncode(aWriter, aTag, MakeOptional(aAccessingFabricIndex));
}

CHIP_ERROR
Type::DoEncode(TLV::TLVWriter &aWriter, TLV::Tag aTag,
               const Optional<FabricIndex> &aAccessingFabricIndex) const {

  DataModel::WrappedStructEncoder encoder{aWriter, aTag};

  encoder.Encode(to_underlying(Fields::kRootPublicKey), rootPublicKey);
  encoder.Encode(to_underlying(Fields::kVendorID), vendorID);
  encoder.Encode(to_underlying(Fields::kFabricID), fabricID);
  encoder.Encode(to_underlying(Fields::kNodeID), nodeID);
  encoder.Encode(to_underlying(Fields::kLabel), label);
  encoder.Encode(to_underlying(Fields::kVIDVerificationStatement),
                 VIDVerificationStatement);
  if (aAccessingFabricIndex.HasValue()) {
    encoder.Encode(to_underlying(Fields::kFabricIndex), fabricIndex);
  }

  return encoder.Finalize();
}

CHIP_ERROR DecodableType::Decode(TLV::TLVReader &reader) {
  detail::StructDecodeIterator __iterator(reader);
  while (true) {
    uint8_t __context_tag = 0;
    CHIP_ERROR err = __iterator.Next(__context_tag);
    VerifyOrReturnError(err != CHIP_ERROR_END_OF_TLV, CHIP_NO_ERROR);
    ReturnErrorOnFailure(err);

    if (__context_tag == to_underlying(Fields::kRootPublicKey)) {
      err = DataModel::Decode(reader, rootPublicKey);
    } else if (__context_tag == to_underlying(Fields::kVendorID)) {
      err = DataModel::Decode(reader, vendorID);
    } else if (__context_tag == to_underlying(Fields::kFabricID)) {
      err = DataModel::Decode(reader, fabricID);
    } else if (__context_tag == to_underlying(Fields::kNodeID)) {
      err = DataModel::Decode(reader, nodeID);
    } else if (__context_tag == to_underlying(Fields::kLabel)) {
      err = DataModel::Decode(reader, label);
    } else if (__context_tag ==
               to_underlying(Fields::kVIDVerificationStatement)) {
      err = DataModel::Decode(reader, VIDVerificationStatement);
    } else if (__context_tag == to_underlying(Fields::kFabricIndex)) {
      err = DataModel::Decode(reader, fabricIndex);
    }

    ReturnErrorOnFailure(err);
  }
}

} // namespace FabricDescriptorStruct

namespace NOCStruct {
CHIP_ERROR Type::EncodeForWrite(TLV::TLVWriter &aWriter, TLV::Tag aTag) const {
  return DoEncode(aWriter, aTag, NullOptional);
}

CHIP_ERROR Type::EncodeForRead(TLV::TLVWriter &aWriter, TLV::Tag aTag,
                               FabricIndex aAccessingFabricIndex) const {
  return DoEncode(aWriter, aTag, MakeOptional(aAccessingFabricIndex));
}

CHIP_ERROR
Type::DoEncode(TLV::TLVWriter &aWriter, TLV::Tag aTag,
               const Optional<FabricIndex> &aAccessingFabricIndex) const {

  DataModel::WrappedStructEncoder encoder{aWriter, aTag};

  encoder.Encode(to_underlying(Fields::kNoc), noc);
  encoder.Encode(to_underlying(Fields::kIcac), icac);
  encoder.Encode(to_underlying(Fields::kVvsc), vvsc);
  if (aAccessingFabricIndex.HasValue()) {
    encoder.Encode(to_underlying(Fields::kFabricIndex), fabricIndex);
  }

  return encoder.Finalize();
}

CHIP_ERROR DecodableType::Decode(TLV::TLVReader &reader) {
  detail::StructDecodeIterator __iterator(reader);
  while (true) {
    uint8_t __context_tag = 0;
    CHIP_ERROR err = __iterator.Next(__context_tag);
    VerifyOrReturnError(err != CHIP_ERROR_END_OF_TLV, CHIP_NO_ERROR);
    ReturnErrorOnFailure(err);

    if (__context_tag == to_underlying(Fields::kNoc)) {
      err = DataModel::Decode(reader, noc);
    } else if (__context_tag == to_underlying(Fields::kIcac)) {
      err = DataModel::Decode(reader, icac);
    } else if (__context_tag == to_underlying(Fields::kVvsc)) {
      err = DataModel::Decode(reader, vvsc);
    } else if (__context_tag == to_underlying(Fields::kFabricIndex)) {
      err = DataModel::Decode(reader, fabricIndex);
    }

    ReturnErrorOnFailure(err);
  }
}

} // namespace NOCStruct
} // namespace Structs
} // namespace OperationalCredentials
} // namespace Clusters
} // namespace app
} // namespace chip
