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

// EditMessageCaptionRequest represents TL type `editMessageCaption#87977241`.
type EditMessageCaptionRequest struct {
	// The chat the message belongs to
	ChatID int64
	// Identifier of the message. Use messageProperties.can_be_edited to check whether the
	// message can be edited
	MessageID int64
	// The new message reply markup; pass null if none; for bots only
	ReplyMarkup ReplyMarkupClass
	// New message content caption; 0-getOption("message_caption_length_max") characters;
	// pass null to remove caption
	Caption FormattedText
	// Pass true to show the caption above the media; otherwise, the caption will be shown
	// below the media. May be true only for animation, photo, and video messages
	ShowCaptionAboveMedia bool
}

// EditMessageCaptionRequestTypeID is TL type id of EditMessageCaptionRequest.
const EditMessageCaptionRequestTypeID = 0x87977241

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

func (e *EditMessageCaptionRequest) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.ChatID == 0) {
		return false
	}
	if !(e.MessageID == 0) {
		return false
	}
	if !(e.ReplyMarkup == nil) {
		return false
	}
	if !(e.Caption.Zero()) {
		return false
	}
	if !(e.ShowCaptionAboveMedia == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EditMessageCaptionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "editMessageCaption",
		ID:   EditMessageCaptionRequestTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageID",
			SchemaName: "message_id",
		},
		{
			Name:       "ReplyMarkup",
			SchemaName: "reply_markup",
		},
		{
			Name:       "Caption",
			SchemaName: "caption",
		},
		{
			Name:       "ShowCaptionAboveMedia",
			SchemaName: "show_caption_above_media",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EditMessageCaptionRequest) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode editMessageCaption#87977241 as nil")
	}
	b.PutID(EditMessageCaptionRequestTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EditMessageCaptionRequest) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode editMessageCaption#87977241 as nil")
	}
	b.PutInt53(e.ChatID)
	b.PutInt53(e.MessageID)
	if e.ReplyMarkup == nil {
		return fmt.Errorf("unable to encode editMessageCaption#87977241: field reply_markup is nil")
	}
	if err := e.ReplyMarkup.Encode(b); err != nil {
		return fmt.Errorf("unable to encode editMessageCaption#87977241: field reply_markup: %w", err)
	}
	if err := e.Caption.Encode(b); err != nil {
		return fmt.Errorf("unable to encode editMessageCaption#87977241: field caption: %w", err)
	}
	b.PutBool(e.ShowCaptionAboveMedia)
	return nil
}

// Decode implements bin.Decoder.
func (e *EditMessageCaptionRequest) Decode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode editMessageCaption#87977241 to nil")
	}
	if err := b.ConsumeID(EditMessageCaptionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode editMessageCaption#87977241: %w", err)
	}
	return e.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (e *EditMessageCaptionRequest) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode editMessageCaption#87977241 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode editMessageCaption#87977241: field chat_id: %w", err)
		}
		e.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode editMessageCaption#87977241: field message_id: %w", err)
		}
		e.MessageID = value
	}
	{
		value, err := DecodeReplyMarkup(b)
		if err != nil {
			return fmt.Errorf("unable to decode editMessageCaption#87977241: field reply_markup: %w", err)
		}
		e.ReplyMarkup = value
	}
	{
		if err := e.Caption.Decode(b); err != nil {
			return fmt.Errorf("unable to decode editMessageCaption#87977241: field caption: %w", err)
		}
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode editMessageCaption#87977241: field show_caption_above_media: %w", err)
		}
		e.ShowCaptionAboveMedia = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EditMessageCaptionRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode editMessageCaption#87977241 as nil")
	}
	b.ObjStart()
	b.PutID("editMessageCaption")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(e.ChatID)
	b.Comma()
	b.FieldStart("message_id")
	b.PutInt53(e.MessageID)
	b.Comma()
	b.FieldStart("reply_markup")
	if e.ReplyMarkup == nil {
		return fmt.Errorf("unable to encode editMessageCaption#87977241: field reply_markup is nil")
	}
	if err := e.ReplyMarkup.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode editMessageCaption#87977241: field reply_markup: %w", err)
	}
	b.Comma()
	b.FieldStart("caption")
	if err := e.Caption.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode editMessageCaption#87977241: field caption: %w", err)
	}
	b.Comma()
	b.FieldStart("show_caption_above_media")
	b.PutBool(e.ShowCaptionAboveMedia)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (e *EditMessageCaptionRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if e == nil {
		return fmt.Errorf("can't decode editMessageCaption#87977241 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("editMessageCaption"); err != nil {
				return fmt.Errorf("unable to decode editMessageCaption#87977241: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode editMessageCaption#87977241: field chat_id: %w", err)
			}
			e.ChatID = value
		case "message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode editMessageCaption#87977241: field message_id: %w", err)
			}
			e.MessageID = value
		case "reply_markup":
			value, err := DecodeTDLibJSONReplyMarkup(b)
			if err != nil {
				return fmt.Errorf("unable to decode editMessageCaption#87977241: field reply_markup: %w", err)
			}
			e.ReplyMarkup = value
		case "caption":
			if err := e.Caption.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode editMessageCaption#87977241: field caption: %w", err)
			}
		case "show_caption_above_media":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode editMessageCaption#87977241: field show_caption_above_media: %w", err)
			}
			e.ShowCaptionAboveMedia = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetMessageID returns value of MessageID field.
func (e *EditMessageCaptionRequest) GetMessageID() (value int64) {
	if e == nil {
		return
	}
	return e.MessageID
}

// GetReplyMarkup returns value of ReplyMarkup field.
func (e *EditMessageCaptionRequest) GetReplyMarkup() (value ReplyMarkupClass) {
	if e == nil {
		return
	}
	return e.ReplyMarkup
}

// GetCaption returns value of Caption field.
func (e *EditMessageCaptionRequest) GetCaption() (value FormattedText) {
	if e == nil {
		return
	}
	return e.Caption
}

// GetShowCaptionAboveMedia returns value of ShowCaptionAboveMedia field.
func (e *EditMessageCaptionRequest) GetShowCaptionAboveMedia() (value bool) {
	if e == nil {
		return
	}
	return e.ShowCaptionAboveMedia
}

// EditMessageCaption invokes method editMessageCaption#87977241 returning error if any.
func (c *Client) EditMessageCaption(ctx context.Context, request *EditMessageCaptionRequest) (*Message, error) {
	var result Message

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