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

// OpenWebAppRequest represents TL type `openWebApp#8297968e`.
type OpenWebAppRequest struct {
	// Identifier of the chat in which the Web App is opened. The Web App can't be opened in
	// secret chats
	ChatID int64
	// Identifier of the bot, providing the Web App. If the bot is restricted for the current
	// user, then show an error instead of calling the method
	BotUserID int64
	// The URL from an inlineKeyboardButtonTypeWebApp button, a botMenuButton button, an
	// internalLinkTypeAttachmentMenuBot link, or an empty string otherwise
	URL string
	// If not 0, the message thread identifier to which the message will be sent
	MessageThreadID int64
	// If not 0, unique identifier of the topic of channel direct messages chat to which the
	// message will be sent
	DirectMessagesChatTopicID int64
	// Information about the message or story to be replied in the message sent by the Web
	// App; pass null if none
	ReplyTo InputMessageReplyToClass
	// Parameters to use to open the Web App
	Parameters WebAppOpenParameters
}

// OpenWebAppRequestTypeID is TL type id of OpenWebAppRequest.
const OpenWebAppRequestTypeID = 0x8297968e

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

func (o *OpenWebAppRequest) Zero() bool {
	if o == nil {
		return true
	}
	if !(o.ChatID == 0) {
		return false
	}
	if !(o.BotUserID == 0) {
		return false
	}
	if !(o.URL == "") {
		return false
	}
	if !(o.MessageThreadID == 0) {
		return false
	}
	if !(o.DirectMessagesChatTopicID == 0) {
		return false
	}
	if !(o.ReplyTo == nil) {
		return false
	}
	if !(o.Parameters.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (o *OpenWebAppRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "openWebApp",
		ID:   OpenWebAppRequestTypeID,
	}
	if o == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
		{
			Name:       "URL",
			SchemaName: "url",
		},
		{
			Name:       "MessageThreadID",
			SchemaName: "message_thread_id",
		},
		{
			Name:       "DirectMessagesChatTopicID",
			SchemaName: "direct_messages_chat_topic_id",
		},
		{
			Name:       "ReplyTo",
			SchemaName: "reply_to",
		},
		{
			Name:       "Parameters",
			SchemaName: "parameters",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (o *OpenWebAppRequest) Encode(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't encode openWebApp#8297968e as nil")
	}
	b.PutID(OpenWebAppRequestTypeID)
	return o.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (o *OpenWebAppRequest) EncodeBare(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't encode openWebApp#8297968e as nil")
	}
	b.PutInt53(o.ChatID)
	b.PutInt53(o.BotUserID)
	b.PutString(o.URL)
	b.PutInt53(o.MessageThreadID)
	b.PutInt53(o.DirectMessagesChatTopicID)
	if o.ReplyTo == nil {
		return fmt.Errorf("unable to encode openWebApp#8297968e: field reply_to is nil")
	}
	if err := o.ReplyTo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode openWebApp#8297968e: field reply_to: %w", err)
	}
	if err := o.Parameters.Encode(b); err != nil {
		return fmt.Errorf("unable to encode openWebApp#8297968e: field parameters: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (o *OpenWebAppRequest) Decode(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't decode openWebApp#8297968e to nil")
	}
	if err := b.ConsumeID(OpenWebAppRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode openWebApp#8297968e: %w", err)
	}
	return o.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (o *OpenWebAppRequest) DecodeBare(b *bin.Buffer) error {
	if o == nil {
		return fmt.Errorf("can't decode openWebApp#8297968e to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode openWebApp#8297968e: field chat_id: %w", err)
		}
		o.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode openWebApp#8297968e: field bot_user_id: %w", err)
		}
		o.BotUserID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode openWebApp#8297968e: field url: %w", err)
		}
		o.URL = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode openWebApp#8297968e: field message_thread_id: %w", err)
		}
		o.MessageThreadID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode openWebApp#8297968e: field direct_messages_chat_topic_id: %w", err)
		}
		o.DirectMessagesChatTopicID = value
	}
	{
		value, err := DecodeInputMessageReplyTo(b)
		if err != nil {
			return fmt.Errorf("unable to decode openWebApp#8297968e: field reply_to: %w", err)
		}
		o.ReplyTo = value
	}
	{
		if err := o.Parameters.Decode(b); err != nil {
			return fmt.Errorf("unable to decode openWebApp#8297968e: field parameters: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (o *OpenWebAppRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if o == nil {
		return fmt.Errorf("can't encode openWebApp#8297968e as nil")
	}
	b.ObjStart()
	b.PutID("openWebApp")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(o.ChatID)
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(o.BotUserID)
	b.Comma()
	b.FieldStart("url")
	b.PutString(o.URL)
	b.Comma()
	b.FieldStart("message_thread_id")
	b.PutInt53(o.MessageThreadID)
	b.Comma()
	b.FieldStart("direct_messages_chat_topic_id")
	b.PutInt53(o.DirectMessagesChatTopicID)
	b.Comma()
	b.FieldStart("reply_to")
	if o.ReplyTo == nil {
		return fmt.Errorf("unable to encode openWebApp#8297968e: field reply_to is nil")
	}
	if err := o.ReplyTo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode openWebApp#8297968e: field reply_to: %w", err)
	}
	b.Comma()
	b.FieldStart("parameters")
	if err := o.Parameters.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode openWebApp#8297968e: field parameters: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (o *OpenWebAppRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if o == nil {
		return fmt.Errorf("can't decode openWebApp#8297968e to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("openWebApp"); err != nil {
				return fmt.Errorf("unable to decode openWebApp#8297968e: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode openWebApp#8297968e: field chat_id: %w", err)
			}
			o.ChatID = value
		case "bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode openWebApp#8297968e: field bot_user_id: %w", err)
			}
			o.BotUserID = value
		case "url":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode openWebApp#8297968e: field url: %w", err)
			}
			o.URL = value
		case "message_thread_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode openWebApp#8297968e: field message_thread_id: %w", err)
			}
			o.MessageThreadID = value
		case "direct_messages_chat_topic_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode openWebApp#8297968e: field direct_messages_chat_topic_id: %w", err)
			}
			o.DirectMessagesChatTopicID = value
		case "reply_to":
			value, err := DecodeTDLibJSONInputMessageReplyTo(b)
			if err != nil {
				return fmt.Errorf("unable to decode openWebApp#8297968e: field reply_to: %w", err)
			}
			o.ReplyTo = value
		case "parameters":
			if err := o.Parameters.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode openWebApp#8297968e: field parameters: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (o *OpenWebAppRequest) GetChatID() (value int64) {
	if o == nil {
		return
	}
	return o.ChatID
}

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

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

// GetMessageThreadID returns value of MessageThreadID field.
func (o *OpenWebAppRequest) GetMessageThreadID() (value int64) {
	if o == nil {
		return
	}
	return o.MessageThreadID
}

// GetDirectMessagesChatTopicID returns value of DirectMessagesChatTopicID field.
func (o *OpenWebAppRequest) GetDirectMessagesChatTopicID() (value int64) {
	if o == nil {
		return
	}
	return o.DirectMessagesChatTopicID
}

// GetReplyTo returns value of ReplyTo field.
func (o *OpenWebAppRequest) GetReplyTo() (value InputMessageReplyToClass) {
	if o == nil {
		return
	}
	return o.ReplyTo
}

// GetParameters returns value of Parameters field.
func (o *OpenWebAppRequest) GetParameters() (value WebAppOpenParameters) {
	if o == nil {
		return
	}
	return o.Parameters
}

// OpenWebApp invokes method openWebApp#8297968e returning error if any.
func (c *Client) OpenWebApp(ctx context.Context, request *OpenWebAppRequest) (*WebAppInfo, error) {
	var result WebAppInfo

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