// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// MessagesDialogFilters represents TL type `messages.dialogFilters#2ad93719`.
// Folder and folder tags¹ information
//
// Links:
//  1. https://core.telegram.org/api/folders
//
// See https://core.telegram.org/constructor/messages.dialogFilters for reference.
type MessagesDialogFilters struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether folder tags¹ are enabled.
	//
	// Links:
	//  1) https://core.telegram.org/api/folders#folder-tags
	TagsEnabled bool
	// Folders.
	Filters []DialogFilterClass
}

// MessagesDialogFiltersTypeID is TL type id of MessagesDialogFilters.
const MessagesDialogFiltersTypeID = 0x2ad93719

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

func (d *MessagesDialogFilters) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Flags.Zero()) {
		return false
	}
	if !(d.TagsEnabled == false) {
		return false
	}
	if !(d.Filters == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesDialogFilters from given interface.
func (d *MessagesDialogFilters) FillFrom(from interface {
	GetTagsEnabled() (value bool)
	GetFilters() (value []DialogFilterClass)
}) {
	d.TagsEnabled = from.GetTagsEnabled()
	d.Filters = from.GetFilters()
}

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

// TypeName returns name of type in TL schema.
func (*MessagesDialogFilters) TypeName() string {
	return "messages.dialogFilters"
}

// TypeInfo returns info about TL type.
func (d *MessagesDialogFilters) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.dialogFilters",
		ID:   MessagesDialogFiltersTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TagsEnabled",
			SchemaName: "tags_enabled",
			Null:       !d.Flags.Has(0),
		},
		{
			Name:       "Filters",
			SchemaName: "filters",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (d *MessagesDialogFilters) SetFlags() {
	if !(d.TagsEnabled == false) {
		d.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (d *MessagesDialogFilters) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode messages.dialogFilters#2ad93719 as nil")
	}
	b.PutID(MessagesDialogFiltersTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *MessagesDialogFilters) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode messages.dialogFilters#2ad93719 as nil")
	}
	d.SetFlags()
	if err := d.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.dialogFilters#2ad93719: field flags: %w", err)
	}
	b.PutVectorHeader(len(d.Filters))
	for idx, v := range d.Filters {
		if v == nil {
			return fmt.Errorf("unable to encode messages.dialogFilters#2ad93719: field filters element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.dialogFilters#2ad93719: field filters element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (d *MessagesDialogFilters) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode messages.dialogFilters#2ad93719 to nil")
	}
	if err := b.ConsumeID(MessagesDialogFiltersTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.dialogFilters#2ad93719: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *MessagesDialogFilters) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode messages.dialogFilters#2ad93719 to nil")
	}
	{
		if err := d.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.dialogFilters#2ad93719: field flags: %w", err)
		}
	}
	d.TagsEnabled = d.Flags.Has(0)
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.dialogFilters#2ad93719: field filters: %w", err)
		}

		if headerLen > 0 {
			d.Filters = make([]DialogFilterClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeDialogFilter(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.dialogFilters#2ad93719: field filters: %w", err)
			}
			d.Filters = append(d.Filters, value)
		}
	}
	return nil
}

// SetTagsEnabled sets value of TagsEnabled conditional field.
func (d *MessagesDialogFilters) SetTagsEnabled(value bool) {
	if value {
		d.Flags.Set(0)
		d.TagsEnabled = true
	} else {
		d.Flags.Unset(0)
		d.TagsEnabled = false
	}
}

// GetTagsEnabled returns value of TagsEnabled conditional field.
func (d *MessagesDialogFilters) GetTagsEnabled() (value bool) {
	if d == nil {
		return
	}
	return d.Flags.Has(0)
}

// GetFilters returns value of Filters field.
func (d *MessagesDialogFilters) GetFilters() (value []DialogFilterClass) {
	if d == nil {
		return
	}
	return d.Filters
}

// MapFilters returns field Filters wrapped in DialogFilterClassArray helper.
func (d *MessagesDialogFilters) MapFilters() (value DialogFilterClassArray) {
	return DialogFilterClassArray(d.Filters)
}
