// Code generated by gotdgen, DO NOT EDIT.

package mt

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"go.mau.fi/mautrix-telegram/pkg/gotd/bin"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdjson"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tdp"
	"go.mau.fi/mautrix-telegram/pkg/gotd/tgerr"
)

// No-op definition for keeping imports.
var (
	_ = bin.Buffer{}
	_ = context.Background()
	_ = fmt.Stringer(nil)
	_ = strings.Builder{}
	_ = errors.Is
	_ = multierr.AppendInto
	_ = sort.Ints
	_ = tdp.Format
	_ = tgerr.Error{}
	_ = tdjson.Encoder{}
)

// ResPQ represents TL type `resPQ#5162463`.
type ResPQ struct {
	// Nonce field of ResPQ.
	Nonce bin.Int128
	// ServerNonce field of ResPQ.
	ServerNonce bin.Int128
	// Pq field of ResPQ.
	Pq []byte
	// ServerPublicKeyFingerprints field of ResPQ.
	ServerPublicKeyFingerprints []int64
}

// ResPQTypeID is TL type id of ResPQ.
const ResPQTypeID = 0x5162463

// Ensuring interfaces in compile-time for ResPQ.
var (
	_ bin.Encoder     = &ResPQ{}
	_ bin.Decoder     = &ResPQ{}
	_ bin.BareEncoder = &ResPQ{}
	_ bin.BareDecoder = &ResPQ{}
)

func (r *ResPQ) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Nonce == bin.Int128{}) {
		return false
	}
	if !(r.ServerNonce == bin.Int128{}) {
		return false
	}
	if !(r.Pq == nil) {
		return false
	}
	if !(r.ServerPublicKeyFingerprints == nil) {
		return false
	}

	return true
}

// String implements fmt.Stringer.
func (r *ResPQ) String() string {
	if r == nil {
		return "ResPQ(nil)"
	}
	type Alias ResPQ
	return fmt.Sprintf("ResPQ%+v", Alias(*r))
}

// TypeID returns type id in TL schema.
//
// See https://core.telegram.org/mtproto/TL-tl#remarks.
func (*ResPQ) TypeID() uint32 {
	return ResPQTypeID
}

// TypeName returns name of type in TL schema.
func (*ResPQ) TypeName() string {
	return "resPQ"
}

// TypeInfo returns info about TL type.
func (r *ResPQ) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "resPQ",
		ID:   ResPQTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Nonce",
			SchemaName: "nonce",
		},
		{
			Name:       "ServerNonce",
			SchemaName: "server_nonce",
		},
		{
			Name:       "Pq",
			SchemaName: "pq",
		},
		{
			Name:       "ServerPublicKeyFingerprints",
			SchemaName: "server_public_key_fingerprints",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *ResPQ) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode resPQ#5162463 as nil")
	}
	b.PutID(ResPQTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *ResPQ) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode resPQ#5162463 as nil")
	}
	b.PutInt128(r.Nonce)
	b.PutInt128(r.ServerNonce)
	b.PutBytes(r.Pq)
	b.PutVectorHeader(len(r.ServerPublicKeyFingerprints))
	for _, v := range r.ServerPublicKeyFingerprints {
		b.PutLong(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (r *ResPQ) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode resPQ#5162463 to nil")
	}
	if err := b.ConsumeID(ResPQTypeID); err != nil {
		return fmt.Errorf("unable to decode resPQ#5162463: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *ResPQ) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode resPQ#5162463 to nil")
	}
	{
		value, err := b.Int128()
		if err != nil {
			return fmt.Errorf("unable to decode resPQ#5162463: field nonce: %w", err)
		}
		r.Nonce = value
	}
	{
		value, err := b.Int128()
		if err != nil {
			return fmt.Errorf("unable to decode resPQ#5162463: field server_nonce: %w", err)
		}
		r.ServerNonce = value
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode resPQ#5162463: field pq: %w", err)
		}
		r.Pq = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode resPQ#5162463: field server_public_key_fingerprints: %w", err)
		}

		if headerLen > 0 {
			r.ServerPublicKeyFingerprints = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode resPQ#5162463: field server_public_key_fingerprints: %w", err)
			}
			r.ServerPublicKeyFingerprints = append(r.ServerPublicKeyFingerprints, value)
		}
	}
	return nil
}

// GetNonce returns value of Nonce field.
func (r *ResPQ) GetNonce() (value bin.Int128) {
	if r == nil {
		return
	}
	return r.Nonce
}

// GetServerNonce returns value of ServerNonce field.
func (r *ResPQ) GetServerNonce() (value bin.Int128) {
	if r == nil {
		return
	}
	return r.ServerNonce
}

// GetPq returns value of Pq field.
func (r *ResPQ) GetPq() (value []byte) {
	if r == nil {
		return
	}
	return r.Pq
}

// GetServerPublicKeyFingerprints returns value of ServerPublicKeyFingerprints field.
func (r *ResPQ) GetServerPublicKeyFingerprints() (value []int64) {
	if r == nil {
		return
	}
	return r.ServerPublicKeyFingerprints
}
