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

// SetSavedMessagesTagLabelRequest represents TL type `setSavedMessagesTagLabel#b03acd10`.
type SetSavedMessagesTagLabelRequest struct {
	// The tag which label will be changed
	Tag ReactionTypeClass
	// New label for the tag; 0-12 characters
	Label string
}

// SetSavedMessagesTagLabelRequestTypeID is TL type id of SetSavedMessagesTagLabelRequest.
const SetSavedMessagesTagLabelRequestTypeID = 0xb03acd10

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

func (s *SetSavedMessagesTagLabelRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Tag == nil) {
		return false
	}
	if !(s.Label == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetSavedMessagesTagLabelRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setSavedMessagesTagLabel",
		ID:   SetSavedMessagesTagLabelRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Tag",
			SchemaName: "tag",
		},
		{
			Name:       "Label",
			SchemaName: "label",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetSavedMessagesTagLabelRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setSavedMessagesTagLabel#b03acd10 as nil")
	}
	b.PutID(SetSavedMessagesTagLabelRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetSavedMessagesTagLabelRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setSavedMessagesTagLabel#b03acd10 as nil")
	}
	if s.Tag == nil {
		return fmt.Errorf("unable to encode setSavedMessagesTagLabel#b03acd10: field tag is nil")
	}
	if err := s.Tag.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setSavedMessagesTagLabel#b03acd10: field tag: %w", err)
	}
	b.PutString(s.Label)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SetSavedMessagesTagLabelRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setSavedMessagesTagLabel#b03acd10 to nil")
	}
	{
		value, err := DecodeReactionType(b)
		if err != nil {
			return fmt.Errorf("unable to decode setSavedMessagesTagLabel#b03acd10: field tag: %w", err)
		}
		s.Tag = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode setSavedMessagesTagLabel#b03acd10: field label: %w", err)
		}
		s.Label = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetSavedMessagesTagLabelRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setSavedMessagesTagLabel#b03acd10 as nil")
	}
	b.ObjStart()
	b.PutID("setSavedMessagesTagLabel")
	b.Comma()
	b.FieldStart("tag")
	if s.Tag == nil {
		return fmt.Errorf("unable to encode setSavedMessagesTagLabel#b03acd10: field tag is nil")
	}
	if err := s.Tag.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setSavedMessagesTagLabel#b03acd10: field tag: %w", err)
	}
	b.Comma()
	b.FieldStart("label")
	b.PutString(s.Label)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setSavedMessagesTagLabel"); err != nil {
				return fmt.Errorf("unable to decode setSavedMessagesTagLabel#b03acd10: %w", err)
			}
		case "tag":
			value, err := DecodeTDLibJSONReactionType(b)
			if err != nil {
				return fmt.Errorf("unable to decode setSavedMessagesTagLabel#b03acd10: field tag: %w", err)
			}
			s.Tag = value
		case "label":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode setSavedMessagesTagLabel#b03acd10: field label: %w", err)
			}
			s.Label = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTag returns value of Tag field.
func (s *SetSavedMessagesTagLabelRequest) GetTag() (value ReactionTypeClass) {
	if s == nil {
		return
	}
	return s.Tag
}

// GetLabel returns value of Label field.
func (s *SetSavedMessagesTagLabelRequest) GetLabel() (value string) {
	if s == nil {
		return
	}
	return s.Label
}

// SetSavedMessagesTagLabel invokes method setSavedMessagesTagLabel#b03acd10 returning error if any.
func (c *Client) SetSavedMessagesTagLabel(ctx context.Context, request *SetSavedMessagesTagLabelRequest) error {
	var ok Ok

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