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

// GetChatFolderDefaultIconNameRequest represents TL type `getChatFolderDefaultIconName#2cf7a067`.
type GetChatFolderDefaultIconNameRequest struct {
	// Chat folder
	Folder ChatFolder
}

// GetChatFolderDefaultIconNameRequestTypeID is TL type id of GetChatFolderDefaultIconNameRequest.
const GetChatFolderDefaultIconNameRequestTypeID = 0x2cf7a067

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

func (g *GetChatFolderDefaultIconNameRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Folder.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetChatFolderDefaultIconNameRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getChatFolderDefaultIconName",
		ID:   GetChatFolderDefaultIconNameRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Folder",
			SchemaName: "folder",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetChatFolderDefaultIconNameRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatFolderDefaultIconName#2cf7a067 as nil")
	}
	b.PutID(GetChatFolderDefaultIconNameRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetChatFolderDefaultIconNameRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatFolderDefaultIconName#2cf7a067 as nil")
	}
	if err := g.Folder.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getChatFolderDefaultIconName#2cf7a067: field folder: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *GetChatFolderDefaultIconNameRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatFolderDefaultIconName#2cf7a067 to nil")
	}
	if err := b.ConsumeID(GetChatFolderDefaultIconNameRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode getChatFolderDefaultIconName#2cf7a067: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GetChatFolderDefaultIconNameRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatFolderDefaultIconName#2cf7a067 to nil")
	}
	{
		if err := g.Folder.Decode(b); err != nil {
			return fmt.Errorf("unable to decode getChatFolderDefaultIconName#2cf7a067: field folder: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetChatFolderDefaultIconNameRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getChatFolderDefaultIconName#2cf7a067 as nil")
	}
	b.ObjStart()
	b.PutID("getChatFolderDefaultIconName")
	b.Comma()
	b.FieldStart("folder")
	if err := g.Folder.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getChatFolderDefaultIconName#2cf7a067: field folder: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (g *GetChatFolderDefaultIconNameRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if g == nil {
		return fmt.Errorf("can't decode getChatFolderDefaultIconName#2cf7a067 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getChatFolderDefaultIconName"); err != nil {
				return fmt.Errorf("unable to decode getChatFolderDefaultIconName#2cf7a067: %w", err)
			}
		case "folder":
			if err := g.Folder.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode getChatFolderDefaultIconName#2cf7a067: field folder: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetFolder returns value of Folder field.
func (g *GetChatFolderDefaultIconNameRequest) GetFolder() (value ChatFolder) {
	if g == nil {
		return
	}
	return g.Folder
}

// GetChatFolderDefaultIconName invokes method getChatFolderDefaultIconName#2cf7a067 returning error if any.
func (c *Client) GetChatFolderDefaultIconName(ctx context.Context, folder ChatFolder) (*ChatFolderIcon, error) {
	var result ChatFolderIcon

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