// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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{}
)

// ReceivedGifts represents TL type `receivedGifts#bfb308f0`.
type ReceivedGifts struct {
	// The total number of received gifts
	TotalCount int32
	// The list of gifts
	Gifts []ReceivedGift
	// True, if notifications about new gifts of the owner are enabled
	AreNotificationsEnabled bool
	// The offset for the next request. If empty, then there are no more results
	NextOffset string
}

// ReceivedGiftsTypeID is TL type id of ReceivedGifts.
const ReceivedGiftsTypeID = 0xbfb308f0

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

func (r *ReceivedGifts) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.TotalCount == 0) {
		return false
	}
	if !(r.Gifts == nil) {
		return false
	}
	if !(r.AreNotificationsEnabled == false) {
		return false
	}
	if !(r.NextOffset == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (r *ReceivedGifts) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "receivedGifts",
		ID:   ReceivedGiftsTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Gifts",
			SchemaName: "gifts",
		},
		{
			Name:       "AreNotificationsEnabled",
			SchemaName: "are_notifications_enabled",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *ReceivedGifts) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode receivedGifts#bfb308f0 as nil")
	}
	b.PutID(ReceivedGiftsTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *ReceivedGifts) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode receivedGifts#bfb308f0 as nil")
	}
	b.PutInt32(r.TotalCount)
	b.PutInt(len(r.Gifts))
	for idx, v := range r.Gifts {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare receivedGifts#bfb308f0: field gifts element with index %d: %w", idx, err)
		}
	}
	b.PutBool(r.AreNotificationsEnabled)
	b.PutString(r.NextOffset)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (r *ReceivedGifts) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode receivedGifts#bfb308f0 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGifts#bfb308f0: field total_count: %w", err)
		}
		r.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGifts#bfb308f0: field gifts: %w", err)
		}

		if headerLen > 0 {
			r.Gifts = make([]ReceivedGift, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value ReceivedGift
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare receivedGifts#bfb308f0: field gifts: %w", err)
			}
			r.Gifts = append(r.Gifts, value)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGifts#bfb308f0: field are_notifications_enabled: %w", err)
		}
		r.AreNotificationsEnabled = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode receivedGifts#bfb308f0: field next_offset: %w", err)
		}
		r.NextOffset = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (r *ReceivedGifts) EncodeTDLibJSON(b tdjson.Encoder) error {
	if r == nil {
		return fmt.Errorf("can't encode receivedGifts#bfb308f0 as nil")
	}
	b.ObjStart()
	b.PutID("receivedGifts")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(r.TotalCount)
	b.Comma()
	b.FieldStart("gifts")
	b.ArrStart()
	for idx, v := range r.Gifts {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode receivedGifts#bfb308f0: field gifts element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("are_notifications_enabled")
	b.PutBool(r.AreNotificationsEnabled)
	b.Comma()
	b.FieldStart("next_offset")
	b.PutString(r.NextOffset)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (r *ReceivedGifts) DecodeTDLibJSON(b tdjson.Decoder) error {
	if r == nil {
		return fmt.Errorf("can't decode receivedGifts#bfb308f0 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("receivedGifts"); err != nil {
				return fmt.Errorf("unable to decode receivedGifts#bfb308f0: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGifts#bfb308f0: field total_count: %w", err)
			}
			r.TotalCount = value
		case "gifts":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value ReceivedGift
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode receivedGifts#bfb308f0: field gifts: %w", err)
				}
				r.Gifts = append(r.Gifts, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode receivedGifts#bfb308f0: field gifts: %w", err)
			}
		case "are_notifications_enabled":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGifts#bfb308f0: field are_notifications_enabled: %w", err)
			}
			r.AreNotificationsEnabled = value
		case "next_offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode receivedGifts#bfb308f0: field next_offset: %w", err)
			}
			r.NextOffset = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTotalCount returns value of TotalCount field.
func (r *ReceivedGifts) GetTotalCount() (value int32) {
	if r == nil {
		return
	}
	return r.TotalCount
}

// GetGifts returns value of Gifts field.
func (r *ReceivedGifts) GetGifts() (value []ReceivedGift) {
	if r == nil {
		return
	}
	return r.Gifts
}

// GetAreNotificationsEnabled returns value of AreNotificationsEnabled field.
func (r *ReceivedGifts) GetAreNotificationsEnabled() (value bool) {
	if r == nil {
		return
	}
	return r.AreNotificationsEnabled
}

// GetNextOffset returns value of NextOffset field.
func (r *ReceivedGifts) GetNextOffset() (value string) {
	if r == nil {
		return
	}
	return r.NextOffset
}
