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

// ChatMember represents TL type `chatMember#6d12e175`.
type ChatMember struct {
	// Identifier of the chat member. Currently, other chats can be only Left or Banned. Only
	// supergroups and channels can have other chats as Left or Banned members and these
	// chats must be supergroups or channels
	MemberID MessageSenderClass
	// Identifier of a user that invited/promoted/banned this member in the chat; 0 if
	// unknown
	InviterUserID int64
	// Point in time (Unix timestamp) when the user joined/was promoted/was banned in the
	// chat
	JoinedChatDate int32
	// Status of the member in the chat
	Status ChatMemberStatusClass
}

// ChatMemberTypeID is TL type id of ChatMember.
const ChatMemberTypeID = 0x6d12e175

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

func (c *ChatMember) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.MemberID == nil) {
		return false
	}
	if !(c.InviterUserID == 0) {
		return false
	}
	if !(c.JoinedChatDate == 0) {
		return false
	}
	if !(c.Status == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatMember) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatMember",
		ID:   ChatMemberTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "MemberID",
			SchemaName: "member_id",
		},
		{
			Name:       "InviterUserID",
			SchemaName: "inviter_user_id",
		},
		{
			Name:       "JoinedChatDate",
			SchemaName: "joined_chat_date",
		},
		{
			Name:       "Status",
			SchemaName: "status",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatMember) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatMember#6d12e175 as nil")
	}
	b.PutID(ChatMemberTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatMember) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatMember#6d12e175 as nil")
	}
	if c.MemberID == nil {
		return fmt.Errorf("unable to encode chatMember#6d12e175: field member_id is nil")
	}
	if err := c.MemberID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatMember#6d12e175: field member_id: %w", err)
	}
	b.PutInt53(c.InviterUserID)
	b.PutInt32(c.JoinedChatDate)
	if c.Status == nil {
		return fmt.Errorf("unable to encode chatMember#6d12e175: field status is nil")
	}
	if err := c.Status.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatMember#6d12e175: field status: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *ChatMember) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatMember#6d12e175 to nil")
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode chatMember#6d12e175: field member_id: %w", err)
		}
		c.MemberID = value
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode chatMember#6d12e175: field inviter_user_id: %w", err)
		}
		c.InviterUserID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode chatMember#6d12e175: field joined_chat_date: %w", err)
		}
		c.JoinedChatDate = value
	}
	{
		value, err := DecodeChatMemberStatus(b)
		if err != nil {
			return fmt.Errorf("unable to decode chatMember#6d12e175: field status: %w", err)
		}
		c.Status = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatMember) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatMember#6d12e175 as nil")
	}
	b.ObjStart()
	b.PutID("chatMember")
	b.Comma()
	b.FieldStart("member_id")
	if c.MemberID == nil {
		return fmt.Errorf("unable to encode chatMember#6d12e175: field member_id is nil")
	}
	if err := c.MemberID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatMember#6d12e175: field member_id: %w", err)
	}
	b.Comma()
	b.FieldStart("inviter_user_id")
	b.PutInt53(c.InviterUserID)
	b.Comma()
	b.FieldStart("joined_chat_date")
	b.PutInt32(c.JoinedChatDate)
	b.Comma()
	b.FieldStart("status")
	if c.Status == nil {
		return fmt.Errorf("unable to encode chatMember#6d12e175: field status is nil")
	}
	if err := c.Status.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatMember#6d12e175: field status: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatMember"); err != nil {
				return fmt.Errorf("unable to decode chatMember#6d12e175: %w", err)
			}
		case "member_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode chatMember#6d12e175: field member_id: %w", err)
			}
			c.MemberID = value
		case "inviter_user_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode chatMember#6d12e175: field inviter_user_id: %w", err)
			}
			c.InviterUserID = value
		case "joined_chat_date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode chatMember#6d12e175: field joined_chat_date: %w", err)
			}
			c.JoinedChatDate = value
		case "status":
			value, err := DecodeTDLibJSONChatMemberStatus(b)
			if err != nil {
				return fmt.Errorf("unable to decode chatMember#6d12e175: field status: %w", err)
			}
			c.Status = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetMemberID returns value of MemberID field.
func (c *ChatMember) GetMemberID() (value MessageSenderClass) {
	if c == nil {
		return
	}
	return c.MemberID
}

// GetInviterUserID returns value of InviterUserID field.
func (c *ChatMember) GetInviterUserID() (value int64) {
	if c == nil {
		return
	}
	return c.InviterUserID
}

// GetJoinedChatDate returns value of JoinedChatDate field.
func (c *ChatMember) GetJoinedChatDate() (value int32) {
	if c == nil {
		return
	}
	return c.JoinedChatDate
}

// GetStatus returns value of Status field.
func (c *ChatMember) GetStatus() (value ChatMemberStatusClass) {
	if c == nil {
		return
	}
	return c.Status
}
