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

// RecommendedChatFolder represents TL type `recommendedChatFolder#81d7b4b6`.
type RecommendedChatFolder struct {
	// The chat folder
	Folder ChatFolder
	// Describes a recommended chat folder
	Description string
}

// RecommendedChatFolderTypeID is TL type id of RecommendedChatFolder.
const RecommendedChatFolderTypeID = 0x81d7b4b6

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

func (r *RecommendedChatFolder) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Folder.Zero()) {
		return false
	}
	if !(r.Description == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (r *RecommendedChatFolder) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "recommendedChatFolder",
		ID:   RecommendedChatFolderTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Folder",
			SchemaName: "folder",
		},
		{
			Name:       "Description",
			SchemaName: "description",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *RecommendedChatFolder) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode recommendedChatFolder#81d7b4b6 as nil")
	}
	b.PutID(RecommendedChatFolderTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *RecommendedChatFolder) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode recommendedChatFolder#81d7b4b6 as nil")
	}
	if err := r.Folder.Encode(b); err != nil {
		return fmt.Errorf("unable to encode recommendedChatFolder#81d7b4b6: field folder: %w", err)
	}
	b.PutString(r.Description)
	return nil
}

// Decode implements bin.Decoder.
func (r *RecommendedChatFolder) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode recommendedChatFolder#81d7b4b6 to nil")
	}
	if err := b.ConsumeID(RecommendedChatFolderTypeID); err != nil {
		return fmt.Errorf("unable to decode recommendedChatFolder#81d7b4b6: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *RecommendedChatFolder) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode recommendedChatFolder#81d7b4b6 to nil")
	}
	{
		if err := r.Folder.Decode(b); err != nil {
			return fmt.Errorf("unable to decode recommendedChatFolder#81d7b4b6: field folder: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode recommendedChatFolder#81d7b4b6: field description: %w", err)
		}
		r.Description = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (r *RecommendedChatFolder) EncodeTDLibJSON(b tdjson.Encoder) error {
	if r == nil {
		return fmt.Errorf("can't encode recommendedChatFolder#81d7b4b6 as nil")
	}
	b.ObjStart()
	b.PutID("recommendedChatFolder")
	b.Comma()
	b.FieldStart("folder")
	if err := r.Folder.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode recommendedChatFolder#81d7b4b6: field folder: %w", err)
	}
	b.Comma()
	b.FieldStart("description")
	b.PutString(r.Description)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (r *RecommendedChatFolder) DecodeTDLibJSON(b tdjson.Decoder) error {
	if r == nil {
		return fmt.Errorf("can't decode recommendedChatFolder#81d7b4b6 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("recommendedChatFolder"); err != nil {
				return fmt.Errorf("unable to decode recommendedChatFolder#81d7b4b6: %w", err)
			}
		case "folder":
			if err := r.Folder.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode recommendedChatFolder#81d7b4b6: field folder: %w", err)
			}
		case "description":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode recommendedChatFolder#81d7b4b6: field description: %w", err)
			}
			r.Description = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetDescription returns value of Description field.
func (r *RecommendedChatFolder) GetDescription() (value string) {
	if r == nil {
		return
	}
	return r.Description
}
