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

// AddQuickReplyShortcutMessageAlbumRequest represents TL type `addQuickReplyShortcutMessageAlbum#9647a3fd`.
type AddQuickReplyShortcutMessageAlbumRequest struct {
	// Name of the target shortcut
	ShortcutName string
	// Identifier of a quick reply message in the same shortcut to be replied; pass 0 if none
	ReplyToMessageID int64
	// Contents of messages to be sent. At most 10 messages can be added to an album. All
	// messages must have the same value of show_caption_above_media
	InputMessageContents []InputMessageContentClass
}

// AddQuickReplyShortcutMessageAlbumRequestTypeID is TL type id of AddQuickReplyShortcutMessageAlbumRequest.
const AddQuickReplyShortcutMessageAlbumRequestTypeID = 0x9647a3fd

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

func (a *AddQuickReplyShortcutMessageAlbumRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.ShortcutName == "") {
		return false
	}
	if !(a.ReplyToMessageID == 0) {
		return false
	}
	if !(a.InputMessageContents == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (a *AddQuickReplyShortcutMessageAlbumRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "addQuickReplyShortcutMessageAlbum",
		ID:   AddQuickReplyShortcutMessageAlbumRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ShortcutName",
			SchemaName: "shortcut_name",
		},
		{
			Name:       "ReplyToMessageID",
			SchemaName: "reply_to_message_id",
		},
		{
			Name:       "InputMessageContents",
			SchemaName: "input_message_contents",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AddQuickReplyShortcutMessageAlbumRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode addQuickReplyShortcutMessageAlbum#9647a3fd as nil")
	}
	b.PutID(AddQuickReplyShortcutMessageAlbumRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AddQuickReplyShortcutMessageAlbumRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode addQuickReplyShortcutMessageAlbum#9647a3fd as nil")
	}
	b.PutString(a.ShortcutName)
	b.PutInt53(a.ReplyToMessageID)
	b.PutInt(len(a.InputMessageContents))
	for idx, v := range a.InputMessageContents {
		if v == nil {
			return fmt.Errorf("unable to encode addQuickReplyShortcutMessageAlbum#9647a3fd: field input_message_contents element with index %d is nil", idx)
		}
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare addQuickReplyShortcutMessageAlbum#9647a3fd: field input_message_contents element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AddQuickReplyShortcutMessageAlbumRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode addQuickReplyShortcutMessageAlbum#9647a3fd to nil")
	}
	if err := b.ConsumeID(AddQuickReplyShortcutMessageAlbumRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode addQuickReplyShortcutMessageAlbum#9647a3fd: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AddQuickReplyShortcutMessageAlbumRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode addQuickReplyShortcutMessageAlbum#9647a3fd to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode addQuickReplyShortcutMessageAlbum#9647a3fd: field shortcut_name: %w", err)
		}
		a.ShortcutName = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode addQuickReplyShortcutMessageAlbum#9647a3fd: field reply_to_message_id: %w", err)
		}
		a.ReplyToMessageID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode addQuickReplyShortcutMessageAlbum#9647a3fd: field input_message_contents: %w", err)
		}

		if headerLen > 0 {
			a.InputMessageContents = make([]InputMessageContentClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputMessageContent(b)
			if err != nil {
				return fmt.Errorf("unable to decode addQuickReplyShortcutMessageAlbum#9647a3fd: field input_message_contents: %w", err)
			}
			a.InputMessageContents = append(a.InputMessageContents, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (a *AddQuickReplyShortcutMessageAlbumRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if a == nil {
		return fmt.Errorf("can't encode addQuickReplyShortcutMessageAlbum#9647a3fd as nil")
	}
	b.ObjStart()
	b.PutID("addQuickReplyShortcutMessageAlbum")
	b.Comma()
	b.FieldStart("shortcut_name")
	b.PutString(a.ShortcutName)
	b.Comma()
	b.FieldStart("reply_to_message_id")
	b.PutInt53(a.ReplyToMessageID)
	b.Comma()
	b.FieldStart("input_message_contents")
	b.ArrStart()
	for idx, v := range a.InputMessageContents {
		if v == nil {
			return fmt.Errorf("unable to encode addQuickReplyShortcutMessageAlbum#9647a3fd: field input_message_contents element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode addQuickReplyShortcutMessageAlbum#9647a3fd: field input_message_contents element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (a *AddQuickReplyShortcutMessageAlbumRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if a == nil {
		return fmt.Errorf("can't decode addQuickReplyShortcutMessageAlbum#9647a3fd to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("addQuickReplyShortcutMessageAlbum"); err != nil {
				return fmt.Errorf("unable to decode addQuickReplyShortcutMessageAlbum#9647a3fd: %w", err)
			}
		case "shortcut_name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode addQuickReplyShortcutMessageAlbum#9647a3fd: field shortcut_name: %w", err)
			}
			a.ShortcutName = value
		case "reply_to_message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode addQuickReplyShortcutMessageAlbum#9647a3fd: field reply_to_message_id: %w", err)
			}
			a.ReplyToMessageID = value
		case "input_message_contents":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := DecodeTDLibJSONInputMessageContent(b)
				if err != nil {
					return fmt.Errorf("unable to decode addQuickReplyShortcutMessageAlbum#9647a3fd: field input_message_contents: %w", err)
				}
				a.InputMessageContents = append(a.InputMessageContents, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode addQuickReplyShortcutMessageAlbum#9647a3fd: field input_message_contents: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetShortcutName returns value of ShortcutName field.
func (a *AddQuickReplyShortcutMessageAlbumRequest) GetShortcutName() (value string) {
	if a == nil {
		return
	}
	return a.ShortcutName
}

// GetReplyToMessageID returns value of ReplyToMessageID field.
func (a *AddQuickReplyShortcutMessageAlbumRequest) GetReplyToMessageID() (value int64) {
	if a == nil {
		return
	}
	return a.ReplyToMessageID
}

// GetInputMessageContents returns value of InputMessageContents field.
func (a *AddQuickReplyShortcutMessageAlbumRequest) GetInputMessageContents() (value []InputMessageContentClass) {
	if a == nil {
		return
	}
	return a.InputMessageContents
}

// AddQuickReplyShortcutMessageAlbum invokes method addQuickReplyShortcutMessageAlbum#9647a3fd returning error if any.
func (c *Client) AddQuickReplyShortcutMessageAlbum(ctx context.Context, request *AddQuickReplyShortcutMessageAlbumRequest) (*QuickReplyMessages, error) {
	var result QuickReplyMessages

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