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

// LinkPreviewAlbumMediaPhoto represents TL type `linkPreviewAlbumMediaPhoto#c83db38e`.
type LinkPreviewAlbumMediaPhoto struct {
	// Photo description
	Photo Photo
}

// LinkPreviewAlbumMediaPhotoTypeID is TL type id of LinkPreviewAlbumMediaPhoto.
const LinkPreviewAlbumMediaPhotoTypeID = 0xc83db38e

// construct implements constructor of LinkPreviewAlbumMediaClass.
func (l LinkPreviewAlbumMediaPhoto) construct() LinkPreviewAlbumMediaClass { return &l }

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

	_ LinkPreviewAlbumMediaClass = &LinkPreviewAlbumMediaPhoto{}
)

func (l *LinkPreviewAlbumMediaPhoto) Zero() bool {
	if l == nil {
		return true
	}
	if !(l.Photo.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (l *LinkPreviewAlbumMediaPhoto) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "linkPreviewAlbumMediaPhoto",
		ID:   LinkPreviewAlbumMediaPhotoTypeID,
	}
	if l == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Photo",
			SchemaName: "photo",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (l *LinkPreviewAlbumMediaPhoto) Encode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode linkPreviewAlbumMediaPhoto#c83db38e as nil")
	}
	b.PutID(LinkPreviewAlbumMediaPhotoTypeID)
	return l.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (l *LinkPreviewAlbumMediaPhoto) EncodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode linkPreviewAlbumMediaPhoto#c83db38e as nil")
	}
	if err := l.Photo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode linkPreviewAlbumMediaPhoto#c83db38e: field photo: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (l *LinkPreviewAlbumMediaPhoto) Decode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode linkPreviewAlbumMediaPhoto#c83db38e to nil")
	}
	if err := b.ConsumeID(LinkPreviewAlbumMediaPhotoTypeID); err != nil {
		return fmt.Errorf("unable to decode linkPreviewAlbumMediaPhoto#c83db38e: %w", err)
	}
	return l.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (l *LinkPreviewAlbumMediaPhoto) DecodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode linkPreviewAlbumMediaPhoto#c83db38e to nil")
	}
	{
		if err := l.Photo.Decode(b); err != nil {
			return fmt.Errorf("unable to decode linkPreviewAlbumMediaPhoto#c83db38e: field photo: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (l *LinkPreviewAlbumMediaPhoto) EncodeTDLibJSON(b tdjson.Encoder) error {
	if l == nil {
		return fmt.Errorf("can't encode linkPreviewAlbumMediaPhoto#c83db38e as nil")
	}
	b.ObjStart()
	b.PutID("linkPreviewAlbumMediaPhoto")
	b.Comma()
	b.FieldStart("photo")
	if err := l.Photo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode linkPreviewAlbumMediaPhoto#c83db38e: field photo: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (l *LinkPreviewAlbumMediaPhoto) DecodeTDLibJSON(b tdjson.Decoder) error {
	if l == nil {
		return fmt.Errorf("can't decode linkPreviewAlbumMediaPhoto#c83db38e to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("linkPreviewAlbumMediaPhoto"); err != nil {
				return fmt.Errorf("unable to decode linkPreviewAlbumMediaPhoto#c83db38e: %w", err)
			}
		case "photo":
			if err := l.Photo.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode linkPreviewAlbumMediaPhoto#c83db38e: field photo: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetPhoto returns value of Photo field.
func (l *LinkPreviewAlbumMediaPhoto) GetPhoto() (value Photo) {
	if l == nil {
		return
	}
	return l.Photo
}

// LinkPreviewAlbumMediaVideo represents TL type `linkPreviewAlbumMediaVideo#174856db`.
type LinkPreviewAlbumMediaVideo struct {
	// Video description
	Video Video
}

// LinkPreviewAlbumMediaVideoTypeID is TL type id of LinkPreviewAlbumMediaVideo.
const LinkPreviewAlbumMediaVideoTypeID = 0x174856db

// construct implements constructor of LinkPreviewAlbumMediaClass.
func (l LinkPreviewAlbumMediaVideo) construct() LinkPreviewAlbumMediaClass { return &l }

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

	_ LinkPreviewAlbumMediaClass = &LinkPreviewAlbumMediaVideo{}
)

func (l *LinkPreviewAlbumMediaVideo) Zero() bool {
	if l == nil {
		return true
	}
	if !(l.Video.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (l *LinkPreviewAlbumMediaVideo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "linkPreviewAlbumMediaVideo",
		ID:   LinkPreviewAlbumMediaVideoTypeID,
	}
	if l == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Video",
			SchemaName: "video",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (l *LinkPreviewAlbumMediaVideo) Encode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode linkPreviewAlbumMediaVideo#174856db as nil")
	}
	b.PutID(LinkPreviewAlbumMediaVideoTypeID)
	return l.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (l *LinkPreviewAlbumMediaVideo) EncodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode linkPreviewAlbumMediaVideo#174856db as nil")
	}
	if err := l.Video.Encode(b); err != nil {
		return fmt.Errorf("unable to encode linkPreviewAlbumMediaVideo#174856db: field video: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (l *LinkPreviewAlbumMediaVideo) Decode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode linkPreviewAlbumMediaVideo#174856db to nil")
	}
	if err := b.ConsumeID(LinkPreviewAlbumMediaVideoTypeID); err != nil {
		return fmt.Errorf("unable to decode linkPreviewAlbumMediaVideo#174856db: %w", err)
	}
	return l.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (l *LinkPreviewAlbumMediaVideo) DecodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode linkPreviewAlbumMediaVideo#174856db to nil")
	}
	{
		if err := l.Video.Decode(b); err != nil {
			return fmt.Errorf("unable to decode linkPreviewAlbumMediaVideo#174856db: field video: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (l *LinkPreviewAlbumMediaVideo) EncodeTDLibJSON(b tdjson.Encoder) error {
	if l == nil {
		return fmt.Errorf("can't encode linkPreviewAlbumMediaVideo#174856db as nil")
	}
	b.ObjStart()
	b.PutID("linkPreviewAlbumMediaVideo")
	b.Comma()
	b.FieldStart("video")
	if err := l.Video.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode linkPreviewAlbumMediaVideo#174856db: field video: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (l *LinkPreviewAlbumMediaVideo) DecodeTDLibJSON(b tdjson.Decoder) error {
	if l == nil {
		return fmt.Errorf("can't decode linkPreviewAlbumMediaVideo#174856db to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("linkPreviewAlbumMediaVideo"); err != nil {
				return fmt.Errorf("unable to decode linkPreviewAlbumMediaVideo#174856db: %w", err)
			}
		case "video":
			if err := l.Video.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode linkPreviewAlbumMediaVideo#174856db: field video: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetVideo returns value of Video field.
func (l *LinkPreviewAlbumMediaVideo) GetVideo() (value Video) {
	if l == nil {
		return
	}
	return l.Video
}

// LinkPreviewAlbumMediaClassName is schema name of LinkPreviewAlbumMediaClass.
const LinkPreviewAlbumMediaClassName = "LinkPreviewAlbumMedia"

// LinkPreviewAlbumMediaClass represents LinkPreviewAlbumMedia generic type.
//
// Example:
//
//	g, err := tdapi.DecodeLinkPreviewAlbumMedia(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.LinkPreviewAlbumMediaPhoto: // linkPreviewAlbumMediaPhoto#c83db38e
//	case *tdapi.LinkPreviewAlbumMediaVideo: // linkPreviewAlbumMediaVideo#174856db
//	default: panic(v)
//	}
type LinkPreviewAlbumMediaClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() LinkPreviewAlbumMediaClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	EncodeTDLibJSON(b tdjson.Encoder) error
	DecodeTDLibJSON(b tdjson.Decoder) error
}

// DecodeLinkPreviewAlbumMedia implements binary de-serialization for LinkPreviewAlbumMediaClass.
func DecodeLinkPreviewAlbumMedia(buf *bin.Buffer) (LinkPreviewAlbumMediaClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case LinkPreviewAlbumMediaPhotoTypeID:
		// Decoding linkPreviewAlbumMediaPhoto#c83db38e.
		v := LinkPreviewAlbumMediaPhoto{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode LinkPreviewAlbumMediaClass: %w", err)
		}
		return &v, nil
	case LinkPreviewAlbumMediaVideoTypeID:
		// Decoding linkPreviewAlbumMediaVideo#174856db.
		v := LinkPreviewAlbumMediaVideo{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode LinkPreviewAlbumMediaClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode LinkPreviewAlbumMediaClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONLinkPreviewAlbumMedia implements binary de-serialization for LinkPreviewAlbumMediaClass.
func DecodeTDLibJSONLinkPreviewAlbumMedia(buf tdjson.Decoder) (LinkPreviewAlbumMediaClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "linkPreviewAlbumMediaPhoto":
		// Decoding linkPreviewAlbumMediaPhoto#c83db38e.
		v := LinkPreviewAlbumMediaPhoto{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode LinkPreviewAlbumMediaClass: %w", err)
		}
		return &v, nil
	case "linkPreviewAlbumMediaVideo":
		// Decoding linkPreviewAlbumMediaVideo#174856db.
		v := LinkPreviewAlbumMediaVideo{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode LinkPreviewAlbumMediaClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode LinkPreviewAlbumMediaClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// LinkPreviewAlbumMedia boxes the LinkPreviewAlbumMediaClass providing a helper.
type LinkPreviewAlbumMediaBox struct {
	LinkPreviewAlbumMedia LinkPreviewAlbumMediaClass
}

// Decode implements bin.Decoder for LinkPreviewAlbumMediaBox.
func (b *LinkPreviewAlbumMediaBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode LinkPreviewAlbumMediaBox to nil")
	}
	v, err := DecodeLinkPreviewAlbumMedia(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.LinkPreviewAlbumMedia = v
	return nil
}

// Encode implements bin.Encode for LinkPreviewAlbumMediaBox.
func (b *LinkPreviewAlbumMediaBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.LinkPreviewAlbumMedia == nil {
		return fmt.Errorf("unable to encode LinkPreviewAlbumMediaClass as nil")
	}
	return b.LinkPreviewAlbumMedia.Encode(buf)
}

// DecodeTDLibJSON implements bin.Decoder for LinkPreviewAlbumMediaBox.
func (b *LinkPreviewAlbumMediaBox) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("unable to decode LinkPreviewAlbumMediaBox to nil")
	}
	v, err := DecodeTDLibJSONLinkPreviewAlbumMedia(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.LinkPreviewAlbumMedia = v
	return nil
}

// EncodeTDLibJSON implements bin.Encode for LinkPreviewAlbumMediaBox.
func (b *LinkPreviewAlbumMediaBox) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil || b.LinkPreviewAlbumMedia == nil {
		return fmt.Errorf("unable to encode LinkPreviewAlbumMediaClass as nil")
	}
	return b.LinkPreviewAlbumMedia.EncodeTDLibJSON(buf)
}
