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

// SearchGiftsForResaleRequest represents TL type `searchGiftsForResale#7a4082ac`.
type SearchGiftsForResaleRequest struct {
	// Identifier of the regular gift that was upgraded to a unique gift
	GiftID int64
	// Order in which the results will be sorted
	Order GiftForResaleOrderClass
	// Attributes used to filter received gifts. If multiple attributes of the same type are
	// specified, then all of them are allowed.
	Attributes []UpgradedGiftAttributeIDClass
	// Offset of the first entry to return as received from the previous request with the
	// same order and attributes; use empty string to get the first chunk of results
	Offset string
	// The maximum number of gifts to return
	Limit int32
}

// SearchGiftsForResaleRequestTypeID is TL type id of SearchGiftsForResaleRequest.
const SearchGiftsForResaleRequestTypeID = 0x7a4082ac

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

func (s *SearchGiftsForResaleRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.GiftID == 0) {
		return false
	}
	if !(s.Order == nil) {
		return false
	}
	if !(s.Attributes == nil) {
		return false
	}
	if !(s.Offset == "") {
		return false
	}
	if !(s.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchGiftsForResaleRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchGiftsForResale",
		ID:   SearchGiftsForResaleRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "GiftID",
			SchemaName: "gift_id",
		},
		{
			Name:       "Order",
			SchemaName: "order",
		},
		{
			Name:       "Attributes",
			SchemaName: "attributes",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchGiftsForResaleRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchGiftsForResale#7a4082ac as nil")
	}
	b.PutID(SearchGiftsForResaleRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchGiftsForResaleRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchGiftsForResale#7a4082ac as nil")
	}
	b.PutLong(s.GiftID)
	if s.Order == nil {
		return fmt.Errorf("unable to encode searchGiftsForResale#7a4082ac: field order is nil")
	}
	if err := s.Order.Encode(b); err != nil {
		return fmt.Errorf("unable to encode searchGiftsForResale#7a4082ac: field order: %w", err)
	}
	b.PutInt(len(s.Attributes))
	for idx, v := range s.Attributes {
		if v == nil {
			return fmt.Errorf("unable to encode searchGiftsForResale#7a4082ac: field attributes element with index %d is nil", idx)
		}
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare searchGiftsForResale#7a4082ac: field attributes element with index %d: %w", idx, err)
		}
	}
	b.PutString(s.Offset)
	b.PutInt32(s.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (s *SearchGiftsForResaleRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchGiftsForResale#7a4082ac to nil")
	}
	if err := b.ConsumeID(SearchGiftsForResaleRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SearchGiftsForResaleRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchGiftsForResale#7a4082ac to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: field gift_id: %w", err)
		}
		s.GiftID = value
	}
	{
		value, err := DecodeGiftForResaleOrder(b)
		if err != nil {
			return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: field order: %w", err)
		}
		s.Order = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: field attributes: %w", err)
		}

		if headerLen > 0 {
			s.Attributes = make([]UpgradedGiftAttributeIDClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUpgradedGiftAttributeID(b)
			if err != nil {
				return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: field attributes: %w", err)
			}
			s.Attributes = append(s.Attributes, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: field offset: %w", err)
		}
		s.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: field limit: %w", err)
		}
		s.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchGiftsForResaleRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchGiftsForResale#7a4082ac as nil")
	}
	b.ObjStart()
	b.PutID("searchGiftsForResale")
	b.Comma()
	b.FieldStart("gift_id")
	b.PutLong(s.GiftID)
	b.Comma()
	b.FieldStart("order")
	if s.Order == nil {
		return fmt.Errorf("unable to encode searchGiftsForResale#7a4082ac: field order is nil")
	}
	if err := s.Order.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode searchGiftsForResale#7a4082ac: field order: %w", err)
	}
	b.Comma()
	b.FieldStart("attributes")
	b.ArrStart()
	for idx, v := range s.Attributes {
		if v == nil {
			return fmt.Errorf("unable to encode searchGiftsForResale#7a4082ac: field attributes element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode searchGiftsForResale#7a4082ac: field attributes element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("offset")
	b.PutString(s.Offset)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(s.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SearchGiftsForResaleRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode searchGiftsForResale#7a4082ac to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("searchGiftsForResale"); err != nil {
				return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: %w", err)
			}
		case "gift_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: field gift_id: %w", err)
			}
			s.GiftID = value
		case "order":
			value, err := DecodeTDLibJSONGiftForResaleOrder(b)
			if err != nil {
				return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: field order: %w", err)
			}
			s.Order = value
		case "attributes":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := DecodeTDLibJSONUpgradedGiftAttributeID(b)
				if err != nil {
					return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: field attributes: %w", err)
				}
				s.Attributes = append(s.Attributes, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: field attributes: %w", err)
			}
		case "offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: field offset: %w", err)
			}
			s.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode searchGiftsForResale#7a4082ac: field limit: %w", err)
			}
			s.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetGiftID returns value of GiftID field.
func (s *SearchGiftsForResaleRequest) GetGiftID() (value int64) {
	if s == nil {
		return
	}
	return s.GiftID
}

// GetOrder returns value of Order field.
func (s *SearchGiftsForResaleRequest) GetOrder() (value GiftForResaleOrderClass) {
	if s == nil {
		return
	}
	return s.Order
}

// GetAttributes returns value of Attributes field.
func (s *SearchGiftsForResaleRequest) GetAttributes() (value []UpgradedGiftAttributeIDClass) {
	if s == nil {
		return
	}
	return s.Attributes
}

// GetOffset returns value of Offset field.
func (s *SearchGiftsForResaleRequest) GetOffset() (value string) {
	if s == nil {
		return
	}
	return s.Offset
}

// GetLimit returns value of Limit field.
func (s *SearchGiftsForResaleRequest) GetLimit() (value int32) {
	if s == nil {
		return
	}
	return s.Limit
}

// SearchGiftsForResale invokes method searchGiftsForResale#7a4082ac returning error if any.
func (c *Client) SearchGiftsForResale(ctx context.Context, request *SearchGiftsForResaleRequest) (*GiftsForResale, error) {
	var result GiftsForResale

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return nil, err
	}
	return &result, nil
}
