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

// ParseTextEntitiesRequest represents TL type `parseTextEntities#9a1fc29f`.
type ParseTextEntitiesRequest struct {
	// The text to parse
	Text string
	// Text parse mode
	ParseMode TextParseModeClass
}

// ParseTextEntitiesRequestTypeID is TL type id of ParseTextEntitiesRequest.
const ParseTextEntitiesRequestTypeID = 0x9a1fc29f

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

func (p *ParseTextEntitiesRequest) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Text == "") {
		return false
	}
	if !(p.ParseMode == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (p *ParseTextEntitiesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "parseTextEntities",
		ID:   ParseTextEntitiesRequestTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Text",
			SchemaName: "text",
		},
		{
			Name:       "ParseMode",
			SchemaName: "parse_mode",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *ParseTextEntitiesRequest) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode parseTextEntities#9a1fc29f as nil")
	}
	b.PutID(ParseTextEntitiesRequestTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *ParseTextEntitiesRequest) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode parseTextEntities#9a1fc29f as nil")
	}
	b.PutString(p.Text)
	if p.ParseMode == nil {
		return fmt.Errorf("unable to encode parseTextEntities#9a1fc29f: field parse_mode is nil")
	}
	if err := p.ParseMode.Encode(b); err != nil {
		return fmt.Errorf("unable to encode parseTextEntities#9a1fc29f: field parse_mode: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *ParseTextEntitiesRequest) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode parseTextEntities#9a1fc29f to nil")
	}
	if err := b.ConsumeID(ParseTextEntitiesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode parseTextEntities#9a1fc29f: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *ParseTextEntitiesRequest) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode parseTextEntities#9a1fc29f to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode parseTextEntities#9a1fc29f: field text: %w", err)
		}
		p.Text = value
	}
	{
		value, err := DecodeTextParseMode(b)
		if err != nil {
			return fmt.Errorf("unable to decode parseTextEntities#9a1fc29f: field parse_mode: %w", err)
		}
		p.ParseMode = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (p *ParseTextEntitiesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if p == nil {
		return fmt.Errorf("can't encode parseTextEntities#9a1fc29f as nil")
	}
	b.ObjStart()
	b.PutID("parseTextEntities")
	b.Comma()
	b.FieldStart("text")
	b.PutString(p.Text)
	b.Comma()
	b.FieldStart("parse_mode")
	if p.ParseMode == nil {
		return fmt.Errorf("unable to encode parseTextEntities#9a1fc29f: field parse_mode is nil")
	}
	if err := p.ParseMode.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode parseTextEntities#9a1fc29f: field parse_mode: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (p *ParseTextEntitiesRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if p == nil {
		return fmt.Errorf("can't decode parseTextEntities#9a1fc29f to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("parseTextEntities"); err != nil {
				return fmt.Errorf("unable to decode parseTextEntities#9a1fc29f: %w", err)
			}
		case "text":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode parseTextEntities#9a1fc29f: field text: %w", err)
			}
			p.Text = value
		case "parse_mode":
			value, err := DecodeTDLibJSONTextParseMode(b)
			if err != nil {
				return fmt.Errorf("unable to decode parseTextEntities#9a1fc29f: field parse_mode: %w", err)
			}
			p.ParseMode = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetText returns value of Text field.
func (p *ParseTextEntitiesRequest) GetText() (value string) {
	if p == nil {
		return
	}
	return p.Text
}

// GetParseMode returns value of ParseMode field.
func (p *ParseTextEntitiesRequest) GetParseMode() (value TextParseModeClass) {
	if p == nil {
		return
	}
	return p.ParseMode
}

// ParseTextEntities invokes method parseTextEntities#9a1fc29f returning error if any.
func (c *Client) ParseTextEntities(ctx context.Context, request *ParseTextEntitiesRequest) (*FormattedText, error) {
	var result FormattedText

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