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

// ChatStatisticsObjectTypeMessage represents TL type `chatStatisticsObjectTypeMessage#6f9f24f6`.
type ChatStatisticsObjectTypeMessage struct {
	// Message identifier
	MessageID int64
}

// ChatStatisticsObjectTypeMessageTypeID is TL type id of ChatStatisticsObjectTypeMessage.
const ChatStatisticsObjectTypeMessageTypeID = 0x6f9f24f6

// construct implements constructor of ChatStatisticsObjectTypeClass.
func (c ChatStatisticsObjectTypeMessage) construct() ChatStatisticsObjectTypeClass { return &c }

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

	_ ChatStatisticsObjectTypeClass = &ChatStatisticsObjectTypeMessage{}
)

func (c *ChatStatisticsObjectTypeMessage) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.MessageID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatStatisticsObjectTypeMessage) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatStatisticsObjectTypeMessage",
		ID:   ChatStatisticsObjectTypeMessageTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "MessageID",
			SchemaName: "message_id",
		},
	}
	return typ
}

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

// EncodeBare implements bin.BareEncoder.
func (c *ChatStatisticsObjectTypeMessage) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatStatisticsObjectTypeMessage#6f9f24f6 as nil")
	}
	b.PutInt53(c.MessageID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *ChatStatisticsObjectTypeMessage) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatStatisticsObjectTypeMessage#6f9f24f6 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode chatStatisticsObjectTypeMessage#6f9f24f6: field message_id: %w", err)
		}
		c.MessageID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatStatisticsObjectTypeMessage) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatStatisticsObjectTypeMessage#6f9f24f6 as nil")
	}
	b.ObjStart()
	b.PutID("chatStatisticsObjectTypeMessage")
	b.Comma()
	b.FieldStart("message_id")
	b.PutInt53(c.MessageID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatStatisticsObjectTypeMessage"); err != nil {
				return fmt.Errorf("unable to decode chatStatisticsObjectTypeMessage#6f9f24f6: %w", err)
			}
		case "message_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode chatStatisticsObjectTypeMessage#6f9f24f6: field message_id: %w", err)
			}
			c.MessageID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetMessageID returns value of MessageID field.
func (c *ChatStatisticsObjectTypeMessage) GetMessageID() (value int64) {
	if c == nil {
		return
	}
	return c.MessageID
}

// ChatStatisticsObjectTypeStory represents TL type `chatStatisticsObjectTypeStory#15baf9b0`.
type ChatStatisticsObjectTypeStory struct {
	// Story identifier
	StoryID int32
}

// ChatStatisticsObjectTypeStoryTypeID is TL type id of ChatStatisticsObjectTypeStory.
const ChatStatisticsObjectTypeStoryTypeID = 0x15baf9b0

// construct implements constructor of ChatStatisticsObjectTypeClass.
func (c ChatStatisticsObjectTypeStory) construct() ChatStatisticsObjectTypeClass { return &c }

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

	_ ChatStatisticsObjectTypeClass = &ChatStatisticsObjectTypeStory{}
)

func (c *ChatStatisticsObjectTypeStory) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.StoryID == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatStatisticsObjectTypeStory) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatStatisticsObjectTypeStory",
		ID:   ChatStatisticsObjectTypeStoryTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StoryID",
			SchemaName: "story_id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatStatisticsObjectTypeStory) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatStatisticsObjectTypeStory#15baf9b0 as nil")
	}
	b.PutID(ChatStatisticsObjectTypeStoryTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatStatisticsObjectTypeStory) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatStatisticsObjectTypeStory#15baf9b0 as nil")
	}
	b.PutInt32(c.StoryID)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *ChatStatisticsObjectTypeStory) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatStatisticsObjectTypeStory#15baf9b0 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode chatStatisticsObjectTypeStory#15baf9b0: field story_id: %w", err)
		}
		c.StoryID = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatStatisticsObjectTypeStory) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatStatisticsObjectTypeStory#15baf9b0 as nil")
	}
	b.ObjStart()
	b.PutID("chatStatisticsObjectTypeStory")
	b.Comma()
	b.FieldStart("story_id")
	b.PutInt32(c.StoryID)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatStatisticsObjectTypeStory"); err != nil {
				return fmt.Errorf("unable to decode chatStatisticsObjectTypeStory#15baf9b0: %w", err)
			}
		case "story_id":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode chatStatisticsObjectTypeStory#15baf9b0: field story_id: %w", err)
			}
			c.StoryID = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetStoryID returns value of StoryID field.
func (c *ChatStatisticsObjectTypeStory) GetStoryID() (value int32) {
	if c == nil {
		return
	}
	return c.StoryID
}

// ChatStatisticsObjectTypeClassName is schema name of ChatStatisticsObjectTypeClass.
const ChatStatisticsObjectTypeClassName = "ChatStatisticsObjectType"

// ChatStatisticsObjectTypeClass represents ChatStatisticsObjectType generic type.
//
// Example:
//
//	g, err := tdapi.DecodeChatStatisticsObjectType(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.ChatStatisticsObjectTypeMessage: // chatStatisticsObjectTypeMessage#6f9f24f6
//	case *tdapi.ChatStatisticsObjectTypeStory: // chatStatisticsObjectTypeStory#15baf9b0
//	default: panic(v)
//	}
type ChatStatisticsObjectTypeClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() ChatStatisticsObjectTypeClass

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

// DecodeChatStatisticsObjectType implements binary de-serialization for ChatStatisticsObjectTypeClass.
func DecodeChatStatisticsObjectType(buf *bin.Buffer) (ChatStatisticsObjectTypeClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case ChatStatisticsObjectTypeMessageTypeID:
		// Decoding chatStatisticsObjectTypeMessage#6f9f24f6.
		v := ChatStatisticsObjectTypeMessage{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatStatisticsObjectTypeClass: %w", err)
		}
		return &v, nil
	case ChatStatisticsObjectTypeStoryTypeID:
		// Decoding chatStatisticsObjectTypeStory#15baf9b0.
		v := ChatStatisticsObjectTypeStory{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatStatisticsObjectTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode ChatStatisticsObjectTypeClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONChatStatisticsObjectType implements binary de-serialization for ChatStatisticsObjectTypeClass.
func DecodeTDLibJSONChatStatisticsObjectType(buf tdjson.Decoder) (ChatStatisticsObjectTypeClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "chatStatisticsObjectTypeMessage":
		// Decoding chatStatisticsObjectTypeMessage#6f9f24f6.
		v := ChatStatisticsObjectTypeMessage{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatStatisticsObjectTypeClass: %w", err)
		}
		return &v, nil
	case "chatStatisticsObjectTypeStory":
		// Decoding chatStatisticsObjectTypeStory#15baf9b0.
		v := ChatStatisticsObjectTypeStory{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatStatisticsObjectTypeClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode ChatStatisticsObjectTypeClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// ChatStatisticsObjectType boxes the ChatStatisticsObjectTypeClass providing a helper.
type ChatStatisticsObjectTypeBox struct {
	ChatStatisticsObjectType ChatStatisticsObjectTypeClass
}

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

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

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

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