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

// CheckWebAppFileDownloadRequest represents TL type `checkWebAppFileDownload#e8ca44e2`.
type CheckWebAppFileDownloadRequest struct {
	// Identifier of the bot, providing the Web App
	BotUserID int64
	// Name of the file
	FileName string
	// URL of the file
	URL string
}

// CheckWebAppFileDownloadRequestTypeID is TL type id of CheckWebAppFileDownloadRequest.
const CheckWebAppFileDownloadRequestTypeID = 0xe8ca44e2

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CheckWebAppFileDownloadRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "checkWebAppFileDownload",
		ID:   CheckWebAppFileDownloadRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "BotUserID",
			SchemaName: "bot_user_id",
		},
		{
			Name:       "FileName",
			SchemaName: "file_name",
		},
		{
			Name:       "URL",
			SchemaName: "url",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CheckWebAppFileDownloadRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode checkWebAppFileDownload#e8ca44e2 as nil")
	}
	b.PutID(CheckWebAppFileDownloadRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CheckWebAppFileDownloadRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode checkWebAppFileDownload#e8ca44e2 as nil")
	}
	b.PutInt53(c.BotUserID)
	b.PutString(c.FileName)
	b.PutString(c.URL)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *CheckWebAppFileDownloadRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode checkWebAppFileDownload#e8ca44e2 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode checkWebAppFileDownload#e8ca44e2: field bot_user_id: %w", err)
		}
		c.BotUserID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode checkWebAppFileDownload#e8ca44e2: field file_name: %w", err)
		}
		c.FileName = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode checkWebAppFileDownload#e8ca44e2: field url: %w", err)
		}
		c.URL = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CheckWebAppFileDownloadRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode checkWebAppFileDownload#e8ca44e2 as nil")
	}
	b.ObjStart()
	b.PutID("checkWebAppFileDownload")
	b.Comma()
	b.FieldStart("bot_user_id")
	b.PutInt53(c.BotUserID)
	b.Comma()
	b.FieldStart("file_name")
	b.PutString(c.FileName)
	b.Comma()
	b.FieldStart("url")
	b.PutString(c.URL)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CheckWebAppFileDownloadRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode checkWebAppFileDownload#e8ca44e2 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("checkWebAppFileDownload"); err != nil {
				return fmt.Errorf("unable to decode checkWebAppFileDownload#e8ca44e2: %w", err)
			}
		case "bot_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode checkWebAppFileDownload#e8ca44e2: field bot_user_id: %w", err)
			}
			c.BotUserID = value
		case "file_name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode checkWebAppFileDownload#e8ca44e2: field file_name: %w", err)
			}
			c.FileName = value
		case "url":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode checkWebAppFileDownload#e8ca44e2: field url: %w", err)
			}
			c.URL = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

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

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

// CheckWebAppFileDownload invokes method checkWebAppFileDownload#e8ca44e2 returning error if any.
func (c *Client) CheckWebAppFileDownload(ctx context.Context, request *CheckWebAppFileDownloadRequest) error {
	var ok Ok

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