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

// ChatPhotos represents TL type `chatPhotos#89ec34e8`.
type ChatPhotos struct {
	// Total number of photos
	TotalCount int32
	// List of photos
	Photos []ChatPhoto
}

// ChatPhotosTypeID is TL type id of ChatPhotos.
const ChatPhotosTypeID = 0x89ec34e8

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

func (c *ChatPhotos) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.TotalCount == 0) {
		return false
	}
	if !(c.Photos == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatPhotos) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatPhotos",
		ID:   ChatPhotosTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Photos",
			SchemaName: "photos",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatPhotos) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatPhotos#89ec34e8 as nil")
	}
	b.PutID(ChatPhotosTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatPhotos) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatPhotos#89ec34e8 as nil")
	}
	b.PutInt32(c.TotalCount)
	b.PutInt(len(c.Photos))
	for idx, v := range c.Photos {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare chatPhotos#89ec34e8: field photos element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *ChatPhotos) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatPhotos#89ec34e8 to nil")
	}
	if err := b.ConsumeID(ChatPhotosTypeID); err != nil {
		return fmt.Errorf("unable to decode chatPhotos#89ec34e8: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ChatPhotos) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatPhotos#89ec34e8 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode chatPhotos#89ec34e8: field total_count: %w", err)
		}
		c.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode chatPhotos#89ec34e8: field photos: %w", err)
		}

		if headerLen > 0 {
			c.Photos = make([]ChatPhoto, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value ChatPhoto
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare chatPhotos#89ec34e8: field photos: %w", err)
			}
			c.Photos = append(c.Photos, value)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatPhotos) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatPhotos#89ec34e8 as nil")
	}
	b.ObjStart()
	b.PutID("chatPhotos")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(c.TotalCount)
	b.Comma()
	b.FieldStart("photos")
	b.ArrStart()
	for idx, v := range c.Photos {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode chatPhotos#89ec34e8: field photos 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 (c *ChatPhotos) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chatPhotos#89ec34e8 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatPhotos"); err != nil {
				return fmt.Errorf("unable to decode chatPhotos#89ec34e8: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode chatPhotos#89ec34e8: field total_count: %w", err)
			}
			c.TotalCount = value
		case "photos":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value ChatPhoto
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode chatPhotos#89ec34e8: field photos: %w", err)
				}
				c.Photos = append(c.Photos, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode chatPhotos#89ec34e8: field photos: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetPhotos returns value of Photos field.
func (c *ChatPhotos) GetPhotos() (value []ChatPhoto) {
	if c == nil {
		return
	}
	return c.Photos
}
