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

// GetReceivedGiftsRequest represents TL type `getReceivedGifts#dd0a2150`.
type GetReceivedGiftsRequest struct {
	// Unique identifier of business connection on behalf of which to send the request; for
	// bots only
	BusinessConnectionID string
	// Identifier of the gift receiver
	OwnerID MessageSenderClass
	// Pass true to exclude gifts that aren't saved to the chat's profile page. Always true
	// for gifts received by other users and channel chats without can_post_messages
	// administrator right
	ExcludeUnsaved bool
	// Pass true to exclude gifts that are saved to the chat's profile page. Always false for
	// gifts received by other users and channel chats without can_post_messages
	// administrator right
	ExcludeSaved bool
	// Pass true to exclude gifts that can be purchased unlimited number of times
	ExcludeUnlimited bool
	// Pass true to exclude gifts that can be purchased limited number of times
	ExcludeLimited bool
	// Pass true to exclude upgraded gifts
	ExcludeUpgraded bool
	// Pass true to sort results by gift price instead of send date
	SortByPrice bool
	// Offset of the first entry to return as received from the previous request; use empty
	// string to get the first chunk of results
	Offset string
	// The maximum number of gifts to be returned; must be positive and can't be greater than
	// 100. For optimal performance, the number of returned objects is chosen by TDLib and
	// can be smaller than the specified limit
	Limit int32
}

// GetReceivedGiftsRequestTypeID is TL type id of GetReceivedGiftsRequest.
const GetReceivedGiftsRequestTypeID = 0xdd0a2150

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

func (g *GetReceivedGiftsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.BusinessConnectionID == "") {
		return false
	}
	if !(g.OwnerID == nil) {
		return false
	}
	if !(g.ExcludeUnsaved == false) {
		return false
	}
	if !(g.ExcludeSaved == false) {
		return false
	}
	if !(g.ExcludeUnlimited == false) {
		return false
	}
	if !(g.ExcludeLimited == false) {
		return false
	}
	if !(g.ExcludeUpgraded == false) {
		return false
	}
	if !(g.SortByPrice == false) {
		return false
	}
	if !(g.Offset == "") {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetReceivedGiftsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getReceivedGifts",
		ID:   GetReceivedGiftsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BusinessConnectionID",
			SchemaName: "business_connection_id",
		},
		{
			Name:       "OwnerID",
			SchemaName: "owner_id",
		},
		{
			Name:       "ExcludeUnsaved",
			SchemaName: "exclude_unsaved",
		},
		{
			Name:       "ExcludeSaved",
			SchemaName: "exclude_saved",
		},
		{
			Name:       "ExcludeUnlimited",
			SchemaName: "exclude_unlimited",
		},
		{
			Name:       "ExcludeLimited",
			SchemaName: "exclude_limited",
		},
		{
			Name:       "ExcludeUpgraded",
			SchemaName: "exclude_upgraded",
		},
		{
			Name:       "SortByPrice",
			SchemaName: "sort_by_price",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetReceivedGiftsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getReceivedGifts#dd0a2150 as nil")
	}
	b.PutID(GetReceivedGiftsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetReceivedGiftsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getReceivedGifts#dd0a2150 as nil")
	}
	b.PutString(g.BusinessConnectionID)
	if g.OwnerID == nil {
		return fmt.Errorf("unable to encode getReceivedGifts#dd0a2150: field owner_id is nil")
	}
	if err := g.OwnerID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getReceivedGifts#dd0a2150: field owner_id: %w", err)
	}
	b.PutBool(g.ExcludeUnsaved)
	b.PutBool(g.ExcludeSaved)
	b.PutBool(g.ExcludeUnlimited)
	b.PutBool(g.ExcludeLimited)
	b.PutBool(g.ExcludeUpgraded)
	b.PutBool(g.SortByPrice)
	b.PutString(g.Offset)
	b.PutInt32(g.Limit)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetReceivedGiftsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getReceivedGifts#dd0a2150 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field business_connection_id: %w", err)
		}
		g.BusinessConnectionID = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field owner_id: %w", err)
		}
		g.OwnerID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field exclude_unsaved: %w", err)
		}
		g.ExcludeUnsaved = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field exclude_saved: %w", err)
		}
		g.ExcludeSaved = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field exclude_unlimited: %w", err)
		}
		g.ExcludeUnlimited = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field exclude_limited: %w", err)
		}
		g.ExcludeLimited = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field exclude_upgraded: %w", err)
		}
		g.ExcludeUpgraded = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field sort_by_price: %w", err)
		}
		g.SortByPrice = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetReceivedGiftsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getReceivedGifts#dd0a2150 as nil")
	}
	b.ObjStart()
	b.PutID("getReceivedGifts")
	b.Comma()
	b.FieldStart("business_connection_id")
	b.PutString(g.BusinessConnectionID)
	b.Comma()
	b.FieldStart("owner_id")
	if g.OwnerID == nil {
		return fmt.Errorf("unable to encode getReceivedGifts#dd0a2150: field owner_id is nil")
	}
	if err := g.OwnerID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getReceivedGifts#dd0a2150: field owner_id: %w", err)
	}
	b.Comma()
	b.FieldStart("exclude_unsaved")
	b.PutBool(g.ExcludeUnsaved)
	b.Comma()
	b.FieldStart("exclude_saved")
	b.PutBool(g.ExcludeSaved)
	b.Comma()
	b.FieldStart("exclude_unlimited")
	b.PutBool(g.ExcludeUnlimited)
	b.Comma()
	b.FieldStart("exclude_limited")
	b.PutBool(g.ExcludeLimited)
	b.Comma()
	b.FieldStart("exclude_upgraded")
	b.PutBool(g.ExcludeUpgraded)
	b.Comma()
	b.FieldStart("sort_by_price")
	b.PutBool(g.SortByPrice)
	b.Comma()
	b.FieldStart("offset")
	b.PutString(g.Offset)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(g.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getReceivedGifts"); err != nil {
				return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: %w", err)
			}
		case "business_connection_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field business_connection_id: %w", err)
			}
			g.BusinessConnectionID = value
		case "owner_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field owner_id: %w", err)
			}
			g.OwnerID = value
		case "exclude_unsaved":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field exclude_unsaved: %w", err)
			}
			g.ExcludeUnsaved = value
		case "exclude_saved":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field exclude_saved: %w", err)
			}
			g.ExcludeSaved = value
		case "exclude_unlimited":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field exclude_unlimited: %w", err)
			}
			g.ExcludeUnlimited = value
		case "exclude_limited":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field exclude_limited: %w", err)
			}
			g.ExcludeLimited = value
		case "exclude_upgraded":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field exclude_upgraded: %w", err)
			}
			g.ExcludeUpgraded = value
		case "sort_by_price":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field sort_by_price: %w", err)
			}
			g.SortByPrice = value
		case "offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field offset: %w", err)
			}
			g.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getReceivedGifts#dd0a2150: field limit: %w", err)
			}
			g.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBusinessConnectionID returns value of BusinessConnectionID field.
func (g *GetReceivedGiftsRequest) GetBusinessConnectionID() (value string) {
	if g == nil {
		return
	}
	return g.BusinessConnectionID
}

// GetOwnerID returns value of OwnerID field.
func (g *GetReceivedGiftsRequest) GetOwnerID() (value MessageSenderClass) {
	if g == nil {
		return
	}
	return g.OwnerID
}

// GetExcludeUnsaved returns value of ExcludeUnsaved field.
func (g *GetReceivedGiftsRequest) GetExcludeUnsaved() (value bool) {
	if g == nil {
		return
	}
	return g.ExcludeUnsaved
}

// GetExcludeSaved returns value of ExcludeSaved field.
func (g *GetReceivedGiftsRequest) GetExcludeSaved() (value bool) {
	if g == nil {
		return
	}
	return g.ExcludeSaved
}

// GetExcludeUnlimited returns value of ExcludeUnlimited field.
func (g *GetReceivedGiftsRequest) GetExcludeUnlimited() (value bool) {
	if g == nil {
		return
	}
	return g.ExcludeUnlimited
}

// GetExcludeLimited returns value of ExcludeLimited field.
func (g *GetReceivedGiftsRequest) GetExcludeLimited() (value bool) {
	if g == nil {
		return
	}
	return g.ExcludeLimited
}

// GetExcludeUpgraded returns value of ExcludeUpgraded field.
func (g *GetReceivedGiftsRequest) GetExcludeUpgraded() (value bool) {
	if g == nil {
		return
	}
	return g.ExcludeUpgraded
}

// GetSortByPrice returns value of SortByPrice field.
func (g *GetReceivedGiftsRequest) GetSortByPrice() (value bool) {
	if g == nil {
		return
	}
	return g.SortByPrice
}

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

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

// GetReceivedGifts invokes method getReceivedGifts#dd0a2150 returning error if any.
func (c *Client) GetReceivedGifts(ctx context.Context, request *GetReceivedGiftsRequest) (*ReceivedGifts, error) {
	var result ReceivedGifts

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