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

// SetDirectMessagesChatTopicDraftMessageRequest represents TL type `setDirectMessagesChatTopicDraftMessage#23fabd3a`.
type SetDirectMessagesChatTopicDraftMessageRequest struct {
	// Chat identifier
	ChatID int64
	// Topic identifier
	TopicID int64
	// New draft message; pass null to remove the draft. All files in draft message content
	// must be of the type inputFileLocal. Media thumbnails and captions are ignored
	DraftMessage DraftMessage
}

// SetDirectMessagesChatTopicDraftMessageRequestTypeID is TL type id of SetDirectMessagesChatTopicDraftMessageRequest.
const SetDirectMessagesChatTopicDraftMessageRequestTypeID = 0x23fabd3a

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

func (s *SetDirectMessagesChatTopicDraftMessageRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ChatID == 0) {
		return false
	}
	if !(s.TopicID == 0) {
		return false
	}
	if !(s.DraftMessage.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetDirectMessagesChatTopicDraftMessageRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setDirectMessagesChatTopicDraftMessage",
		ID:   SetDirectMessagesChatTopicDraftMessageRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "TopicID",
			SchemaName: "topic_id",
		},
		{
			Name:       "DraftMessage",
			SchemaName: "draft_message",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetDirectMessagesChatTopicDraftMessageRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setDirectMessagesChatTopicDraftMessage#23fabd3a as nil")
	}
	b.PutID(SetDirectMessagesChatTopicDraftMessageRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetDirectMessagesChatTopicDraftMessageRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setDirectMessagesChatTopicDraftMessage#23fabd3a as nil")
	}
	b.PutInt53(s.ChatID)
	b.PutInt53(s.TopicID)
	if err := s.DraftMessage.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setDirectMessagesChatTopicDraftMessage#23fabd3a: field draft_message: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetDirectMessagesChatTopicDraftMessageRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setDirectMessagesChatTopicDraftMessage#23fabd3a to nil")
	}
	if err := b.ConsumeID(SetDirectMessagesChatTopicDraftMessageRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setDirectMessagesChatTopicDraftMessage#23fabd3a: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetDirectMessagesChatTopicDraftMessageRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setDirectMessagesChatTopicDraftMessage#23fabd3a to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setDirectMessagesChatTopicDraftMessage#23fabd3a: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setDirectMessagesChatTopicDraftMessage#23fabd3a: field topic_id: %w", err)
		}
		s.TopicID = value
	}
	{
		if err := s.DraftMessage.Decode(b); err != nil {
			return fmt.Errorf("unable to decode setDirectMessagesChatTopicDraftMessage#23fabd3a: field draft_message: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetDirectMessagesChatTopicDraftMessageRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setDirectMessagesChatTopicDraftMessage#23fabd3a as nil")
	}
	b.ObjStart()
	b.PutID("setDirectMessagesChatTopicDraftMessage")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("topic_id")
	b.PutInt53(s.TopicID)
	b.Comma()
	b.FieldStart("draft_message")
	if err := s.DraftMessage.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setDirectMessagesChatTopicDraftMessage#23fabd3a: field draft_message: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetDirectMessagesChatTopicDraftMessageRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setDirectMessagesChatTopicDraftMessage#23fabd3a to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setDirectMessagesChatTopicDraftMessage"); err != nil {
				return fmt.Errorf("unable to decode setDirectMessagesChatTopicDraftMessage#23fabd3a: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setDirectMessagesChatTopicDraftMessage#23fabd3a: field chat_id: %w", err)
			}
			s.ChatID = value
		case "topic_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setDirectMessagesChatTopicDraftMessage#23fabd3a: field topic_id: %w", err)
			}
			s.TopicID = value
		case "draft_message":
			if err := s.DraftMessage.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode setDirectMessagesChatTopicDraftMessage#23fabd3a: field draft_message: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetTopicID returns value of TopicID field.
func (s *SetDirectMessagesChatTopicDraftMessageRequest) GetTopicID() (value int64) {
	if s == nil {
		return
	}
	return s.TopicID
}

// GetDraftMessage returns value of DraftMessage field.
func (s *SetDirectMessagesChatTopicDraftMessageRequest) GetDraftMessage() (value DraftMessage) {
	if s == nil {
		return
	}
	return s.DraftMessage
}

// SetDirectMessagesChatTopicDraftMessage invokes method setDirectMessagesChatTopicDraftMessage#23fabd3a returning error if any.
func (c *Client) SetDirectMessagesChatTopicDraftMessage(ctx context.Context, request *SetDirectMessagesChatTopicDraftMessageRequest) error {
	var ok Ok

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