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

// ChatEvent represents TL type `chatEvent#d921b3d0`.
type ChatEvent struct {
	// Chat event identifier
	ID int64
	// Point in time (Unix timestamp) when the event happened
	Date int32
	// Identifier of the user or chat who performed the action
	MemberID MessageSenderClass
	// The action
	Action ChatEventActionClass
}

// ChatEventTypeID is TL type id of ChatEvent.
const ChatEventTypeID = 0xd921b3d0

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

func (c *ChatEvent) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.ID == 0) {
		return false
	}
	if !(c.Date == 0) {
		return false
	}
	if !(c.MemberID == nil) {
		return false
	}
	if !(c.Action == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatEvent) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatEvent",
		ID:   ChatEventTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "MemberID",
			SchemaName: "member_id",
		},
		{
			Name:       "Action",
			SchemaName: "action",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatEvent) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatEvent#d921b3d0 as nil")
	}
	b.PutID(ChatEventTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatEvent) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatEvent#d921b3d0 as nil")
	}
	b.PutLong(c.ID)
	b.PutInt32(c.Date)
	if c.MemberID == nil {
		return fmt.Errorf("unable to encode chatEvent#d921b3d0: field member_id is nil")
	}
	if err := c.MemberID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatEvent#d921b3d0: field member_id: %w", err)
	}
	if c.Action == nil {
		return fmt.Errorf("unable to encode chatEvent#d921b3d0: field action is nil")
	}
	if err := c.Action.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatEvent#d921b3d0: field action: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *ChatEvent) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatEvent#d921b3d0 to nil")
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode chatEvent#d921b3d0: field id: %w", err)
		}
		c.ID = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode chatEvent#d921b3d0: field date: %w", err)
		}
		c.Date = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode chatEvent#d921b3d0: field member_id: %w", err)
		}
		c.MemberID = value
	}
	{
		value, err := DecodeChatEventAction(b)
		if err != nil {
			return fmt.Errorf("unable to decode chatEvent#d921b3d0: field action: %w", err)
		}
		c.Action = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatEvent) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatEvent#d921b3d0 as nil")
	}
	b.ObjStart()
	b.PutID("chatEvent")
	b.Comma()
	b.FieldStart("id")
	b.PutLong(c.ID)
	b.Comma()
	b.FieldStart("date")
	b.PutInt32(c.Date)
	b.Comma()
	b.FieldStart("member_id")
	if c.MemberID == nil {
		return fmt.Errorf("unable to encode chatEvent#d921b3d0: field member_id is nil")
	}
	if err := c.MemberID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatEvent#d921b3d0: field member_id: %w", err)
	}
	b.Comma()
	b.FieldStart("action")
	if c.Action == nil {
		return fmt.Errorf("unable to encode chatEvent#d921b3d0: field action is nil")
	}
	if err := c.Action.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatEvent#d921b3d0: field action: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatEvent"); err != nil {
				return fmt.Errorf("unable to decode chatEvent#d921b3d0: %w", err)
			}
		case "id":
			value, err := b.Long()
			if err != nil {
				return fmt.Errorf("unable to decode chatEvent#d921b3d0: field id: %w", err)
			}
			c.ID = value
		case "date":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode chatEvent#d921b3d0: field date: %w", err)
			}
			c.Date = value
		case "member_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode chatEvent#d921b3d0: field member_id: %w", err)
			}
			c.MemberID = value
		case "action":
			value, err := DecodeTDLibJSONChatEventAction(b)
			if err != nil {
				return fmt.Errorf("unable to decode chatEvent#d921b3d0: field action: %w", err)
			}
			c.Action = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetID returns value of ID field.
func (c *ChatEvent) GetID() (value int64) {
	if c == nil {
		return
	}
	return c.ID
}

// GetDate returns value of Date field.
func (c *ChatEvent) GetDate() (value int32) {
	if c == nil {
		return
	}
	return c.Date
}

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

// GetAction returns value of Action field.
func (c *ChatEvent) GetAction() (value ChatEventActionClass) {
	if c == nil {
		return
	}
	return c.Action
}
