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

// NotificationGroup represents TL type `notificationGroup#d02a41ba`.
type NotificationGroup struct {
	// Unique persistent auto-incremented from 1 identifier of the notification group
	ID int32
	// Type of the group
	Type NotificationGroupTypeClass
	// Identifier of a chat to which all notifications in the group belong
	ChatID int64
	// Total number of active notifications in the group
	TotalCount int32
	// The list of active notifications
	Notifications []Notification
}

// NotificationGroupTypeID is TL type id of NotificationGroup.
const NotificationGroupTypeID = 0xd02a41ba

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

func (n *NotificationGroup) Zero() bool {
	if n == nil {
		return true
	}
	if !(n.ID == 0) {
		return false
	}
	if !(n.Type == nil) {
		return false
	}
	if !(n.ChatID == 0) {
		return false
	}
	if !(n.TotalCount == 0) {
		return false
	}
	if !(n.Notifications == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (n *NotificationGroup) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "notificationGroup",
		ID:   NotificationGroupTypeID,
	}
	if n == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Notifications",
			SchemaName: "notifications",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (n *NotificationGroup) Encode(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't encode notificationGroup#d02a41ba as nil")
	}
	b.PutID(NotificationGroupTypeID)
	return n.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (n *NotificationGroup) EncodeBare(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't encode notificationGroup#d02a41ba as nil")
	}
	b.PutInt32(n.ID)
	if n.Type == nil {
		return fmt.Errorf("unable to encode notificationGroup#d02a41ba: field type is nil")
	}
	if err := n.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode notificationGroup#d02a41ba: field type: %w", err)
	}
	b.PutInt53(n.ChatID)
	b.PutInt32(n.TotalCount)
	b.PutInt(len(n.Notifications))
	for idx, v := range n.Notifications {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare notificationGroup#d02a41ba: field notifications element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (n *NotificationGroup) Decode(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't decode notificationGroup#d02a41ba to nil")
	}
	if err := b.ConsumeID(NotificationGroupTypeID); err != nil {
		return fmt.Errorf("unable to decode notificationGroup#d02a41ba: %w", err)
	}
	return n.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (n *NotificationGroup) DecodeBare(b *bin.Buffer) error {
	if n == nil {
		return fmt.Errorf("can't decode notificationGroup#d02a41ba to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode notificationGroup#d02a41ba: field id: %w", err)
		}
		n.ID = value
	}
	{
		value, err := DecodeNotificationGroupType(b)
		if err != nil {
			return fmt.Errorf("unable to decode notificationGroup#d02a41ba: field type: %w", err)
		}
		n.Type = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode notificationGroup#d02a41ba: field chat_id: %w", err)
		}
		n.ChatID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode notificationGroup#d02a41ba: field total_count: %w", err)
		}
		n.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode notificationGroup#d02a41ba: field notifications: %w", err)
		}

		if headerLen > 0 {
			n.Notifications = make([]Notification, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value Notification
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare notificationGroup#d02a41ba: field notifications: %w", err)
			}
			n.Notifications = append(n.Notifications, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (n *NotificationGroup) EncodeTDLibJSON(b tdjson.Encoder) error {
	if n == nil {
		return fmt.Errorf("can't encode notificationGroup#d02a41ba as nil")
	}
	b.ObjStart()
	b.PutID("notificationGroup")
	b.Comma()
	b.FieldStart("id")
	b.PutInt32(n.ID)
	b.Comma()
	b.FieldStart("type")
	if n.Type == nil {
		return fmt.Errorf("unable to encode notificationGroup#d02a41ba: field type is nil")
	}
	if err := n.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode notificationGroup#d02a41ba: field type: %w", err)
	}
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(n.ChatID)
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(n.TotalCount)
	b.Comma()
	b.FieldStart("notifications")
	b.ArrStart()
	for idx, v := range n.Notifications {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode notificationGroup#d02a41ba: field notifications 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 (n *NotificationGroup) DecodeTDLibJSON(b tdjson.Decoder) error {
	if n == nil {
		return fmt.Errorf("can't decode notificationGroup#d02a41ba to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("notificationGroup"); err != nil {
				return fmt.Errorf("unable to decode notificationGroup#d02a41ba: %w", err)
			}
		case "id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode notificationGroup#d02a41ba: field id: %w", err)
			}
			n.ID = value
		case "type":
			value, err := DecodeTDLibJSONNotificationGroupType(b)
			if err != nil {
				return fmt.Errorf("unable to decode notificationGroup#d02a41ba: field type: %w", err)
			}
			n.Type = value
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode notificationGroup#d02a41ba: field chat_id: %w", err)
			}
			n.ChatID = value
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode notificationGroup#d02a41ba: field total_count: %w", err)
			}
			n.TotalCount = value
		case "notifications":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value Notification
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode notificationGroup#d02a41ba: field notifications: %w", err)
				}
				n.Notifications = append(n.Notifications, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode notificationGroup#d02a41ba: field notifications: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (n *NotificationGroup) GetID() (value int32) {
	if n == nil {
		return
	}
	return n.ID
}

// GetType returns value of Type field.
func (n *NotificationGroup) GetType() (value NotificationGroupTypeClass) {
	if n == nil {
		return
	}
	return n.Type
}

// GetChatID returns value of ChatID field.
func (n *NotificationGroup) GetChatID() (value int64) {
	if n == nil {
		return
	}
	return n.ChatID
}

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

// GetNotifications returns value of Notifications field.
func (n *NotificationGroup) GetNotifications() (value []Notification) {
	if n == nil {
		return
	}
	return n.Notifications
}
