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

// SendBusinessMessageAlbumRequest represents TL type `sendBusinessMessageAlbum#70bd4e6f`.
type SendBusinessMessageAlbumRequest struct {
	// Unique identifier of business connection on behalf of which to send the request
	BusinessConnectionID string
	// Target chat
	ChatID int64
	// Information about the message to be replied; pass null if none
	ReplyTo InputMessageReplyToClass
	// Pass true to disable notification for the message
	DisableNotification bool
	// Pass true if the content of the message must be protected from forwarding and saving
	ProtectContent bool
	// Identifier of the effect to apply to the message
	EffectID 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
}

// SendBusinessMessageAlbumRequestTypeID is TL type id of SendBusinessMessageAlbumRequest.
const SendBusinessMessageAlbumRequestTypeID = 0x70bd4e6f

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

func (s *SendBusinessMessageAlbumRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.BusinessConnectionID == "") {
		return false
	}
	if !(s.ChatID == 0) {
		return false
	}
	if !(s.ReplyTo == nil) {
		return false
	}
	if !(s.DisableNotification == false) {
		return false
	}
	if !(s.ProtectContent == false) {
		return false
	}
	if !(s.EffectID == 0) {
		return false
	}
	if !(s.InputMessageContents == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SendBusinessMessageAlbumRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "sendBusinessMessageAlbum",
		ID:   SendBusinessMessageAlbumRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BusinessConnectionID",
			SchemaName: "business_connection_id",
		},
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "ReplyTo",
			SchemaName: "reply_to",
		},
		{
			Name:       "DisableNotification",
			SchemaName: "disable_notification",
		},
		{
			Name:       "ProtectContent",
			SchemaName: "protect_content",
		},
		{
			Name:       "EffectID",
			SchemaName: "effect_id",
		},
		{
			Name:       "InputMessageContents",
			SchemaName: "input_message_contents",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SendBusinessMessageAlbumRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendBusinessMessageAlbum#70bd4e6f as nil")
	}
	b.PutID(SendBusinessMessageAlbumRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SendBusinessMessageAlbumRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendBusinessMessageAlbum#70bd4e6f as nil")
	}
	b.PutString(s.BusinessConnectionID)
	b.PutInt53(s.ChatID)
	if s.ReplyTo == nil {
		return fmt.Errorf("unable to encode sendBusinessMessageAlbum#70bd4e6f: field reply_to is nil")
	}
	if err := s.ReplyTo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode sendBusinessMessageAlbum#70bd4e6f: field reply_to: %w", err)
	}
	b.PutBool(s.DisableNotification)
	b.PutBool(s.ProtectContent)
	b.PutLong(s.EffectID)
	b.PutInt(len(s.InputMessageContents))
	for idx, v := range s.InputMessageContents {
		if v == nil {
			return fmt.Errorf("unable to encode sendBusinessMessageAlbum#70bd4e6f: 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 sendBusinessMessageAlbum#70bd4e6f: field input_message_contents element with index %d: %w", idx, err)
		}
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SendBusinessMessageAlbumRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sendBusinessMessageAlbum#70bd4e6f to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field business_connection_id: %w", err)
		}
		s.BusinessConnectionID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := DecodeInputMessageReplyTo(b)
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field reply_to: %w", err)
		}
		s.ReplyTo = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field disable_notification: %w", err)
		}
		s.DisableNotification = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field protect_content: %w", err)
		}
		s.ProtectContent = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field effect_id: %w", err)
		}
		s.EffectID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field input_message_contents: %w", err)
		}

		if headerLen > 0 {
			s.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 sendBusinessMessageAlbum#70bd4e6f: field input_message_contents: %w", err)
			}
			s.InputMessageContents = append(s.InputMessageContents, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SendBusinessMessageAlbumRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode sendBusinessMessageAlbum#70bd4e6f as nil")
	}
	b.ObjStart()
	b.PutID("sendBusinessMessageAlbum")
	b.Comma()
	b.FieldStart("business_connection_id")
	b.PutString(s.BusinessConnectionID)
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("reply_to")
	if s.ReplyTo == nil {
		return fmt.Errorf("unable to encode sendBusinessMessageAlbum#70bd4e6f: field reply_to is nil")
	}
	if err := s.ReplyTo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode sendBusinessMessageAlbum#70bd4e6f: field reply_to: %w", err)
	}
	b.Comma()
	b.FieldStart("disable_notification")
	b.PutBool(s.DisableNotification)
	b.Comma()
	b.FieldStart("protect_content")
	b.PutBool(s.ProtectContent)
	b.Comma()
	b.FieldStart("effect_id")
	b.PutLong(s.EffectID)
	b.Comma()
	b.FieldStart("input_message_contents")
	b.ArrStart()
	for idx, v := range s.InputMessageContents {
		if v == nil {
			return fmt.Errorf("unable to encode sendBusinessMessageAlbum#70bd4e6f: field input_message_contents element with index %d is nil", idx)
		}
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode sendBusinessMessageAlbum#70bd4e6f: 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 (s *SendBusinessMessageAlbumRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode sendBusinessMessageAlbum#70bd4e6f to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("sendBusinessMessageAlbum"); err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: %w", err)
			}
		case "business_connection_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field business_connection_id: %w", err)
			}
			s.BusinessConnectionID = value
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field chat_id: %w", err)
			}
			s.ChatID = value
		case "reply_to":
			value, err := DecodeTDLibJSONInputMessageReplyTo(b)
			if err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field reply_to: %w", err)
			}
			s.ReplyTo = value
		case "disable_notification":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field disable_notification: %w", err)
			}
			s.DisableNotification = value
		case "protect_content":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field protect_content: %w", err)
			}
			s.ProtectContent = value
		case "effect_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field effect_id: %w", err)
			}
			s.EffectID = 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 sendBusinessMessageAlbum#70bd4e6f: field input_message_contents: %w", err)
				}
				s.InputMessageContents = append(s.InputMessageContents, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessageAlbum#70bd4e6f: field input_message_contents: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetChatID returns value of ChatID field.
func (s *SendBusinessMessageAlbumRequest) GetChatID() (value int64) {
	if s == nil {
		return
	}
	return s.ChatID
}

// GetReplyTo returns value of ReplyTo field.
func (s *SendBusinessMessageAlbumRequest) GetReplyTo() (value InputMessageReplyToClass) {
	if s == nil {
		return
	}
	return s.ReplyTo
}

// GetDisableNotification returns value of DisableNotification field.
func (s *SendBusinessMessageAlbumRequest) GetDisableNotification() (value bool) {
	if s == nil {
		return
	}
	return s.DisableNotification
}

// GetProtectContent returns value of ProtectContent field.
func (s *SendBusinessMessageAlbumRequest) GetProtectContent() (value bool) {
	if s == nil {
		return
	}
	return s.ProtectContent
}

// GetEffectID returns value of EffectID field.
func (s *SendBusinessMessageAlbumRequest) GetEffectID() (value int64) {
	if s == nil {
		return
	}
	return s.EffectID
}

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

// SendBusinessMessageAlbum invokes method sendBusinessMessageAlbum#70bd4e6f returning error if any.
func (c *Client) SendBusinessMessageAlbum(ctx context.Context, request *SendBusinessMessageAlbumRequest) (*BusinessMessages, error) {
	var result BusinessMessages

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