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

// FailedToAddMember represents TL type `failedToAddMember#ef236cc2`.
type FailedToAddMember struct {
	// User identifier
	UserID int64
	// True, if subscription to Telegram Premium would have allowed to add the user to the
	// chat
	PremiumWouldAllowInvite bool
	// True, if subscription to Telegram Premium is required to send the user chat invite
	// link
	PremiumRequiredToSendMessages bool
}

// FailedToAddMemberTypeID is TL type id of FailedToAddMember.
const FailedToAddMemberTypeID = 0xef236cc2

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

func (f *FailedToAddMember) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.UserID == 0) {
		return false
	}
	if !(f.PremiumWouldAllowInvite == false) {
		return false
	}
	if !(f.PremiumRequiredToSendMessages == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (f *FailedToAddMember) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "failedToAddMember",
		ID:   FailedToAddMemberTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "UserID",
			SchemaName: "user_id",
		},
		{
			Name:       "PremiumWouldAllowInvite",
			SchemaName: "premium_would_allow_invite",
		},
		{
			Name:       "PremiumRequiredToSendMessages",
			SchemaName: "premium_required_to_send_messages",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *FailedToAddMember) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode failedToAddMember#ef236cc2 as nil")
	}
	b.PutID(FailedToAddMemberTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *FailedToAddMember) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode failedToAddMember#ef236cc2 as nil")
	}
	b.PutInt53(f.UserID)
	b.PutBool(f.PremiumWouldAllowInvite)
	b.PutBool(f.PremiumRequiredToSendMessages)
	return nil
}

// Decode implements bin.Decoder.
func (f *FailedToAddMember) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode failedToAddMember#ef236cc2 to nil")
	}
	if err := b.ConsumeID(FailedToAddMemberTypeID); err != nil {
		return fmt.Errorf("unable to decode failedToAddMember#ef236cc2: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *FailedToAddMember) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode failedToAddMember#ef236cc2 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode failedToAddMember#ef236cc2: field user_id: %w", err)
		}
		f.UserID = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode failedToAddMember#ef236cc2: field premium_would_allow_invite: %w", err)
		}
		f.PremiumWouldAllowInvite = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode failedToAddMember#ef236cc2: field premium_required_to_send_messages: %w", err)
		}
		f.PremiumRequiredToSendMessages = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (f *FailedToAddMember) EncodeTDLibJSON(b tdjson.Encoder) error {
	if f == nil {
		return fmt.Errorf("can't encode failedToAddMember#ef236cc2 as nil")
	}
	b.ObjStart()
	b.PutID("failedToAddMember")
	b.Comma()
	b.FieldStart("user_id")
	b.PutInt53(f.UserID)
	b.Comma()
	b.FieldStart("premium_would_allow_invite")
	b.PutBool(f.PremiumWouldAllowInvite)
	b.Comma()
	b.FieldStart("premium_required_to_send_messages")
	b.PutBool(f.PremiumRequiredToSendMessages)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (f *FailedToAddMember) DecodeTDLibJSON(b tdjson.Decoder) error {
	if f == nil {
		return fmt.Errorf("can't decode failedToAddMember#ef236cc2 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("failedToAddMember"); err != nil {
				return fmt.Errorf("unable to decode failedToAddMember#ef236cc2: %w", err)
			}
		case "user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode failedToAddMember#ef236cc2: field user_id: %w", err)
			}
			f.UserID = value
		case "premium_would_allow_invite":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode failedToAddMember#ef236cc2: field premium_would_allow_invite: %w", err)
			}
			f.PremiumWouldAllowInvite = value
		case "premium_required_to_send_messages":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode failedToAddMember#ef236cc2: field premium_required_to_send_messages: %w", err)
			}
			f.PremiumRequiredToSendMessages = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetUserID returns value of UserID field.
func (f *FailedToAddMember) GetUserID() (value int64) {
	if f == nil {
		return
	}
	return f.UserID
}

// GetPremiumWouldAllowInvite returns value of PremiumWouldAllowInvite field.
func (f *FailedToAddMember) GetPremiumWouldAllowInvite() (value bool) {
	if f == nil {
		return
	}
	return f.PremiumWouldAllowInvite
}

// GetPremiumRequiredToSendMessages returns value of PremiumRequiredToSendMessages field.
func (f *FailedToAddMember) GetPremiumRequiredToSendMessages() (value bool) {
	if f == nil {
		return
	}
	return f.PremiumRequiredToSendMessages
}
