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

// StarGiftAuctionAcquiredGift represents TL type `starGiftAuctionAcquiredGift#42b00348`.
//
// See https://core.telegram.org/constructor/starGiftAuctionAcquiredGift for reference.
type StarGiftAuctionAcquiredGift struct {
	// Flags field of StarGiftAuctionAcquiredGift.
	Flags bin.Fields
	// NameHidden field of StarGiftAuctionAcquiredGift.
	NameHidden bool
	// Peer field of StarGiftAuctionAcquiredGift.
	Peer PeerClass
	// Date field of StarGiftAuctionAcquiredGift.
	Date int
	// BidAmount field of StarGiftAuctionAcquiredGift.
	BidAmount int64
	// Round field of StarGiftAuctionAcquiredGift.
	Round int
	// Pos field of StarGiftAuctionAcquiredGift.
	Pos int
	// Message field of StarGiftAuctionAcquiredGift.
	//
	// Use SetMessage and GetMessage helpers.
	Message TextWithEntities
	// GiftNum field of StarGiftAuctionAcquiredGift.
	//
	// Use SetGiftNum and GetGiftNum helpers.
	GiftNum int
}

// StarGiftAuctionAcquiredGiftTypeID is TL type id of StarGiftAuctionAcquiredGift.
const StarGiftAuctionAcquiredGiftTypeID = 0x42b00348

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

func (s *StarGiftAuctionAcquiredGift) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.NameHidden == false) {
		return false
	}
	if !(s.Peer == nil) {
		return false
	}
	if !(s.Date == 0) {
		return false
	}
	if !(s.BidAmount == 0) {
		return false
	}
	if !(s.Round == 0) {
		return false
	}
	if !(s.Pos == 0) {
		return false
	}
	if !(s.Message.Zero()) {
		return false
	}
	if !(s.GiftNum == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StarGiftAuctionAcquiredGift from given interface.
func (s *StarGiftAuctionAcquiredGift) FillFrom(from interface {
	GetNameHidden() (value bool)
	GetPeer() (value PeerClass)
	GetDate() (value int)
	GetBidAmount() (value int64)
	GetRound() (value int)
	GetPos() (value int)
	GetMessage() (value TextWithEntities, ok bool)
	GetGiftNum() (value int, ok bool)
}) {
	s.NameHidden = from.GetNameHidden()
	s.Peer = from.GetPeer()
	s.Date = from.GetDate()
	s.BidAmount = from.GetBidAmount()
	s.Round = from.GetRound()
	s.Pos = from.GetPos()
	if val, ok := from.GetMessage(); ok {
		s.Message = val
	}

	if val, ok := from.GetGiftNum(); ok {
		s.GiftNum = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (s *StarGiftAuctionAcquiredGift) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starGiftAuctionAcquiredGift",
		ID:   StarGiftAuctionAcquiredGiftTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "NameHidden",
			SchemaName: "name_hidden",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "BidAmount",
			SchemaName: "bid_amount",
		},
		{
			Name:       "Round",
			SchemaName: "round",
		},
		{
			Name:       "Pos",
			SchemaName: "pos",
		},
		{
			Name:       "Message",
			SchemaName: "message",
			Null:       !s.Flags.Has(1),
		},
		{
			Name:       "GiftNum",
			SchemaName: "gift_num",
			Null:       !s.Flags.Has(2),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *StarGiftAuctionAcquiredGift) SetFlags() {
	if !(s.NameHidden == false) {
		s.Flags.Set(0)
	}
	if !(s.Message.Zero()) {
		s.Flags.Set(1)
	}
	if !(s.GiftNum == 0) {
		s.Flags.Set(2)
	}
}

// Encode implements bin.Encoder.
func (s *StarGiftAuctionAcquiredGift) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starGiftAuctionAcquiredGift#42b00348 as nil")
	}
	b.PutID(StarGiftAuctionAcquiredGiftTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarGiftAuctionAcquiredGift) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starGiftAuctionAcquiredGift#42b00348 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starGiftAuctionAcquiredGift#42b00348: field flags: %w", err)
	}
	if s.Peer == nil {
		return fmt.Errorf("unable to encode starGiftAuctionAcquiredGift#42b00348: field peer is nil")
	}
	if err := s.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starGiftAuctionAcquiredGift#42b00348: field peer: %w", err)
	}
	b.PutInt(s.Date)
	b.PutLong(s.BidAmount)
	b.PutInt(s.Round)
	b.PutInt(s.Pos)
	if s.Flags.Has(1) {
		if err := s.Message.Encode(b); err != nil {
			return fmt.Errorf("unable to encode starGiftAuctionAcquiredGift#42b00348: field message: %w", err)
		}
	}
	if s.Flags.Has(2) {
		b.PutInt(s.GiftNum)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *StarGiftAuctionAcquiredGift) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starGiftAuctionAcquiredGift#42b00348 to nil")
	}
	if err := b.ConsumeID(StarGiftAuctionAcquiredGiftTypeID); err != nil {
		return fmt.Errorf("unable to decode starGiftAuctionAcquiredGift#42b00348: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StarGiftAuctionAcquiredGift) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starGiftAuctionAcquiredGift#42b00348 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionAcquiredGift#42b00348: field flags: %w", err)
		}
	}
	s.NameHidden = s.Flags.Has(0)
	{
		value, err := DecodePeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionAcquiredGift#42b00348: field peer: %w", err)
		}
		s.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionAcquiredGift#42b00348: field date: %w", err)
		}
		s.Date = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionAcquiredGift#42b00348: field bid_amount: %w", err)
		}
		s.BidAmount = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionAcquiredGift#42b00348: field round: %w", err)
		}
		s.Round = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionAcquiredGift#42b00348: field pos: %w", err)
		}
		s.Pos = value
	}
	if s.Flags.Has(1) {
		if err := s.Message.Decode(b); err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionAcquiredGift#42b00348: field message: %w", err)
		}
	}
	if s.Flags.Has(2) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionAcquiredGift#42b00348: field gift_num: %w", err)
		}
		s.GiftNum = value
	}
	return nil
}

// SetNameHidden sets value of NameHidden conditional field.
func (s *StarGiftAuctionAcquiredGift) SetNameHidden(value bool) {
	if value {
		s.Flags.Set(0)
		s.NameHidden = true
	} else {
		s.Flags.Unset(0)
		s.NameHidden = false
	}
}

// GetNameHidden returns value of NameHidden conditional field.
func (s *StarGiftAuctionAcquiredGift) GetNameHidden() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(0)
}

// GetPeer returns value of Peer field.
func (s *StarGiftAuctionAcquiredGift) GetPeer() (value PeerClass) {
	if s == nil {
		return
	}
	return s.Peer
}

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

// GetBidAmount returns value of BidAmount field.
func (s *StarGiftAuctionAcquiredGift) GetBidAmount() (value int64) {
	if s == nil {
		return
	}
	return s.BidAmount
}

// GetRound returns value of Round field.
func (s *StarGiftAuctionAcquiredGift) GetRound() (value int) {
	if s == nil {
		return
	}
	return s.Round
}

// GetPos returns value of Pos field.
func (s *StarGiftAuctionAcquiredGift) GetPos() (value int) {
	if s == nil {
		return
	}
	return s.Pos
}

// SetMessage sets value of Message conditional field.
func (s *StarGiftAuctionAcquiredGift) SetMessage(value TextWithEntities) {
	s.Flags.Set(1)
	s.Message = value
}

// GetMessage returns value of Message conditional field and
// boolean which is true if field was set.
func (s *StarGiftAuctionAcquiredGift) GetMessage() (value TextWithEntities, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.Message, true
}

// SetGiftNum sets value of GiftNum conditional field.
func (s *StarGiftAuctionAcquiredGift) SetGiftNum(value int) {
	s.Flags.Set(2)
	s.GiftNum = value
}

// GetGiftNum returns value of GiftNum conditional field and
// boolean which is true if field was set.
func (s *StarGiftAuctionAcquiredGift) GetGiftNum() (value int, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(2) {
		return value, false
	}
	return s.GiftNum, true
}
