// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// MessagesSendWebViewDataRequest represents TL type `messages.sendWebViewData#dc0242c8`.
// Used by the user to relay data from an opened reply keyboard bot web app¹ to the bot
// that owns it.
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps
//
// See https://core.telegram.org/method/messages.sendWebViewData for reference.
type MessagesSendWebViewDataRequest struct {
	// Bot that owns the web app
	Bot InputUserClass
	// Unique client message ID to prevent duplicate sending of the same event
	RandomID int64
	// Text of the keyboardButtonSimpleWebView¹ that was pressed to open the web app.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/keyboardButtonSimpleWebView
	ButtonText string
	// Data to relay to the bot, obtained from a web_app_data_send JS event¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/web-events#web-app-data-send
	Data string
}

// MessagesSendWebViewDataRequestTypeID is TL type id of MessagesSendWebViewDataRequest.
const MessagesSendWebViewDataRequestTypeID = 0xdc0242c8

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

func (s *MessagesSendWebViewDataRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Bot == nil) {
		return false
	}
	if !(s.RandomID == 0) {
		return false
	}
	if !(s.ButtonText == "") {
		return false
	}
	if !(s.Data == "") {
		return false
	}

	return true
}

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

// FillFrom fills MessagesSendWebViewDataRequest from given interface.
func (s *MessagesSendWebViewDataRequest) FillFrom(from interface {
	GetBot() (value InputUserClass)
	GetRandomID() (value int64)
	GetButtonText() (value string)
	GetData() (value string)
}) {
	s.Bot = from.GetBot()
	s.RandomID = from.GetRandomID()
	s.ButtonText = from.GetButtonText()
	s.Data = from.GetData()
}

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

// TypeName returns name of type in TL schema.
func (*MessagesSendWebViewDataRequest) TypeName() string {
	return "messages.sendWebViewData"
}

// TypeInfo returns info about TL type.
func (s *MessagesSendWebViewDataRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.sendWebViewData",
		ID:   MessagesSendWebViewDataRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Bot",
			SchemaName: "bot",
		},
		{
			Name:       "RandomID",
			SchemaName: "random_id",
		},
		{
			Name:       "ButtonText",
			SchemaName: "button_text",
		},
		{
			Name:       "Data",
			SchemaName: "data",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *MessagesSendWebViewDataRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.sendWebViewData#dc0242c8 as nil")
	}
	b.PutID(MessagesSendWebViewDataRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *MessagesSendWebViewDataRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode messages.sendWebViewData#dc0242c8 as nil")
	}
	if s.Bot == nil {
		return fmt.Errorf("unable to encode messages.sendWebViewData#dc0242c8: field bot is nil")
	}
	if err := s.Bot.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.sendWebViewData#dc0242c8: field bot: %w", err)
	}
	b.PutLong(s.RandomID)
	b.PutString(s.ButtonText)
	b.PutString(s.Data)
	return nil
}

// Decode implements bin.Decoder.
func (s *MessagesSendWebViewDataRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.sendWebViewData#dc0242c8 to nil")
	}
	if err := b.ConsumeID(MessagesSendWebViewDataRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.sendWebViewData#dc0242c8: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *MessagesSendWebViewDataRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode messages.sendWebViewData#dc0242c8 to nil")
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.sendWebViewData#dc0242c8: field bot: %w", err)
		}
		s.Bot = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.sendWebViewData#dc0242c8: field random_id: %w", err)
		}
		s.RandomID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.sendWebViewData#dc0242c8: field button_text: %w", err)
		}
		s.ButtonText = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messages.sendWebViewData#dc0242c8: field data: %w", err)
		}
		s.Data = value
	}
	return nil
}

// GetBot returns value of Bot field.
func (s *MessagesSendWebViewDataRequest) GetBot() (value InputUserClass) {
	if s == nil {
		return
	}
	return s.Bot
}

// GetRandomID returns value of RandomID field.
func (s *MessagesSendWebViewDataRequest) GetRandomID() (value int64) {
	if s == nil {
		return
	}
	return s.RandomID
}

// GetButtonText returns value of ButtonText field.
func (s *MessagesSendWebViewDataRequest) GetButtonText() (value string) {
	if s == nil {
		return
	}
	return s.ButtonText
}

// GetData returns value of Data field.
func (s *MessagesSendWebViewDataRequest) GetData() (value string) {
	if s == nil {
		return
	}
	return s.Data
}

// MessagesSendWebViewData invokes method messages.sendWebViewData#dc0242c8 returning error if any.
// Used by the user to relay data from an opened reply keyboard bot web app¹ to the bot
// that owns it.
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps
//
// See https://core.telegram.org/method/messages.sendWebViewData for reference.
func (c *Client) MessagesSendWebViewData(ctx context.Context, request *MessagesSendWebViewDataRequest) (UpdatesClass, error) {
	var result UpdatesBox

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