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

// GetWebAppLinkURLRequest represents TL type `getWebAppLinkUrl#60fe62c1`.
type GetWebAppLinkURLRequest struct {
	// Identifier of the chat in which the link was clicked; pass 0 if none
	ChatID int64
	// Identifier of the target bot
	BotUserID int64
	// Short name of the Web App
	WebAppShortName string
	// Start parameter from internalLinkTypeWebApp
	StartParameter string
	// Pass true if the current user allowed the bot to send them messages
	AllowWriteAccess bool
	// Parameters to use to open the Web App
	Parameters WebAppOpenParameters
}

// GetWebAppLinkURLRequestTypeID is TL type id of GetWebAppLinkURLRequest.
const GetWebAppLinkURLRequestTypeID = 0x60fe62c1

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

func (g *GetWebAppLinkURLRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.ChatID == 0) {
		return false
	}
	if !(g.BotUserID == 0) {
		return false
	}
	if !(g.WebAppShortName == "") {
		return false
	}
	if !(g.StartParameter == "") {
		return false
	}
	if !(g.AllowWriteAccess == false) {
		return false
	}
	if !(g.Parameters.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetWebAppLinkURLRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getWebAppLinkUrl",
		ID:   GetWebAppLinkURLRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
		{
			Name:       "WebAppShortName",
			SchemaName: "web_app_short_name",
		},
		{
			Name:       "StartParameter",
			SchemaName: "start_parameter",
		},
		{
			Name:       "AllowWriteAccess",
			SchemaName: "allow_write_access",
		},
		{
			Name:       "Parameters",
			SchemaName: "parameters",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetWebAppLinkURLRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getWebAppLinkUrl#60fe62c1 as nil")
	}
	b.PutID(GetWebAppLinkURLRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetWebAppLinkURLRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getWebAppLinkUrl#60fe62c1 as nil")
	}
	b.PutInt53(g.ChatID)
	b.PutInt53(g.BotUserID)
	b.PutString(g.WebAppShortName)
	b.PutString(g.StartParameter)
	b.PutBool(g.AllowWriteAccess)
	if err := g.Parameters.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getWebAppLinkUrl#60fe62c1: field parameters: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetWebAppLinkURLRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getWebAppLinkUrl#60fe62c1 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getWebAppLinkUrl#60fe62c1: field chat_id: %w", err)
		}
		g.ChatID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode getWebAppLinkUrl#60fe62c1: field bot_user_id: %w", err)
		}
		g.BotUserID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getWebAppLinkUrl#60fe62c1: field web_app_short_name: %w", err)
		}
		g.WebAppShortName = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getWebAppLinkUrl#60fe62c1: field start_parameter: %w", err)
		}
		g.StartParameter = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode getWebAppLinkUrl#60fe62c1: field allow_write_access: %w", err)
		}
		g.AllowWriteAccess = value
	}
	{
		if err := g.Parameters.Decode(b); err != nil {
			return fmt.Errorf("unable to decode getWebAppLinkUrl#60fe62c1: field parameters: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetWebAppLinkURLRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getWebAppLinkUrl#60fe62c1 as nil")
	}
	b.ObjStart()
	b.PutID("getWebAppLinkUrl")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(g.ChatID)
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(g.BotUserID)
	b.Comma()
	b.FieldStart("web_app_short_name")
	b.PutString(g.WebAppShortName)
	b.Comma()
	b.FieldStart("start_parameter")
	b.PutString(g.StartParameter)
	b.Comma()
	b.FieldStart("allow_write_access")
	b.PutBool(g.AllowWriteAccess)
	b.Comma()
	b.FieldStart("parameters")
	if err := g.Parameters.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getWebAppLinkUrl#60fe62c1: field parameters: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getWebAppLinkUrl"); err != nil {
				return fmt.Errorf("unable to decode getWebAppLinkUrl#60fe62c1: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getWebAppLinkUrl#60fe62c1: field chat_id: %w", err)
			}
			g.ChatID = value
		case "bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode getWebAppLinkUrl#60fe62c1: field bot_user_id: %w", err)
			}
			g.BotUserID = value
		case "web_app_short_name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getWebAppLinkUrl#60fe62c1: field web_app_short_name: %w", err)
			}
			g.WebAppShortName = value
		case "start_parameter":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getWebAppLinkUrl#60fe62c1: field start_parameter: %w", err)
			}
			g.StartParameter = value
		case "allow_write_access":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode getWebAppLinkUrl#60fe62c1: field allow_write_access: %w", err)
			}
			g.AllowWriteAccess = value
		case "parameters":
			if err := g.Parameters.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode getWebAppLinkUrl#60fe62c1: field parameters: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

// GetWebAppShortName returns value of WebAppShortName field.
func (g *GetWebAppLinkURLRequest) GetWebAppShortName() (value string) {
	if g == nil {
		return
	}
	return g.WebAppShortName
}

// GetStartParameter returns value of StartParameter field.
func (g *GetWebAppLinkURLRequest) GetStartParameter() (value string) {
	if g == nil {
		return
	}
	return g.StartParameter
}

// GetAllowWriteAccess returns value of AllowWriteAccess field.
func (g *GetWebAppLinkURLRequest) GetAllowWriteAccess() (value bool) {
	if g == nil {
		return
	}
	return g.AllowWriteAccess
}

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

// GetWebAppLinkURL invokes method getWebAppLinkUrl#60fe62c1 returning error if any.
func (c *Client) GetWebAppLinkURL(ctx context.Context, request *GetWebAppLinkURLRequest) (*HTTPURL, error) {
	var result HTTPURL

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