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

// MessagesReportMessagesDeliveryRequest represents TL type `messages.reportMessagesDelivery#5a6d7395`.
// Used for Telegram Gateway verification messages »¹: indicate to the server that one
// or more message²s were received by the client, if requested by the message³
// report_delivery_until_date flag or the equivalent flag in push notifications⁴.
//
// Links:
//  1. https://telegram.org/blog/star-messages-gateway-2-0-and-more#save-even-more-on-user-verification
//  2. https://core.telegram.org/constructor/message
//  3. https://core.telegram.org/constructor/message
//  4. https://core.telegram.org/api/push-updates
//
// See https://core.telegram.org/method/messages.reportMessagesDelivery for reference.
type MessagesReportMessagesDeliveryRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Must be set if the messages were received from a push notification¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/push-updates
	Push bool
	// The peer where the messages were received.
	Peer InputPeerClass
	// The IDs of the received messages.
	ID []int
}

// MessagesReportMessagesDeliveryRequestTypeID is TL type id of MessagesReportMessagesDeliveryRequest.
const MessagesReportMessagesDeliveryRequestTypeID = 0x5a6d7395

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

func (r *MessagesReportMessagesDeliveryRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Flags.Zero()) {
		return false
	}
	if !(r.Push == false) {
		return false
	}
	if !(r.Peer == nil) {
		return false
	}
	if !(r.ID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesReportMessagesDeliveryRequest from given interface.
func (r *MessagesReportMessagesDeliveryRequest) FillFrom(from interface {
	GetPush() (value bool)
	GetPeer() (value InputPeerClass)
	GetID() (value []int)
}) {
	r.Push = from.GetPush()
	r.Peer = from.GetPeer()
	r.ID = from.GetID()
}

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

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

// TypeInfo returns info about TL type.
func (r *MessagesReportMessagesDeliveryRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.reportMessagesDelivery",
		ID:   MessagesReportMessagesDeliveryRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Push",
			SchemaName: "push",
			Null:       !r.Flags.Has(0),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "ID",
			SchemaName: "id",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (r *MessagesReportMessagesDeliveryRequest) SetFlags() {
	if !(r.Push == false) {
		r.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (r *MessagesReportMessagesDeliveryRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.reportMessagesDelivery#5a6d7395 as nil")
	}
	b.PutID(MessagesReportMessagesDeliveryRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *MessagesReportMessagesDeliveryRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.reportMessagesDelivery#5a6d7395 as nil")
	}
	r.SetFlags()
	if err := r.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.reportMessagesDelivery#5a6d7395: field flags: %w", err)
	}
	if r.Peer == nil {
		return fmt.Errorf("unable to encode messages.reportMessagesDelivery#5a6d7395: field peer is nil")
	}
	if err := r.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.reportMessagesDelivery#5a6d7395: field peer: %w", err)
	}
	b.PutVectorHeader(len(r.ID))
	for _, v := range r.ID {
		b.PutInt(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (r *MessagesReportMessagesDeliveryRequest) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.reportMessagesDelivery#5a6d7395 to nil")
	}
	if err := b.ConsumeID(MessagesReportMessagesDeliveryRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.reportMessagesDelivery#5a6d7395: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *MessagesReportMessagesDeliveryRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.reportMessagesDelivery#5a6d7395 to nil")
	}
	{
		if err := r.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messages.reportMessagesDelivery#5a6d7395: field flags: %w", err)
		}
	}
	r.Push = r.Flags.Has(0)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.reportMessagesDelivery#5a6d7395: field peer: %w", err)
		}
		r.Peer = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.reportMessagesDelivery#5a6d7395: field id: %w", err)
		}

		if headerLen > 0 {
			r.ID = make([]int, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int()
			if err != nil {
				return fmt.Errorf("unable to decode messages.reportMessagesDelivery#5a6d7395: field id: %w", err)
			}
			r.ID = append(r.ID, value)
		}
	}
	return nil
}

// SetPush sets value of Push conditional field.
func (r *MessagesReportMessagesDeliveryRequest) SetPush(value bool) {
	if value {
		r.Flags.Set(0)
		r.Push = true
	} else {
		r.Flags.Unset(0)
		r.Push = false
	}
}

// GetPush returns value of Push conditional field.
func (r *MessagesReportMessagesDeliveryRequest) GetPush() (value bool) {
	if r == nil {
		return
	}
	return r.Flags.Has(0)
}

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

// GetID returns value of ID field.
func (r *MessagesReportMessagesDeliveryRequest) GetID() (value []int) {
	if r == nil {
		return
	}
	return r.ID
}

// MessagesReportMessagesDelivery invokes method messages.reportMessagesDelivery#5a6d7395 returning error if any.
// Used for Telegram Gateway verification messages »¹: indicate to the server that one
// or more message²s were received by the client, if requested by the message³
// report_delivery_until_date flag or the equivalent flag in push notifications⁴.
//
// Links:
//  1. https://telegram.org/blog/star-messages-gateway-2-0-and-more#save-even-more-on-user-verification
//  2. https://core.telegram.org/constructor/message
//  3. https://core.telegram.org/constructor/message
//  4. https://core.telegram.org/api/push-updates
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//
// See https://core.telegram.org/method/messages.reportMessagesDelivery for reference.
func (c *Client) MessagesReportMessagesDelivery(ctx context.Context, request *MessagesReportMessagesDeliveryRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
