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

// ChatPhoto represents TL type `chatPhoto#96e90714`.
type ChatPhoto struct {
	// Unique photo identifier
	ID int64
	// Point in time (Unix timestamp) when the photo has been added
	AddedDate int32
	// Photo minithumbnail; may be null
	Minithumbnail Minithumbnail
	// Available variants of the photo in JPEG format, in different size
	Sizes []PhotoSize
	// A big (up to 1280x1280) animated variant of the photo in MPEG4 format; may be null
	Animation AnimatedChatPhoto
	// A small (160x160) animated variant of the photo in MPEG4 format; may be null even the
	// big animation is available
	SmallAnimation AnimatedChatPhoto
	// Sticker-based version of the chat photo; may be null
	Sticker ChatPhotoSticker
}

// ChatPhotoTypeID is TL type id of ChatPhoto.
const ChatPhotoTypeID = 0x96e90714

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

func (c *ChatPhoto) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.ID == 0) {
		return false
	}
	if !(c.AddedDate == 0) {
		return false
	}
	if !(c.Minithumbnail.Zero()) {
		return false
	}
	if !(c.Sizes == nil) {
		return false
	}
	if !(c.Animation.Zero()) {
		return false
	}
	if !(c.SmallAnimation.Zero()) {
		return false
	}
	if !(c.Sticker.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatPhoto) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatPhoto",
		ID:   ChatPhotoTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "AddedDate",
			SchemaName: "added_date",
		},
		{
			Name:       "Minithumbnail",
			SchemaName: "minithumbnail",
		},
		{
			Name:       "Sizes",
			SchemaName: "sizes",
		},
		{
			Name:       "Animation",
			SchemaName: "animation",
		},
		{
			Name:       "SmallAnimation",
			SchemaName: "small_animation",
		},
		{
			Name:       "Sticker",
			SchemaName: "sticker",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatPhoto) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatPhoto#96e90714 as nil")
	}
	b.PutID(ChatPhotoTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatPhoto) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatPhoto#96e90714 as nil")
	}
	b.PutLong(c.ID)
	b.PutInt32(c.AddedDate)
	if err := c.Minithumbnail.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatPhoto#96e90714: field minithumbnail: %w", err)
	}
	b.PutInt(len(c.Sizes))
	for idx, v := range c.Sizes {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare chatPhoto#96e90714: field sizes element with index %d: %w", idx, err)
		}
	}
	if err := c.Animation.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatPhoto#96e90714: field animation: %w", err)
	}
	if err := c.SmallAnimation.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatPhoto#96e90714: field small_animation: %w", err)
	}
	if err := c.Sticker.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatPhoto#96e90714: field sticker: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *ChatPhoto) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatPhoto#96e90714 to nil")
	}
	if err := b.ConsumeID(ChatPhotoTypeID); err != nil {
		return fmt.Errorf("unable to decode chatPhoto#96e90714: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ChatPhoto) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatPhoto#96e90714 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode chatPhoto#96e90714: field id: %w", err)
		}
		c.ID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode chatPhoto#96e90714: field added_date: %w", err)
		}
		c.AddedDate = value
	}
	{
		if err := c.Minithumbnail.Decode(b); err != nil {
			return fmt.Errorf("unable to decode chatPhoto#96e90714: field minithumbnail: %w", err)
		}
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode chatPhoto#96e90714: field sizes: %w", err)
		}

		if headerLen > 0 {
			c.Sizes = make([]PhotoSize, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value PhotoSize
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare chatPhoto#96e90714: field sizes: %w", err)
			}
			c.Sizes = append(c.Sizes, value)
		}
	}
	{
		if err := c.Animation.Decode(b); err != nil {
			return fmt.Errorf("unable to decode chatPhoto#96e90714: field animation: %w", err)
		}
	}
	{
		if err := c.SmallAnimation.Decode(b); err != nil {
			return fmt.Errorf("unable to decode chatPhoto#96e90714: field small_animation: %w", err)
		}
	}
	{
		if err := c.Sticker.Decode(b); err != nil {
			return fmt.Errorf("unable to decode chatPhoto#96e90714: field sticker: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatPhoto) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatPhoto#96e90714 as nil")
	}
	b.ObjStart()
	b.PutID("chatPhoto")
	b.Comma()
	b.FieldStart("id")
	b.PutLong(c.ID)
	b.Comma()
	b.FieldStart("added_date")
	b.PutInt32(c.AddedDate)
	b.Comma()
	b.FieldStart("minithumbnail")
	if err := c.Minithumbnail.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatPhoto#96e90714: field minithumbnail: %w", err)
	}
	b.Comma()
	b.FieldStart("sizes")
	b.ArrStart()
	for idx, v := range c.Sizes {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode chatPhoto#96e90714: field sizes element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("animation")
	if err := c.Animation.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatPhoto#96e90714: field animation: %w", err)
	}
	b.Comma()
	b.FieldStart("small_animation")
	if err := c.SmallAnimation.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatPhoto#96e90714: field small_animation: %w", err)
	}
	b.Comma()
	b.FieldStart("sticker")
	if err := c.Sticker.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatPhoto#96e90714: field sticker: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *ChatPhoto) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode chatPhoto#96e90714 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatPhoto"); err != nil {
				return fmt.Errorf("unable to decode chatPhoto#96e90714: %w", err)
			}
		case "id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode chatPhoto#96e90714: field id: %w", err)
			}
			c.ID = value
		case "added_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode chatPhoto#96e90714: field added_date: %w", err)
			}
			c.AddedDate = value
		case "minithumbnail":
			if err := c.Minithumbnail.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode chatPhoto#96e90714: field minithumbnail: %w", err)
			}
		case "sizes":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value PhotoSize
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode chatPhoto#96e90714: field sizes: %w", err)
				}
				c.Sizes = append(c.Sizes, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode chatPhoto#96e90714: field sizes: %w", err)
			}
		case "animation":
			if err := c.Animation.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode chatPhoto#96e90714: field animation: %w", err)
			}
		case "small_animation":
			if err := c.SmallAnimation.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode chatPhoto#96e90714: field small_animation: %w", err)
			}
		case "sticker":
			if err := c.Sticker.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode chatPhoto#96e90714: field sticker: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (c *ChatPhoto) GetID() (value int64) {
	if c == nil {
		return
	}
	return c.ID
}

// GetAddedDate returns value of AddedDate field.
func (c *ChatPhoto) GetAddedDate() (value int32) {
	if c == nil {
		return
	}
	return c.AddedDate
}

// GetMinithumbnail returns value of Minithumbnail field.
func (c *ChatPhoto) GetMinithumbnail() (value Minithumbnail) {
	if c == nil {
		return
	}
	return c.Minithumbnail
}

// GetSizes returns value of Sizes field.
func (c *ChatPhoto) GetSizes() (value []PhotoSize) {
	if c == nil {
		return
	}
	return c.Sizes
}

// GetAnimation returns value of Animation field.
func (c *ChatPhoto) GetAnimation() (value AnimatedChatPhoto) {
	if c == nil {
		return
	}
	return c.Animation
}

// GetSmallAnimation returns value of SmallAnimation field.
func (c *ChatPhoto) GetSmallAnimation() (value AnimatedChatPhoto) {
	if c == nil {
		return
	}
	return c.SmallAnimation
}

// GetSticker returns value of Sticker field.
func (c *ChatPhoto) GetSticker() (value ChatPhotoSticker) {
	if c == nil {
		return
	}
	return c.Sticker
}
