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

// MessagesGetPersonalChannelHistoryRequest represents TL type `messages.getPersonalChannelHistory#55fb0996`.
//
// See https://core.telegram.org/method/messages.getPersonalChannelHistory for reference.
type MessagesGetPersonalChannelHistoryRequest struct {
	// UserID field of MessagesGetPersonalChannelHistoryRequest.
	UserID InputUserClass
	// Limit field of MessagesGetPersonalChannelHistoryRequest.
	Limit int
	// MaxID field of MessagesGetPersonalChannelHistoryRequest.
	MaxID int
	// MinID field of MessagesGetPersonalChannelHistoryRequest.
	MinID int
	// Hash field of MessagesGetPersonalChannelHistoryRequest.
	Hash int64
}

// MessagesGetPersonalChannelHistoryRequestTypeID is TL type id of MessagesGetPersonalChannelHistoryRequest.
const MessagesGetPersonalChannelHistoryRequestTypeID = 0x55fb0996

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

func (g *MessagesGetPersonalChannelHistoryRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.UserID == nil) {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}
	if !(g.MaxID == 0) {
		return false
	}
	if !(g.MinID == 0) {
		return false
	}
	if !(g.Hash == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesGetPersonalChannelHistoryRequest from given interface.
func (g *MessagesGetPersonalChannelHistoryRequest) FillFrom(from interface {
	GetUserID() (value InputUserClass)
	GetLimit() (value int)
	GetMaxID() (value int)
	GetMinID() (value int)
	GetHash() (value int64)
}) {
	g.UserID = from.GetUserID()
	g.Limit = from.GetLimit()
	g.MaxID = from.GetMaxID()
	g.MinID = from.GetMinID()
	g.Hash = from.GetHash()
}

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

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

// TypeInfo returns info about TL type.
func (g *MessagesGetPersonalChannelHistoryRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.getPersonalChannelHistory",
		ID:   MessagesGetPersonalChannelHistoryRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
		{
			Name:       "MaxID",
			SchemaName: "max_id",
		},
		{
			Name:       "MinID",
			SchemaName: "min_id",
		},
		{
			Name:       "Hash",
			SchemaName: "hash",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *MessagesGetPersonalChannelHistoryRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getPersonalChannelHistory#55fb0996 as nil")
	}
	b.PutID(MessagesGetPersonalChannelHistoryRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *MessagesGetPersonalChannelHistoryRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode messages.getPersonalChannelHistory#55fb0996 as nil")
	}
	if g.UserID == nil {
		return fmt.Errorf("unable to encode messages.getPersonalChannelHistory#55fb0996: field user_id is nil")
	}
	if err := g.UserID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.getPersonalChannelHistory#55fb0996: field user_id: %w", err)
	}
	b.PutInt(g.Limit)
	b.PutInt(g.MaxID)
	b.PutInt(g.MinID)
	b.PutLong(g.Hash)
	return nil
}

// Decode implements bin.Decoder.
func (g *MessagesGetPersonalChannelHistoryRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getPersonalChannelHistory#55fb0996 to nil")
	}
	if err := b.ConsumeID(MessagesGetPersonalChannelHistoryRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.getPersonalChannelHistory#55fb0996: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *MessagesGetPersonalChannelHistoryRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode messages.getPersonalChannelHistory#55fb0996 to nil")
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.getPersonalChannelHistory#55fb0996: field user_id: %w", err)
		}
		g.UserID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getPersonalChannelHistory#55fb0996: field limit: %w", err)
		}
		g.Limit = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getPersonalChannelHistory#55fb0996: field max_id: %w", err)
		}
		g.MaxID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getPersonalChannelHistory#55fb0996: field min_id: %w", err)
		}
		g.MinID = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode messages.getPersonalChannelHistory#55fb0996: field hash: %w", err)
		}
		g.Hash = value
	}
	return nil
}

// GetUserID returns value of UserID field.
func (g *MessagesGetPersonalChannelHistoryRequest) GetUserID() (value InputUserClass) {
	if g == nil {
		return
	}
	return g.UserID
}

// GetLimit returns value of Limit field.
func (g *MessagesGetPersonalChannelHistoryRequest) GetLimit() (value int) {
	if g == nil {
		return
	}
	return g.Limit
}

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

// GetMinID returns value of MinID field.
func (g *MessagesGetPersonalChannelHistoryRequest) GetMinID() (value int) {
	if g == nil {
		return
	}
	return g.MinID
}

// GetHash returns value of Hash field.
func (g *MessagesGetPersonalChannelHistoryRequest) GetHash() (value int64) {
	if g == nil {
		return
	}
	return g.Hash
}

// MessagesGetPersonalChannelHistory invokes method messages.getPersonalChannelHistory#55fb0996 returning error if any.
//
// See https://core.telegram.org/method/messages.getPersonalChannelHistory for reference.
func (c *Client) MessagesGetPersonalChannelHistory(ctx context.Context, request *MessagesGetPersonalChannelHistoryRequest) (MessagesMessagesClass, error) {
	var result MessagesMessagesBox

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