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

// BotPreviewMedia represents TL type `botPreviewMedia#23e91ba3`.
// Represents a Main Mini App preview media, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps#main-mini-app-previews
//
// See https://core.telegram.org/constructor/botPreviewMedia for reference.
type BotPreviewMedia struct {
	// When was this media last updated.
	Date int
	// The actual photo/video.
	Media MessageMediaClass
}

// BotPreviewMediaTypeID is TL type id of BotPreviewMedia.
const BotPreviewMediaTypeID = 0x23e91ba3

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

func (b *BotPreviewMedia) Zero() bool {
	if b == nil {
		return true
	}
	if !(b.Date == 0) {
		return false
	}
	if !(b.Media == nil) {
		return false
	}

	return true
}

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

// FillFrom fills BotPreviewMedia from given interface.
func (b *BotPreviewMedia) FillFrom(from interface {
	GetDate() (value int)
	GetMedia() (value MessageMediaClass)
}) {
	b.Date = from.GetDate()
	b.Media = from.GetMedia()
}

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

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

// TypeInfo returns info about TL type.
func (b *BotPreviewMedia) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "botPreviewMedia",
		ID:   BotPreviewMediaTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "Media",
			SchemaName: "media",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *BotPreviewMedia) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botPreviewMedia#23e91ba3 as nil")
	}
	buf.PutID(BotPreviewMediaTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BotPreviewMedia) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botPreviewMedia#23e91ba3 as nil")
	}
	buf.PutInt(b.Date)
	if b.Media == nil {
		return fmt.Errorf("unable to encode botPreviewMedia#23e91ba3: field media is nil")
	}
	if err := b.Media.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode botPreviewMedia#23e91ba3: field media: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *BotPreviewMedia) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botPreviewMedia#23e91ba3 to nil")
	}
	if err := buf.ConsumeID(BotPreviewMediaTypeID); err != nil {
		return fmt.Errorf("unable to decode botPreviewMedia#23e91ba3: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BotPreviewMedia) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botPreviewMedia#23e91ba3 to nil")
	}
	{
		value, err := buf.Int()
		if err != nil {
			return fmt.Errorf("unable to decode botPreviewMedia#23e91ba3: field date: %w", err)
		}
		b.Date = value
	}
	{
		value, err := DecodeMessageMedia(buf)
		if err != nil {
			return fmt.Errorf("unable to decode botPreviewMedia#23e91ba3: field media: %w", err)
		}
		b.Media = value
	}
	return nil
}

// GetDate returns value of Date field.
func (b *BotPreviewMedia) GetDate() (value int) {
	if b == nil {
		return
	}
	return b.Date
}

// GetMedia returns value of Media field.
func (b *BotPreviewMedia) GetMedia() (value MessageMediaClass) {
	if b == nil {
		return
	}
	return b.Media
}
