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

// SendChatActionRequest represents TL type `sendChatAction#8823f29e`.
type SendChatActionRequest struct {
	// Chat identifier
	ChatID int64
	// If not 0, the message thread identifier in which the action was performed
	MessageThreadID int64
	// Unique identifier of business connection on behalf of which to send the request; for
	// bots only
	BusinessConnectionID string
	// The action description; pass null to cancel the currently active action
	Action ChatActionClass
}

// SendChatActionRequestTypeID is TL type id of SendChatActionRequest.
const SendChatActionRequestTypeID = 0x8823f29e

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

func (s *SendChatActionRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ChatID == 0) {
		return false
	}
	if !(s.MessageThreadID == 0) {
		return false
	}
	if !(s.BusinessConnectionID == "") {
		return false
	}
	if !(s.Action == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SendChatActionRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "sendChatAction",
		ID:   SendChatActionRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageThreadID",
			SchemaName: "message_thread_id",
		},
		{
			Name:       "BusinessConnectionID",
			SchemaName: "business_connection_id",
		},
		{
			Name:       "Action",
			SchemaName: "action",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SendChatActionRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendChatAction#8823f29e as nil")
	}
	b.PutID(SendChatActionRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SendChatActionRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendChatAction#8823f29e as nil")
	}
	b.PutInt53(s.ChatID)
	b.PutInt53(s.MessageThreadID)
	b.PutString(s.BusinessConnectionID)
	if s.Action == nil {
		return fmt.Errorf("unable to encode sendChatAction#8823f29e: field action is nil")
	}
	if err := s.Action.Encode(b); err != nil {
		return fmt.Errorf("unable to encode sendChatAction#8823f29e: field action: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SendChatActionRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sendChatAction#8823f29e to nil")
	}
	if err := b.ConsumeID(SendChatActionRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode sendChatAction#8823f29e: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SendChatActionRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sendChatAction#8823f29e to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode sendChatAction#8823f29e: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode sendChatAction#8823f29e: field message_thread_id: %w", err)
		}
		s.MessageThreadID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode sendChatAction#8823f29e: field business_connection_id: %w", err)
		}
		s.BusinessConnectionID = value
	}
	{
		value, err := DecodeChatAction(b)
		if err != nil {
			return fmt.Errorf("unable to decode sendChatAction#8823f29e: field action: %w", err)
		}
		s.Action = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SendChatActionRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode sendChatAction#8823f29e as nil")
	}
	b.ObjStart()
	b.PutID("sendChatAction")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("message_thread_id")
	b.PutInt53(s.MessageThreadID)
	b.Comma()
	b.FieldStart("business_connection_id")
	b.PutString(s.BusinessConnectionID)
	b.Comma()
	b.FieldStart("action")
	if s.Action == nil {
		return fmt.Errorf("unable to encode sendChatAction#8823f29e: field action is nil")
	}
	if err := s.Action.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode sendChatAction#8823f29e: field action: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SendChatActionRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode sendChatAction#8823f29e to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("sendChatAction"); err != nil {
				return fmt.Errorf("unable to decode sendChatAction#8823f29e: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode sendChatAction#8823f29e: field chat_id: %w", err)
			}
			s.ChatID = value
		case "message_thread_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode sendChatAction#8823f29e: field message_thread_id: %w", err)
			}
			s.MessageThreadID = value
		case "business_connection_id":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode sendChatAction#8823f29e: field business_connection_id: %w", err)
			}
			s.BusinessConnectionID = value
		case "action":
			value, err := DecodeTDLibJSONChatAction(b)
			if err != nil {
				return fmt.Errorf("unable to decode sendChatAction#8823f29e: field action: %w", err)
			}
			s.Action = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

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

// GetAction returns value of Action field.
func (s *SendChatActionRequest) GetAction() (value ChatActionClass) {
	if s == nil {
		return
	}
	return s.Action
}

// SendChatAction invokes method sendChatAction#8823f29e returning error if any.
func (c *Client) SendChatAction(ctx context.Context, request *SendChatActionRequest) error {
	var ok Ok

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