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

// ChatSourceMtprotoProxy represents TL type `chatSourceMtprotoProxy#177d1803`.
type ChatSourceMtprotoProxy struct {
}

// ChatSourceMtprotoProxyTypeID is TL type id of ChatSourceMtprotoProxy.
const ChatSourceMtprotoProxyTypeID = 0x177d1803

// construct implements constructor of ChatSourceClass.
func (c ChatSourceMtprotoProxy) construct() ChatSourceClass { return &c }

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

	_ ChatSourceClass = &ChatSourceMtprotoProxy{}
)

func (c *ChatSourceMtprotoProxy) Zero() bool {
	if c == nil {
		return true
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatSourceMtprotoProxy) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatSourceMtprotoProxy",
		ID:   ChatSourceMtprotoProxyTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatSourceMtprotoProxy) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatSourceMtprotoProxy#177d1803 as nil")
	}
	b.PutID(ChatSourceMtprotoProxyTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatSourceMtprotoProxy) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatSourceMtprotoProxy#177d1803 as nil")
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *ChatSourceMtprotoProxy) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatSourceMtprotoProxy#177d1803 to nil")
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatSourceMtprotoProxy) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatSourceMtprotoProxy#177d1803 as nil")
	}
	b.ObjStart()
	b.PutID("chatSourceMtprotoProxy")
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatSourceMtprotoProxy"); err != nil {
				return fmt.Errorf("unable to decode chatSourceMtprotoProxy#177d1803: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// ChatSourcePublicServiceAnnouncement represents TL type `chatSourcePublicServiceAnnouncement#ec6a6694`.
type ChatSourcePublicServiceAnnouncement struct {
	// The type of the announcement
	Type string
	// The text of the announcement
	Text string
}

// ChatSourcePublicServiceAnnouncementTypeID is TL type id of ChatSourcePublicServiceAnnouncement.
const ChatSourcePublicServiceAnnouncementTypeID = 0xec6a6694

// construct implements constructor of ChatSourceClass.
func (c ChatSourcePublicServiceAnnouncement) construct() ChatSourceClass { return &c }

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

	_ ChatSourceClass = &ChatSourcePublicServiceAnnouncement{}
)

func (c *ChatSourcePublicServiceAnnouncement) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Type == "") {
		return false
	}
	if !(c.Text == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatSourcePublicServiceAnnouncement) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatSourcePublicServiceAnnouncement",
		ID:   ChatSourcePublicServiceAnnouncementTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Type",
			SchemaName: "type",
		},
		{
			Name:       "Text",
			SchemaName: "text",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatSourcePublicServiceAnnouncement) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatSourcePublicServiceAnnouncement#ec6a6694 as nil")
	}
	b.PutID(ChatSourcePublicServiceAnnouncementTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatSourcePublicServiceAnnouncement) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatSourcePublicServiceAnnouncement#ec6a6694 as nil")
	}
	b.PutString(c.Type)
	b.PutString(c.Text)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *ChatSourcePublicServiceAnnouncement) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatSourcePublicServiceAnnouncement#ec6a6694 to nil")
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode chatSourcePublicServiceAnnouncement#ec6a6694: field type: %w", err)
		}
		c.Type = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode chatSourcePublicServiceAnnouncement#ec6a6694: field text: %w", err)
		}
		c.Text = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatSourcePublicServiceAnnouncement) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatSourcePublicServiceAnnouncement#ec6a6694 as nil")
	}
	b.ObjStart()
	b.PutID("chatSourcePublicServiceAnnouncement")
	b.Comma()
	b.FieldStart("type")
	b.PutString(c.Type)
	b.Comma()
	b.FieldStart("text")
	b.PutString(c.Text)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatSourcePublicServiceAnnouncement"); err != nil {
				return fmt.Errorf("unable to decode chatSourcePublicServiceAnnouncement#ec6a6694: %w", err)
			}
		case "type":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode chatSourcePublicServiceAnnouncement#ec6a6694: field type: %w", err)
			}
			c.Type = value
		case "text":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode chatSourcePublicServiceAnnouncement#ec6a6694: field text: %w", err)
			}
			c.Text = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetType returns value of Type field.
func (c *ChatSourcePublicServiceAnnouncement) GetType() (value string) {
	if c == nil {
		return
	}
	return c.Type
}

// GetText returns value of Text field.
func (c *ChatSourcePublicServiceAnnouncement) GetText() (value string) {
	if c == nil {
		return
	}
	return c.Text
}

// ChatSourceClassName is schema name of ChatSourceClass.
const ChatSourceClassName = "ChatSource"

// ChatSourceClass represents ChatSource generic type.
//
// Example:
//
//	g, err := tdapi.DecodeChatSource(buf)
//	if err != nil {
//	    panic(err)
//	}
//	switch v := g.(type) {
//	case *tdapi.ChatSourceMtprotoProxy: // chatSourceMtprotoProxy#177d1803
//	case *tdapi.ChatSourcePublicServiceAnnouncement: // chatSourcePublicServiceAnnouncement#ec6a6694
//	default: panic(v)
//	}
type ChatSourceClass interface {
	bin.Encoder
	bin.Decoder
	bin.BareEncoder
	bin.BareDecoder
	construct() ChatSourceClass

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

// DecodeChatSource implements binary de-serialization for ChatSourceClass.
func DecodeChatSource(buf *bin.Buffer) (ChatSourceClass, error) {
	id, err := buf.PeekID()
	if err != nil {
		return nil, err
	}
	switch id {
	case ChatSourceMtprotoProxyTypeID:
		// Decoding chatSourceMtprotoProxy#177d1803.
		v := ChatSourceMtprotoProxy{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatSourceClass: %w", err)
		}
		return &v, nil
	case ChatSourcePublicServiceAnnouncementTypeID:
		// Decoding chatSourcePublicServiceAnnouncement#ec6a6694.
		v := ChatSourcePublicServiceAnnouncement{}
		if err := v.Decode(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatSourceClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode ChatSourceClass: %w", bin.NewUnexpectedID(id))
	}
}

// DecodeTDLibJSONChatSource implements binary de-serialization for ChatSourceClass.
func DecodeTDLibJSONChatSource(buf tdjson.Decoder) (ChatSourceClass, error) {
	id, err := buf.FindTypeID()
	if err != nil {
		return nil, err
	}
	switch id {
	case "chatSourceMtprotoProxy":
		// Decoding chatSourceMtprotoProxy#177d1803.
		v := ChatSourceMtprotoProxy{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatSourceClass: %w", err)
		}
		return &v, nil
	case "chatSourcePublicServiceAnnouncement":
		// Decoding chatSourcePublicServiceAnnouncement#ec6a6694.
		v := ChatSourcePublicServiceAnnouncement{}
		if err := v.DecodeTDLibJSON(buf); err != nil {
			return nil, fmt.Errorf("unable to decode ChatSourceClass: %w", err)
		}
		return &v, nil
	default:
		return nil, fmt.Errorf("unable to decode ChatSourceClass: %w", tdjson.NewUnexpectedID(id))
	}
}

// ChatSource boxes the ChatSourceClass providing a helper.
type ChatSourceBox struct {
	ChatSource ChatSourceClass
}

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

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

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

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