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

// ChatMessageSender represents TL type `chatMessageSender#2d55aeba`.
type ChatMessageSender struct {
	// The message sender
	Sender MessageSenderClass
	// True, if Telegram Premium is needed to use the message sender
	NeedsPremium bool
}

// ChatMessageSenderTypeID is TL type id of ChatMessageSender.
const ChatMessageSenderTypeID = 0x2d55aeba

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

func (c *ChatMessageSender) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Sender == nil) {
		return false
	}
	if !(c.NeedsPremium == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ChatMessageSender) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "chatMessageSender",
		ID:   ChatMessageSenderTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Sender",
			SchemaName: "sender",
		},
		{
			Name:       "NeedsPremium",
			SchemaName: "needs_premium",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ChatMessageSender) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatMessageSender#2d55aeba as nil")
	}
	b.PutID(ChatMessageSenderTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ChatMessageSender) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode chatMessageSender#2d55aeba as nil")
	}
	if c.Sender == nil {
		return fmt.Errorf("unable to encode chatMessageSender#2d55aeba: field sender is nil")
	}
	if err := c.Sender.Encode(b); err != nil {
		return fmt.Errorf("unable to encode chatMessageSender#2d55aeba: field sender: %w", err)
	}
	b.PutBool(c.NeedsPremium)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (c *ChatMessageSender) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode chatMessageSender#2d55aeba to nil")
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode chatMessageSender#2d55aeba: field sender: %w", err)
		}
		c.Sender = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode chatMessageSender#2d55aeba: field needs_premium: %w", err)
		}
		c.NeedsPremium = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ChatMessageSender) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode chatMessageSender#2d55aeba as nil")
	}
	b.ObjStart()
	b.PutID("chatMessageSender")
	b.Comma()
	b.FieldStart("sender")
	if c.Sender == nil {
		return fmt.Errorf("unable to encode chatMessageSender#2d55aeba: field sender is nil")
	}
	if err := c.Sender.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode chatMessageSender#2d55aeba: field sender: %w", err)
	}
	b.Comma()
	b.FieldStart("needs_premium")
	b.PutBool(c.NeedsPremium)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("chatMessageSender"); err != nil {
				return fmt.Errorf("unable to decode chatMessageSender#2d55aeba: %w", err)
			}
		case "sender":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode chatMessageSender#2d55aeba: field sender: %w", err)
			}
			c.Sender = value
		case "needs_premium":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode chatMessageSender#2d55aeba: field needs_premium: %w", err)
			}
			c.NeedsPremium = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSender returns value of Sender field.
func (c *ChatMessageSender) GetSender() (value MessageSenderClass) {
	if c == nil {
		return
	}
	return c.Sender
}

// GetNeedsPremium returns value of NeedsPremium field.
func (c *ChatMessageSender) GetNeedsPremium() (value bool) {
	if c == nil {
		return
	}
	return c.NeedsPremium
}
