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

// PaymentsStarGiftAuctionState represents TL type `payments.starGiftAuctionState#e98e474`.
//
// See https://core.telegram.org/constructor/payments.starGiftAuctionState for reference.
type PaymentsStarGiftAuctionState struct {
	// Gift field of PaymentsStarGiftAuctionState.
	Gift StarGiftClass
	// State field of PaymentsStarGiftAuctionState.
	State StarGiftAuctionStateClass
	// UserState field of PaymentsStarGiftAuctionState.
	UserState StarGiftAuctionUserState
	// Timeout field of PaymentsStarGiftAuctionState.
	Timeout int
	// Users field of PaymentsStarGiftAuctionState.
	Users []UserClass
}

// PaymentsStarGiftAuctionStateTypeID is TL type id of PaymentsStarGiftAuctionState.
const PaymentsStarGiftAuctionStateTypeID = 0xe98e474

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

func (s *PaymentsStarGiftAuctionState) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Gift == nil) {
		return false
	}
	if !(s.State == nil) {
		return false
	}
	if !(s.UserState.Zero()) {
		return false
	}
	if !(s.Timeout == 0) {
		return false
	}
	if !(s.Users == nil) {
		return false
	}

	return true
}

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

// FillFrom fills PaymentsStarGiftAuctionState from given interface.
func (s *PaymentsStarGiftAuctionState) FillFrom(from interface {
	GetGift() (value StarGiftClass)
	GetState() (value StarGiftAuctionStateClass)
	GetUserState() (value StarGiftAuctionUserState)
	GetTimeout() (value int)
	GetUsers() (value []UserClass)
}) {
	s.Gift = from.GetGift()
	s.State = from.GetState()
	s.UserState = from.GetUserState()
	s.Timeout = from.GetTimeout()
	s.Users = from.GetUsers()
}

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

// TypeName returns name of type in TL schema.
func (*PaymentsStarGiftAuctionState) TypeName() string {
	return "payments.starGiftAuctionState"
}

// TypeInfo returns info about TL type.
func (s *PaymentsStarGiftAuctionState) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "payments.starGiftAuctionState",
		ID:   PaymentsStarGiftAuctionStateTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Gift",
			SchemaName: "gift",
		},
		{
			Name:       "State",
			SchemaName: "state",
		},
		{
			Name:       "UserState",
			SchemaName: "user_state",
		},
		{
			Name:       "Timeout",
			SchemaName: "timeout",
		},
		{
			Name:       "Users",
			SchemaName: "users",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *PaymentsStarGiftAuctionState) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starGiftAuctionState#e98e474 as nil")
	}
	b.PutID(PaymentsStarGiftAuctionStateTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *PaymentsStarGiftAuctionState) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode payments.starGiftAuctionState#e98e474 as nil")
	}
	if s.Gift == nil {
		return fmt.Errorf("unable to encode payments.starGiftAuctionState#e98e474: field gift is nil")
	}
	if err := s.Gift.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.starGiftAuctionState#e98e474: field gift: %w", err)
	}
	if s.State == nil {
		return fmt.Errorf("unable to encode payments.starGiftAuctionState#e98e474: field state is nil")
	}
	if err := s.State.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.starGiftAuctionState#e98e474: field state: %w", err)
	}
	if err := s.UserState.Encode(b); err != nil {
		return fmt.Errorf("unable to encode payments.starGiftAuctionState#e98e474: field user_state: %w", err)
	}
	b.PutInt(s.Timeout)
	b.PutVectorHeader(len(s.Users))
	for idx, v := range s.Users {
		if v == nil {
			return fmt.Errorf("unable to encode payments.starGiftAuctionState#e98e474: field users element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode payments.starGiftAuctionState#e98e474: field users element with index %d: %w", idx, err)
		}
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *PaymentsStarGiftAuctionState) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.starGiftAuctionState#e98e474 to nil")
	}
	if err := b.ConsumeID(PaymentsStarGiftAuctionStateTypeID); err != nil {
		return fmt.Errorf("unable to decode payments.starGiftAuctionState#e98e474: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *PaymentsStarGiftAuctionState) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode payments.starGiftAuctionState#e98e474 to nil")
	}
	{
		value, err := DecodeStarGift(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.starGiftAuctionState#e98e474: field gift: %w", err)
		}
		s.Gift = value
	}
	{
		value, err := DecodeStarGiftAuctionState(b)
		if err != nil {
			return fmt.Errorf("unable to decode payments.starGiftAuctionState#e98e474: field state: %w", err)
		}
		s.State = value
	}
	{
		if err := s.UserState.Decode(b); err != nil {
			return fmt.Errorf("unable to decode payments.starGiftAuctionState#e98e474: field user_state: %w", err)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starGiftAuctionState#e98e474: field timeout: %w", err)
		}
		s.Timeout = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode payments.starGiftAuctionState#e98e474: field users: %w", err)
		}

		if headerLen > 0 {
			s.Users = make([]UserClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeUser(b)
			if err != nil {
				return fmt.Errorf("unable to decode payments.starGiftAuctionState#e98e474: field users: %w", err)
			}
			s.Users = append(s.Users, value)
		}
	}
	return nil
}

// GetGift returns value of Gift field.
func (s *PaymentsStarGiftAuctionState) GetGift() (value StarGiftClass) {
	if s == nil {
		return
	}
	return s.Gift
}

// GetState returns value of State field.
func (s *PaymentsStarGiftAuctionState) GetState() (value StarGiftAuctionStateClass) {
	if s == nil {
		return
	}
	return s.State
}

// GetUserState returns value of UserState field.
func (s *PaymentsStarGiftAuctionState) GetUserState() (value StarGiftAuctionUserState) {
	if s == nil {
		return
	}
	return s.UserState
}

// GetTimeout returns value of Timeout field.
func (s *PaymentsStarGiftAuctionState) GetTimeout() (value int) {
	if s == nil {
		return
	}
	return s.Timeout
}

// GetUsers returns value of Users field.
func (s *PaymentsStarGiftAuctionState) GetUsers() (value []UserClass) {
	if s == nil {
		return
	}
	return s.Users
}

// GetStateAsModified returns mapped value of State field.
func (s *PaymentsStarGiftAuctionState) GetStateAsModified() (ModifiedStarGiftAuctionState, bool) {
	return s.State.AsModified()
}

// MapUsers returns field Users wrapped in UserClassArray helper.
func (s *PaymentsStarGiftAuctionState) MapUsers() (value UserClassArray) {
	return UserClassArray(s.Users)
}
