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

// LinkPreviewOptions represents TL type `linkPreviewOptions#3e61b3f3`.
type LinkPreviewOptions struct {
	// True, if link preview must be disabled
	IsDisabled bool
	// URL to use for link preview. If empty, then the first URL found in the message text
	// will be used
	URL string
	// True, if shown media preview must be small; ignored in secret chats or if the URL
	// isn't explicitly specified
	ForceSmallMedia bool
	// True, if shown media preview must be large; ignored in secret chats or if the URL
	// isn't explicitly specified
	ForceLargeMedia bool
	// True, if link preview must be shown above message text; otherwise, the link preview
	// will be shown below the message text; ignored in secret chats
	ShowAboveText bool
}

// LinkPreviewOptionsTypeID is TL type id of LinkPreviewOptions.
const LinkPreviewOptionsTypeID = 0x3e61b3f3

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

func (l *LinkPreviewOptions) Zero() bool {
	if l == nil {
		return true
	}
	if !(l.IsDisabled == false) {
		return false
	}
	if !(l.URL == "") {
		return false
	}
	if !(l.ForceSmallMedia == false) {
		return false
	}
	if !(l.ForceLargeMedia == false) {
		return false
	}
	if !(l.ShowAboveText == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (l *LinkPreviewOptions) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "linkPreviewOptions",
		ID:   LinkPreviewOptionsTypeID,
	}
	if l == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "IsDisabled",
			SchemaName: "is_disabled",
		},
		{
			Name:       "URL",
			SchemaName: "url",
		},
		{
			Name:       "ForceSmallMedia",
			SchemaName: "force_small_media",
		},
		{
			Name:       "ForceLargeMedia",
			SchemaName: "force_large_media",
		},
		{
			Name:       "ShowAboveText",
			SchemaName: "show_above_text",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (l *LinkPreviewOptions) Encode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode linkPreviewOptions#3e61b3f3 as nil")
	}
	b.PutID(LinkPreviewOptionsTypeID)
	return l.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (l *LinkPreviewOptions) EncodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode linkPreviewOptions#3e61b3f3 as nil")
	}
	b.PutBool(l.IsDisabled)
	b.PutString(l.URL)
	b.PutBool(l.ForceSmallMedia)
	b.PutBool(l.ForceLargeMedia)
	b.PutBool(l.ShowAboveText)
	return nil
}

// Decode implements bin.Decoder.
func (l *LinkPreviewOptions) Decode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode linkPreviewOptions#3e61b3f3 to nil")
	}
	if err := b.ConsumeID(LinkPreviewOptionsTypeID); err != nil {
		return fmt.Errorf("unable to decode linkPreviewOptions#3e61b3f3: %w", err)
	}
	return l.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (l *LinkPreviewOptions) DecodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode linkPreviewOptions#3e61b3f3 to nil")
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreviewOptions#3e61b3f3: field is_disabled: %w", err)
		}
		l.IsDisabled = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreviewOptions#3e61b3f3: field url: %w", err)
		}
		l.URL = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreviewOptions#3e61b3f3: field force_small_media: %w", err)
		}
		l.ForceSmallMedia = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreviewOptions#3e61b3f3: field force_large_media: %w", err)
		}
		l.ForceLargeMedia = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode linkPreviewOptions#3e61b3f3: field show_above_text: %w", err)
		}
		l.ShowAboveText = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (l *LinkPreviewOptions) EncodeTDLibJSON(b tdjson.Encoder) error {
	if l == nil {
		return fmt.Errorf("can't encode linkPreviewOptions#3e61b3f3 as nil")
	}
	b.ObjStart()
	b.PutID("linkPreviewOptions")
	b.Comma()
	b.FieldStart("is_disabled")
	b.PutBool(l.IsDisabled)
	b.Comma()
	b.FieldStart("url")
	b.PutString(l.URL)
	b.Comma()
	b.FieldStart("force_small_media")
	b.PutBool(l.ForceSmallMedia)
	b.Comma()
	b.FieldStart("force_large_media")
	b.PutBool(l.ForceLargeMedia)
	b.Comma()
	b.FieldStart("show_above_text")
	b.PutBool(l.ShowAboveText)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (l *LinkPreviewOptions) DecodeTDLibJSON(b tdjson.Decoder) error {
	if l == nil {
		return fmt.Errorf("can't decode linkPreviewOptions#3e61b3f3 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("linkPreviewOptions"); err != nil {
				return fmt.Errorf("unable to decode linkPreviewOptions#3e61b3f3: %w", err)
			}
		case "is_disabled":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreviewOptions#3e61b3f3: field is_disabled: %w", err)
			}
			l.IsDisabled = value
		case "url":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreviewOptions#3e61b3f3: field url: %w", err)
			}
			l.URL = value
		case "force_small_media":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreviewOptions#3e61b3f3: field force_small_media: %w", err)
			}
			l.ForceSmallMedia = value
		case "force_large_media":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreviewOptions#3e61b3f3: field force_large_media: %w", err)
			}
			l.ForceLargeMedia = value
		case "show_above_text":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode linkPreviewOptions#3e61b3f3: field show_above_text: %w", err)
			}
			l.ShowAboveText = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetIsDisabled returns value of IsDisabled field.
func (l *LinkPreviewOptions) GetIsDisabled() (value bool) {
	if l == nil {
		return
	}
	return l.IsDisabled
}

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

// GetForceSmallMedia returns value of ForceSmallMedia field.
func (l *LinkPreviewOptions) GetForceSmallMedia() (value bool) {
	if l == nil {
		return
	}
	return l.ForceSmallMedia
}

// GetForceLargeMedia returns value of ForceLargeMedia field.
func (l *LinkPreviewOptions) GetForceLargeMedia() (value bool) {
	if l == nil {
		return
	}
	return l.ForceLargeMedia
}

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