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

// TodoList represents TL type `todoList#49b92a26`.
// Represents a todo list »¹.
//
// Links:
//  1. https://core.telegram.org/api/todo
//
// See https://core.telegram.org/constructor/todoList for reference.
type TodoList struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// If set, users different from the creator of the list can append items to the list.
	OthersCanAppend bool
	// If set, users different from the creator of the list can complete items in the list.
	OthersCanComplete bool
	// Title of the todo list, maximum length equal to todo_title_length_max »¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/config#todo-title-length-max
	Title TextWithEntities
	// Items of the list.
	List []TodoItem
}

// TodoListTypeID is TL type id of TodoList.
const TodoListTypeID = 0x49b92a26

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

func (t *TodoList) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Flags.Zero()) {
		return false
	}
	if !(t.OthersCanAppend == false) {
		return false
	}
	if !(t.OthersCanComplete == false) {
		return false
	}
	if !(t.Title.Zero()) {
		return false
	}
	if !(t.List == nil) {
		return false
	}

	return true
}

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

// FillFrom fills TodoList from given interface.
func (t *TodoList) FillFrom(from interface {
	GetOthersCanAppend() (value bool)
	GetOthersCanComplete() (value bool)
	GetTitle() (value TextWithEntities)
	GetList() (value []TodoItem)
}) {
	t.OthersCanAppend = from.GetOthersCanAppend()
	t.OthersCanComplete = from.GetOthersCanComplete()
	t.Title = from.GetTitle()
	t.List = from.GetList()
}

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

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

// TypeInfo returns info about TL type.
func (t *TodoList) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "todoList",
		ID:   TodoListTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "OthersCanAppend",
			SchemaName: "others_can_append",
			Null:       !t.Flags.Has(0),
		},
		{
			Name:       "OthersCanComplete",
			SchemaName: "others_can_complete",
			Null:       !t.Flags.Has(1),
		},
		{
			Name:       "Title",
			SchemaName: "title",
		},
		{
			Name:       "List",
			SchemaName: "list",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (t *TodoList) SetFlags() {
	if !(t.OthersCanAppend == false) {
		t.Flags.Set(0)
	}
	if !(t.OthersCanComplete == false) {
		t.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (t *TodoList) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode todoList#49b92a26 as nil")
	}
	b.PutID(TodoListTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *TodoList) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode todoList#49b92a26 as nil")
	}
	t.SetFlags()
	if err := t.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode todoList#49b92a26: field flags: %w", err)
	}
	if err := t.Title.Encode(b); err != nil {
		return fmt.Errorf("unable to encode todoList#49b92a26: field title: %w", err)
	}
	b.PutVectorHeader(len(t.List))
	for idx, v := range t.List {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode todoList#49b92a26: field list element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (t *TodoList) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode todoList#49b92a26 to nil")
	}
	if err := b.ConsumeID(TodoListTypeID); err != nil {
		return fmt.Errorf("unable to decode todoList#49b92a26: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *TodoList) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode todoList#49b92a26 to nil")
	}
	{
		if err := t.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode todoList#49b92a26: field flags: %w", err)
		}
	}
	t.OthersCanAppend = t.Flags.Has(0)
	t.OthersCanComplete = t.Flags.Has(1)
	{
		if err := t.Title.Decode(b); err != nil {
			return fmt.Errorf("unable to decode todoList#49b92a26: field title: %w", err)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode todoList#49b92a26: field list: %w", err)
		}

		if headerLen > 0 {
			t.List = make([]TodoItem, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value TodoItem
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode todoList#49b92a26: field list: %w", err)
			}
			t.List = append(t.List, value)
		}
	}
	return nil
}

// SetOthersCanAppend sets value of OthersCanAppend conditional field.
func (t *TodoList) SetOthersCanAppend(value bool) {
	if value {
		t.Flags.Set(0)
		t.OthersCanAppend = true
	} else {
		t.Flags.Unset(0)
		t.OthersCanAppend = false
	}
}

// GetOthersCanAppend returns value of OthersCanAppend conditional field.
func (t *TodoList) GetOthersCanAppend() (value bool) {
	if t == nil {
		return
	}
	return t.Flags.Has(0)
}

// SetOthersCanComplete sets value of OthersCanComplete conditional field.
func (t *TodoList) SetOthersCanComplete(value bool) {
	if value {
		t.Flags.Set(1)
		t.OthersCanComplete = true
	} else {
		t.Flags.Unset(1)
		t.OthersCanComplete = false
	}
}

// GetOthersCanComplete returns value of OthersCanComplete conditional field.
func (t *TodoList) GetOthersCanComplete() (value bool) {
	if t == nil {
		return
	}
	return t.Flags.Has(1)
}

// GetTitle returns value of Title field.
func (t *TodoList) GetTitle() (value TextWithEntities) {
	if t == nil {
		return
	}
	return t.Title
}

// GetList returns value of List field.
func (t *TodoList) GetList() (value []TodoItem) {
	if t == nil {
		return
	}
	return t.List
}
