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

// StarGiftAuctionUserState represents TL type `starGiftAuctionUserState#2eeed1c4`.
//
// See https://core.telegram.org/constructor/starGiftAuctionUserState for reference.
type StarGiftAuctionUserState struct {
	// Flags field of StarGiftAuctionUserState.
	Flags bin.Fields
	// Returned field of StarGiftAuctionUserState.
	Returned bool
	// BidAmount field of StarGiftAuctionUserState.
	//
	// Use SetBidAmount and GetBidAmount helpers.
	BidAmount int64
	// BidDate field of StarGiftAuctionUserState.
	//
	// Use SetBidDate and GetBidDate helpers.
	BidDate int
	// MinBidAmount field of StarGiftAuctionUserState.
	//
	// Use SetMinBidAmount and GetMinBidAmount helpers.
	MinBidAmount int64
	// BidPeer field of StarGiftAuctionUserState.
	//
	// Use SetBidPeer and GetBidPeer helpers.
	BidPeer PeerClass
	// AcquiredCount field of StarGiftAuctionUserState.
	AcquiredCount int
}

// StarGiftAuctionUserStateTypeID is TL type id of StarGiftAuctionUserState.
const StarGiftAuctionUserStateTypeID = 0x2eeed1c4

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

func (s *StarGiftAuctionUserState) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Returned == false) {
		return false
	}
	if !(s.BidAmount == 0) {
		return false
	}
	if !(s.BidDate == 0) {
		return false
	}
	if !(s.MinBidAmount == 0) {
		return false
	}
	if !(s.BidPeer == nil) {
		return false
	}
	if !(s.AcquiredCount == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StarGiftAuctionUserState from given interface.
func (s *StarGiftAuctionUserState) FillFrom(from interface {
	GetReturned() (value bool)
	GetBidAmount() (value int64, ok bool)
	GetBidDate() (value int, ok bool)
	GetMinBidAmount() (value int64, ok bool)
	GetBidPeer() (value PeerClass, ok bool)
	GetAcquiredCount() (value int)
}) {
	s.Returned = from.GetReturned()
	if val, ok := from.GetBidAmount(); ok {
		s.BidAmount = val
	}

	if val, ok := from.GetBidDate(); ok {
		s.BidDate = val
	}

	if val, ok := from.GetMinBidAmount(); ok {
		s.MinBidAmount = val
	}

	if val, ok := from.GetBidPeer(); ok {
		s.BidPeer = val
	}

	s.AcquiredCount = from.GetAcquiredCount()
}

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

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

// TypeInfo returns info about TL type.
func (s *StarGiftAuctionUserState) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "starGiftAuctionUserState",
		ID:   StarGiftAuctionUserStateTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Returned",
			SchemaName: "returned",
			Null:       !s.Flags.Has(1),
		},
		{
			Name:       "BidAmount",
			SchemaName: "bid_amount",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "BidDate",
			SchemaName: "bid_date",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "MinBidAmount",
			SchemaName: "min_bid_amount",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "BidPeer",
			SchemaName: "bid_peer",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "AcquiredCount",
			SchemaName: "acquired_count",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *StarGiftAuctionUserState) SetFlags() {
	if !(s.Returned == false) {
		s.Flags.Set(1)
	}
	if !(s.BidAmount == 0) {
		s.Flags.Set(0)
	}
	if !(s.BidDate == 0) {
		s.Flags.Set(0)
	}
	if !(s.MinBidAmount == 0) {
		s.Flags.Set(0)
	}
	if !(s.BidPeer == nil) {
		s.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (s *StarGiftAuctionUserState) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starGiftAuctionUserState#2eeed1c4 as nil")
	}
	b.PutID(StarGiftAuctionUserStateTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StarGiftAuctionUserState) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode starGiftAuctionUserState#2eeed1c4 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode starGiftAuctionUserState#2eeed1c4: field flags: %w", err)
	}
	if s.Flags.Has(0) {
		b.PutLong(s.BidAmount)
	}
	if s.Flags.Has(0) {
		b.PutInt(s.BidDate)
	}
	if s.Flags.Has(0) {
		b.PutLong(s.MinBidAmount)
	}
	if s.Flags.Has(0) {
		if s.BidPeer == nil {
			return fmt.Errorf("unable to encode starGiftAuctionUserState#2eeed1c4: field bid_peer is nil")
		}
		if err := s.BidPeer.Encode(b); err != nil {
			return fmt.Errorf("unable to encode starGiftAuctionUserState#2eeed1c4: field bid_peer: %w", err)
		}
	}
	b.PutInt(s.AcquiredCount)
	return nil
}

// Decode implements bin.Decoder.
func (s *StarGiftAuctionUserState) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starGiftAuctionUserState#2eeed1c4 to nil")
	}
	if err := b.ConsumeID(StarGiftAuctionUserStateTypeID); err != nil {
		return fmt.Errorf("unable to decode starGiftAuctionUserState#2eeed1c4: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *StarGiftAuctionUserState) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode starGiftAuctionUserState#2eeed1c4 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionUserState#2eeed1c4: field flags: %w", err)
		}
	}
	s.Returned = s.Flags.Has(1)
	if s.Flags.Has(0) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionUserState#2eeed1c4: field bid_amount: %w", err)
		}
		s.BidAmount = value
	}
	if s.Flags.Has(0) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionUserState#2eeed1c4: field bid_date: %w", err)
		}
		s.BidDate = value
	}
	if s.Flags.Has(0) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionUserState#2eeed1c4: field min_bid_amount: %w", err)
		}
		s.MinBidAmount = value
	}
	if s.Flags.Has(0) {
		value, err := DecodePeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionUserState#2eeed1c4: field bid_peer: %w", err)
		}
		s.BidPeer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode starGiftAuctionUserState#2eeed1c4: field acquired_count: %w", err)
		}
		s.AcquiredCount = value
	}
	return nil
}

// SetReturned sets value of Returned conditional field.
func (s *StarGiftAuctionUserState) SetReturned(value bool) {
	if value {
		s.Flags.Set(1)
		s.Returned = true
	} else {
		s.Flags.Unset(1)
		s.Returned = false
	}
}

// GetReturned returns value of Returned conditional field.
func (s *StarGiftAuctionUserState) GetReturned() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(1)
}

// SetBidAmount sets value of BidAmount conditional field.
func (s *StarGiftAuctionUserState) SetBidAmount(value int64) {
	s.Flags.Set(0)
	s.BidAmount = value
}

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

// SetBidDate sets value of BidDate conditional field.
func (s *StarGiftAuctionUserState) SetBidDate(value int) {
	s.Flags.Set(0)
	s.BidDate = value
}

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

// SetMinBidAmount sets value of MinBidAmount conditional field.
func (s *StarGiftAuctionUserState) SetMinBidAmount(value int64) {
	s.Flags.Set(0)
	s.MinBidAmount = value
}

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

// SetBidPeer sets value of BidPeer conditional field.
func (s *StarGiftAuctionUserState) SetBidPeer(value PeerClass) {
	s.Flags.Set(0)
	s.BidPeer = value
}

// GetBidPeer returns value of BidPeer conditional field and
// boolean which is true if field was set.
func (s *StarGiftAuctionUserState) GetBidPeer() (value PeerClass, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.BidPeer, true
}

// GetAcquiredCount returns value of AcquiredCount field.
func (s *StarGiftAuctionUserState) GetAcquiredCount() (value int) {
	if s == nil {
		return
	}
	return s.AcquiredCount
}
