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

// GetExternalLinkRequest represents TL type `getExternalLink#5e92f0eb`.
type GetExternalLinkRequest struct {
	// The HTTP link
	Link string
	// Pass true if the current user allowed the bot, returned in getExternalLinkInfo, to
	// send them messages
	AllowWriteAccess bool
}

// GetExternalLinkRequestTypeID is TL type id of GetExternalLinkRequest.
const GetExternalLinkRequestTypeID = 0x5e92f0eb

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (g *GetExternalLinkRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getExternalLink#5e92f0eb as nil")
	}
	b.PutID(GetExternalLinkRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetExternalLinkRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getExternalLink#5e92f0eb as nil")
	}
	b.PutString(g.Link)
	b.PutBool(g.AllowWriteAccess)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetExternalLinkRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getExternalLink#5e92f0eb to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getExternalLink#5e92f0eb: field link: %w", err)
		}
		g.Link = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getExternalLink#5e92f0eb: field allow_write_access: %w", err)
		}
		g.AllowWriteAccess = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetExternalLinkRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getExternalLink#5e92f0eb as nil")
	}
	b.ObjStart()
	b.PutID("getExternalLink")
	b.Comma()
	b.FieldStart("link")
	b.PutString(g.Link)
	b.Comma()
	b.FieldStart("allow_write_access")
	b.PutBool(g.AllowWriteAccess)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

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

// GetAllowWriteAccess returns value of AllowWriteAccess field.
func (g *GetExternalLinkRequest) GetAllowWriteAccess() (value bool) {
	if g == nil {
		return
	}
	return g.AllowWriteAccess
}

// GetExternalLink invokes method getExternalLink#5e92f0eb returning error if any.
func (c *Client) GetExternalLink(ctx context.Context, request *GetExternalLinkRequest) (*HTTPURL, error) {
	var result HTTPURL

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