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

// GiftsForResale represents TL type `giftsForResale#3ff9dc54`.
type GiftsForResale struct {
	// Total number of gifts found
	TotalCount int32
	// The gifts
	Gifts []GiftForResale
	// Available models; for searchGiftsForResale requests without offset and attributes only
	Models []UpgradedGiftModelCount
	// Available symbols; for searchGiftsForResale requests without offset and attributes
	// only
	Symbols []UpgradedGiftSymbolCount
	// Available backdrops; for searchGiftsForResale requests without offset and attributes
	// only
	Backdrops []UpgradedGiftBackdropCount
	// The offset for the next request. If empty, then there are no more results
	NextOffset string
}

// GiftsForResaleTypeID is TL type id of GiftsForResale.
const GiftsForResaleTypeID = 0x3ff9dc54

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

func (g *GiftsForResale) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.TotalCount == 0) {
		return false
	}
	if !(g.Gifts == nil) {
		return false
	}
	if !(g.Models == nil) {
		return false
	}
	if !(g.Symbols == nil) {
		return false
	}
	if !(g.Backdrops == nil) {
		return false
	}
	if !(g.NextOffset == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GiftsForResale) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "giftsForResale",
		ID:   GiftsForResaleTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Gifts",
			SchemaName: "gifts",
		},
		{
			Name:       "Models",
			SchemaName: "models",
		},
		{
			Name:       "Symbols",
			SchemaName: "symbols",
		},
		{
			Name:       "Backdrops",
			SchemaName: "backdrops",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GiftsForResale) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode giftsForResale#3ff9dc54 as nil")
	}
	b.PutID(GiftsForResaleTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GiftsForResale) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode giftsForResale#3ff9dc54 as nil")
	}
	b.PutInt32(g.TotalCount)
	b.PutInt(len(g.Gifts))
	for idx, v := range g.Gifts {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare giftsForResale#3ff9dc54: field gifts element with index %d: %w", idx, err)
		}
	}
	b.PutInt(len(g.Models))
	for idx, v := range g.Models {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare giftsForResale#3ff9dc54: field models element with index %d: %w", idx, err)
		}
	}
	b.PutInt(len(g.Symbols))
	for idx, v := range g.Symbols {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare giftsForResale#3ff9dc54: field symbols element with index %d: %w", idx, err)
		}
	}
	b.PutInt(len(g.Backdrops))
	for idx, v := range g.Backdrops {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare giftsForResale#3ff9dc54: field backdrops element with index %d: %w", idx, err)
		}
	}
	b.PutString(g.NextOffset)
	return nil
}

// Decode implements bin.Decoder.
func (g *GiftsForResale) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode giftsForResale#3ff9dc54 to nil")
	}
	if err := b.ConsumeID(GiftsForResaleTypeID); err != nil {
		return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: %w", err)
	}
	return g.DecodeBare(b)
}

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

		if headerLen > 0 {
			g.Gifts = make([]GiftForResale, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value GiftForResale
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare giftsForResale#3ff9dc54: field gifts: %w", err)
			}
			g.Gifts = append(g.Gifts, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field models: %w", err)
		}

		if headerLen > 0 {
			g.Models = make([]UpgradedGiftModelCount, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value UpgradedGiftModelCount
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare giftsForResale#3ff9dc54: field models: %w", err)
			}
			g.Models = append(g.Models, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field symbols: %w", err)
		}

		if headerLen > 0 {
			g.Symbols = make([]UpgradedGiftSymbolCount, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value UpgradedGiftSymbolCount
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare giftsForResale#3ff9dc54: field symbols: %w", err)
			}
			g.Symbols = append(g.Symbols, value)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field backdrops: %w", err)
		}

		if headerLen > 0 {
			g.Backdrops = make([]UpgradedGiftBackdropCount, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value UpgradedGiftBackdropCount
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare giftsForResale#3ff9dc54: field backdrops: %w", err)
			}
			g.Backdrops = append(g.Backdrops, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field next_offset: %w", err)
		}
		g.NextOffset = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GiftsForResale) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode giftsForResale#3ff9dc54 as nil")
	}
	b.ObjStart()
	b.PutID("giftsForResale")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(g.TotalCount)
	b.Comma()
	b.FieldStart("gifts")
	b.ArrStart()
	for idx, v := range g.Gifts {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode giftsForResale#3ff9dc54: field gifts element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("models")
	b.ArrStart()
	for idx, v := range g.Models {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode giftsForResale#3ff9dc54: field models element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("symbols")
	b.ArrStart()
	for idx, v := range g.Symbols {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode giftsForResale#3ff9dc54: field symbols element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("backdrops")
	b.ArrStart()
	for idx, v := range g.Backdrops {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode giftsForResale#3ff9dc54: field backdrops element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("next_offset")
	b.PutString(g.NextOffset)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GiftsForResale) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode giftsForResale#3ff9dc54 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("giftsForResale"); err != nil {
				return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field total_count: %w", err)
			}
			g.TotalCount = value
		case "gifts":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value GiftForResale
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field gifts: %w", err)
				}
				g.Gifts = append(g.Gifts, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field gifts: %w", err)
			}
		case "models":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value UpgradedGiftModelCount
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field models: %w", err)
				}
				g.Models = append(g.Models, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field models: %w", err)
			}
		case "symbols":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value UpgradedGiftSymbolCount
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field symbols: %w", err)
				}
				g.Symbols = append(g.Symbols, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field symbols: %w", err)
			}
		case "backdrops":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value UpgradedGiftBackdropCount
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field backdrops: %w", err)
				}
				g.Backdrops = append(g.Backdrops, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field backdrops: %w", err)
			}
		case "next_offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode giftsForResale#3ff9dc54: field next_offset: %w", err)
			}
			g.NextOffset = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetGifts returns value of Gifts field.
func (g *GiftsForResale) GetGifts() (value []GiftForResale) {
	if g == nil {
		return
	}
	return g.Gifts
}

// GetModels returns value of Models field.
func (g *GiftsForResale) GetModels() (value []UpgradedGiftModelCount) {
	if g == nil {
		return
	}
	return g.Models
}

// GetSymbols returns value of Symbols field.
func (g *GiftsForResale) GetSymbols() (value []UpgradedGiftSymbolCount) {
	if g == nil {
		return
	}
	return g.Symbols
}

// GetBackdrops returns value of Backdrops field.
func (g *GiftsForResale) GetBackdrops() (value []UpgradedGiftBackdropCount) {
	if g == nil {
		return
	}
	return g.Backdrops
}

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