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

// MessageCopyOptions represents TL type `messageCopyOptions#405c03ba`.
type MessageCopyOptions struct {
	// True, if content of the message needs to be copied without reference to the original
	// sender. Always true if the message is forwarded to a secret chat or is local.
	SendCopy bool
	// True, if media caption of the message copy needs to be replaced. Ignored if send_copy
	// is false
	ReplaceCaption bool
	// New message caption; pass null to copy message without caption. Ignored if
	// replace_caption is false
	NewCaption FormattedText
	// True, if new caption must be shown above the media; otherwise, new caption must be
	// shown below the media; not supported in secret chats. Ignored if replace_caption is
	// false
	NewShowCaptionAboveMedia bool
}

// MessageCopyOptionsTypeID is TL type id of MessageCopyOptions.
const MessageCopyOptionsTypeID = 0x405c03ba

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

func (m *MessageCopyOptions) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.SendCopy == false) {
		return false
	}
	if !(m.ReplaceCaption == false) {
		return false
	}
	if !(m.NewCaption.Zero()) {
		return false
	}
	if !(m.NewShowCaptionAboveMedia == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageCopyOptions) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageCopyOptions",
		ID:   MessageCopyOptionsTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SendCopy",
			SchemaName: "send_copy",
		},
		{
			Name:       "ReplaceCaption",
			SchemaName: "replace_caption",
		},
		{
			Name:       "NewCaption",
			SchemaName: "new_caption",
		},
		{
			Name:       "NewShowCaptionAboveMedia",
			SchemaName: "new_show_caption_above_media",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageCopyOptions) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageCopyOptions#405c03ba as nil")
	}
	b.PutID(MessageCopyOptionsTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageCopyOptions) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageCopyOptions#405c03ba as nil")
	}
	b.PutBool(m.SendCopy)
	b.PutBool(m.ReplaceCaption)
	if err := m.NewCaption.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messageCopyOptions#405c03ba: field new_caption: %w", err)
	}
	b.PutBool(m.NewShowCaptionAboveMedia)
	return nil
}

// Decode implements bin.Decoder.
func (m *MessageCopyOptions) Decode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageCopyOptions#405c03ba to nil")
	}
	if err := b.ConsumeID(MessageCopyOptionsTypeID); err != nil {
		return fmt.Errorf("unable to decode messageCopyOptions#405c03ba: %w", err)
	}
	return m.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (m *MessageCopyOptions) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageCopyOptions#405c03ba to nil")
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode messageCopyOptions#405c03ba: field send_copy: %w", err)
		}
		m.SendCopy = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode messageCopyOptions#405c03ba: field replace_caption: %w", err)
		}
		m.ReplaceCaption = value
	}
	{
		if err := m.NewCaption.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messageCopyOptions#405c03ba: field new_caption: %w", err)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode messageCopyOptions#405c03ba: field new_show_caption_above_media: %w", err)
		}
		m.NewShowCaptionAboveMedia = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageCopyOptions) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageCopyOptions#405c03ba as nil")
	}
	b.ObjStart()
	b.PutID("messageCopyOptions")
	b.Comma()
	b.FieldStart("send_copy")
	b.PutBool(m.SendCopy)
	b.Comma()
	b.FieldStart("replace_caption")
	b.PutBool(m.ReplaceCaption)
	b.Comma()
	b.FieldStart("new_caption")
	if err := m.NewCaption.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode messageCopyOptions#405c03ba: field new_caption: %w", err)
	}
	b.Comma()
	b.FieldStart("new_show_caption_above_media")
	b.PutBool(m.NewShowCaptionAboveMedia)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (m *MessageCopyOptions) DecodeTDLibJSON(b tdjson.Decoder) error {
	if m == nil {
		return fmt.Errorf("can't decode messageCopyOptions#405c03ba to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageCopyOptions"); err != nil {
				return fmt.Errorf("unable to decode messageCopyOptions#405c03ba: %w", err)
			}
		case "send_copy":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode messageCopyOptions#405c03ba: field send_copy: %w", err)
			}
			m.SendCopy = value
		case "replace_caption":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode messageCopyOptions#405c03ba: field replace_caption: %w", err)
			}
			m.ReplaceCaption = value
		case "new_caption":
			if err := m.NewCaption.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode messageCopyOptions#405c03ba: field new_caption: %w", err)
			}
		case "new_show_caption_above_media":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode messageCopyOptions#405c03ba: field new_show_caption_above_media: %w", err)
			}
			m.NewShowCaptionAboveMedia = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetReplaceCaption returns value of ReplaceCaption field.
func (m *MessageCopyOptions) GetReplaceCaption() (value bool) {
	if m == nil {
		return
	}
	return m.ReplaceCaption
}

// GetNewCaption returns value of NewCaption field.
func (m *MessageCopyOptions) GetNewCaption() (value FormattedText) {
	if m == nil {
		return
	}
	return m.NewCaption
}

// GetNewShowCaptionAboveMedia returns value of NewShowCaptionAboveMedia field.
func (m *MessageCopyOptions) GetNewShowCaptionAboveMedia() (value bool) {
	if m == nil {
		return
	}
	return m.NewShowCaptionAboveMedia
}
