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

// GetWebAppPlaceholderRequest represents TL type `getWebAppPlaceholder#22c70d8f`.
type GetWebAppPlaceholderRequest struct {
	// Identifier of the target bot
	BotUserID int64
}

// GetWebAppPlaceholderRequestTypeID is TL type id of GetWebAppPlaceholderRequest.
const GetWebAppPlaceholderRequestTypeID = 0x22c70d8f

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

func (g *GetWebAppPlaceholderRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.BotUserID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetWebAppPlaceholderRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getWebAppPlaceholder",
		ID:   GetWebAppPlaceholderRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetWebAppPlaceholderRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getWebAppPlaceholder#22c70d8f as nil")
	}
	b.PutID(GetWebAppPlaceholderRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetWebAppPlaceholderRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getWebAppPlaceholder#22c70d8f as nil")
	}
	b.PutInt53(g.BotUserID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetWebAppPlaceholderRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getWebAppPlaceholder#22c70d8f to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getWebAppPlaceholder#22c70d8f: field bot_user_id: %w", err)
		}
		g.BotUserID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetWebAppPlaceholderRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getWebAppPlaceholder#22c70d8f as nil")
	}
	b.ObjStart()
	b.PutID("getWebAppPlaceholder")
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(g.BotUserID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getWebAppPlaceholder"); err != nil {
				return fmt.Errorf("unable to decode getWebAppPlaceholder#22c70d8f: %w", err)
			}
		case "bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getWebAppPlaceholder#22c70d8f: field bot_user_id: %w", err)
			}
			g.BotUserID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetBotUserID returns value of BotUserID field.
func (g *GetWebAppPlaceholderRequest) GetBotUserID() (value int64) {
	if g == nil {
		return
	}
	return g.BotUserID
}

// GetWebAppPlaceholder invokes method getWebAppPlaceholder#22c70d8f returning error if any.
func (c *Client) GetWebAppPlaceholder(ctx context.Context, botuserid int64) (*Outline, error) {
	var result Outline

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