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

// StarSubscriptionTypeChannel represents TL type `starSubscriptionTypeChannel#c29ab6f5`.
type StarSubscriptionTypeChannel struct {
	// True, if the subscription is active and the user can use the method
	// reuseStarSubscription to join the subscribed chat again
	CanReuse bool
	// The invite link that can be used to renew the subscription if it has been expired; may
	// be empty, if the link isn't available anymore
	InviteLink string
}

// StarSubscriptionTypeChannelTypeID is TL type id of StarSubscriptionTypeChannel.
const StarSubscriptionTypeChannelTypeID = 0xc29ab6f5

// construct implements constructor of StarSubscriptionTypeClass.
func (s StarSubscriptionTypeChannel) construct() StarSubscriptionTypeClass { return &s }

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

	_ StarSubscriptionTypeClass = &StarSubscriptionTypeChannel{}
)

func (s *StarSubscriptionTypeChannel) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.CanReuse == false) {
		return false
	}
	if !(s.InviteLink == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StarSubscriptionTypeChannel) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starSubscriptionTypeChannel",
		ID:   StarSubscriptionTypeChannelTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "CanReuse",
			SchemaName: "can_reuse",
		},
		{
			Name:       "InviteLink",
			SchemaName: "invite_link",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StarSubscriptionTypeChannel) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starSubscriptionTypeChannel#c29ab6f5 as nil")
	}
	b.PutID(StarSubscriptionTypeChannelTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarSubscriptionTypeChannel) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starSubscriptionTypeChannel#c29ab6f5 as nil")
	}
	b.PutBool(s.CanReuse)
	b.PutString(s.InviteLink)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StarSubscriptionTypeChannel) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starSubscriptionTypeChannel#c29ab6f5 to nil")
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode starSubscriptionTypeChannel#c29ab6f5: field can_reuse: %w", err)
		}
		s.CanReuse = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode starSubscriptionTypeChannel#c29ab6f5: field invite_link: %w", err)
		}
		s.InviteLink = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StarSubscriptionTypeChannel) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode starSubscriptionTypeChannel#c29ab6f5 as nil")
	}
	b.ObjStart()
	b.PutID("starSubscriptionTypeChannel")
	b.Comma()
	b.FieldStart("can_reuse")
	b.PutBool(s.CanReuse)
	b.Comma()
	b.FieldStart("invite_link")
	b.PutString(s.InviteLink)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *StarSubscriptionTypeChannel) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode starSubscriptionTypeChannel#c29ab6f5 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("starSubscriptionTypeChannel"); err != nil {
				return fmt.Errorf("unable to decode starSubscriptionTypeChannel#c29ab6f5: %w", err)
			}
		case "can_reuse":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode starSubscriptionTypeChannel#c29ab6f5: field can_reuse: %w", err)
			}
			s.CanReuse = value
		case "invite_link":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode starSubscriptionTypeChannel#c29ab6f5: field invite_link: %w", err)
			}
			s.InviteLink = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetCanReuse returns value of CanReuse field.
func (s *StarSubscriptionTypeChannel) GetCanReuse() (value bool) {
	if s == nil {
		return
	}
	return s.CanReuse
}

// GetInviteLink returns value of InviteLink field.
func (s *StarSubscriptionTypeChannel) GetInviteLink() (value string) {
	if s == nil {
		return
	}
	return s.InviteLink
}

// StarSubscriptionTypeBot represents TL type `starSubscriptionTypeBot#d78ddd2`.
type StarSubscriptionTypeBot struct {
	// True, if the subscription was canceled by the bot and can't be extended
	IsCanceledByBot bool
	// Subscription invoice title
	Title string
	// Subscription invoice photo
	Photo Photo
	// The link to the subscription invoice
	InvoiceLink string
}

// StarSubscriptionTypeBotTypeID is TL type id of StarSubscriptionTypeBot.
const StarSubscriptionTypeBotTypeID = 0xd78ddd2

// construct implements constructor of StarSubscriptionTypeClass.
func (s StarSubscriptionTypeBot) construct() StarSubscriptionTypeClass { return &s }

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

	_ StarSubscriptionTypeClass = &StarSubscriptionTypeBot{}
)

func (s *StarSubscriptionTypeBot) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.IsCanceledByBot == false) {
		return false
	}
	if !(s.Title == "") {
		return false
	}
	if !(s.Photo.Zero()) {
		return false
	}
	if !(s.InvoiceLink == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *StarSubscriptionTypeBot) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starSubscriptionTypeBot",
		ID:   StarSubscriptionTypeBotTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "IsCanceledByBot",
			SchemaName: "is_canceled_by_bot",
		},
		{
			Name:       "Title",
			SchemaName: "title",
		},
		{
			Name:       "Photo",
			SchemaName: "photo",
		},
		{
			Name:       "InvoiceLink",
			SchemaName: "invoice_link",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *StarSubscriptionTypeBot) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starSubscriptionTypeBot#d78ddd2 as nil")
	}
	b.PutID(StarSubscriptionTypeBotTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarSubscriptionTypeBot) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starSubscriptionTypeBot#d78ddd2 as nil")
	}
	b.PutBool(s.IsCanceledByBot)
	b.PutString(s.Title)
	if err := s.Photo.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starSubscriptionTypeBot#d78ddd2: field photo: %w", err)
	}
	b.PutString(s.InvoiceLink)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StarSubscriptionTypeBot) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starSubscriptionTypeBot#d78ddd2 to nil")
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode starSubscriptionTypeBot#d78ddd2: field is_canceled_by_bot: %w", err)
		}
		s.IsCanceledByBot = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode starSubscriptionTypeBot#d78ddd2: field title: %w", err)
		}
		s.Title = value
	}
	{
		if err := s.Photo.Decode(b); err != nil {
			return fmt.Errorf("unable to decode starSubscriptionTypeBot#d78ddd2: field photo: %w", err)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode starSubscriptionTypeBot#d78ddd2: field invoice_link: %w", err)
		}
		s.InvoiceLink = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *StarSubscriptionTypeBot) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode starSubscriptionTypeBot#d78ddd2 as nil")
	}
	b.ObjStart()
	b.PutID("starSubscriptionTypeBot")
	b.Comma()
	b.FieldStart("is_canceled_by_bot")
	b.PutBool(s.IsCanceledByBot)
	b.Comma()
	b.FieldStart("title")
	b.PutString(s.Title)
	b.Comma()
	b.FieldStart("photo")
	if err := s.Photo.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode starSubscriptionTypeBot#d78ddd2: field photo: %w", err)
	}
	b.Comma()
	b.FieldStart("invoice_link")
	b.PutString(s.InvoiceLink)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *StarSubscriptionTypeBot) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode starSubscriptionTypeBot#d78ddd2 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("starSubscriptionTypeBot"); err != nil {
				return fmt.Errorf("unable to decode starSubscriptionTypeBot#d78ddd2: %w", err)
			}
		case "is_canceled_by_bot":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode starSubscriptionTypeBot#d78ddd2: field is_canceled_by_bot: %w", err)
			}
			s.IsCanceledByBot = value
		case "title":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode starSubscriptionTypeBot#d78ddd2: field title: %w", err)
			}
			s.Title = value
		case "photo":
			if err := s.Photo.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode starSubscriptionTypeBot#d78ddd2: field photo: %w", err)
			}
		case "invoice_link":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode starSubscriptionTypeBot#d78ddd2: field invoice_link: %w", err)
			}
			s.InvoiceLink = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetIsCanceledByBot returns value of IsCanceledByBot field.
func (s *StarSubscriptionTypeBot) GetIsCanceledByBot() (value bool) {
	if s == nil {
		return
	}
	return s.IsCanceledByBot
}

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

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

// GetInvoiceLink returns value of InvoiceLink field.
func (s *StarSubscriptionTypeBot) GetInvoiceLink() (value string) {
	if s == nil {
		return
	}
	return s.InvoiceLink
}

// StarSubscriptionTypeClassName is schema name of StarSubscriptionTypeClass.
const StarSubscriptionTypeClassName = "StarSubscriptionType"

// StarSubscriptionTypeClass represents StarSubscriptionType generic type.
//
// Example:
//
//	g, err := tdapi.DecodeStarSubscriptionType(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.StarSubscriptionTypeChannel: // starSubscriptionTypeChannel#c29ab6f5
//	case *tdapi.StarSubscriptionTypeBot: // starSubscriptionTypeBot#d78ddd2
//	default: panic(v)
//	}
type StarSubscriptionTypeClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() StarSubscriptionTypeClass

	// TypeID returns type id in TL schema.
	//
	// See https://core.telegram.org/mtproto/TL-tl#remarks.
	TypeID() uint32
	// TypeName returns name of type in TL schema.
	TypeName() string
	// String implements fmt.Stringer.
	String() string
	// Zero returns true if current object has a zero value.
	Zero() bool

	EncodeTDLibJSON(b tdjson.Encoder) error
	DecodeTDLibJSON(b tdjson.Decoder) error
}

// DecodeStarSubscriptionType implements binary de-serialization for StarSubscriptionTypeClass.
func DecodeStarSubscriptionType(buf *bin.Buffer) (StarSubscriptionTypeClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case StarSubscriptionTypeChannelTypeID:
		// Decoding starSubscriptionTypeChannel#c29ab6f5.
		v := StarSubscriptionTypeChannel{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StarSubscriptionTypeClass: %w", err)
		}
		return &v, nil
	case StarSubscriptionTypeBotTypeID:
		// Decoding starSubscriptionTypeBot#d78ddd2.
		v := StarSubscriptionTypeBot{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StarSubscriptionTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode StarSubscriptionTypeClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONStarSubscriptionType implements binary de-serialization for StarSubscriptionTypeClass.
func DecodeTDLibJSONStarSubscriptionType(buf tdjson.Decoder) (StarSubscriptionTypeClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "starSubscriptionTypeChannel":
		// Decoding starSubscriptionTypeChannel#c29ab6f5.
		v := StarSubscriptionTypeChannel{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StarSubscriptionTypeClass: %w", err)
		}
		return &v, nil
	case "starSubscriptionTypeBot":
		// Decoding starSubscriptionTypeBot#d78ddd2.
		v := StarSubscriptionTypeBot{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode StarSubscriptionTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode StarSubscriptionTypeClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// StarSubscriptionType boxes the StarSubscriptionTypeClass providing a helper.
type StarSubscriptionTypeBox struct {
	StarSubscriptionType StarSubscriptionTypeClass
}

// Decode implements bin.Decoder for StarSubscriptionTypeBox.
func (b *StarSubscriptionTypeBox) Decode(buf *bin.Buffer) error {
	if b == nil {
		return fmt.Errorf("unable to decode StarSubscriptionTypeBox to nil")
	}
	v, err := DecodeStarSubscriptionType(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.StarSubscriptionType = v
	return nil
}

// Encode implements bin.Encode for StarSubscriptionTypeBox.
func (b *StarSubscriptionTypeBox) Encode(buf *bin.Buffer) error {
	if b == nil || b.StarSubscriptionType == nil {
		return fmt.Errorf("unable to encode StarSubscriptionTypeClass as nil")
	}
	return b.StarSubscriptionType.Encode(buf)
}

// DecodeTDLibJSON implements bin.Decoder for StarSubscriptionTypeBox.
func (b *StarSubscriptionTypeBox) DecodeTDLibJSON(buf tdjson.Decoder) error {
	if b == nil {
		return fmt.Errorf("unable to decode StarSubscriptionTypeBox to nil")
	}
	v, err := DecodeTDLibJSONStarSubscriptionType(buf)
	if err != nil {
		return fmt.Errorf("unable to decode boxed value: %w", err)
	}
	b.StarSubscriptionType = v
	return nil
}

// EncodeTDLibJSON implements bin.Encode for StarSubscriptionTypeBox.
func (b *StarSubscriptionTypeBox) EncodeTDLibJSON(buf tdjson.Encoder) error {
	if b == nil || b.StarSubscriptionType == nil {
		return fmt.Errorf("unable to encode StarSubscriptionTypeClass as nil")
	}
	return b.StarSubscriptionType.EncodeTDLibJSON(buf)
}
