// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// BotsRequestWebViewButtonRequest represents TL type `bots.requestWebViewButton#31a2a35e`.
//
// See https://core.telegram.org/method/bots.requestWebViewButton for reference.
type BotsRequestWebViewButtonRequest struct {
	// UserID field of BotsRequestWebViewButtonRequest.
	UserID InputUserClass
	// Button field of BotsRequestWebViewButtonRequest.
	Button KeyboardButtonClass
}

// BotsRequestWebViewButtonRequestTypeID is TL type id of BotsRequestWebViewButtonRequest.
const BotsRequestWebViewButtonRequestTypeID = 0x31a2a35e

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

func (r *BotsRequestWebViewButtonRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.UserID == nil) {
		return false
	}
	if !(r.Button == nil) {
		return false
	}

	return true
}

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

// FillFrom fills BotsRequestWebViewButtonRequest from given interface.
func (r *BotsRequestWebViewButtonRequest) FillFrom(from interface {
	GetUserID() (value InputUserClass)
	GetButton() (value KeyboardButtonClass)
}) {
	r.UserID = from.GetUserID()
	r.Button = from.GetButton()
}

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

// TypeName returns name of type in TL schema.
func (*BotsRequestWebViewButtonRequest) TypeName() string {
	return "bots.requestWebViewButton"
}

// TypeInfo returns info about TL type.
func (r *BotsRequestWebViewButtonRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "bots.requestWebViewButton",
		ID:   BotsRequestWebViewButtonRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "Button",
			SchemaName: "button",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *BotsRequestWebViewButtonRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode bots.requestWebViewButton#31a2a35e as nil")
	}
	b.PutID(BotsRequestWebViewButtonRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *BotsRequestWebViewButtonRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode bots.requestWebViewButton#31a2a35e as nil")
	}
	if r.UserID == nil {
		return fmt.Errorf("unable to encode bots.requestWebViewButton#31a2a35e: field user_id is nil")
	}
	if err := r.UserID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.requestWebViewButton#31a2a35e: field user_id: %w", err)
	}
	if r.Button == nil {
		return fmt.Errorf("unable to encode bots.requestWebViewButton#31a2a35e: field button is nil")
	}
	if err := r.Button.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.requestWebViewButton#31a2a35e: field button: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (r *BotsRequestWebViewButtonRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode bots.requestWebViewButton#31a2a35e to nil")
	}
	if err := b.ConsumeID(BotsRequestWebViewButtonRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode bots.requestWebViewButton#31a2a35e: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *BotsRequestWebViewButtonRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode bots.requestWebViewButton#31a2a35e to nil")
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode bots.requestWebViewButton#31a2a35e: field user_id: %w", err)
		}
		r.UserID = value
	}
	{
		value, err := DecodeKeyboardButton(b)
		if err != nil {
			return fmt.Errorf("unable to decode bots.requestWebViewButton#31a2a35e: field button: %w", err)
		}
		r.Button = value
	}
	return nil
}

// GetUserID returns value of UserID field.
func (r *BotsRequestWebViewButtonRequest) GetUserID() (value InputUserClass) {
	if r == nil {
		return
	}
	return r.UserID
}

// GetButton returns value of Button field.
func (r *BotsRequestWebViewButtonRequest) GetButton() (value KeyboardButtonClass) {
	if r == nil {
		return
	}
	return r.Button
}

// BotsRequestWebViewButton invokes method bots.requestWebViewButton#31a2a35e returning error if any.
//
// See https://core.telegram.org/method/bots.requestWebViewButton for reference.
func (c *Client) BotsRequestWebViewButton(ctx context.Context, request *BotsRequestWebViewButtonRequest) (*BotsRequestedButton, error) {
	var result BotsRequestedButton

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