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

// TranslateMessageTextRequest represents TL type `translateMessageText#53c51ed2`.
type TranslateMessageTextRequest struct {
	// Identifier of the chat to which the message belongs
	ChatID int64
	// Identifier of the message
	MessageID int64
	// Language code of the language to which the message is translated. Must be one of
	ToLanguageCode string
}

// TranslateMessageTextRequestTypeID is TL type id of TranslateMessageTextRequest.
const TranslateMessageTextRequestTypeID = 0x53c51ed2

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

func (t *TranslateMessageTextRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.ChatID == 0) {
		return false
	}
	if !(t.MessageID == 0) {
		return false
	}
	if !(t.ToLanguageCode == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *TranslateMessageTextRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "translateMessageText",
		ID:   TranslateMessageTextRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MessageID",
			SchemaName: "message_id",
		},
		{
			Name:       "ToLanguageCode",
			SchemaName: "to_language_code",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *TranslateMessageTextRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode translateMessageText#53c51ed2 as nil")
	}
	b.PutID(TranslateMessageTextRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *TranslateMessageTextRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode translateMessageText#53c51ed2 as nil")
	}
	b.PutInt53(t.ChatID)
	b.PutInt53(t.MessageID)
	b.PutString(t.ToLanguageCode)
	return nil
}

// Decode implements bin.Decoder.
func (t *TranslateMessageTextRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode translateMessageText#53c51ed2 to nil")
	}
	if err := b.ConsumeID(TranslateMessageTextRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode translateMessageText#53c51ed2: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *TranslateMessageTextRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode translateMessageText#53c51ed2 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode translateMessageText#53c51ed2: field chat_id: %w", err)
		}
		t.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode translateMessageText#53c51ed2: field message_id: %w", err)
		}
		t.MessageID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode translateMessageText#53c51ed2: field to_language_code: %w", err)
		}
		t.ToLanguageCode = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *TranslateMessageTextRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode translateMessageText#53c51ed2 as nil")
	}
	b.ObjStart()
	b.PutID("translateMessageText")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(t.ChatID)
	b.Comma()
	b.FieldStart("message_id")
	b.PutInt53(t.MessageID)
	b.Comma()
	b.FieldStart("to_language_code")
	b.PutString(t.ToLanguageCode)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *TranslateMessageTextRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode translateMessageText#53c51ed2 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("translateMessageText"); err != nil {
				return fmt.Errorf("unable to decode translateMessageText#53c51ed2: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode translateMessageText#53c51ed2: field chat_id: %w", err)
			}
			t.ChatID = value
		case "message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode translateMessageText#53c51ed2: field message_id: %w", err)
			}
			t.MessageID = value
		case "to_language_code":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode translateMessageText#53c51ed2: field to_language_code: %w", err)
			}
			t.ToLanguageCode = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

// GetToLanguageCode returns value of ToLanguageCode field.
func (t *TranslateMessageTextRequest) GetToLanguageCode() (value string) {
	if t == nil {
		return
	}
	return t.ToLanguageCode
}

// TranslateMessageText invokes method translateMessageText#53c51ed2 returning error if any.
func (c *Client) TranslateMessageText(ctx context.Context, request *TranslateMessageTextRequest) (*FormattedText, error) {
	var result FormattedText

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