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

// SendWebAppCustomRequestRequest represents TL type `sendWebAppCustomRequest#36ff5dc8`.
type SendWebAppCustomRequestRequest struct {
	// Identifier of the bot
	BotUserID int64
	// The method name
	Method string
	// JSON-serialized method parameters
	Parameters string
}

// SendWebAppCustomRequestRequestTypeID is TL type id of SendWebAppCustomRequestRequest.
const SendWebAppCustomRequestRequestTypeID = 0x36ff5dc8

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SendWebAppCustomRequestRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "sendWebAppCustomRequest",
		ID:   SendWebAppCustomRequestRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
		{
			Name:       "Method",
			SchemaName: "method",
		},
		{
			Name:       "Parameters",
			SchemaName: "parameters",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SendWebAppCustomRequestRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendWebAppCustomRequest#36ff5dc8 as nil")
	}
	b.PutID(SendWebAppCustomRequestRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SendWebAppCustomRequestRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sendWebAppCustomRequest#36ff5dc8 as nil")
	}
	b.PutInt53(s.BotUserID)
	b.PutString(s.Method)
	b.PutString(s.Parameters)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SendWebAppCustomRequestRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sendWebAppCustomRequest#36ff5dc8 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode sendWebAppCustomRequest#36ff5dc8: field bot_user_id: %w", err)
		}
		s.BotUserID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode sendWebAppCustomRequest#36ff5dc8: field method: %w", err)
		}
		s.Method = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode sendWebAppCustomRequest#36ff5dc8: field parameters: %w", err)
		}
		s.Parameters = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SendWebAppCustomRequestRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode sendWebAppCustomRequest#36ff5dc8 as nil")
	}
	b.ObjStart()
	b.PutID("sendWebAppCustomRequest")
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(s.BotUserID)
	b.Comma()
	b.FieldStart("method")
	b.PutString(s.Method)
	b.Comma()
	b.FieldStart("parameters")
	b.PutString(s.Parameters)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

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

// GetMethod returns value of Method field.
func (s *SendWebAppCustomRequestRequest) GetMethod() (value string) {
	if s == nil {
		return
	}
	return s.Method
}

// GetParameters returns value of Parameters field.
func (s *SendWebAppCustomRequestRequest) GetParameters() (value string) {
	if s == nil {
		return
	}
	return s.Parameters
}

// SendWebAppCustomRequest invokes method sendWebAppCustomRequest#36ff5dc8 returning error if any.
func (c *Client) SendWebAppCustomRequest(ctx context.Context, request *SendWebAppCustomRequestRequest) (*CustomRequestResult, error) {
	var result CustomRequestResult

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