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

// SponsoredPeer represents TL type `sponsoredPeer#c69708d3`.
// A sponsored peer.
//
// See https://core.telegram.org/constructor/sponsoredPeer for reference.
type SponsoredPeer struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// ID of the sponsored peer, to be passed to messages.viewSponsoredMessage¹, messages
	// clickSponsoredMessage² or messages.reportSponsoredMessage³ (the same methods used
	// for sponsored messages &raquo⁴).
	//
	// Links:
	//  1) https://core.telegram.org/method/messages.viewSponsoredMessage
	//  2) https://core.telegram.org/method/messages.clickSponsoredMessage
	//  3) https://core.telegram.org/method/messages.reportSponsoredMessage
	//  4) https://core.telegram.org/api/sponsored-messages
	RandomID []byte
	// The sponsored peer.
	Peer PeerClass
	// If set, contains additional information about the sponsor to be shown along with the
	// peer.
	//
	// Use SetSponsorInfo and GetSponsorInfo helpers.
	SponsorInfo string
	// If set, contains additional information about the sponsored message to be shown along
	// with the peer.
	//
	// Use SetAdditionalInfo and GetAdditionalInfo helpers.
	AdditionalInfo string
}

// SponsoredPeerTypeID is TL type id of SponsoredPeer.
const SponsoredPeerTypeID = 0xc69708d3

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

func (s *SponsoredPeer) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.RandomID == nil) {
		return false
	}
	if !(s.Peer == nil) {
		return false
	}
	if !(s.SponsorInfo == "") {
		return false
	}
	if !(s.AdditionalInfo == "") {
		return false
	}

	return true
}

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

// FillFrom fills SponsoredPeer from given interface.
func (s *SponsoredPeer) FillFrom(from interface {
	GetRandomID() (value []byte)
	GetPeer() (value PeerClass)
	GetSponsorInfo() (value string, ok bool)
	GetAdditionalInfo() (value string, ok bool)
}) {
	s.RandomID = from.GetRandomID()
	s.Peer = from.GetPeer()
	if val, ok := from.GetSponsorInfo(); ok {
		s.SponsorInfo = val
	}

	if val, ok := from.GetAdditionalInfo(); ok {
		s.AdditionalInfo = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (s *SponsoredPeer) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "sponsoredPeer",
		ID:   SponsoredPeerTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "RandomID",
			SchemaName: "random_id",
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "SponsorInfo",
			SchemaName: "sponsor_info",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "AdditionalInfo",
			SchemaName: "additional_info",
			Null:       !s.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *SponsoredPeer) SetFlags() {
	if !(s.SponsorInfo == "") {
		s.Flags.Set(0)
	}
	if !(s.AdditionalInfo == "") {
		s.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (s *SponsoredPeer) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sponsoredPeer#c69708d3 as nil")
	}
	b.PutID(SponsoredPeerTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SponsoredPeer) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode sponsoredPeer#c69708d3 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode sponsoredPeer#c69708d3: field flags: %w", err)
	}
	b.PutBytes(s.RandomID)
	if s.Peer == nil {
		return fmt.Errorf("unable to encode sponsoredPeer#c69708d3: field peer is nil")
	}
	if err := s.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode sponsoredPeer#c69708d3: field peer: %w", err)
	}
	if s.Flags.Has(0) {
		b.PutString(s.SponsorInfo)
	}
	if s.Flags.Has(1) {
		b.PutString(s.AdditionalInfo)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SponsoredPeer) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sponsoredPeer#c69708d3 to nil")
	}
	if err := b.ConsumeID(SponsoredPeerTypeID); err != nil {
		return fmt.Errorf("unable to decode sponsoredPeer#c69708d3: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SponsoredPeer) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode sponsoredPeer#c69708d3 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode sponsoredPeer#c69708d3: field flags: %w", err)
		}
	}
	{
		value, err := b.Bytes()
		if err != nil {
			return fmt.Errorf("unable to decode sponsoredPeer#c69708d3: field random_id: %w", err)
		}
		s.RandomID = value
	}
	{
		value, err := DecodePeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode sponsoredPeer#c69708d3: field peer: %w", err)
		}
		s.Peer = value
	}
	if s.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode sponsoredPeer#c69708d3: field sponsor_info: %w", err)
		}
		s.SponsorInfo = value
	}
	if s.Flags.Has(1) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode sponsoredPeer#c69708d3: field additional_info: %w", err)
		}
		s.AdditionalInfo = value
	}
	return nil
}

// GetRandomID returns value of RandomID field.
func (s *SponsoredPeer) GetRandomID() (value []byte) {
	if s == nil {
		return
	}
	return s.RandomID
}

// GetPeer returns value of Peer field.
func (s *SponsoredPeer) GetPeer() (value PeerClass) {
	if s == nil {
		return
	}
	return s.Peer
}

// SetSponsorInfo sets value of SponsorInfo conditional field.
func (s *SponsoredPeer) SetSponsorInfo(value string) {
	s.Flags.Set(0)
	s.SponsorInfo = value
}

// GetSponsorInfo returns value of SponsorInfo conditional field and
// boolean which is true if field was set.
func (s *SponsoredPeer) GetSponsorInfo() (value string, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.SponsorInfo, true
}

// SetAdditionalInfo sets value of AdditionalInfo conditional field.
func (s *SponsoredPeer) SetAdditionalInfo(value string) {
	s.Flags.Set(1)
	s.AdditionalInfo = value
}

// GetAdditionalInfo returns value of AdditionalInfo conditional field and
// boolean which is true if field was set.
func (s *SponsoredPeer) GetAdditionalInfo() (value string, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.AdditionalInfo, true
}
