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

// MessageSponsor represents TL type `messageSponsor#77c251de`.
type MessageSponsor struct {
	// URL of the sponsor to be opened when the message is clicked
	URL string
	// Photo of the sponsor; may be null if must not be shown
	Photo Photo
	// Additional optional information about the sponsor to be shown along with the message
	Info string
}

// MessageSponsorTypeID is TL type id of MessageSponsor.
const MessageSponsorTypeID = 0x77c251de

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

func (m *MessageSponsor) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.URL == "") {
		return false
	}
	if !(m.Photo.Zero()) {
		return false
	}
	if !(m.Info == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (m *MessageSponsor) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "messageSponsor",
		ID:   MessageSponsorTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "URL",
			SchemaName: "url",
		},
		{
			Name:       "Photo",
			SchemaName: "photo",
		},
		{
			Name:       "Info",
			SchemaName: "info",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (m *MessageSponsor) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageSponsor#77c251de as nil")
	}
	b.PutID(MessageSponsorTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MessageSponsor) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode messageSponsor#77c251de as nil")
	}
	b.PutString(m.URL)
	if err := m.Photo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode messageSponsor#77c251de: field photo: %w", err)
	}
	b.PutString(m.Info)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (m *MessageSponsor) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode messageSponsor#77c251de to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messageSponsor#77c251de: field url: %w", err)
		}
		m.URL = value
	}
	{
		if err := m.Photo.Decode(b); err != nil {
			return fmt.Errorf("unable to decode messageSponsor#77c251de: field photo: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode messageSponsor#77c251de: field info: %w", err)
		}
		m.Info = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (m *MessageSponsor) EncodeTDLibJSON(b tdjson.Encoder) error {
	if m == nil {
		return fmt.Errorf("can't encode messageSponsor#77c251de as nil")
	}
	b.ObjStart()
	b.PutID("messageSponsor")
	b.Comma()
	b.FieldStart("url")
	b.PutString(m.URL)
	b.Comma()
	b.FieldStart("photo")
	if err := m.Photo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode messageSponsor#77c251de: field photo: %w", err)
	}
	b.Comma()
	b.FieldStart("info")
	b.PutString(m.Info)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("messageSponsor"); err != nil {
				return fmt.Errorf("unable to decode messageSponsor#77c251de: %w", err)
			}
		case "url":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode messageSponsor#77c251de: field url: %w", err)
			}
			m.URL = value
		case "photo":
			if err := m.Photo.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode messageSponsor#77c251de: field photo: %w", err)
			}
		case "info":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode messageSponsor#77c251de: field info: %w", err)
			}
			m.Info = value
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetPhoto returns value of Photo field.
func (m *MessageSponsor) GetPhoto() (value Photo) {
	if m == nil {
		return
	}
	return m.Photo
}

// GetInfo returns value of Info field.
func (m *MessageSponsor) GetInfo() (value string) {
	if m == nil {
		return
	}
	return m.Info
}
