// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// BotsInvokeWebViewCustomMethodRequest represents TL type `bots.invokeWebViewCustomMethod#87fc5e7`.
// Send a custom request from a mini bot app¹, triggered by a
// web_app_invoke_custom_method event »².
// The response should be sent using a custom_method_invoked¹ event, see here »² for
// more info on the flow.
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps
//  2. https://core.telegram.org/api/web-events#web-app-invoke-custom-method
//  3. https://core.telegram.org/api/bots/webapps#custom-method-invoked
//  4. https://core.telegram.org/api/web-events#web-app-invoke-custom-method
//
// See https://core.telegram.org/method/bots.invokeWebViewCustomMethod for reference.
type BotsInvokeWebViewCustomMethodRequest struct {
	// Identifier of the bot associated to the mini bot app¹
	//
	// Links:
	//  1) https://core.telegram.org/api/bots/webapps
	Bot InputUserClass
	// Identifier of the custom method to invoke
	CustomMethod string
	// Method parameters
	Params DataJSON
}

// BotsInvokeWebViewCustomMethodRequestTypeID is TL type id of BotsInvokeWebViewCustomMethodRequest.
const BotsInvokeWebViewCustomMethodRequestTypeID = 0x87fc5e7

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

func (i *BotsInvokeWebViewCustomMethodRequest) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Bot == nil) {
		return false
	}
	if !(i.CustomMethod == "") {
		return false
	}
	if !(i.Params.Zero()) {
		return false
	}

	return true
}

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

// FillFrom fills BotsInvokeWebViewCustomMethodRequest from given interface.
func (i *BotsInvokeWebViewCustomMethodRequest) FillFrom(from interface {
	GetBot() (value InputUserClass)
	GetCustomMethod() (value string)
	GetParams() (value DataJSON)
}) {
	i.Bot = from.GetBot()
	i.CustomMethod = from.GetCustomMethod()
	i.Params = from.GetParams()
}

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

// TypeName returns name of type in TL schema.
func (*BotsInvokeWebViewCustomMethodRequest) TypeName() string {
	return "bots.invokeWebViewCustomMethod"
}

// TypeInfo returns info about TL type.
func (i *BotsInvokeWebViewCustomMethodRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "bots.invokeWebViewCustomMethod",
		ID:   BotsInvokeWebViewCustomMethodRequestTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Bot",
			SchemaName: "bot",
		},
		{
			Name:       "CustomMethod",
			SchemaName: "custom_method",
		},
		{
			Name:       "Params",
			SchemaName: "params",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *BotsInvokeWebViewCustomMethodRequest) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode bots.invokeWebViewCustomMethod#87fc5e7 as nil")
	}
	b.PutID(BotsInvokeWebViewCustomMethodRequestTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *BotsInvokeWebViewCustomMethodRequest) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode bots.invokeWebViewCustomMethod#87fc5e7 as nil")
	}
	if i.Bot == nil {
		return fmt.Errorf("unable to encode bots.invokeWebViewCustomMethod#87fc5e7: field bot is nil")
	}
	if err := i.Bot.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.invokeWebViewCustomMethod#87fc5e7: field bot: %w", err)
	}
	b.PutString(i.CustomMethod)
	if err := i.Params.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.invokeWebViewCustomMethod#87fc5e7: field params: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *BotsInvokeWebViewCustomMethodRequest) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode bots.invokeWebViewCustomMethod#87fc5e7 to nil")
	}
	if err := b.ConsumeID(BotsInvokeWebViewCustomMethodRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode bots.invokeWebViewCustomMethod#87fc5e7: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *BotsInvokeWebViewCustomMethodRequest) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode bots.invokeWebViewCustomMethod#87fc5e7 to nil")
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode bots.invokeWebViewCustomMethod#87fc5e7: field bot: %w", err)
		}
		i.Bot = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode bots.invokeWebViewCustomMethod#87fc5e7: field custom_method: %w", err)
		}
		i.CustomMethod = value
	}
	{
		if err := i.Params.Decode(b); err != nil {
			return fmt.Errorf("unable to decode bots.invokeWebViewCustomMethod#87fc5e7: field params: %w", err)
		}
	}
	return nil
}

// GetBot returns value of Bot field.
func (i *BotsInvokeWebViewCustomMethodRequest) GetBot() (value InputUserClass) {
	if i == nil {
		return
	}
	return i.Bot
}

// GetCustomMethod returns value of CustomMethod field.
func (i *BotsInvokeWebViewCustomMethodRequest) GetCustomMethod() (value string) {
	if i == nil {
		return
	}
	return i.CustomMethod
}

// GetParams returns value of Params field.
func (i *BotsInvokeWebViewCustomMethodRequest) GetParams() (value DataJSON) {
	if i == nil {
		return
	}
	return i.Params
}

// BotsInvokeWebViewCustomMethod invokes method bots.invokeWebViewCustomMethod#87fc5e7 returning error if any.
// Send a custom request from a mini bot app¹, triggered by a
// web_app_invoke_custom_method event »².
// The response should be sent using a custom_method_invoked¹ event, see here »² for
// more info on the flow.
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps
//  2. https://core.telegram.org/api/web-events#web-app-invoke-custom-method
//  3. https://core.telegram.org/api/bots/webapps#custom-method-invoked
//  4. https://core.telegram.org/api/web-events#web-app-invoke-custom-method
//
// Possible errors:
//
//	400 BOT_INVALID: This is not a valid bot.
//	400 DATA_JSON_INVALID: The provided JSON data is invalid.
//	400 METHOD_INVALID: The specified method is invalid.
//
// See https://core.telegram.org/method/bots.invokeWebViewCustomMethod for reference.
func (c *Client) BotsInvokeWebViewCustomMethod(ctx context.Context, request *BotsInvokeWebViewCustomMethodRequest) (*DataJSON, error) {
	var result DataJSON

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