// Code generated by gotdgen, DO NOT EDIT.

package tg

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

// GroupCallMessage represents TL type `groupCallMessage#1a8afc7e`.
//
// See https://core.telegram.org/constructor/groupCallMessage for reference.
type GroupCallMessage struct {
	// Flags field of GroupCallMessage.
	Flags bin.Fields
	// FromAdmin field of GroupCallMessage.
	FromAdmin bool
	// ID field of GroupCallMessage.
	ID int
	// FromID field of GroupCallMessage.
	FromID PeerClass
	// Date field of GroupCallMessage.
	Date int
	// Message field of GroupCallMessage.
	Message TextWithEntities
	// PaidMessageStars field of GroupCallMessage.
	//
	// Use SetPaidMessageStars and GetPaidMessageStars helpers.
	PaidMessageStars int64
}

// GroupCallMessageTypeID is TL type id of GroupCallMessage.
const GroupCallMessageTypeID = 0x1a8afc7e

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

func (g *GroupCallMessage) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.FromAdmin == false) {
		return false
	}
	if !(g.ID == 0) {
		return false
	}
	if !(g.FromID == nil) {
		return false
	}
	if !(g.Date == 0) {
		return false
	}
	if !(g.Message.Zero()) {
		return false
	}
	if !(g.PaidMessageStars == 0) {
		return false
	}

	return true
}

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

// FillFrom fills GroupCallMessage from given interface.
func (g *GroupCallMessage) FillFrom(from interface {
	GetFromAdmin() (value bool)
	GetID() (value int)
	GetFromID() (value PeerClass)
	GetDate() (value int)
	GetMessage() (value TextWithEntities)
	GetPaidMessageStars() (value int64, ok bool)
}) {
	g.FromAdmin = from.GetFromAdmin()
	g.ID = from.GetID()
	g.FromID = from.GetFromID()
	g.Date = from.GetDate()
	g.Message = from.GetMessage()
	if val, ok := from.GetPaidMessageStars(); ok {
		g.PaidMessageStars = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (g *GroupCallMessage) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "groupCallMessage",
		ID:   GroupCallMessageTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "FromAdmin",
			SchemaName: "from_admin",
			Null:       !g.Flags.Has(1),
		},
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "FromID",
			SchemaName: "from_id",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "Message",
			SchemaName: "message",
		},
		{
			Name:       "PaidMessageStars",
			SchemaName: "paid_message_stars",
			Null:       !g.Flags.Has(0),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *GroupCallMessage) SetFlags() {
	if !(g.FromAdmin == false) {
		g.Flags.Set(1)
	}
	if !(g.PaidMessageStars == 0) {
		g.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (g *GroupCallMessage) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode groupCallMessage#1a8afc7e as nil")
	}
	b.PutID(GroupCallMessageTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GroupCallMessage) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode groupCallMessage#1a8afc7e as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode groupCallMessage#1a8afc7e: field flags: %w", err)
	}
	b.PutInt(g.ID)
	if g.FromID == nil {
		return fmt.Errorf("unable to encode groupCallMessage#1a8afc7e: field from_id is nil")
	}
	if err := g.FromID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode groupCallMessage#1a8afc7e: field from_id: %w", err)
	}
	b.PutInt(g.Date)
	if err := g.Message.Encode(b); err != nil {
		return fmt.Errorf("unable to encode groupCallMessage#1a8afc7e: field message: %w", err)
	}
	if g.Flags.Has(0) {
		b.PutLong(g.PaidMessageStars)
	}
	return nil
}

// Decode implements bin.Decoder.
func (g *GroupCallMessage) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode groupCallMessage#1a8afc7e to nil")
	}
	if err := b.ConsumeID(GroupCallMessageTypeID); err != nil {
		return fmt.Errorf("unable to decode groupCallMessage#1a8afc7e: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *GroupCallMessage) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode groupCallMessage#1a8afc7e to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode groupCallMessage#1a8afc7e: field flags: %w", err)
		}
	}
	g.FromAdmin = g.Flags.Has(1)
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode groupCallMessage#1a8afc7e: field id: %w", err)
		}
		g.ID = value
	}
	{
		value, err := DecodePeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode groupCallMessage#1a8afc7e: field from_id: %w", err)
		}
		g.FromID = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode groupCallMessage#1a8afc7e: field date: %w", err)
		}
		g.Date = value
	}
	{
		if err := g.Message.Decode(b); err != nil {
			return fmt.Errorf("unable to decode groupCallMessage#1a8afc7e: field message: %w", err)
		}
	}
	if g.Flags.Has(0) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode groupCallMessage#1a8afc7e: field paid_message_stars: %w", err)
		}
		g.PaidMessageStars = value
	}
	return nil
}

// SetFromAdmin sets value of FromAdmin conditional field.
func (g *GroupCallMessage) SetFromAdmin(value bool) {
	if value {
		g.Flags.Set(1)
		g.FromAdmin = true
	} else {
		g.Flags.Unset(1)
		g.FromAdmin = false
	}
}

// GetFromAdmin returns value of FromAdmin conditional field.
func (g *GroupCallMessage) GetFromAdmin() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(1)
}

// GetID returns value of ID field.
func (g *GroupCallMessage) GetID() (value int) {
	if g == nil {
		return
	}
	return g.ID
}

// GetFromID returns value of FromID field.
func (g *GroupCallMessage) GetFromID() (value PeerClass) {
	if g == nil {
		return
	}
	return g.FromID
}

// GetDate returns value of Date field.
func (g *GroupCallMessage) GetDate() (value int) {
	if g == nil {
		return
	}
	return g.Date
}

// GetMessage returns value of Message field.
func (g *GroupCallMessage) GetMessage() (value TextWithEntities) {
	if g == nil {
		return
	}
	return g.Message
}

// SetPaidMessageStars sets value of PaidMessageStars conditional field.
func (g *GroupCallMessage) SetPaidMessageStars(value int64) {
	g.Flags.Set(0)
	g.PaidMessageStars = value
}

// GetPaidMessageStars returns value of PaidMessageStars conditional field and
// boolean which is true if field was set.
func (g *GroupCallMessage) GetPaidMessageStars() (value int64, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(0) {
		return value, false
	}
	return g.PaidMessageStars, true
}
