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

// AccountAutoSaveSettings represents TL type `account.autoSaveSettings#4c3e069d`.
// Contains media autosave settings
//
// See https://core.telegram.org/constructor/account.autoSaveSettings for reference.
type AccountAutoSaveSettings struct {
	// Default media autosave settings for private chats
	UsersSettings AutoSaveSettings
	// Default media autosave settings for groups and supergroups¹
	//
	// Links:
	//  1) https://core.telegram.org/api/channel
	ChatsSettings AutoSaveSettings
	// Default media autosave settings for channels¹
	//
	// Links:
	//  1) https://core.telegram.org/api/channel
	BroadcastsSettings AutoSaveSettings
	// Peer-specific granular autosave settings
	Exceptions []AutoSaveException
	// Chats mentioned in the peer-specific granular autosave settings
	Chats []ChatClass
	// Users mentioned in the peer-specific granular autosave settings
	Users []UserClass
}

// AccountAutoSaveSettingsTypeID is TL type id of AccountAutoSaveSettings.
const AccountAutoSaveSettingsTypeID = 0x4c3e069d

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

func (a *AccountAutoSaveSettings) Zero() bool {
	if a == nil {
		return true
	}
	if !(a.UsersSettings.Zero()) {
		return false
	}
	if !(a.ChatsSettings.Zero()) {
		return false
	}
	if !(a.BroadcastsSettings.Zero()) {
		return false
	}
	if !(a.Exceptions == nil) {
		return false
	}
	if !(a.Chats == nil) {
		return false
	}
	if !(a.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills AccountAutoSaveSettings from given interface.
func (a *AccountAutoSaveSettings) FillFrom(from interface {
	GetUsersSettings() (value AutoSaveSettings)
	GetChatsSettings() (value AutoSaveSettings)
	GetBroadcastsSettings() (value AutoSaveSettings)
	GetExceptions() (value []AutoSaveException)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	a.UsersSettings = from.GetUsersSettings()
	a.ChatsSettings = from.GetChatsSettings()
	a.BroadcastsSettings = from.GetBroadcastsSettings()
	a.Exceptions = from.GetExceptions()
	a.Chats = from.GetChats()
	a.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*AccountAutoSaveSettings) TypeName() string {
	return "account.autoSaveSettings"
}

// TypeInfo returns info about TL type.
func (a *AccountAutoSaveSettings) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "account.autoSaveSettings",
		ID:   AccountAutoSaveSettingsTypeID,
	}
	if a == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UsersSettings",
			SchemaName: "users_settings",
		},
		{
			Name:       "ChatsSettings",
			SchemaName: "chats_settings",
		},
		{
			Name:       "BroadcastsSettings",
			SchemaName: "broadcasts_settings",
		},
		{
			Name:       "Exceptions",
			SchemaName: "exceptions",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (a *AccountAutoSaveSettings) Encode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode account.autoSaveSettings#4c3e069d as nil")
	}
	b.PutID(AccountAutoSaveSettingsTypeID)
	return a.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (a *AccountAutoSaveSettings) EncodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't encode account.autoSaveSettings#4c3e069d as nil")
	}
	if err := a.UsersSettings.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.autoSaveSettings#4c3e069d: field users_settings: %w", err)
	}
	if err := a.ChatsSettings.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.autoSaveSettings#4c3e069d: field chats_settings: %w", err)
	}
	if err := a.BroadcastsSettings.Encode(b); err != nil {
		return fmt.Errorf("unable to encode account.autoSaveSettings#4c3e069d: field broadcasts_settings: %w", err)
	}
	b.PutVectorHeader(len(a.Exceptions))
	for idx, v := range a.Exceptions {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.autoSaveSettings#4c3e069d: field exceptions element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(a.Chats))
	for idx, v := range a.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode account.autoSaveSettings#4c3e069d: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.autoSaveSettings#4c3e069d: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(a.Users))
	for idx, v := range a.Users {
		if v == nil {
			return fmt.Errorf("unable to encode account.autoSaveSettings#4c3e069d: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode account.autoSaveSettings#4c3e069d: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (a *AccountAutoSaveSettings) Decode(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode account.autoSaveSettings#4c3e069d to nil")
	}
	if err := b.ConsumeID(AccountAutoSaveSettingsTypeID); err != nil {
		return fmt.Errorf("unable to decode account.autoSaveSettings#4c3e069d: %w", err)
	}
	return a.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (a *AccountAutoSaveSettings) DecodeBare(b *bin.Buffer) error {
	if a == nil {
		return fmt.Errorf("can't decode account.autoSaveSettings#4c3e069d to nil")
	}
	{
		if err := a.UsersSettings.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.autoSaveSettings#4c3e069d: field users_settings: %w", err)
		}
	}
	{
		if err := a.ChatsSettings.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.autoSaveSettings#4c3e069d: field chats_settings: %w", err)
		}
	}
	{
		if err := a.BroadcastsSettings.Decode(b); err != nil {
			return fmt.Errorf("unable to decode account.autoSaveSettings#4c3e069d: field broadcasts_settings: %w", err)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode account.autoSaveSettings#4c3e069d: field exceptions: %w", err)
		}

		if headerLen > 0 {
			a.Exceptions = make([]AutoSaveException, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value AutoSaveException
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode account.autoSaveSettings#4c3e069d: field exceptions: %w", err)
			}
			a.Exceptions = append(a.Exceptions, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode account.autoSaveSettings#4c3e069d: field chats: %w", err)
		}

		if headerLen > 0 {
			a.Chats = make([]ChatClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeChat(b)
			if err != nil {
				return fmt.Errorf("unable to decode account.autoSaveSettings#4c3e069d: field chats: %w", err)
			}
			a.Chats = append(a.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode account.autoSaveSettings#4c3e069d: field users: %w", err)
		}

		if headerLen > 0 {
			a.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 account.autoSaveSettings#4c3e069d: field users: %w", err)
			}
			a.Users = append(a.Users, value)
		}
	}
	return nil
}

// GetUsersSettings returns value of UsersSettings field.
func (a *AccountAutoSaveSettings) GetUsersSettings() (value AutoSaveSettings) {
	if a == nil {
		return
	}
	return a.UsersSettings
}

// GetChatsSettings returns value of ChatsSettings field.
func (a *AccountAutoSaveSettings) GetChatsSettings() (value AutoSaveSettings) {
	if a == nil {
		return
	}
	return a.ChatsSettings
}

// GetBroadcastsSettings returns value of BroadcastsSettings field.
func (a *AccountAutoSaveSettings) GetBroadcastsSettings() (value AutoSaveSettings) {
	if a == nil {
		return
	}
	return a.BroadcastsSettings
}

// GetExceptions returns value of Exceptions field.
func (a *AccountAutoSaveSettings) GetExceptions() (value []AutoSaveException) {
	if a == nil {
		return
	}
	return a.Exceptions
}

// GetChats returns value of Chats field.
func (a *AccountAutoSaveSettings) GetChats() (value []ChatClass) {
	if a == nil {
		return
	}
	return a.Chats
}

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

// MapChats returns field Chats wrapped in ChatClassArray helper.
func (a *AccountAutoSaveSettings) MapChats() (value ChatClassArray) {
	return ChatClassArray(a.Chats)
}

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