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

// UpdatesState represents TL type `updates.state#a56c2a3e`.
// Updates state.
//
// See https://core.telegram.org/constructor/updates.state for reference.
type UpdatesState struct {
	// Number of events occurred in a text box
	Pts int
	// Position in a sequence of updates in secret chats. For further details refer to
	// article secret chats¹
	//
	// Links:
	//  1) https://core.telegram.org/api/end-to-end
	Qts int
	// Date of condition
	Date int
	// Number of sent updates
	Seq int
	// Number of unread messages
	UnreadCount int
}

// UpdatesStateTypeID is TL type id of UpdatesState.
const UpdatesStateTypeID = 0xa56c2a3e

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

func (s *UpdatesState) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Pts == 0) {
		return false
	}
	if !(s.Qts == 0) {
		return false
	}
	if !(s.Date == 0) {
		return false
	}
	if !(s.Seq == 0) {
		return false
	}
	if !(s.UnreadCount == 0) {
		return false
	}

	return true
}

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

// FillFrom fills UpdatesState from given interface.
func (s *UpdatesState) FillFrom(from interface {
	GetPts() (value int)
	GetQts() (value int)
	GetDate() (value int)
	GetSeq() (value int)
	GetUnreadCount() (value int)
}) {
	s.Pts = from.GetPts()
	s.Qts = from.GetQts()
	s.Date = from.GetDate()
	s.Seq = from.GetSeq()
	s.UnreadCount = from.GetUnreadCount()
}

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

// TypeName returns name of type in TL schema.
func (*UpdatesState) TypeName() string {
	return "updates.state"
}

// TypeInfo returns info about TL type.
func (s *UpdatesState) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "updates.state",
		ID:   UpdatesStateTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Pts",
			SchemaName: "pts",
		},
		{
			Name:       "Qts",
			SchemaName: "qts",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "Seq",
			SchemaName: "seq",
		},
		{
			Name:       "UnreadCount",
			SchemaName: "unread_count",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *UpdatesState) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode updates.state#a56c2a3e as nil")
	}
	b.PutID(UpdatesStateTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *UpdatesState) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode updates.state#a56c2a3e as nil")
	}
	b.PutInt(s.Pts)
	b.PutInt(s.Qts)
	b.PutInt(s.Date)
	b.PutInt(s.Seq)
	b.PutInt(s.UnreadCount)
	return nil
}

// Decode implements bin.Decoder.
func (s *UpdatesState) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode updates.state#a56c2a3e to nil")
	}
	if err := b.ConsumeID(UpdatesStateTypeID); err != nil {
		return fmt.Errorf("unable to decode updates.state#a56c2a3e: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *UpdatesState) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode updates.state#a56c2a3e to nil")
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode updates.state#a56c2a3e: field pts: %w", err)
		}
		s.Pts = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode updates.state#a56c2a3e: field qts: %w", err)
		}
		s.Qts = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode updates.state#a56c2a3e: field date: %w", err)
		}
		s.Date = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode updates.state#a56c2a3e: field seq: %w", err)
		}
		s.Seq = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode updates.state#a56c2a3e: field unread_count: %w", err)
		}
		s.UnreadCount = value
	}
	return nil
}

// GetPts returns value of Pts field.
func (s *UpdatesState) GetPts() (value int) {
	if s == nil {
		return
	}
	return s.Pts
}

// GetQts returns value of Qts field.
func (s *UpdatesState) GetQts() (value int) {
	if s == nil {
		return
	}
	return s.Qts
}

// GetDate returns value of Date field.
func (s *UpdatesState) GetDate() (value int) {
	if s == nil {
		return
	}
	return s.Date
}

// GetSeq returns value of Seq field.
func (s *UpdatesState) GetSeq() (value int) {
	if s == nil {
		return
	}
	return s.Seq
}

// GetUnreadCount returns value of UnreadCount field.
func (s *UpdatesState) GetUnreadCount() (value int) {
	if s == nil {
		return
	}
	return s.UnreadCount
}
