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

// GetChatBoostLinkInfoRequest represents TL type `getChatBoostLinkInfo#26fc4b5c`.
type GetChatBoostLinkInfoRequest struct {
	// The link to boost a chat
	URL string
}

// GetChatBoostLinkInfoRequestTypeID is TL type id of GetChatBoostLinkInfoRequest.
const GetChatBoostLinkInfoRequestTypeID = 0x26fc4b5c

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetChatBoostLinkInfoRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getChatBoostLinkInfo",
		ID:   GetChatBoostLinkInfoRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "URL",
			SchemaName: "url",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetChatBoostLinkInfoRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatBoostLinkInfo#26fc4b5c as nil")
	}
	b.PutID(GetChatBoostLinkInfoRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetChatBoostLinkInfoRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatBoostLinkInfo#26fc4b5c as nil")
	}
	b.PutString(g.URL)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetChatBoostLinkInfoRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatBoostLinkInfo#26fc4b5c to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getChatBoostLinkInfo#26fc4b5c: field url: %w", err)
		}
		g.URL = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetChatBoostLinkInfoRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatBoostLinkInfo#26fc4b5c as nil")
	}
	b.ObjStart()
	b.PutID("getChatBoostLinkInfo")
	b.Comma()
	b.FieldStart("url")
	b.PutString(g.URL)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

// GetURL returns value of URL field.
func (g *GetChatBoostLinkInfoRequest) GetURL() (value string) {
	if g == nil {
		return
	}
	return g.URL
}

// GetChatBoostLinkInfo invokes method getChatBoostLinkInfo#26fc4b5c returning error if any.
func (c *Client) GetChatBoostLinkInfo(ctx context.Context, url string) (*ChatBoostLinkInfo, error) {
	var result ChatBoostLinkInfo

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