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

// GetBusinessChatLinkInfoRequest represents TL type `getBusinessChatLinkInfo#2f8b7e4a`.
type GetBusinessChatLinkInfoRequest struct {
	// Name of the link
	LinkName string
}

// GetBusinessChatLinkInfoRequestTypeID is TL type id of GetBusinessChatLinkInfoRequest.
const GetBusinessChatLinkInfoRequestTypeID = 0x2f8b7e4a

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetBusinessChatLinkInfoRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getBusinessChatLinkInfo",
		ID:   GetBusinessChatLinkInfoRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "LinkName",
			SchemaName: "link_name",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetBusinessChatLinkInfoRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBusinessChatLinkInfo#2f8b7e4a as nil")
	}
	b.PutID(GetBusinessChatLinkInfoRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetBusinessChatLinkInfoRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBusinessChatLinkInfo#2f8b7e4a as nil")
	}
	b.PutString(g.LinkName)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetBusinessChatLinkInfoRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getBusinessChatLinkInfo#2f8b7e4a to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getBusinessChatLinkInfo#2f8b7e4a: field link_name: %w", err)
		}
		g.LinkName = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetBusinessChatLinkInfoRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getBusinessChatLinkInfo#2f8b7e4a as nil")
	}
	b.ObjStart()
	b.PutID("getBusinessChatLinkInfo")
	b.Comma()
	b.FieldStart("link_name")
	b.PutString(g.LinkName)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// GetLinkName returns value of LinkName field.
func (g *GetBusinessChatLinkInfoRequest) GetLinkName() (value string) {
	if g == nil {
		return
	}
	return g.LinkName
}

// GetBusinessChatLinkInfo invokes method getBusinessChatLinkInfo#2f8b7e4a returning error if any.
func (c *Client) GetBusinessChatLinkInfo(ctx context.Context, linkname string) (*BusinessChatLinkInfo, error) {
	var result BusinessChatLinkInfo

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