// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// MessagePositions represents TL type `messagePositions#912eab72`.
type MessagePositions struct {
	// Total number of messages found
	TotalCount int32
	// List of message positions
	Positions []MessagePosition
}

// MessagePositionsTypeID is TL type id of MessagePositions.
const MessagePositionsTypeID = 0x912eab72

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

func (m *MessagePositions) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.TotalCount == 0) {
		return false
	}
	if !(m.Positions == nil) {
		return false
	}

	return true
}

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

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

// TypeName returns name of type in TL schema.
func (*MessagePositions) TypeName() string {
	return "messagePositions"
}

// TypeInfo returns info about TL type.
func (m *MessagePositions) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messagePositions",
		ID:   MessagePositionsTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Positions",
			SchemaName: "positions",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessagePositions) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messagePositions#912eab72 as nil")
	}
	b.PutID(MessagePositionsTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessagePositions) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messagePositions#912eab72 as nil")
	}
	b.PutInt32(m.TotalCount)
	b.PutInt(len(m.Positions))
	for idx, v := range m.Positions {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare messagePositions#912eab72: field positions element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (m *MessagePositions) Decode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messagePositions#912eab72 to nil")
	}
	if err := b.ConsumeID(MessagePositionsTypeID); err != nil {
		return fmt.Errorf("unable to decode messagePositions#912eab72: %w", err)
	}
	return m.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (m *MessagePositions) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messagePositions#912eab72 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode messagePositions#912eab72: field total_count: %w", err)
		}
		m.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode messagePositions#912eab72: field positions: %w", err)
		}

		if headerLen > 0 {
			m.Positions = make([]MessagePosition, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value MessagePosition
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare messagePositions#912eab72: field positions: %w", err)
			}
			m.Positions = append(m.Positions, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessagePositions) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messagePositions#912eab72 as nil")
	}
	b.ObjStart()
	b.PutID("messagePositions")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(m.TotalCount)
	b.Comma()
	b.FieldStart("positions")
	b.ArrStart()
	for idx, v := range m.Positions {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode messagePositions#912eab72: field positions element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (m *MessagePositions) DecodeTDLibJSON(b tdjson.Decoder) error {
	if m == nil {
		return fmt.Errorf("can't decode messagePositions#912eab72 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messagePositions"); err != nil {
				return fmt.Errorf("unable to decode messagePositions#912eab72: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode messagePositions#912eab72: field total_count: %w", err)
			}
			m.TotalCount = value
		case "positions":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value MessagePosition
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode messagePositions#912eab72: field positions: %w", err)
				}
				m.Positions = append(m.Positions, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode messagePositions#912eab72: field positions: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTotalCount returns value of TotalCount field.
func (m *MessagePositions) GetTotalCount() (value int32) {
	if m == nil {
		return
	}
	return m.TotalCount
}

// GetPositions returns value of Positions field.
func (m *MessagePositions) GetPositions() (value []MessagePosition) {
	if m == nil {
		return
	}
	return m.Positions
}
