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

// GetLinkPreviewRequest represents TL type `getLinkPreview#c2096321`.
type GetLinkPreviewRequest struct {
	// Message text with formatting
	Text FormattedText
	// Options to be used for generation of the link preview; pass null to use default link
	// preview options
	LinkPreviewOptions LinkPreviewOptions
}

// GetLinkPreviewRequestTypeID is TL type id of GetLinkPreviewRequest.
const GetLinkPreviewRequestTypeID = 0xc2096321

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

func (g *GetLinkPreviewRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Text.Zero()) {
		return false
	}
	if !(g.LinkPreviewOptions.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetLinkPreviewRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getLinkPreview",
		ID:   GetLinkPreviewRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Text",
			SchemaName: "text",
		},
		{
			Name:       "LinkPreviewOptions",
			SchemaName: "link_preview_options",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetLinkPreviewRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getLinkPreview#c2096321 as nil")
	}
	b.PutID(GetLinkPreviewRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetLinkPreviewRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getLinkPreview#c2096321 as nil")
	}
	if err := g.Text.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getLinkPreview#c2096321: field text: %w", err)
	}
	if err := g.LinkPreviewOptions.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getLinkPreview#c2096321: field link_preview_options: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *GetLinkPreviewRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getLinkPreview#c2096321 to nil")
	}
	if err := b.ConsumeID(GetLinkPreviewRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getLinkPreview#c2096321: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetLinkPreviewRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getLinkPreview#c2096321 to nil")
	}
	{
		if err := g.Text.Decode(b); err != nil {
			return fmt.Errorf("unable to decode getLinkPreview#c2096321: field text: %w", err)
		}
	}
	{
		if err := g.LinkPreviewOptions.Decode(b); err != nil {
			return fmt.Errorf("unable to decode getLinkPreview#c2096321: field link_preview_options: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetLinkPreviewRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getLinkPreview#c2096321 as nil")
	}
	b.ObjStart()
	b.PutID("getLinkPreview")
	b.Comma()
	b.FieldStart("text")
	if err := g.Text.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getLinkPreview#c2096321: field text: %w", err)
	}
	b.Comma()
	b.FieldStart("link_preview_options")
	if err := g.LinkPreviewOptions.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getLinkPreview#c2096321: field link_preview_options: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetLinkPreviewRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getLinkPreview#c2096321 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getLinkPreview"); err != nil {
				return fmt.Errorf("unable to decode getLinkPreview#c2096321: %w", err)
			}
		case "text":
			if err := g.Text.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode getLinkPreview#c2096321: field text: %w", err)
			}
		case "link_preview_options":
			if err := g.LinkPreviewOptions.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode getLinkPreview#c2096321: field link_preview_options: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetLinkPreviewOptions returns value of LinkPreviewOptions field.
func (g *GetLinkPreviewRequest) GetLinkPreviewOptions() (value LinkPreviewOptions) {
	if g == nil {
		return
	}
	return g.LinkPreviewOptions
}

// GetLinkPreview invokes method getLinkPreview#c2096321 returning error if any.
func (c *Client) GetLinkPreview(ctx context.Context, request *GetLinkPreviewRequest) (*LinkPreview, error) {
	var result LinkPreview

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