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

// SendBusinessMessageRequest represents TL type `sendBusinessMessage#987b403`.
type SendBusinessMessageRequest 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
	// Markup for replying to the message; pass null if none
	ReplyMarkup ReplyMarkupClass
	// The content of the message to be sent
	InputMessageContent InputMessageContentClass
}

// SendBusinessMessageRequestTypeID is TL type id of SendBusinessMessageRequest.
const SendBusinessMessageRequestTypeID = 0x987b403

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

func (s *SendBusinessMessageRequest) 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.ReplyMarkup == nil) {
		return false
	}
	if !(s.InputMessageContent == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SendBusinessMessageRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "sendBusinessMessage",
		ID:   SendBusinessMessageRequestTypeID,
	}
	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:       "ReplyMarkup",
			SchemaName: "reply_markup",
		},
		{
			Name:       "InputMessageContent",
			SchemaName: "input_message_content",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SendBusinessMessageRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendBusinessMessage#987b403 as nil")
	}
	b.PutID(SendBusinessMessageRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SendBusinessMessageRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendBusinessMessage#987b403 as nil")
	}
	b.PutString(s.BusinessConnectionID)
	b.PutInt53(s.ChatID)
	if s.ReplyTo == nil {
		return fmt.Errorf("unable to encode sendBusinessMessage#987b403: field reply_to is nil")
	}
	if err := s.ReplyTo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode sendBusinessMessage#987b403: field reply_to: %w", err)
	}
	b.PutBool(s.DisableNotification)
	b.PutBool(s.ProtectContent)
	b.PutLong(s.EffectID)
	if s.ReplyMarkup == nil {
		return fmt.Errorf("unable to encode sendBusinessMessage#987b403: field reply_markup is nil")
	}
	if err := s.ReplyMarkup.Encode(b); err != nil {
		return fmt.Errorf("unable to encode sendBusinessMessage#987b403: field reply_markup: %w", err)
	}
	if s.InputMessageContent == nil {
		return fmt.Errorf("unable to encode sendBusinessMessage#987b403: field input_message_content is nil")
	}
	if err := s.InputMessageContent.Encode(b); err != nil {
		return fmt.Errorf("unable to encode sendBusinessMessage#987b403: field input_message_content: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SendBusinessMessageRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sendBusinessMessage#987b403 to nil")
	}
	if err := b.ConsumeID(SendBusinessMessageRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode sendBusinessMessage#987b403: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SendBusinessMessageRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sendBusinessMessage#987b403 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field business_connection_id: %w", err)
		}
		s.BusinessConnectionID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := DecodeInputMessageReplyTo(b)
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field reply_to: %w", err)
		}
		s.ReplyTo = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field disable_notification: %w", err)
		}
		s.DisableNotification = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field protect_content: %w", err)
		}
		s.ProtectContent = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field effect_id: %w", err)
		}
		s.EffectID = value
	}
	{
		value, err := DecodeReplyMarkup(b)
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field reply_markup: %w", err)
		}
		s.ReplyMarkup = value
	}
	{
		value, err := DecodeInputMessageContent(b)
		if err != nil {
			return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field input_message_content: %w", err)
		}
		s.InputMessageContent = value
	}
	return nil
}

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SendBusinessMessageRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode sendBusinessMessage#987b403 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("sendBusinessMessage"); err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessage#987b403: %w", err)
			}
		case "business_connection_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessage#987b403: 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 sendBusinessMessage#987b403: field chat_id: %w", err)
			}
			s.ChatID = value
		case "reply_to":
			value, err := DecodeTDLibJSONInputMessageReplyTo(b)
			if err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field reply_to: %w", err)
			}
			s.ReplyTo = value
		case "disable_notification":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field disable_notification: %w", err)
			}
			s.DisableNotification = value
		case "protect_content":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field protect_content: %w", err)
			}
			s.ProtectContent = value
		case "effect_id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field effect_id: %w", err)
			}
			s.EffectID = value
		case "reply_markup":
			value, err := DecodeTDLibJSONReplyMarkup(b)
			if err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field reply_markup: %w", err)
			}
			s.ReplyMarkup = value
		case "input_message_content":
			value, err := DecodeTDLibJSONInputMessageContent(b)
			if err != nil {
				return fmt.Errorf("unable to decode sendBusinessMessage#987b403: field input_message_content: %w", err)
			}
			s.InputMessageContent = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

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

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

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

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

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

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

// SendBusinessMessage invokes method sendBusinessMessage#987b403 returning error if any.
func (c *Client) SendBusinessMessage(ctx context.Context, request *SendBusinessMessageRequest) (*BusinessMessage, error) {
	var result BusinessMessage

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