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

// BotMediaPreview represents TL type `botMediaPreview#9eb59ce8`.
type BotMediaPreview struct {
	// Point in time (Unix timestamp) when the preview was added or changed last time
	Date int32
	// Content of the preview
	Content StoryContentClass
}

// BotMediaPreviewTypeID is TL type id of BotMediaPreview.
const BotMediaPreviewTypeID = 0x9eb59ce8

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (b *BotMediaPreview) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "botMediaPreview",
		ID:   BotMediaPreviewTypeID,
	}
	if b == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "Content",
			SchemaName: "content",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (b *BotMediaPreview) Encode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botMediaPreview#9eb59ce8 as nil")
	}
	buf.PutID(BotMediaPreviewTypeID)
	return b.EncodeBare(buf)
}

// EncodeBare implements bin.BareEncoder.
func (b *BotMediaPreview) EncodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't encode botMediaPreview#9eb59ce8 as nil")
	}
	buf.PutInt32(b.Date)
	if b.Content == nil {
		return fmt.Errorf("unable to encode botMediaPreview#9eb59ce8: field content is nil")
	}
	if err := b.Content.Encode(buf); err != nil {
		return fmt.Errorf("unable to encode botMediaPreview#9eb59ce8: field content: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (b *BotMediaPreview) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botMediaPreview#9eb59ce8 to nil")
	}
	if err := buf.ConsumeID(BotMediaPreviewTypeID); err != nil {
		return fmt.Errorf("unable to decode botMediaPreview#9eb59ce8: %w", err)
	}
	return b.DecodeBare(buf)
}

// DecodeBare implements bin.BareDecoder.
func (b *BotMediaPreview) DecodeBare(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("can't decode botMediaPreview#9eb59ce8 to nil")
	}
	{
		value, err := buf.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode botMediaPreview#9eb59ce8: field date: %w", err)
		}
		b.Date = value
	}
	{
		value, err := DecodeStoryContent(buf)
		if err != nil {
			return fmt.Errorf("unable to decode botMediaPreview#9eb59ce8: field content: %w", err)
		}
		b.Content = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (b *BotMediaPreview) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil {
		return fmt.Errorf("can't encode botMediaPreview#9eb59ce8 as nil")
	}
	buf.ObjStart()
	buf.PutID("botMediaPreview")
	buf.Comma()
	buf.FieldStart("date")
	buf.PutInt32(b.Date)
	buf.Comma()
	buf.FieldStart("content")
	if b.Content == nil {
		return fmt.Errorf("unable to encode botMediaPreview#9eb59ce8: field content is nil")
	}
	if err := b.Content.EncodeTDLibJSON(buf); err != nil {
		return fmt.Errorf("unable to encode botMediaPreview#9eb59ce8: field content: %w", err)
	}
	buf.Comma()
	buf.StripComma()
	buf.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (b *BotMediaPreview) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("can't decode botMediaPreview#9eb59ce8 to nil")
	}

	return buf.Obj(func(buf tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := buf.ConsumeID("botMediaPreview"); err != nil {
				return fmt.Errorf("unable to decode botMediaPreview#9eb59ce8: %w", err)
			}
		case "date":
			value, err := buf.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode botMediaPreview#9eb59ce8: field date: %w", err)
			}
			b.Date = value
		case "content":
			value, err := DecodeTDLibJSONStoryContent(buf)
			if err != nil {
				return fmt.Errorf("unable to decode botMediaPreview#9eb59ce8: field content: %w", err)
			}
			b.Content = value
		default:
			return buf.Skip()
		}
		return nil
	})
}

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

// GetContent returns value of Content field.
func (b *BotMediaPreview) GetContent() (value StoryContentClass) {
	if b == nil {
		return
	}
	return b.Content
}
