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

// GroupCallDonor represents TL type `groupCallDonor#ee430c85`.
//
// See https://core.telegram.org/constructor/groupCallDonor for reference.
type GroupCallDonor struct {
	// Flags field of GroupCallDonor.
	Flags bin.Fields
	// Top field of GroupCallDonor.
	Top bool
	// My field of GroupCallDonor.
	My bool
	// PeerID field of GroupCallDonor.
	//
	// Use SetPeerID and GetPeerID helpers.
	PeerID PeerClass
	// Stars field of GroupCallDonor.
	Stars int64
}

// GroupCallDonorTypeID is TL type id of GroupCallDonor.
const GroupCallDonorTypeID = 0xee430c85

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

func (g *GroupCallDonor) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.Top == false) {
		return false
	}
	if !(g.My == false) {
		return false
	}
	if !(g.PeerID == nil) {
		return false
	}
	if !(g.Stars == 0) {
		return false
	}

	return true
}

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

// FillFrom fills GroupCallDonor from given interface.
func (g *GroupCallDonor) FillFrom(from interface {
	GetTop() (value bool)
	GetMy() (value bool)
	GetPeerID() (value PeerClass, ok bool)
	GetStars() (value int64)
}) {
	g.Top = from.GetTop()
	g.My = from.GetMy()
	if val, ok := from.GetPeerID(); ok {
		g.PeerID = val
	}

	g.Stars = from.GetStars()
}

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

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

// TypeInfo returns info about TL type.
func (g *GroupCallDonor) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "groupCallDonor",
		ID:   GroupCallDonorTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Top",
			SchemaName: "top",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "My",
			SchemaName: "my",
			Null:       !g.Flags.Has(1),
		},
		{
			Name:       "PeerID",
			SchemaName: "peer_id",
			Null:       !g.Flags.Has(3),
		},
		{
			Name:       "Stars",
			SchemaName: "stars",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *GroupCallDonor) SetFlags() {
	if !(g.Top == false) {
		g.Flags.Set(0)
	}
	if !(g.My == false) {
		g.Flags.Set(1)
	}
	if !(g.PeerID == nil) {
		g.Flags.Set(3)
	}
}

// Encode implements bin.Encoder.
func (g *GroupCallDonor) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode groupCallDonor#ee430c85 as nil")
	}
	b.PutID(GroupCallDonorTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GroupCallDonor) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode groupCallDonor#ee430c85 as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode groupCallDonor#ee430c85: field flags: %w", err)
	}
	if g.Flags.Has(3) {
		if g.PeerID == nil {
			return fmt.Errorf("unable to encode groupCallDonor#ee430c85: field peer_id is nil")
		}
		if err := g.PeerID.Encode(b); err != nil {
			return fmt.Errorf("unable to encode groupCallDonor#ee430c85: field peer_id: %w", err)
		}
	}
	b.PutLong(g.Stars)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GroupCallDonor) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode groupCallDonor#ee430c85 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode groupCallDonor#ee430c85: field flags: %w", err)
		}
	}
	g.Top = g.Flags.Has(0)
	g.My = g.Flags.Has(1)
	if g.Flags.Has(3) {
		value, err := DecodePeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode groupCallDonor#ee430c85: field peer_id: %w", err)
		}
		g.PeerID = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode groupCallDonor#ee430c85: field stars: %w", err)
		}
		g.Stars = value
	}
	return nil
}

// SetTop sets value of Top conditional field.
func (g *GroupCallDonor) SetTop(value bool) {
	if value {
		g.Flags.Set(0)
		g.Top = true
	} else {
		g.Flags.Unset(0)
		g.Top = false
	}
}

// GetTop returns value of Top conditional field.
func (g *GroupCallDonor) GetTop() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(0)
}

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

// GetMy returns value of My conditional field.
func (g *GroupCallDonor) GetMy() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(1)
}

// SetPeerID sets value of PeerID conditional field.
func (g *GroupCallDonor) SetPeerID(value PeerClass) {
	g.Flags.Set(3)
	g.PeerID = value
}

// GetPeerID returns value of PeerID conditional field and
// boolean which is true if field was set.
func (g *GroupCallDonor) GetPeerID() (value PeerClass, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(3) {
		return value, false
	}
	return g.PeerID, true
}

// GetStars returns value of Stars field.
func (g *GroupCallDonor) GetStars() (value int64) {
	if g == nil {
		return
	}
	return g.Stars
}
