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

// BotsPopularAppBots represents TL type `bots.popularAppBots#1991b13b`.
// Popular Main Mini Apps¹, to be used in the apps tab of global search »².
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps#main-mini-apps
//  2. https://core.telegram.org/api/search#apps-tab
//
// See https://core.telegram.org/constructor/bots.popularAppBots for reference.
type BotsPopularAppBots struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Offset for pagination¹.
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	//
	// Use SetNextOffset and GetNextOffset helpers.
	NextOffset string
	// The bots associated to each Main Mini App, see here »¹ for more info.
	//
	// Links:
	//  1) https://core.telegram.org/api/bots/webapps#main-mini-apps
	Users []UserClass
}

// BotsPopularAppBotsTypeID is TL type id of BotsPopularAppBots.
const BotsPopularAppBotsTypeID = 0x1991b13b

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

func (p *BotsPopularAppBots) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Flags.Zero()) {
		return false
	}
	if !(p.NextOffset == "") {
		return false
	}
	if !(p.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills BotsPopularAppBots from given interface.
func (p *BotsPopularAppBots) FillFrom(from interface {
	GetNextOffset() (value string, ok bool)
	GetUsers() (value []UserClass)
}) {
	if val, ok := from.GetNextOffset(); ok {
		p.NextOffset = val
	}

	p.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*BotsPopularAppBots) TypeName() string {
	return "bots.popularAppBots"
}

// TypeInfo returns info about TL type.
func (p *BotsPopularAppBots) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "bots.popularAppBots",
		ID:   BotsPopularAppBotsTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
			Null:       !p.Flags.Has(0),
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (p *BotsPopularAppBots) SetFlags() {
	if !(p.NextOffset == "") {
		p.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (p *BotsPopularAppBots) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode bots.popularAppBots#1991b13b as nil")
	}
	b.PutID(BotsPopularAppBotsTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *BotsPopularAppBots) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode bots.popularAppBots#1991b13b as nil")
	}
	p.SetFlags()
	if err := p.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.popularAppBots#1991b13b: field flags: %w", err)
	}
	if p.Flags.Has(0) {
		b.PutString(p.NextOffset)
	}
	b.PutVectorHeader(len(p.Users))
	for idx, v := range p.Users {
		if v == nil {
			return fmt.Errorf("unable to encode bots.popularAppBots#1991b13b: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode bots.popularAppBots#1991b13b: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *BotsPopularAppBots) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode bots.popularAppBots#1991b13b to nil")
	}
	if err := b.ConsumeID(BotsPopularAppBotsTypeID); err != nil {
		return fmt.Errorf("unable to decode bots.popularAppBots#1991b13b: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *BotsPopularAppBots) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode bots.popularAppBots#1991b13b to nil")
	}
	{
		if err := p.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode bots.popularAppBots#1991b13b: field flags: %w", err)
		}
	}
	if p.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode bots.popularAppBots#1991b13b: field next_offset: %w", err)
		}
		p.NextOffset = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode bots.popularAppBots#1991b13b: field users: %w", err)
		}

		if headerLen > 0 {
			p.Users = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode bots.popularAppBots#1991b13b: field users: %w", err)
			}
			p.Users = append(p.Users, value)
		}
	}
	return nil
}

// SetNextOffset sets value of NextOffset conditional field.
func (p *BotsPopularAppBots) SetNextOffset(value string) {
	p.Flags.Set(0)
	p.NextOffset = value
}

// GetNextOffset returns value of NextOffset conditional field and
// boolean which is true if field was set.
func (p *BotsPopularAppBots) GetNextOffset() (value string, ok bool) {
	if p == nil {
		return
	}
	if !p.Flags.Has(0) {
		return value, false
	}
	return p.NextOffset, true
}

// GetUsers returns value of Users field.
func (p *BotsPopularAppBots) GetUsers() (value []UserClass) {
	if p == nil {
		return
	}
	return p.Users
}

// MapUsers returns field Users wrapped in UserClassArray helper.
func (p *BotsPopularAppBots) MapUsers() (value UserClassArray) {
	return UserClassArray(p.Users)
}
