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

// SetStickerKeywordsRequest represents TL type `setStickerKeywords#462481d`.
type SetStickerKeywordsRequest struct {
	// Sticker
	Sticker InputFileClass
	// List of up to 20 keywords with total length up to 64 characters, which can be used to
	// find the sticker
	Keywords []string
}

// SetStickerKeywordsRequestTypeID is TL type id of SetStickerKeywordsRequest.
const SetStickerKeywordsRequestTypeID = 0x462481d

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

func (s *SetStickerKeywordsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Sticker == nil) {
		return false
	}
	if !(s.Keywords == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetStickerKeywordsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setStickerKeywords",
		ID:   SetStickerKeywordsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Sticker",
			SchemaName: "sticker",
		},
		{
			Name:       "Keywords",
			SchemaName: "keywords",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetStickerKeywordsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setStickerKeywords#462481d as nil")
	}
	b.PutID(SetStickerKeywordsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetStickerKeywordsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setStickerKeywords#462481d as nil")
	}
	if s.Sticker == nil {
		return fmt.Errorf("unable to encode setStickerKeywords#462481d: field sticker is nil")
	}
	if err := s.Sticker.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setStickerKeywords#462481d: field sticker: %w", err)
	}
	b.PutInt(len(s.Keywords))
	for _, v := range s.Keywords {
		b.PutString(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetStickerKeywordsRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setStickerKeywords#462481d to nil")
	}
	if err := b.ConsumeID(SetStickerKeywordsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setStickerKeywords#462481d: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetStickerKeywordsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setStickerKeywords#462481d to nil")
	}
	{
		value, err := DecodeInputFile(b)
		if err != nil {
			return fmt.Errorf("unable to decode setStickerKeywords#462481d: field sticker: %w", err)
		}
		s.Sticker = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode setStickerKeywords#462481d: field keywords: %w", err)
		}

		if headerLen > 0 {
			s.Keywords = make([]string, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setStickerKeywords#462481d: field keywords: %w", err)
			}
			s.Keywords = append(s.Keywords, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetStickerKeywordsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setStickerKeywords#462481d as nil")
	}
	b.ObjStart()
	b.PutID("setStickerKeywords")
	b.Comma()
	b.FieldStart("sticker")
	if s.Sticker == nil {
		return fmt.Errorf("unable to encode setStickerKeywords#462481d: field sticker is nil")
	}
	if err := s.Sticker.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setStickerKeywords#462481d: field sticker: %w", err)
	}
	b.Comma()
	b.FieldStart("keywords")
	b.ArrStart()
	for _, v := range s.Keywords {
		b.PutString(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetStickerKeywordsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setStickerKeywords#462481d to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setStickerKeywords"); err != nil {
				return fmt.Errorf("unable to decode setStickerKeywords#462481d: %w", err)
			}
		case "sticker":
			value, err := DecodeTDLibJSONInputFile(b)
			if err != nil {
				return fmt.Errorf("unable to decode setStickerKeywords#462481d: field sticker: %w", err)
			}
			s.Sticker = value
		case "keywords":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.String()
				if err != nil {
					return fmt.Errorf("unable to decode setStickerKeywords#462481d: field keywords: %w", err)
				}
				s.Keywords = append(s.Keywords, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode setStickerKeywords#462481d: field keywords: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetKeywords returns value of Keywords field.
func (s *SetStickerKeywordsRequest) GetKeywords() (value []string) {
	if s == nil {
		return
	}
	return s.Keywords
}

// SetStickerKeywords invokes method setStickerKeywords#462481d returning error if any.
func (c *Client) SetStickerKeywords(ctx context.Context, request *SetStickerKeywordsRequest) error {
	var ok Ok

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