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

// GetExternalLinkInfoRequest represents TL type `getExternalLinkInfo#460d7a3f`.
type GetExternalLinkInfoRequest struct {
	// The link
	Link string
}

// GetExternalLinkInfoRequestTypeID is TL type id of GetExternalLinkInfoRequest.
const GetExternalLinkInfoRequestTypeID = 0x460d7a3f

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

func (g *GetExternalLinkInfoRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Link == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetExternalLinkInfoRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getExternalLinkInfo",
		ID:   GetExternalLinkInfoRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Link",
			SchemaName: "link",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetExternalLinkInfoRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getExternalLinkInfo#460d7a3f as nil")
	}
	b.PutID(GetExternalLinkInfoRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetExternalLinkInfoRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getExternalLinkInfo#460d7a3f as nil")
	}
	b.PutString(g.Link)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetExternalLinkInfoRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getExternalLinkInfo#460d7a3f to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getExternalLinkInfo#460d7a3f: field link: %w", err)
		}
		g.Link = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetExternalLinkInfoRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getExternalLinkInfo#460d7a3f as nil")
	}
	b.ObjStart()
	b.PutID("getExternalLinkInfo")
	b.Comma()
	b.FieldStart("link")
	b.PutString(g.Link)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getExternalLinkInfo"); err != nil {
				return fmt.Errorf("unable to decode getExternalLinkInfo#460d7a3f: %w", err)
			}
		case "link":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getExternalLinkInfo#460d7a3f: field link: %w", err)
			}
			g.Link = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetLink returns value of Link field.
func (g *GetExternalLinkInfoRequest) GetLink() (value string) {
	if g == nil {
		return
	}
	return g.Link
}

// GetExternalLinkInfo invokes method getExternalLinkInfo#460d7a3f returning error if any.
func (c *Client) GetExternalLinkInfo(ctx context.Context, link string) (LoginURLInfoClass, error) {
	var result LoginURLInfoBox

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