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

// CreatedBasicGroupChat represents TL type `createdBasicGroupChat#fec875d4`.
type CreatedBasicGroupChat struct {
	// Chat identifier
	ChatID int64
	// Information about failed to add members
	FailedToAddMembers FailedToAddMembers
}

// CreatedBasicGroupChatTypeID is TL type id of CreatedBasicGroupChat.
const CreatedBasicGroupChatTypeID = 0xfec875d4

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

func (c *CreatedBasicGroupChat) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.ChatID == 0) {
		return false
	}
	if !(c.FailedToAddMembers.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CreatedBasicGroupChat) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "createdBasicGroupChat",
		ID:   CreatedBasicGroupChatTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "FailedToAddMembers",
			SchemaName: "failed_to_add_members",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CreatedBasicGroupChat) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createdBasicGroupChat#fec875d4 as nil")
	}
	b.PutID(CreatedBasicGroupChatTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CreatedBasicGroupChat) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createdBasicGroupChat#fec875d4 as nil")
	}
	b.PutInt53(c.ChatID)
	if err := c.FailedToAddMembers.Encode(b); err != nil {
		return fmt.Errorf("unable to encode createdBasicGroupChat#fec875d4: field failed_to_add_members: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *CreatedBasicGroupChat) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode createdBasicGroupChat#fec875d4 to nil")
	}
	if err := b.ConsumeID(CreatedBasicGroupChatTypeID); err != nil {
		return fmt.Errorf("unable to decode createdBasicGroupChat#fec875d4: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CreatedBasicGroupChat) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode createdBasicGroupChat#fec875d4 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode createdBasicGroupChat#fec875d4: field chat_id: %w", err)
		}
		c.ChatID = value
	}
	{
		if err := c.FailedToAddMembers.Decode(b); err != nil {
			return fmt.Errorf("unable to decode createdBasicGroupChat#fec875d4: field failed_to_add_members: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CreatedBasicGroupChat) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode createdBasicGroupChat#fec875d4 as nil")
	}
	b.ObjStart()
	b.PutID("createdBasicGroupChat")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(c.ChatID)
	b.Comma()
	b.FieldStart("failed_to_add_members")
	if err := c.FailedToAddMembers.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode createdBasicGroupChat#fec875d4: field failed_to_add_members: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CreatedBasicGroupChat) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode createdBasicGroupChat#fec875d4 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("createdBasicGroupChat"); err != nil {
				return fmt.Errorf("unable to decode createdBasicGroupChat#fec875d4: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode createdBasicGroupChat#fec875d4: field chat_id: %w", err)
			}
			c.ChatID = value
		case "failed_to_add_members":
			if err := c.FailedToAddMembers.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode createdBasicGroupChat#fec875d4: field failed_to_add_members: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (c *CreatedBasicGroupChat) GetChatID() (value int64) {
	if c == nil {
		return
	}
	return c.ChatID
}

// GetFailedToAddMembers returns value of FailedToAddMembers field.
func (c *CreatedBasicGroupChat) GetFailedToAddMembers() (value FailedToAddMembers) {
	if c == nil {
		return
	}
	return c.FailedToAddMembers
}
