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

// MessagesAppendTodoListRequest represents TL type `messages.appendTodoList#21a61057`.
// Appends one or more items to a todo list »¹.
//
// Links:
//  1. https://core.telegram.org/api/todo
//
// See https://core.telegram.org/method/messages.appendTodoList for reference.
type MessagesAppendTodoListRequest struct {
	// Peer where the todo list was posted.
	Peer InputPeerClass
	// ID of the message with the todo list.
	MsgID int
	// Items to append.
	List []TodoItem
}

// MessagesAppendTodoListRequestTypeID is TL type id of MessagesAppendTodoListRequest.
const MessagesAppendTodoListRequestTypeID = 0x21a61057

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

func (a *MessagesAppendTodoListRequest) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.Peer == nil) {
		return false
	}
	if !(a.MsgID == 0) {
		return false
	}
	if !(a.List == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesAppendTodoListRequest from given interface.
func (a *MessagesAppendTodoListRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetMsgID() (value int)
	GetList() (value []TodoItem)
}) {
	a.Peer = from.GetPeer()
	a.MsgID = from.GetMsgID()
	a.List = from.GetList()
}

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

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

// TypeInfo returns info about TL type.
func (a *MessagesAppendTodoListRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.appendTodoList",
		ID:   MessagesAppendTodoListRequestTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "MsgID",
			SchemaName: "msg_id",
		},
		{
			Name:       "List",
			SchemaName: "list",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *MessagesAppendTodoListRequest) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode messages.appendTodoList#21a61057 as nil")
	}
	b.PutID(MessagesAppendTodoListRequestTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *MessagesAppendTodoListRequest) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode messages.appendTodoList#21a61057 as nil")
	}
	if a.Peer == nil {
		return fmt.Errorf("unable to encode messages.appendTodoList#21a61057: field peer is nil")
	}
	if err := a.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.appendTodoList#21a61057: field peer: %w", err)
	}
	b.PutInt(a.MsgID)
	b.PutVectorHeader(len(a.List))
	for idx, v := range a.List {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.appendTodoList#21a61057: field list element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *MessagesAppendTodoListRequest) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode messages.appendTodoList#21a61057 to nil")
	}
	if err := b.ConsumeID(MessagesAppendTodoListRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.appendTodoList#21a61057: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *MessagesAppendTodoListRequest) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode messages.appendTodoList#21a61057 to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.appendTodoList#21a61057: field peer: %w", err)
		}
		a.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.appendTodoList#21a61057: field msg_id: %w", err)
		}
		a.MsgID = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.appendTodoList#21a61057: field list: %w", err)
		}

		if headerLen > 0 {
			a.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 messages.appendTodoList#21a61057: field list: %w", err)
			}
			a.List = append(a.List, value)
		}
	}
	return nil
}

// GetPeer returns value of Peer field.
func (a *MessagesAppendTodoListRequest) GetPeer() (value InputPeerClass) {
	if a == nil {
		return
	}
	return a.Peer
}

// GetMsgID returns value of MsgID field.
func (a *MessagesAppendTodoListRequest) GetMsgID() (value int) {
	if a == nil {
		return
	}
	return a.MsgID
}

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

// MessagesAppendTodoList invokes method messages.appendTodoList#21a61057 returning error if any.
// Appends one or more items to a todo list »¹.
//
// Links:
//  1. https://core.telegram.org/api/todo
//
// Possible errors:
//
//	400 MESSAGE_ID_INVALID: The provided message id is invalid.
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//	400 TODO_ITEM_DUPLICATE: Duplicate checklist items detected.
//	400 TODO_NOT_MODIFIED: No todo items were specified, so no changes were made to the todo list.
//
// See https://core.telegram.org/method/messages.appendTodoList for reference.
func (c *Client) MessagesAppendTodoList(ctx context.Context, request *MessagesAppendTodoListRequest) (UpdatesClass, error) {
	var result UpdatesBox

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