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

// GetWebAppURLRequest represents TL type `getWebAppUrl#a4ff1f44`.
type GetWebAppURLRequest struct {
	// Identifier of the target bot. If the bot is restricted for the current user, then show
	// an error instead of calling the method
	BotUserID int64
	// The URL from a keyboardButtonTypeWebApp button, inlineQueryResultsButtonTypeWebApp
	// button, or an empty string when the bot is opened from the side menu
	URL string
	// Parameters to use to open the Web App
	Parameters WebAppOpenParameters
}

// GetWebAppURLRequestTypeID is TL type id of GetWebAppURLRequest.
const GetWebAppURLRequestTypeID = 0xa4ff1f44

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

func (g *GetWebAppURLRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.BotUserID == 0) {
		return false
	}
	if !(g.URL == "") {
		return false
	}
	if !(g.Parameters.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetWebAppURLRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getWebAppUrl",
		ID:   GetWebAppURLRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
		{
			Name:       "URL",
			SchemaName: "url",
		},
		{
			Name:       "Parameters",
			SchemaName: "parameters",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetWebAppURLRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getWebAppUrl#a4ff1f44 as nil")
	}
	b.PutID(GetWebAppURLRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetWebAppURLRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getWebAppUrl#a4ff1f44 as nil")
	}
	b.PutInt53(g.BotUserID)
	b.PutString(g.URL)
	if err := g.Parameters.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getWebAppUrl#a4ff1f44: field parameters: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetWebAppURLRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getWebAppUrl#a4ff1f44 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getWebAppUrl#a4ff1f44: field bot_user_id: %w", err)
		}
		g.BotUserID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getWebAppUrl#a4ff1f44: field url: %w", err)
		}
		g.URL = value
	}
	{
		if err := g.Parameters.Decode(b); err != nil {
			return fmt.Errorf("unable to decode getWebAppUrl#a4ff1f44: field parameters: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetWebAppURLRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getWebAppUrl#a4ff1f44 as nil")
	}
	b.ObjStart()
	b.PutID("getWebAppUrl")
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(g.BotUserID)
	b.Comma()
	b.FieldStart("url")
	b.PutString(g.URL)
	b.Comma()
	b.FieldStart("parameters")
	if err := g.Parameters.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getWebAppUrl#a4ff1f44: field parameters: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

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

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

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

// GetWebAppURL invokes method getWebAppUrl#a4ff1f44 returning error if any.
func (c *Client) GetWebAppURL(ctx context.Context, request *GetWebAppURLRequest) (*HTTPURL, error) {
	var result HTTPURL

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