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

// ForwardMessagesRequest represents TL type `forwardMessages#34de5b20`.
type ForwardMessagesRequest struct {
	// Identifier of the chat to which to forward messages
	ChatID int64
	// If not 0, the message thread identifier in which the message will be sent; for forum
	// threads only
	MessageThreadID int64
	// Identifier of the chat from which to forward messages
	FromChatID int64
	// Identifiers of the messages to forward. Message identifiers must be in a strictly
	// increasing order. At most 100 messages can be forwarded simultaneously. A message can
	// be forwarded only if messageProperties.can_be_forwarded
	MessageIDs []int64
	// Options to be used to send the messages; pass null to use default options
	Options MessageSendOptions
	// Pass true to copy content of the messages without reference to the original sender.
	// Always true if the messages are forwarded to a secret chat or are local.
	SendCopy bool
	// Pass true to remove media captions of message copies. Ignored if send_copy is false
	RemoveCaption bool
}

// ForwardMessagesRequestTypeID is TL type id of ForwardMessagesRequest.
const ForwardMessagesRequestTypeID = 0x34de5b20

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

func (f *ForwardMessagesRequest) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.ChatID == 0) {
		return false
	}
	if !(f.MessageThreadID == 0) {
		return false
	}
	if !(f.FromChatID == 0) {
		return false
	}
	if !(f.MessageIDs == nil) {
		return false
	}
	if !(f.Options.Zero()) {
		return false
	}
	if !(f.SendCopy == false) {
		return false
	}
	if !(f.RemoveCaption == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *ForwardMessagesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "forwardMessages",
		ID:   ForwardMessagesRequestTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageThreadID",
			SchemaName: "message_thread_id",
		},
		{
			Name:       "FromChatID",
			SchemaName: "from_chat_id",
		},
		{
			Name:       "MessageIDs",
			SchemaName: "message_ids",
		},
		{
			Name:       "Options",
			SchemaName: "options",
		},
		{
			Name:       "SendCopy",
			SchemaName: "send_copy",
		},
		{
			Name:       "RemoveCaption",
			SchemaName: "remove_caption",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *ForwardMessagesRequest) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode forwardMessages#34de5b20 as nil")
	}
	b.PutID(ForwardMessagesRequestTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *ForwardMessagesRequest) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode forwardMessages#34de5b20 as nil")
	}
	b.PutInt53(f.ChatID)
	b.PutInt53(f.MessageThreadID)
	b.PutInt53(f.FromChatID)
	b.PutInt(len(f.MessageIDs))
	for _, v := range f.MessageIDs {
		b.PutInt53(v)
	}
	if err := f.Options.Encode(b); err != nil {
		return fmt.Errorf("unable to encode forwardMessages#34de5b20: field options: %w", err)
	}
	b.PutBool(f.SendCopy)
	b.PutBool(f.RemoveCaption)
	return nil
}

// Decode implements bin.Decoder.
func (f *ForwardMessagesRequest) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode forwardMessages#34de5b20 to nil")
	}
	if err := b.ConsumeID(ForwardMessagesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode forwardMessages#34de5b20: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *ForwardMessagesRequest) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode forwardMessages#34de5b20 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode forwardMessages#34de5b20: field chat_id: %w", err)
		}
		f.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode forwardMessages#34de5b20: field message_thread_id: %w", err)
		}
		f.MessageThreadID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode forwardMessages#34de5b20: field from_chat_id: %w", err)
		}
		f.FromChatID = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode forwardMessages#34de5b20: field message_ids: %w", err)
		}

		if headerLen > 0 {
			f.MessageIDs = make([]int64, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode forwardMessages#34de5b20: field message_ids: %w", err)
			}
			f.MessageIDs = append(f.MessageIDs, value)
		}
	}
	{
		if err := f.Options.Decode(b); err != nil {
			return fmt.Errorf("unable to decode forwardMessages#34de5b20: field options: %w", err)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode forwardMessages#34de5b20: field send_copy: %w", err)
		}
		f.SendCopy = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode forwardMessages#34de5b20: field remove_caption: %w", err)
		}
		f.RemoveCaption = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (f *ForwardMessagesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if f == nil {
		return fmt.Errorf("can't encode forwardMessages#34de5b20 as nil")
	}
	b.ObjStart()
	b.PutID("forwardMessages")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(f.ChatID)
	b.Comma()
	b.FieldStart("message_thread_id")
	b.PutInt53(f.MessageThreadID)
	b.Comma()
	b.FieldStart("from_chat_id")
	b.PutInt53(f.FromChatID)
	b.Comma()
	b.FieldStart("message_ids")
	b.ArrStart()
	for _, v := range f.MessageIDs {
		b.PutInt53(v)
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("options")
	if err := f.Options.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode forwardMessages#34de5b20: field options: %w", err)
	}
	b.Comma()
	b.FieldStart("send_copy")
	b.PutBool(f.SendCopy)
	b.Comma()
	b.FieldStart("remove_caption")
	b.PutBool(f.RemoveCaption)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (f *ForwardMessagesRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if f == nil {
		return fmt.Errorf("can't decode forwardMessages#34de5b20 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("forwardMessages"); err != nil {
				return fmt.Errorf("unable to decode forwardMessages#34de5b20: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode forwardMessages#34de5b20: field chat_id: %w", err)
			}
			f.ChatID = value
		case "message_thread_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode forwardMessages#34de5b20: field message_thread_id: %w", err)
			}
			f.MessageThreadID = value
		case "from_chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode forwardMessages#34de5b20: field from_chat_id: %w", err)
			}
			f.FromChatID = value
		case "message_ids":
			if err := b.Arr(func(b tdjson.Decoder) error {
				value, err := b.Int53()
				if err != nil {
					return fmt.Errorf("unable to decode forwardMessages#34de5b20: field message_ids: %w", err)
				}
				f.MessageIDs = append(f.MessageIDs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode forwardMessages#34de5b20: field message_ids: %w", err)
			}
		case "options":
			if err := f.Options.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode forwardMessages#34de5b20: field options: %w", err)
			}
		case "send_copy":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode forwardMessages#34de5b20: field send_copy: %w", err)
			}
			f.SendCopy = value
		case "remove_caption":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode forwardMessages#34de5b20: field remove_caption: %w", err)
			}
			f.RemoveCaption = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetMessageThreadID returns value of MessageThreadID field.
func (f *ForwardMessagesRequest) GetMessageThreadID() (value int64) {
	if f == nil {
		return
	}
	return f.MessageThreadID
}

// GetFromChatID returns value of FromChatID field.
func (f *ForwardMessagesRequest) GetFromChatID() (value int64) {
	if f == nil {
		return
	}
	return f.FromChatID
}

// GetMessageIDs returns value of MessageIDs field.
func (f *ForwardMessagesRequest) GetMessageIDs() (value []int64) {
	if f == nil {
		return
	}
	return f.MessageIDs
}

// GetOptions returns value of Options field.
func (f *ForwardMessagesRequest) GetOptions() (value MessageSendOptions) {
	if f == nil {
		return
	}
	return f.Options
}

// GetSendCopy returns value of SendCopy field.
func (f *ForwardMessagesRequest) GetSendCopy() (value bool) {
	if f == nil {
		return
	}
	return f.SendCopy
}

// GetRemoveCaption returns value of RemoveCaption field.
func (f *ForwardMessagesRequest) GetRemoveCaption() (value bool) {
	if f == nil {
		return
	}
	return f.RemoveCaption
}

// ForwardMessages invokes method forwardMessages#34de5b20 returning error if any.
func (c *Client) ForwardMessages(ctx context.Context, request *ForwardMessagesRequest) (*Messages, error) {
	var result Messages

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