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

// BotsPreviewInfo represents TL type `bots.previewInfo#ca71d64`.
// Contains info about Main Mini App previews, see here »¹ for more info.
//
// Links:
//  1. https://core.telegram.org/api/bots/webapps#main-mini-app-previews
//
// See https://core.telegram.org/constructor/bots.previewInfo for reference.
type BotsPreviewInfo struct {
	// All preview medias for the language code passed to bots.getPreviewInfo¹.
	//
	// Links:
	//  1) https://core.telegram.org/method/bots.getPreviewInfo
	Media []BotPreviewMedia
	// All available language codes for which preview medias were uploaded (regardless of the
	// language code passed to bots.getPreviewInfo¹).
	//
	// Links:
	//  1) https://core.telegram.org/method/bots.getPreviewInfo
	LangCodes []string
}

// BotsPreviewInfoTypeID is TL type id of BotsPreviewInfo.
const BotsPreviewInfoTypeID = 0xca71d64

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

func (p *BotsPreviewInfo) Zero() bool {
	if p == nil {
		return true
	}
	if !(p.Media == nil) {
		return false
	}
	if !(p.LangCodes == nil) {
		return false
	}

	return true
}

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

// FillFrom fills BotsPreviewInfo from given interface.
func (p *BotsPreviewInfo) FillFrom(from interface {
	GetMedia() (value []BotPreviewMedia)
	GetLangCodes() (value []string)
}) {
	p.Media = from.GetMedia()
	p.LangCodes = from.GetLangCodes()
}

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

// TypeName returns name of type in TL schema.
func (*BotsPreviewInfo) TypeName() string {
	return "bots.previewInfo"
}

// TypeInfo returns info about TL type.
func (p *BotsPreviewInfo) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "bots.previewInfo",
		ID:   BotsPreviewInfoTypeID,
	}
	if p == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Media",
			SchemaName: "media",
		},
		{
			Name:       "LangCodes",
			SchemaName: "lang_codes",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (p *BotsPreviewInfo) Encode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode bots.previewInfo#ca71d64 as nil")
	}
	b.PutID(BotsPreviewInfoTypeID)
	return p.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (p *BotsPreviewInfo) EncodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't encode bots.previewInfo#ca71d64 as nil")
	}
	b.PutVectorHeader(len(p.Media))
	for idx, v := range p.Media {
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode bots.previewInfo#ca71d64: field media element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(p.LangCodes))
	for _, v := range p.LangCodes {
		b.PutString(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (p *BotsPreviewInfo) Decode(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode bots.previewInfo#ca71d64 to nil")
	}
	if err := b.ConsumeID(BotsPreviewInfoTypeID); err != nil {
		return fmt.Errorf("unable to decode bots.previewInfo#ca71d64: %w", err)
	}
	return p.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (p *BotsPreviewInfo) DecodeBare(b *bin.Buffer) error {
	if p == nil {
		return fmt.Errorf("can't decode bots.previewInfo#ca71d64 to nil")
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode bots.previewInfo#ca71d64: field media: %w", err)
		}

		if headerLen > 0 {
			p.Media = make([]BotPreviewMedia, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value BotPreviewMedia
			if err := value.Decode(b); err != nil {
				return fmt.Errorf("unable to decode bots.previewInfo#ca71d64: field media: %w", err)
			}
			p.Media = append(p.Media, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode bots.previewInfo#ca71d64: field lang_codes: %w", err)
		}

		if headerLen > 0 {
			p.LangCodes = make([]string, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode bots.previewInfo#ca71d64: field lang_codes: %w", err)
			}
			p.LangCodes = append(p.LangCodes, value)
		}
	}
	return nil
}

// GetMedia returns value of Media field.
func (p *BotsPreviewInfo) GetMedia() (value []BotPreviewMedia) {
	if p == nil {
		return
	}
	return p.Media
}

// GetLangCodes returns value of LangCodes field.
func (p *BotsPreviewInfo) GetLangCodes() (value []string) {
	if p == nil {
		return
	}
	return p.LangCodes
}
