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

// MessagesReadSavedHistoryRequest represents TL type `messages.readSavedHistory#ba4a3b5b`.
// Mark messages as read in a monoforum topic »¹.
//
// Links:
//  1. https://core.telegram.org/api/monoforum
//
// See https://core.telegram.org/method/messages.readSavedHistory for reference.
type MessagesReadSavedHistoryRequest struct {
	// ID of the monoforum group.
	ParentPeer InputPeerClass
	// ID of the topic.
	Peer InputPeerClass
	// If a positive value is passed, only messages with identifiers less or equal than the
	// given one will be read.
	MaxID int
}

// MessagesReadSavedHistoryRequestTypeID is TL type id of MessagesReadSavedHistoryRequest.
const MessagesReadSavedHistoryRequestTypeID = 0xba4a3b5b

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

func (r *MessagesReadSavedHistoryRequest) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.ParentPeer == nil) {
		return false
	}
	if !(r.Peer == nil) {
		return false
	}
	if !(r.MaxID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesReadSavedHistoryRequest from given interface.
func (r *MessagesReadSavedHistoryRequest) FillFrom(from interface {
	GetParentPeer() (value InputPeerClass)
	GetPeer() (value InputPeerClass)
	GetMaxID() (value int)
}) {
	r.ParentPeer = from.GetParentPeer()
	r.Peer = from.GetPeer()
	r.MaxID = from.GetMaxID()
}

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

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

// TypeInfo returns info about TL type.
func (r *MessagesReadSavedHistoryRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.readSavedHistory",
		ID:   MessagesReadSavedHistoryRequestTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ParentPeer",
			SchemaName: "parent_peer",
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "MaxID",
			SchemaName: "max_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (r *MessagesReadSavedHistoryRequest) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.readSavedHistory#ba4a3b5b as nil")
	}
	b.PutID(MessagesReadSavedHistoryRequestTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *MessagesReadSavedHistoryRequest) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode messages.readSavedHistory#ba4a3b5b as nil")
	}
	if r.ParentPeer == nil {
		return fmt.Errorf("unable to encode messages.readSavedHistory#ba4a3b5b: field parent_peer is nil")
	}
	if err := r.ParentPeer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.readSavedHistory#ba4a3b5b: field parent_peer: %w", err)
	}
	if r.Peer == nil {
		return fmt.Errorf("unable to encode messages.readSavedHistory#ba4a3b5b: field peer is nil")
	}
	if err := r.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.readSavedHistory#ba4a3b5b: field peer: %w", err)
	}
	b.PutInt(r.MaxID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (r *MessagesReadSavedHistoryRequest) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode messages.readSavedHistory#ba4a3b5b to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.readSavedHistory#ba4a3b5b: field parent_peer: %w", err)
		}
		r.ParentPeer = value
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.readSavedHistory#ba4a3b5b: field peer: %w", err)
		}
		r.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.readSavedHistory#ba4a3b5b: field max_id: %w", err)
		}
		r.MaxID = value
	}
	return nil
}

// GetParentPeer returns value of ParentPeer field.
func (r *MessagesReadSavedHistoryRequest) GetParentPeer() (value InputPeerClass) {
	if r == nil {
		return
	}
	return r.ParentPeer
}

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

// GetMaxID returns value of MaxID field.
func (r *MessagesReadSavedHistoryRequest) GetMaxID() (value int) {
	if r == nil {
		return
	}
	return r.MaxID
}

// MessagesReadSavedHistory invokes method messages.readSavedHistory#ba4a3b5b returning error if any.
// Mark messages as read in a monoforum topic »¹.
//
// Links:
//  1. https://core.telegram.org/api/monoforum
//
// Possible errors:
//
//	400 CHAT_ADMIN_REQUIRED: You must be an admin in this chat to do this.
//	400 PARENT_PEER_INVALID: The specified parent_peer is invalid.
//
// See https://core.telegram.org/method/messages.readSavedHistory for reference.
func (c *Client) MessagesReadSavedHistory(ctx context.Context, request *MessagesReadSavedHistoryRequest) (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
}
