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

// GetFileRequest represents TL type `getFile#5c9efd4e`.
type GetFileRequest struct {
	// Identifier of the file to get
	FileID int32
}

// GetFileRequestTypeID is TL type id of GetFileRequest.
const GetFileRequestTypeID = 0x5c9efd4e

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

func (g *GetFileRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.FileID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetFileRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getFile",
		ID:   GetFileRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "FileID",
			SchemaName: "file_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetFileRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getFile#5c9efd4e as nil")
	}
	b.PutID(GetFileRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetFileRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getFile#5c9efd4e as nil")
	}
	b.PutInt32(g.FileID)
	return nil
}

// Decode implements bin.Decoder.
func (g *GetFileRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getFile#5c9efd4e to nil")
	}
	if err := b.ConsumeID(GetFileRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getFile#5c9efd4e: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetFileRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getFile#5c9efd4e to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getFile#5c9efd4e: field file_id: %w", err)
		}
		g.FileID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetFileRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getFile#5c9efd4e as nil")
	}
	b.ObjStart()
	b.PutID("getFile")
	b.Comma()
	b.FieldStart("file_id")
	b.PutInt32(g.FileID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetFileRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getFile#5c9efd4e to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getFile"); err != nil {
				return fmt.Errorf("unable to decode getFile#5c9efd4e: %w", err)
			}
		case "file_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getFile#5c9efd4e: field file_id: %w", err)
			}
			g.FileID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetFileID returns value of FileID field.
func (g *GetFileRequest) GetFileID() (value int32) {
	if g == nil {
		return
	}
	return g.FileID
}

// GetFile invokes method getFile#5c9efd4e returning error if any.
func (c *Client) GetFile(ctx context.Context, fileid int32) (*File, error) {
	var result File

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