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

// LinkPreview represents TL type `linkPreview#6714d1f2`.
type LinkPreview struct {
	// Original URL of the link
	URL string
	// URL to display
	DisplayURL string
	// Short name of the site (e.g., Google Docs, App Store)
	SiteName string
	// Title of the content
	Title string
	// Describes a link preview
	Description FormattedText
	// Author of the content
	Author string
	// Type of the link preview
	Type LinkPreviewTypeClass
	// True, if size of media in the preview can be changed
	HasLargeMedia bool
	// True, if large media preview must be shown; otherwise, the media preview must be shown
	// small and only the first frame must be shown for videos
	ShowLargeMedia bool
	// True, if media must be shown above link preview description; otherwise, the media must
	// be shown below the description
	ShowMediaAboveDescription bool
	// True, if there is no need to show an ordinary open URL confirmation, when opening the
	// URL from the preview, because the URL is shown in the message text in clear
	SkipConfirmation bool
	// True, if the link preview must be shown above message text; otherwise, the link
	// preview must be shown below the message text
	ShowAboveText bool
	// Version of instant view (currently, can be 1 or 2) for the web page; 0 if none
	InstantViewVersion int32
}

// LinkPreviewTypeID is TL type id of LinkPreview.
const LinkPreviewTypeID = 0x6714d1f2

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

func (l *LinkPreview) Zero() bool {
	if l == nil {
		return true
	}
	if !(l.URL == "") {
		return false
	}
	if !(l.DisplayURL == "") {
		return false
	}
	if !(l.SiteName == "") {
		return false
	}
	if !(l.Title == "") {
		return false
	}
	if !(l.Description.Zero()) {
		return false
	}
	if !(l.Author == "") {
		return false
	}
	if !(l.Type == nil) {
		return false
	}
	if !(l.HasLargeMedia == false) {
		return false
	}
	if !(l.ShowLargeMedia == false) {
		return false
	}
	if !(l.ShowMediaAboveDescription == false) {
		return false
	}
	if !(l.SkipConfirmation == false) {
		return false
	}
	if !(l.ShowAboveText == false) {
		return false
	}
	if !(l.InstantViewVersion == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (l *LinkPreview) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "linkPreview",
		ID:   LinkPreviewTypeID,
	}
	if l == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "URL",
			SchemaName: "url",
		},
		{
			Name:       "DisplayURL",
			SchemaName: "display_url",
		},
		{
			Name:       "SiteName",
			SchemaName: "site_name",
		},
		{
			Name:       "Title",
			SchemaName: "title",
		},
		{
			Name:       "Description",
			SchemaName: "description",
		},
		{
			Name:       "Author",
			SchemaName: "author",
		},
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "HasLargeMedia",
			SchemaName: "has_large_media",
		},
		{
			Name:       "ShowLargeMedia",
			SchemaName: "show_large_media",
		},
		{
			Name:       "ShowMediaAboveDescription",
			SchemaName: "show_media_above_description",
		},
		{
			Name:       "SkipConfirmation",
			SchemaName: "skip_confirmation",
		},
		{
			Name:       "ShowAboveText",
			SchemaName: "show_above_text",
		},
		{
			Name:       "InstantViewVersion",
			SchemaName: "instant_view_version",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (l *LinkPreview) Encode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode linkPreview#6714d1f2 as nil")
	}
	b.PutID(LinkPreviewTypeID)
	return l.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (l *LinkPreview) EncodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode linkPreview#6714d1f2 as nil")
	}
	b.PutString(l.URL)
	b.PutString(l.DisplayURL)
	b.PutString(l.SiteName)
	b.PutString(l.Title)
	if err := l.Description.Encode(b); err != nil {
		return fmt.Errorf("unable to encode linkPreview#6714d1f2: field description: %w", err)
	}
	b.PutString(l.Author)
	if l.Type == nil {
		return fmt.Errorf("unable to encode linkPreview#6714d1f2: field type is nil")
	}
	if err := l.Type.Encode(b); err != nil {
		return fmt.Errorf("unable to encode linkPreview#6714d1f2: field type: %w", err)
	}
	b.PutBool(l.HasLargeMedia)
	b.PutBool(l.ShowLargeMedia)
	b.PutBool(l.ShowMediaAboveDescription)
	b.PutBool(l.SkipConfirmation)
	b.PutBool(l.ShowAboveText)
	b.PutInt32(l.InstantViewVersion)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (l *LinkPreview) DecodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode linkPreview#6714d1f2 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreview#6714d1f2: field url: %w", err)
		}
		l.URL = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreview#6714d1f2: field display_url: %w", err)
		}
		l.DisplayURL = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreview#6714d1f2: field site_name: %w", err)
		}
		l.SiteName = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreview#6714d1f2: field title: %w", err)
		}
		l.Title = value
	}
	{
		if err := l.Description.Decode(b); err != nil {
			return fmt.Errorf("unable to decode linkPreview#6714d1f2: field description: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreview#6714d1f2: field author: %w", err)
		}
		l.Author = value
	}
	{
		value, err := DecodeLinkPreviewType(b)
		if err != nil {
			return fmt.Errorf("unable to decode linkPreview#6714d1f2: field type: %w", err)
		}
		l.Type = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreview#6714d1f2: field has_large_media: %w", err)
		}
		l.HasLargeMedia = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreview#6714d1f2: field show_large_media: %w", err)
		}
		l.ShowLargeMedia = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreview#6714d1f2: field show_media_above_description: %w", err)
		}
		l.ShowMediaAboveDescription = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreview#6714d1f2: field skip_confirmation: %w", err)
		}
		l.SkipConfirmation = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreview#6714d1f2: field show_above_text: %w", err)
		}
		l.ShowAboveText = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreview#6714d1f2: field instant_view_version: %w", err)
		}
		l.InstantViewVersion = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (l *LinkPreview) EncodeTDLibJSON(b tdjson.Encoder) error {
	if l == nil {
		return fmt.Errorf("can't encode linkPreview#6714d1f2 as nil")
	}
	b.ObjStart()
	b.PutID("linkPreview")
	b.Comma()
	b.FieldStart("url")
	b.PutString(l.URL)
	b.Comma()
	b.FieldStart("display_url")
	b.PutString(l.DisplayURL)
	b.Comma()
	b.FieldStart("site_name")
	b.PutString(l.SiteName)
	b.Comma()
	b.FieldStart("title")
	b.PutString(l.Title)
	b.Comma()
	b.FieldStart("description")
	if err := l.Description.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode linkPreview#6714d1f2: field description: %w", err)
	}
	b.Comma()
	b.FieldStart("author")
	b.PutString(l.Author)
	b.Comma()
	b.FieldStart("type")
	if l.Type == nil {
		return fmt.Errorf("unable to encode linkPreview#6714d1f2: field type is nil")
	}
	if err := l.Type.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode linkPreview#6714d1f2: field type: %w", err)
	}
	b.Comma()
	b.FieldStart("has_large_media")
	b.PutBool(l.HasLargeMedia)
	b.Comma()
	b.FieldStart("show_large_media")
	b.PutBool(l.ShowLargeMedia)
	b.Comma()
	b.FieldStart("show_media_above_description")
	b.PutBool(l.ShowMediaAboveDescription)
	b.Comma()
	b.FieldStart("skip_confirmation")
	b.PutBool(l.SkipConfirmation)
	b.Comma()
	b.FieldStart("show_above_text")
	b.PutBool(l.ShowAboveText)
	b.Comma()
	b.FieldStart("instant_view_version")
	b.PutInt32(l.InstantViewVersion)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("linkPreview"); err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: %w", err)
			}
		case "url":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: field url: %w", err)
			}
			l.URL = value
		case "display_url":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: field display_url: %w", err)
			}
			l.DisplayURL = value
		case "site_name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: field site_name: %w", err)
			}
			l.SiteName = value
		case "title":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: field title: %w", err)
			}
			l.Title = value
		case "description":
			if err := l.Description.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: field description: %w", err)
			}
		case "author":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: field author: %w", err)
			}
			l.Author = value
		case "type":
			value, err := DecodeTDLibJSONLinkPreviewType(b)
			if err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: field type: %w", err)
			}
			l.Type = value
		case "has_large_media":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: field has_large_media: %w", err)
			}
			l.HasLargeMedia = value
		case "show_large_media":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: field show_large_media: %w", err)
			}
			l.ShowLargeMedia = value
		case "show_media_above_description":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: field show_media_above_description: %w", err)
			}
			l.ShowMediaAboveDescription = value
		case "skip_confirmation":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: field skip_confirmation: %w", err)
			}
			l.SkipConfirmation = value
		case "show_above_text":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: field show_above_text: %w", err)
			}
			l.ShowAboveText = value
		case "instant_view_version":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreview#6714d1f2: field instant_view_version: %w", err)
			}
			l.InstantViewVersion = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetURL returns value of URL field.
func (l *LinkPreview) GetURL() (value string) {
	if l == nil {
		return
	}
	return l.URL
}

// GetDisplayURL returns value of DisplayURL field.
func (l *LinkPreview) GetDisplayURL() (value string) {
	if l == nil {
		return
	}
	return l.DisplayURL
}

// GetSiteName returns value of SiteName field.
func (l *LinkPreview) GetSiteName() (value string) {
	if l == nil {
		return
	}
	return l.SiteName
}

// GetTitle returns value of Title field.
func (l *LinkPreview) GetTitle() (value string) {
	if l == nil {
		return
	}
	return l.Title
}

// GetDescription returns value of Description field.
func (l *LinkPreview) GetDescription() (value FormattedText) {
	if l == nil {
		return
	}
	return l.Description
}

// GetAuthor returns value of Author field.
func (l *LinkPreview) GetAuthor() (value string) {
	if l == nil {
		return
	}
	return l.Author
}

// GetType returns value of Type field.
func (l *LinkPreview) GetType() (value LinkPreviewTypeClass) {
	if l == nil {
		return
	}
	return l.Type
}

// GetHasLargeMedia returns value of HasLargeMedia field.
func (l *LinkPreview) GetHasLargeMedia() (value bool) {
	if l == nil {
		return
	}
	return l.HasLargeMedia
}

// GetShowLargeMedia returns value of ShowLargeMedia field.
func (l *LinkPreview) GetShowLargeMedia() (value bool) {
	if l == nil {
		return
	}
	return l.ShowLargeMedia
}

// GetShowMediaAboveDescription returns value of ShowMediaAboveDescription field.
func (l *LinkPreview) GetShowMediaAboveDescription() (value bool) {
	if l == nil {
		return
	}
	return l.ShowMediaAboveDescription
}

// GetSkipConfirmation returns value of SkipConfirmation field.
func (l *LinkPreview) GetSkipConfirmation() (value bool) {
	if l == nil {
		return
	}
	return l.SkipConfirmation
}

// GetShowAboveText returns value of ShowAboveText field.
func (l *LinkPreview) GetShowAboveText() (value bool) {
	if l == nil {
		return
	}
	return l.ShowAboveText
}

// GetInstantViewVersion returns value of InstantViewVersion field.
func (l *LinkPreview) GetInstantViewVersion() (value int32) {
	if l == nil {
		return
	}
	return l.InstantViewVersion
}
