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

// EditBusinessMessageMediaRequest represents TL type `editBusinessMessageMedia#fc614778`.
type EditBusinessMessageMediaRequest struct {
	// Unique identifier of business connection on behalf of which the message was sent
	BusinessConnectionID string
	// The chat the message belongs to
	ChatID int64
	// Identifier of the message
	MessageID int64
	// The new message reply markup; pass null if none; for bots only
	ReplyMarkup ReplyMarkupClass
	// New content of the message. Must be one of the following types: inputMessageAnimation,
	// inputMessageAudio, inputMessageDocument, inputMessagePhoto or inputMessageVideo
	InputMessageContent InputMessageContentClass
}

// EditBusinessMessageMediaRequestTypeID is TL type id of EditBusinessMessageMediaRequest.
const EditBusinessMessageMediaRequestTypeID = 0xfc614778

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

func (e *EditBusinessMessageMediaRequest) Zero() bool {
	if e == nil {
		return true
	}
	if !(e.BusinessConnectionID == "") {
		return false
	}
	if !(e.ChatID == 0) {
		return false
	}
	if !(e.MessageID == 0) {
		return false
	}
	if !(e.ReplyMarkup == nil) {
		return false
	}
	if !(e.InputMessageContent == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (e *EditBusinessMessageMediaRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "editBusinessMessageMedia",
		ID:   EditBusinessMessageMediaRequestTypeID,
	}
	if e == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BusinessConnectionID",
			SchemaName: "business_connection_id",
		},
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageID",
			SchemaName: "message_id",
		},
		{
			Name:       "ReplyMarkup",
			SchemaName: "reply_markup",
		},
		{
			Name:       "InputMessageContent",
			SchemaName: "input_message_content",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (e *EditBusinessMessageMediaRequest) Encode(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode editBusinessMessageMedia#fc614778 as nil")
	}
	b.PutID(EditBusinessMessageMediaRequestTypeID)
	return e.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (e *EditBusinessMessageMediaRequest) EncodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't encode editBusinessMessageMedia#fc614778 as nil")
	}
	b.PutString(e.BusinessConnectionID)
	b.PutInt53(e.ChatID)
	b.PutInt53(e.MessageID)
	if e.ReplyMarkup == nil {
		return fmt.Errorf("unable to encode editBusinessMessageMedia#fc614778: field reply_markup is nil")
	}
	if err := e.ReplyMarkup.Encode(b); err != nil {
		return fmt.Errorf("unable to encode editBusinessMessageMedia#fc614778: field reply_markup: %w", err)
	}
	if e.InputMessageContent == nil {
		return fmt.Errorf("unable to encode editBusinessMessageMedia#fc614778: field input_message_content is nil")
	}
	if err := e.InputMessageContent.Encode(b); err != nil {
		return fmt.Errorf("unable to encode editBusinessMessageMedia#fc614778: field input_message_content: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (e *EditBusinessMessageMediaRequest) DecodeBare(b *bin.Buffer) error {
	if e == nil {
		return fmt.Errorf("can't decode editBusinessMessageMedia#fc614778 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode editBusinessMessageMedia#fc614778: field business_connection_id: %w", err)
		}
		e.BusinessConnectionID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode editBusinessMessageMedia#fc614778: field chat_id: %w", err)
		}
		e.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode editBusinessMessageMedia#fc614778: field message_id: %w", err)
		}
		e.MessageID = value
	}
	{
		value, err := DecodeReplyMarkup(b)
		if err != nil {
			return fmt.Errorf("unable to decode editBusinessMessageMedia#fc614778: field reply_markup: %w", err)
		}
		e.ReplyMarkup = value
	}
	{
		value, err := DecodeInputMessageContent(b)
		if err != nil {
			return fmt.Errorf("unable to decode editBusinessMessageMedia#fc614778: field input_message_content: %w", err)
		}
		e.InputMessageContent = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (e *EditBusinessMessageMediaRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if e == nil {
		return fmt.Errorf("can't encode editBusinessMessageMedia#fc614778 as nil")
	}
	b.ObjStart()
	b.PutID("editBusinessMessageMedia")
	b.Comma()
	b.FieldStart("business_connection_id")
	b.PutString(e.BusinessConnectionID)
	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 editBusinessMessageMedia#fc614778: field reply_markup is nil")
	}
	if err := e.ReplyMarkup.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode editBusinessMessageMedia#fc614778: field reply_markup: %w", err)
	}
	b.Comma()
	b.FieldStart("input_message_content")
	if e.InputMessageContent == nil {
		return fmt.Errorf("unable to encode editBusinessMessageMedia#fc614778: field input_message_content is nil")
	}
	if err := e.InputMessageContent.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode editBusinessMessageMedia#fc614778: field input_message_content: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("editBusinessMessageMedia"); err != nil {
				return fmt.Errorf("unable to decode editBusinessMessageMedia#fc614778: %w", err)
			}
		case "business_connection_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode editBusinessMessageMedia#fc614778: field business_connection_id: %w", err)
			}
			e.BusinessConnectionID = value
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode editBusinessMessageMedia#fc614778: field chat_id: %w", err)
			}
			e.ChatID = value
		case "message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode editBusinessMessageMedia#fc614778: field message_id: %w", err)
			}
			e.MessageID = value
		case "reply_markup":
			value, err := DecodeTDLibJSONReplyMarkup(b)
			if err != nil {
				return fmt.Errorf("unable to decode editBusinessMessageMedia#fc614778: field reply_markup: %w", err)
			}
			e.ReplyMarkup = value
		case "input_message_content":
			value, err := DecodeTDLibJSONInputMessageContent(b)
			if err != nil {
				return fmt.Errorf("unable to decode editBusinessMessageMedia#fc614778: field input_message_content: %w", err)
			}
			e.InputMessageContent = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

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

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

// GetInputMessageContent returns value of InputMessageContent field.
func (e *EditBusinessMessageMediaRequest) GetInputMessageContent() (value InputMessageContentClass) {
	if e == nil {
		return
	}
	return e.InputMessageContent
}

// EditBusinessMessageMedia invokes method editBusinessMessageMedia#fc614778 returning error if any.
func (c *Client) EditBusinessMessageMedia(ctx context.Context, request *EditBusinessMessageMediaRequest) (*BusinessMessage, error) {
	var result BusinessMessage

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