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

// TranslateTextRequest represents TL type `translateText#252264f2`.
type TranslateTextRequest struct {
	// Text to translate
	Text FormattedText
	// Language code of the language to which the message is translated. Must be one of
	ToLanguageCode string
}

// TranslateTextRequestTypeID is TL type id of TranslateTextRequest.
const TranslateTextRequestTypeID = 0x252264f2

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

func (t *TranslateTextRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Text.Zero()) {
		return false
	}
	if !(t.ToLanguageCode == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *TranslateTextRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "translateText",
		ID:   TranslateTextRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Text",
			SchemaName: "text",
		},
		{
			Name:       "ToLanguageCode",
			SchemaName: "to_language_code",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *TranslateTextRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode translateText#252264f2 as nil")
	}
	b.PutID(TranslateTextRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *TranslateTextRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode translateText#252264f2 as nil")
	}
	if err := t.Text.Encode(b); err != nil {
		return fmt.Errorf("unable to encode translateText#252264f2: field text: %w", err)
	}
	b.PutString(t.ToLanguageCode)
	return nil
}

// Decode implements bin.Decoder.
func (t *TranslateTextRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode translateText#252264f2 to nil")
	}
	if err := b.ConsumeID(TranslateTextRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode translateText#252264f2: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *TranslateTextRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode translateText#252264f2 to nil")
	}
	{
		if err := t.Text.Decode(b); err != nil {
			return fmt.Errorf("unable to decode translateText#252264f2: field text: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode translateText#252264f2: field to_language_code: %w", err)
		}
		t.ToLanguageCode = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *TranslateTextRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode translateText#252264f2 as nil")
	}
	b.ObjStart()
	b.PutID("translateText")
	b.Comma()
	b.FieldStart("text")
	if err := t.Text.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode translateText#252264f2: field text: %w", err)
	}
	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 *TranslateTextRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode translateText#252264f2 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("translateText"); err != nil {
				return fmt.Errorf("unable to decode translateText#252264f2: %w", err)
			}
		case "text":
			if err := t.Text.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode translateText#252264f2: field text: %w", err)
			}
		case "to_language_code":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode translateText#252264f2: field to_language_code: %w", err)
			}
			t.ToLanguageCode = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetText returns value of Text field.
func (t *TranslateTextRequest) GetText() (value FormattedText) {
	if t == nil {
		return
	}
	return t.Text
}

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

// TranslateText invokes method translateText#252264f2 returning error if any.
func (c *Client) TranslateText(ctx context.Context, request *TranslateTextRequest) (*FormattedText, error) {
	var result FormattedText

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