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

// MessagesWebPage represents TL type `messages.webPage#fd5e12bd`.
// Represents an Instant View webpage.
//
// See https://core.telegram.org/constructor/messages.webPage for reference.
type MessagesWebPage struct {
	// The instant view webpage.
	Webpage WebPageClass
	// Chats mentioned in the webpage.
	Chats []ChatClass
	// Users mentioned in the webpage.
	Users []UserClass
}

// MessagesWebPageTypeID is TL type id of MessagesWebPage.
const MessagesWebPageTypeID = 0xfd5e12bd

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

func (w *MessagesWebPage) Zero() bool {
	if w == nil {
		return true
	}
	if !(w.Webpage == nil) {
		return false
	}
	if !(w.Chats == nil) {
		return false
	}
	if !(w.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills MessagesWebPage from given interface.
func (w *MessagesWebPage) FillFrom(from interface {
	GetWebpage() (value WebPageClass)
	GetChats() (value []ChatClass)
	GetUsers() (value []UserClass)
}) {
	w.Webpage = from.GetWebpage()
	w.Chats = from.GetChats()
	w.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*MessagesWebPage) TypeName() string {
	return "messages.webPage"
}

// TypeInfo returns info about TL type.
func (w *MessagesWebPage) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messages.webPage",
		ID:   MessagesWebPageTypeID,
	}
	if w == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Webpage",
			SchemaName: "webpage",
		},
		{
			Name:       "Chats",
			SchemaName: "chats",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (w *MessagesWebPage) Encode(b *bin.Buffer) error {
	if w == nil {
		return fmt.Errorf("can't encode messages.webPage#fd5e12bd as nil")
	}
	b.PutID(MessagesWebPageTypeID)
	return w.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (w *MessagesWebPage) EncodeBare(b *bin.Buffer) error {
	if w == nil {
		return fmt.Errorf("can't encode messages.webPage#fd5e12bd as nil")
	}
	if w.Webpage == nil {
		return fmt.Errorf("unable to encode messages.webPage#fd5e12bd: field webpage is nil")
	}
	if err := w.Webpage.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messages.webPage#fd5e12bd: field webpage: %w", err)
	}
	b.PutVectorHeader(len(w.Chats))
	for idx, v := range w.Chats {
		if v == nil {
			return fmt.Errorf("unable to encode messages.webPage#fd5e12bd: field chats element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.webPage#fd5e12bd: field chats element with index %d: %w", idx, err)
		}
	}
	b.PutVectorHeader(len(w.Users))
	for idx, v := range w.Users {
		if v == nil {
			return fmt.Errorf("unable to encode messages.webPage#fd5e12bd: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode messages.webPage#fd5e12bd: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (w *MessagesWebPage) Decode(b *bin.Buffer) error {
	if w == nil {
		return fmt.Errorf("can't decode messages.webPage#fd5e12bd to nil")
	}
	if err := b.ConsumeID(MessagesWebPageTypeID); err != nil {
		return fmt.Errorf("unable to decode messages.webPage#fd5e12bd: %w", err)
	}
	return w.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (w *MessagesWebPage) DecodeBare(b *bin.Buffer) error {
	if w == nil {
		return fmt.Errorf("can't decode messages.webPage#fd5e12bd to nil")
	}
	{
		value, err := DecodeWebPage(b)
		if err != nil {
			return fmt.Errorf("unable to decode messages.webPage#fd5e12bd: field webpage: %w", err)
		}
		w.Webpage = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.webPage#fd5e12bd: field chats: %w", err)
		}

		if headerLen > 0 {
			w.Chats = make([]ChatClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeChat(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.webPage#fd5e12bd: field chats: %w", err)
			}
			w.Chats = append(w.Chats, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode messages.webPage#fd5e12bd: field users: %w", err)
		}

		if headerLen > 0 {
			w.Users = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode messages.webPage#fd5e12bd: field users: %w", err)
			}
			w.Users = append(w.Users, value)
		}
	}
	return nil
}

// GetWebpage returns value of Webpage field.
func (w *MessagesWebPage) GetWebpage() (value WebPageClass) {
	if w == nil {
		return
	}
	return w.Webpage
}

// GetChats returns value of Chats field.
func (w *MessagesWebPage) GetChats() (value []ChatClass) {
	if w == nil {
		return
	}
	return w.Chats
}

// GetUsers returns value of Users field.
func (w *MessagesWebPage) GetUsers() (value []UserClass) {
	if w == nil {
		return
	}
	return w.Users
}

// GetWebpageAsModified returns mapped value of Webpage field.
func (w *MessagesWebPage) GetWebpageAsModified() (ModifiedWebPage, bool) {
	return w.Webpage.AsModified()
}

// MapChats returns field Chats wrapped in ChatClassArray helper.
func (w *MessagesWebPage) MapChats() (value ChatClassArray) {
	return ChatClassArray(w.Chats)
}

// MapUsers returns field Users wrapped in UserClassArray helper.
func (w *MessagesWebPage) MapUsers() (value UserClassArray) {
	return UserClassArray(w.Users)
}
