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

// BotsCheckDownloadFileParamsRequest represents TL type `bots.checkDownloadFileParams#50077589`.
// Check if a mini app¹ can request the download of a specific file: called when
// handling web_app_request_file_download events »²
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps
//  2. https://core.telegram.org/api/web-events#web-app-request-file-download
//
// See https://core.telegram.org/method/bots.checkDownloadFileParams for reference.
type BotsCheckDownloadFileParamsRequest struct {
	// The bot that owns the mini app¹ that requested the download
	//
	// Links:
	//  1) https://core.telegram.org/api/bots/webapps
	Bot InputUserClass
	// The filename from the web_app_request_file_download event »¹
	//
	// Links:
	//  1) https://core.telegram.org/api/web-events#web-app-request-file-download
	FileName string
	// The url from the web_app_request_file_download event »¹
	//
	// Links:
	//  1) https://core.telegram.org/api/web-events#web-app-request-file-download
	URL string
}

// BotsCheckDownloadFileParamsRequestTypeID is TL type id of BotsCheckDownloadFileParamsRequest.
const BotsCheckDownloadFileParamsRequestTypeID = 0x50077589

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

func (c *BotsCheckDownloadFileParamsRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Bot == nil) {
		return false
	}
	if !(c.FileName == "") {
		return false
	}
	if !(c.URL == "") {
		return false
	}

	return true
}

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

// FillFrom fills BotsCheckDownloadFileParamsRequest from given interface.
func (c *BotsCheckDownloadFileParamsRequest) FillFrom(from interface {
	GetBot() (value InputUserClass)
	GetFileName() (value string)
	GetURL() (value string)
}) {
	c.Bot = from.GetBot()
	c.FileName = from.GetFileName()
	c.URL = from.GetURL()
}

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

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

// TypeInfo returns info about TL type.
func (c *BotsCheckDownloadFileParamsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "bots.checkDownloadFileParams",
		ID:   BotsCheckDownloadFileParamsRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Bot",
			SchemaName: "bot",
		},
		{
			Name:       "FileName",
			SchemaName: "file_name",
		},
		{
			Name:       "URL",
			SchemaName: "url",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *BotsCheckDownloadFileParamsRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode bots.checkDownloadFileParams#50077589 as nil")
	}
	b.PutID(BotsCheckDownloadFileParamsRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *BotsCheckDownloadFileParamsRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode bots.checkDownloadFileParams#50077589 as nil")
	}
	if c.Bot == nil {
		return fmt.Errorf("unable to encode bots.checkDownloadFileParams#50077589: field bot is nil")
	}
	if err := c.Bot.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.checkDownloadFileParams#50077589: field bot: %w", err)
	}
	b.PutString(c.FileName)
	b.PutString(c.URL)
	return nil
}

// Decode implements bin.Decoder.
func (c *BotsCheckDownloadFileParamsRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode bots.checkDownloadFileParams#50077589 to nil")
	}
	if err := b.ConsumeID(BotsCheckDownloadFileParamsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode bots.checkDownloadFileParams#50077589: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *BotsCheckDownloadFileParamsRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode bots.checkDownloadFileParams#50077589 to nil")
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode bots.checkDownloadFileParams#50077589: field bot: %w", err)
		}
		c.Bot = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode bots.checkDownloadFileParams#50077589: field file_name: %w", err)
		}
		c.FileName = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode bots.checkDownloadFileParams#50077589: field url: %w", err)
		}
		c.URL = value
	}
	return nil
}

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

// GetFileName returns value of FileName field.
func (c *BotsCheckDownloadFileParamsRequest) GetFileName() (value string) {
	if c == nil {
		return
	}
	return c.FileName
}

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

// BotsCheckDownloadFileParams invokes method bots.checkDownloadFileParams#50077589 returning error if any.
// Check if a mini app¹ can request the download of a specific file: called when
// handling web_app_request_file_download events »²
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps
//  2. https://core.telegram.org/api/web-events#web-app-request-file-download
//
// Possible errors:
//
//	400 BOT_INVALID: This is not a valid bot.
//
// See https://core.telegram.org/method/bots.checkDownloadFileParams for reference.
func (c *Client) BotsCheckDownloadFileParams(ctx context.Context, request *BotsCheckDownloadFileParamsRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
