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

// SearchPostsFlood represents TL type `searchPostsFlood#3e0b5b6a`.
// Indicates if the specified global post search »¹ requires payment.
//
// Links:
//  1. https://core.telegram.org/api/search#posts-tab
//
// See https://core.telegram.org/constructor/searchPostsFlood for reference.
type SearchPostsFlood struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// The specified query is free (and it will not use up free search slots).
	QueryIsFree bool
	// Total number of daily free search slots.
	TotalDaily int
	// Remaining number of free search slots.
	Remains int
	// If there are no more search slots, specifies the unixtime when more search slots will
	// be available.
	//
	// Use SetWaitTill and GetWaitTill helpers.
	WaitTill int
	// The number of Telegram Stars¹ to pay for each non-free search.
	//
	// Links:
	//  1) https://core.telegram.org/api/stars
	StarsAmount int64
}

// SearchPostsFloodTypeID is TL type id of SearchPostsFlood.
const SearchPostsFloodTypeID = 0x3e0b5b6a

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

func (s *SearchPostsFlood) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.QueryIsFree == false) {
		return false
	}
	if !(s.TotalDaily == 0) {
		return false
	}
	if !(s.Remains == 0) {
		return false
	}
	if !(s.WaitTill == 0) {
		return false
	}
	if !(s.StarsAmount == 0) {
		return false
	}

	return true
}

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

// FillFrom fills SearchPostsFlood from given interface.
func (s *SearchPostsFlood) FillFrom(from interface {
	GetQueryIsFree() (value bool)
	GetTotalDaily() (value int)
	GetRemains() (value int)
	GetWaitTill() (value int, ok bool)
	GetStarsAmount() (value int64)
}) {
	s.QueryIsFree = from.GetQueryIsFree()
	s.TotalDaily = from.GetTotalDaily()
	s.Remains = from.GetRemains()
	if val, ok := from.GetWaitTill(); ok {
		s.WaitTill = val
	}

	s.StarsAmount = from.GetStarsAmount()
}

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

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

// TypeInfo returns info about TL type.
func (s *SearchPostsFlood) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchPostsFlood",
		ID:   SearchPostsFloodTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "QueryIsFree",
			SchemaName: "query_is_free",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "TotalDaily",
			SchemaName: "total_daily",
		},
		{
			Name:       "Remains",
			SchemaName: "remains",
		},
		{
			Name:       "WaitTill",
			SchemaName: "wait_till",
			Null:       !s.Flags.Has(1),
		},
		{
			Name:       "StarsAmount",
			SchemaName: "stars_amount",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *SearchPostsFlood) SetFlags() {
	if !(s.QueryIsFree == false) {
		s.Flags.Set(0)
	}
	if !(s.WaitTill == 0) {
		s.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (s *SearchPostsFlood) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPostsFlood#3e0b5b6a as nil")
	}
	b.PutID(SearchPostsFloodTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchPostsFlood) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchPostsFlood#3e0b5b6a as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode searchPostsFlood#3e0b5b6a: field flags: %w", err)
	}
	b.PutInt(s.TotalDaily)
	b.PutInt(s.Remains)
	if s.Flags.Has(1) {
		b.PutInt(s.WaitTill)
	}
	b.PutLong(s.StarsAmount)
	return nil
}

// Decode implements bin.Decoder.
func (s *SearchPostsFlood) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchPostsFlood#3e0b5b6a to nil")
	}
	if err := b.ConsumeID(SearchPostsFloodTypeID); err != nil {
		return fmt.Errorf("unable to decode searchPostsFlood#3e0b5b6a: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SearchPostsFlood) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchPostsFlood#3e0b5b6a to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode searchPostsFlood#3e0b5b6a: field flags: %w", err)
		}
	}
	s.QueryIsFree = s.Flags.Has(0)
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode searchPostsFlood#3e0b5b6a: field total_daily: %w", err)
		}
		s.TotalDaily = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode searchPostsFlood#3e0b5b6a: field remains: %w", err)
		}
		s.Remains = value
	}
	if s.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode searchPostsFlood#3e0b5b6a: field wait_till: %w", err)
		}
		s.WaitTill = value
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode searchPostsFlood#3e0b5b6a: field stars_amount: %w", err)
		}
		s.StarsAmount = value
	}
	return nil
}

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

// GetQueryIsFree returns value of QueryIsFree conditional field.
func (s *SearchPostsFlood) GetQueryIsFree() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(0)
}

// GetTotalDaily returns value of TotalDaily field.
func (s *SearchPostsFlood) GetTotalDaily() (value int) {
	if s == nil {
		return
	}
	return s.TotalDaily
}

// GetRemains returns value of Remains field.
func (s *SearchPostsFlood) GetRemains() (value int) {
	if s == nil {
		return
	}
	return s.Remains
}

// SetWaitTill sets value of WaitTill conditional field.
func (s *SearchPostsFlood) SetWaitTill(value int) {
	s.Flags.Set(1)
	s.WaitTill = value
}

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

// GetStarsAmount returns value of StarsAmount field.
func (s *SearchPostsFlood) GetStarsAmount() (value int64) {
	if s == nil {
		return
	}
	return s.StarsAmount
}
