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

// MessagesViewSponsoredMessageRequest represents TL type `messages.viewSponsoredMessage#269e3643`.
// Mark a specific sponsored message »¹ as read
//
// Links:
//  1. https://core.telegram.org/api/sponsored-messages
//
// See https://core.telegram.org/method/messages.viewSponsoredMessage for reference.
type MessagesViewSponsoredMessageRequest struct {
	// The ad's unique ID.
	RandomID []byte
}

// MessagesViewSponsoredMessageRequestTypeID is TL type id of MessagesViewSponsoredMessageRequest.
const MessagesViewSponsoredMessageRequestTypeID = 0x269e3643

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

func (v *MessagesViewSponsoredMessageRequest) Zero() bool {
	if v == nil {
		return true
	}
	if !(v.RandomID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesViewSponsoredMessageRequest from given interface.
func (v *MessagesViewSponsoredMessageRequest) FillFrom(from interface {
	GetRandomID() (value []byte)
}) {
	v.RandomID = from.GetRandomID()
}

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

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

// TypeInfo returns info about TL type.
func (v *MessagesViewSponsoredMessageRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.viewSponsoredMessage",
		ID:   MessagesViewSponsoredMessageRequestTypeID,
	}
	if v == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "RandomID",
			SchemaName: "random_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (v *MessagesViewSponsoredMessageRequest) Encode(b *bin.Buffer) error {
	if v == nil {
		return fmt.Errorf("can't encode messages.viewSponsoredMessage#269e3643 as nil")
	}
	b.PutID(MessagesViewSponsoredMessageRequestTypeID)
	return v.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (v *MessagesViewSponsoredMessageRequest) EncodeBare(b *bin.Buffer) error {
	if v == nil {
		return fmt.Errorf("can't encode messages.viewSponsoredMessage#269e3643 as nil")
	}
	b.PutBytes(v.RandomID)
	return nil
}

// Decode implements bin.Decoder.
func (v *MessagesViewSponsoredMessageRequest) Decode(b *bin.Buffer) error {
	if v == nil {
		return fmt.Errorf("can't decode messages.viewSponsoredMessage#269e3643 to nil")
	}
	if err := b.ConsumeID(MessagesViewSponsoredMessageRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.viewSponsoredMessage#269e3643: %w", err)
	}
	return v.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (v *MessagesViewSponsoredMessageRequest) DecodeBare(b *bin.Buffer) error {
	if v == nil {
		return fmt.Errorf("can't decode messages.viewSponsoredMessage#269e3643 to nil")
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode messages.viewSponsoredMessage#269e3643: field random_id: %w", err)
		}
		v.RandomID = value
	}
	return nil
}

// GetRandomID returns value of RandomID field.
func (v *MessagesViewSponsoredMessageRequest) GetRandomID() (value []byte) {
	if v == nil {
		return
	}
	return v.RandomID
}

// MessagesViewSponsoredMessage invokes method messages.viewSponsoredMessage#269e3643 returning error if any.
// Mark a specific sponsored message »¹ as read
//
// Links:
//  1. https://core.telegram.org/api/sponsored-messages
//
// See https://core.telegram.org/method/messages.viewSponsoredMessage for reference.
func (c *Client) MessagesViewSponsoredMessage(ctx context.Context, randomid []byte) (bool, error) {
	var result BoolBox

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