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

// MessagesDeleteTopicHistoryRequest represents TL type `messages.deleteTopicHistory#d2816f10`.
//
// See https://core.telegram.org/method/messages.deleteTopicHistory for reference.
type MessagesDeleteTopicHistoryRequest struct {
	// Peer field of MessagesDeleteTopicHistoryRequest.
	Peer InputPeerClass
	// TopMsgID field of MessagesDeleteTopicHistoryRequest.
	TopMsgID int
}

// MessagesDeleteTopicHistoryRequestTypeID is TL type id of MessagesDeleteTopicHistoryRequest.
const MessagesDeleteTopicHistoryRequestTypeID = 0xd2816f10

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

func (d *MessagesDeleteTopicHistoryRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Peer == nil) {
		return false
	}
	if !(d.TopMsgID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesDeleteTopicHistoryRequest from given interface.
func (d *MessagesDeleteTopicHistoryRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetTopMsgID() (value int)
}) {
	d.Peer = from.GetPeer()
	d.TopMsgID = from.GetTopMsgID()
}

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

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

// TypeInfo returns info about TL type.
func (d *MessagesDeleteTopicHistoryRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.deleteTopicHistory",
		ID:   MessagesDeleteTopicHistoryRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "TopMsgID",
			SchemaName: "top_msg_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *MessagesDeleteTopicHistoryRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode messages.deleteTopicHistory#d2816f10 as nil")
	}
	b.PutID(MessagesDeleteTopicHistoryRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *MessagesDeleteTopicHistoryRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode messages.deleteTopicHistory#d2816f10 as nil")
	}
	if d.Peer == nil {
		return fmt.Errorf("unable to encode messages.deleteTopicHistory#d2816f10: field peer is nil")
	}
	if err := d.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.deleteTopicHistory#d2816f10: field peer: %w", err)
	}
	b.PutInt(d.TopMsgID)
	return nil
}

// Decode implements bin.Decoder.
func (d *MessagesDeleteTopicHistoryRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode messages.deleteTopicHistory#d2816f10 to nil")
	}
	if err := b.ConsumeID(MessagesDeleteTopicHistoryRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.deleteTopicHistory#d2816f10: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *MessagesDeleteTopicHistoryRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode messages.deleteTopicHistory#d2816f10 to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.deleteTopicHistory#d2816f10: field peer: %w", err)
		}
		d.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.deleteTopicHistory#d2816f10: field top_msg_id: %w", err)
		}
		d.TopMsgID = value
	}
	return nil
}

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

// GetTopMsgID returns value of TopMsgID field.
func (d *MessagesDeleteTopicHistoryRequest) GetTopMsgID() (value int) {
	if d == nil {
		return
	}
	return d.TopMsgID
}

// MessagesDeleteTopicHistory invokes method messages.deleteTopicHistory#d2816f10 returning error if any.
//
// See https://core.telegram.org/method/messages.deleteTopicHistory for reference.
func (c *Client) MessagesDeleteTopicHistory(ctx context.Context, request *MessagesDeleteTopicHistoryRequest) (*MessagesAffectedHistory, error) {
	var result MessagesAffectedHistory

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