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

// BasicGroupFullInfo represents TL type `basicGroupFullInfo#d62e521e`.
type BasicGroupFullInfo struct {
	// Chat photo; may be null if empty or unknown. If non-null, then it is the same photo as
	// in chat.photo
	Photo ChatPhoto
	// Contains full information about a basic group
	Description string
	// User identifier of the creator of the group; 0 if unknown
	CreatorUserID int64
	// Group members
	Members []ChatMember
	// True, if non-administrators and non-bots can be hidden in responses to
	// getSupergroupMembers and searchChatMembers for non-administrators after upgrading the
	// basic group to a supergroup
	CanHideMembers bool
	// True, if aggressive anti-spam checks can be enabled or disabled in the supergroup
	// after upgrading the basic group to a supergroup
	CanToggleAggressiveAntiSpam bool
	// Primary invite link for this group; may be null. For chat administrators with
	// can_invite_users right only. Updated only after the basic group is opened
	InviteLink ChatInviteLink
	// List of commands of bots in the group
	BotCommands []BotCommands
}

// BasicGroupFullInfoTypeID is TL type id of BasicGroupFullInfo.
const BasicGroupFullInfoTypeID = 0xd62e521e

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

func (b *BasicGroupFullInfo) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Photo.Zero()) {
		return false
	}
	if !(b.Description == "") {
		return false
	}
	if !(b.CreatorUserID == 0) {
		return false
	}
	if !(b.Members == nil) {
		return false
	}
	if !(b.CanHideMembers == false) {
		return false
	}
	if !(b.CanToggleAggressiveAntiSpam == false) {
		return false
	}
	if !(b.InviteLink.Zero()) {
		return false
	}
	if !(b.BotCommands == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (b *BasicGroupFullInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "basicGroupFullInfo",
		ID:   BasicGroupFullInfoTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Photo",
			SchemaName: "photo",
		},
		{
			Name:       "Description",
			SchemaName: "description",
		},
		{
			Name:       "CreatorUserID",
			SchemaName: "creator_user_id",
		},
		{
			Name:       "Members",
			SchemaName: "members",
		},
		{
			Name:       "CanHideMembers",
			SchemaName: "can_hide_members",
		},
		{
			Name:       "CanToggleAggressiveAntiSpam",
			SchemaName: "can_toggle_aggressive_anti_spam",
		},
		{
			Name:       "InviteLink",
			SchemaName: "invite_link",
		},
		{
			Name:       "BotCommands",
			SchemaName: "bot_commands",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *BasicGroupFullInfo) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode basicGroupFullInfo#d62e521e as nil")
	}
	buf.PutID(BasicGroupFullInfoTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BasicGroupFullInfo) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode basicGroupFullInfo#d62e521e as nil")
	}
	if err := b.Photo.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode basicGroupFullInfo#d62e521e: field photo: %w", err)
	}
	buf.PutString(b.Description)
	buf.PutInt53(b.CreatorUserID)
	buf.PutInt(len(b.Members))
	for idx, v := range b.Members {
		if err := v.EncodeBare(buf); err != nil {
			return fmt.Errorf("unable to encode bare basicGroupFullInfo#d62e521e: field members element with index %d: %w", idx, err)
		}
	}
	buf.PutBool(b.CanHideMembers)
	buf.PutBool(b.CanToggleAggressiveAntiSpam)
	if err := b.InviteLink.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode basicGroupFullInfo#d62e521e: field invite_link: %w", err)
	}
	buf.PutInt(len(b.BotCommands))
	for idx, v := range b.BotCommands {
		if err := v.EncodeBare(buf); err != nil {
			return fmt.Errorf("unable to encode bare basicGroupFullInfo#d62e521e: field bot_commands element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *BasicGroupFullInfo) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode basicGroupFullInfo#d62e521e to nil")
	}
	if err := buf.ConsumeID(BasicGroupFullInfoTypeID); err != nil {
		return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BasicGroupFullInfo) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode basicGroupFullInfo#d62e521e to nil")
	}
	{
		if err := b.Photo.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field photo: %w", err)
		}
	}
	{
		value, err := buf.String()
		if err != nil {
			return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field description: %w", err)
		}
		b.Description = value
	}
	{
		value, err := buf.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field creator_user_id: %w", err)
		}
		b.CreatorUserID = value
	}
	{
		headerLen, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field members: %w", err)
		}

		if headerLen > 0 {
			b.Members = make([]ChatMember, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value ChatMember
			if err := value.DecodeBare(buf); err != nil {
				return fmt.Errorf("unable to decode bare basicGroupFullInfo#d62e521e: field members: %w", err)
			}
			b.Members = append(b.Members, value)
		}
	}
	{
		value, err := buf.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field can_hide_members: %w", err)
		}
		b.CanHideMembers = value
	}
	{
		value, err := buf.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field can_toggle_aggressive_anti_spam: %w", err)
		}
		b.CanToggleAggressiveAntiSpam = value
	}
	{
		if err := b.InviteLink.Decode(buf); err != nil {
			return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field invite_link: %w", err)
		}
	}
	{
		headerLen, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field bot_commands: %w", err)
		}

		if headerLen > 0 {
			b.BotCommands = make([]BotCommands, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value BotCommands
			if err := value.DecodeBare(buf); err != nil {
				return fmt.Errorf("unable to decode bare basicGroupFullInfo#d62e521e: field bot_commands: %w", err)
			}
			b.BotCommands = append(b.BotCommands, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *BasicGroupFullInfo) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode basicGroupFullInfo#d62e521e as nil")
	}
	buf.ObjStart()
	buf.PutID("basicGroupFullInfo")
	buf.Comma()
	buf.FieldStart("photo")
	if err := b.Photo.EncodeTDLibJSON(buf); err != nil {
		return fmt.Errorf("unable to encode basicGroupFullInfo#d62e521e: field photo: %w", err)
	}
	buf.Comma()
	buf.FieldStart("description")
	buf.PutString(b.Description)
	buf.Comma()
	buf.FieldStart("creator_user_id")
	buf.PutInt53(b.CreatorUserID)
	buf.Comma()
	buf.FieldStart("members")
	buf.ArrStart()
	for idx, v := range b.Members {
		if err := v.EncodeTDLibJSON(buf); err != nil {
			return fmt.Errorf("unable to encode basicGroupFullInfo#d62e521e: field members element with index %d: %w", idx, err)
		}
		buf.Comma()
	}
	buf.StripComma()
	buf.ArrEnd()
	buf.Comma()
	buf.FieldStart("can_hide_members")
	buf.PutBool(b.CanHideMembers)
	buf.Comma()
	buf.FieldStart("can_toggle_aggressive_anti_spam")
	buf.PutBool(b.CanToggleAggressiveAntiSpam)
	buf.Comma()
	buf.FieldStart("invite_link")
	if err := b.InviteLink.EncodeTDLibJSON(buf); err != nil {
		return fmt.Errorf("unable to encode basicGroupFullInfo#d62e521e: field invite_link: %w", err)
	}
	buf.Comma()
	buf.FieldStart("bot_commands")
	buf.ArrStart()
	for idx, v := range b.BotCommands {
		if err := v.EncodeTDLibJSON(buf); err != nil {
			return fmt.Errorf("unable to encode basicGroupFullInfo#d62e521e: field bot_commands element with index %d: %w", idx, err)
		}
		buf.Comma()
	}
	buf.StripComma()
	buf.ArrEnd()
	buf.Comma()
	buf.StripComma()
	buf.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (b *BasicGroupFullInfo) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("can't decode basicGroupFullInfo#d62e521e to nil")
	}

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("basicGroupFullInfo"); err != nil {
				return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: %w", err)
			}
		case "photo":
			if err := b.Photo.DecodeTDLibJSON(buf); err != nil {
				return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field photo: %w", err)
			}
		case "description":
			value, err := buf.String()
			if err != nil {
				return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field description: %w", err)
			}
			b.Description = value
		case "creator_user_id":
			value, err := buf.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field creator_user_id: %w", err)
			}
			b.CreatorUserID = value
		case "members":
			if err := buf.Arr(func(buf tdjson.Decoder) error {
				var value ChatMember
				if err := value.DecodeTDLibJSON(buf); err != nil {
					return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field members: %w", err)
				}
				b.Members = append(b.Members, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field members: %w", err)
			}
		case "can_hide_members":
			value, err := buf.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field can_hide_members: %w", err)
			}
			b.CanHideMembers = value
		case "can_toggle_aggressive_anti_spam":
			value, err := buf.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field can_toggle_aggressive_anti_spam: %w", err)
			}
			b.CanToggleAggressiveAntiSpam = value
		case "invite_link":
			if err := b.InviteLink.DecodeTDLibJSON(buf); err != nil {
				return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field invite_link: %w", err)
			}
		case "bot_commands":
			if err := buf.Arr(func(buf tdjson.Decoder) error {
				var value BotCommands
				if err := value.DecodeTDLibJSON(buf); err != nil {
					return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field bot_commands: %w", err)
				}
				b.BotCommands = append(b.BotCommands, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode basicGroupFullInfo#d62e521e: field bot_commands: %w", err)
			}
		default:
			return buf.Skip()
		}
		return nil
	})
}

// GetPhoto returns value of Photo field.
func (b *BasicGroupFullInfo) GetPhoto() (value ChatPhoto) {
	if b == nil {
		return
	}
	return b.Photo
}

// GetDescription returns value of Description field.
func (b *BasicGroupFullInfo) GetDescription() (value string) {
	if b == nil {
		return
	}
	return b.Description
}

// GetCreatorUserID returns value of CreatorUserID field.
func (b *BasicGroupFullInfo) GetCreatorUserID() (value int64) {
	if b == nil {
		return
	}
	return b.CreatorUserID
}

// GetMembers returns value of Members field.
func (b *BasicGroupFullInfo) GetMembers() (value []ChatMember) {
	if b == nil {
		return
	}
	return b.Members
}

// GetCanHideMembers returns value of CanHideMembers field.
func (b *BasicGroupFullInfo) GetCanHideMembers() (value bool) {
	if b == nil {
		return
	}
	return b.CanHideMembers
}

// GetCanToggleAggressiveAntiSpam returns value of CanToggleAggressiveAntiSpam field.
func (b *BasicGroupFullInfo) GetCanToggleAggressiveAntiSpam() (value bool) {
	if b == nil {
		return
	}
	return b.CanToggleAggressiveAntiSpam
}

// GetInviteLink returns value of InviteLink field.
func (b *BasicGroupFullInfo) GetInviteLink() (value ChatInviteLink) {
	if b == nil {
		return
	}
	return b.InviteLink
}

// GetBotCommands returns value of BotCommands field.
func (b *BasicGroupFullInfo) GetBotCommands() (value []BotCommands) {
	if b == nil {
		return
	}
	return b.BotCommands
}
