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

// DeleteFileRequest represents TL type `deleteFile#6bbe9b2c`.
type DeleteFileRequest struct {
	// Identifier of the file to delete
	FileID int32
}

// DeleteFileRequestTypeID is TL type id of DeleteFileRequest.
const DeleteFileRequestTypeID = 0x6bbe9b2c

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (d *DeleteFileRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteFile#6bbe9b2c as nil")
	}
	b.PutID(DeleteFileRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *DeleteFileRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteFile#6bbe9b2c as nil")
	}
	b.PutInt32(d.FileID)
	return nil
}

// Decode implements bin.Decoder.
func (d *DeleteFileRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteFile#6bbe9b2c to nil")
	}
	if err := b.ConsumeID(DeleteFileRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode deleteFile#6bbe9b2c: %w", err)
	}
	return d.DecodeBare(b)
}

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

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (d *DeleteFileRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if d == nil {
		return fmt.Errorf("can't encode deleteFile#6bbe9b2c as nil")
	}
	b.ObjStart()
	b.PutID("deleteFile")
	b.Comma()
	b.FieldStart("file_id")
	b.PutInt32(d.FileID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (d *DeleteFileRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if d == nil {
		return fmt.Errorf("can't decode deleteFile#6bbe9b2c to nil")
	}

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

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

// DeleteFile invokes method deleteFile#6bbe9b2c returning error if any.
func (c *Client) DeleteFile(ctx context.Context, fileid int32) error {
	var ok Ok

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