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

// AddFavoriteStickerRequest represents TL type `addFavoriteSticker#13578cdf`.
type AddFavoriteStickerRequest struct {
	// Sticker file to add
	Sticker InputFileClass
}

// AddFavoriteStickerRequestTypeID is TL type id of AddFavoriteStickerRequest.
const AddFavoriteStickerRequestTypeID = 0x13578cdf

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

func (a *AddFavoriteStickerRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.Sticker == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AddFavoriteStickerRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "addFavoriteSticker",
		ID:   AddFavoriteStickerRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Sticker",
			SchemaName: "sticker",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AddFavoriteStickerRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode addFavoriteSticker#13578cdf as nil")
	}
	b.PutID(AddFavoriteStickerRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AddFavoriteStickerRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode addFavoriteSticker#13578cdf as nil")
	}
	if a.Sticker == nil {
		return fmt.Errorf("unable to encode addFavoriteSticker#13578cdf: field sticker is nil")
	}
	if err := a.Sticker.Encode(b); err != nil {
		return fmt.Errorf("unable to encode addFavoriteSticker#13578cdf: field sticker: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AddFavoriteStickerRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode addFavoriteSticker#13578cdf to nil")
	}
	if err := b.ConsumeID(AddFavoriteStickerRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode addFavoriteSticker#13578cdf: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AddFavoriteStickerRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode addFavoriteSticker#13578cdf to nil")
	}
	{
		value, err := DecodeInputFile(b)
		if err != nil {
			return fmt.Errorf("unable to decode addFavoriteSticker#13578cdf: field sticker: %w", err)
		}
		a.Sticker = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AddFavoriteStickerRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode addFavoriteSticker#13578cdf as nil")
	}
	b.ObjStart()
	b.PutID("addFavoriteSticker")
	b.Comma()
	b.FieldStart("sticker")
	if a.Sticker == nil {
		return fmt.Errorf("unable to encode addFavoriteSticker#13578cdf: field sticker is nil")
	}
	if err := a.Sticker.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode addFavoriteSticker#13578cdf: field sticker: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AddFavoriteStickerRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode addFavoriteSticker#13578cdf to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("addFavoriteSticker"); err != nil {
				return fmt.Errorf("unable to decode addFavoriteSticker#13578cdf: %w", err)
			}
		case "sticker":
			value, err := DecodeTDLibJSONInputFile(b)
			if err != nil {
				return fmt.Errorf("unable to decode addFavoriteSticker#13578cdf: field sticker: %w", err)
			}
			a.Sticker = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSticker returns value of Sticker field.
func (a *AddFavoriteStickerRequest) GetSticker() (value InputFileClass) {
	if a == nil {
		return
	}
	return a.Sticker
}

// AddFavoriteSticker invokes method addFavoriteSticker#13578cdf returning error if any.
func (c *Client) AddFavoriteSticker(ctx context.Context, sticker InputFileClass) error {
	var ok Ok

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