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

// MessageLink represents TL type `messageLink#af4a3aa6`.
type MessageLink struct {
	// The link
	Link string
	// True, if the link will work for non-members of the chat
	IsPublic bool
}

// MessageLinkTypeID is TL type id of MessageLink.
const MessageLinkTypeID = 0xaf4a3aa6

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

func (m *MessageLink) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.Link == "") {
		return false
	}
	if !(m.IsPublic == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageLink) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageLink",
		ID:   MessageLinkTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Link",
			SchemaName: "link",
		},
		{
			Name:       "IsPublic",
			SchemaName: "is_public",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageLink) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageLink#af4a3aa6 as nil")
	}
	b.PutID(MessageLinkTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageLink) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageLink#af4a3aa6 as nil")
	}
	b.PutString(m.Link)
	b.PutBool(m.IsPublic)
	return nil
}

// Decode implements bin.Decoder.
func (m *MessageLink) Decode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageLink#af4a3aa6 to nil")
	}
	if err := b.ConsumeID(MessageLinkTypeID); err != nil {
		return fmt.Errorf("unable to decode messageLink#af4a3aa6: %w", err)
	}
	return m.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (m *MessageLink) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageLink#af4a3aa6 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messageLink#af4a3aa6: field link: %w", err)
		}
		m.Link = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode messageLink#af4a3aa6: field is_public: %w", err)
		}
		m.IsPublic = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageLink) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageLink#af4a3aa6 as nil")
	}
	b.ObjStart()
	b.PutID("messageLink")
	b.Comma()
	b.FieldStart("link")
	b.PutString(m.Link)
	b.Comma()
	b.FieldStart("is_public")
	b.PutBool(m.IsPublic)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (m *MessageLink) DecodeTDLibJSON(b tdjson.Decoder) error {
	if m == nil {
		return fmt.Errorf("can't decode messageLink#af4a3aa6 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageLink"); err != nil {
				return fmt.Errorf("unable to decode messageLink#af4a3aa6: %w", err)
			}
		case "link":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode messageLink#af4a3aa6: field link: %w", err)
			}
			m.Link = value
		case "is_public":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode messageLink#af4a3aa6: field is_public: %w", err)
			}
			m.IsPublic = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetLink returns value of Link field.
func (m *MessageLink) GetLink() (value string) {
	if m == nil {
		return
	}
	return m.Link
}

// GetIsPublic returns value of IsPublic field.
func (m *MessageLink) GetIsPublic() (value bool) {
	if m == nil {
		return
	}
	return m.IsPublic
}
