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

// SearchWebAppRequest represents TL type `searchWebApp#b5fc8a35`.
type SearchWebAppRequest struct {
	// Identifier of the target bot
	BotUserID int64
	// Short name of the Web App
	WebAppShortName string
}

// SearchWebAppRequestTypeID is TL type id of SearchWebAppRequest.
const SearchWebAppRequestTypeID = 0xb5fc8a35

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

func (s *SearchWebAppRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.BotUserID == 0) {
		return false
	}
	if !(s.WebAppShortName == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchWebAppRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchWebApp",
		ID:   SearchWebAppRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
		{
			Name:       "WebAppShortName",
			SchemaName: "web_app_short_name",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchWebAppRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchWebApp#b5fc8a35 as nil")
	}
	b.PutID(SearchWebAppRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchWebAppRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchWebApp#b5fc8a35 as nil")
	}
	b.PutInt53(s.BotUserID)
	b.PutString(s.WebAppShortName)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SearchWebAppRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchWebApp#b5fc8a35 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode searchWebApp#b5fc8a35: field bot_user_id: %w", err)
		}
		s.BotUserID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchWebApp#b5fc8a35: field web_app_short_name: %w", err)
		}
		s.WebAppShortName = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SearchWebAppRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode searchWebApp#b5fc8a35 as nil")
	}
	b.ObjStart()
	b.PutID("searchWebApp")
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(s.BotUserID)
	b.Comma()
	b.FieldStart("web_app_short_name")
	b.PutString(s.WebAppShortName)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SearchWebAppRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode searchWebApp#b5fc8a35 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("searchWebApp"); err != nil {
				return fmt.Errorf("unable to decode searchWebApp#b5fc8a35: %w", err)
			}
		case "bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode searchWebApp#b5fc8a35: field bot_user_id: %w", err)
			}
			s.BotUserID = value
		case "web_app_short_name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode searchWebApp#b5fc8a35: field web_app_short_name: %w", err)
			}
			s.WebAppShortName = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

// SearchWebApp invokes method searchWebApp#b5fc8a35 returning error if any.
func (c *Client) SearchWebApp(ctx context.Context, request *SearchWebAppRequest) (*FoundWebApp, error) {
	var result FoundWebApp

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