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

// CleanFileNameRequest represents TL type `cleanFileName#39b1f7fb`.
type CleanFileNameRequest struct {
	// File name or path to the file
	FileName string
}

// CleanFileNameRequestTypeID is TL type id of CleanFileNameRequest.
const CleanFileNameRequestTypeID = 0x39b1f7fb

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CleanFileNameRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "cleanFileName",
		ID:   CleanFileNameRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "FileName",
			SchemaName: "file_name",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CleanFileNameRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode cleanFileName#39b1f7fb as nil")
	}
	b.PutID(CleanFileNameRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CleanFileNameRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode cleanFileName#39b1f7fb as nil")
	}
	b.PutString(c.FileName)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *CleanFileNameRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode cleanFileName#39b1f7fb to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode cleanFileName#39b1f7fb: field file_name: %w", err)
		}
		c.FileName = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CleanFileNameRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode cleanFileName#39b1f7fb as nil")
	}
	b.ObjStart()
	b.PutID("cleanFileName")
	b.Comma()
	b.FieldStart("file_name")
	b.PutString(c.FileName)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

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

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

// CleanFileName invokes method cleanFileName#39b1f7fb returning error if any.
func (c *Client) CleanFileName(ctx context.Context, filename string) (*Text, error) {
	var result Text

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