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

// MyBoost represents TL type `myBoost#c448415c`.
// Contains information about a single boost slot »¹.
//
// Links:
//  1. https://core.telegram.org/api/boost
//
// See https://core.telegram.org/constructor/myBoost for reference.
type MyBoost struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Boost slot ID »¹
	//
	// Links:
	//  1) https://core.telegram.org/api/boost
	Slot int
	// If set, indicates this slot is currently occupied, i.e. we are boosting¹ this peer.
	// Note that we can assign multiple boost slots to the same peer.
	//
	// Links:
	//  1) https://core.telegram.org/api/boost
	//
	// Use SetPeer and GetPeer helpers.
	Peer PeerClass
	// When (unixtime) we started boosting the peer, 0 otherwise.
	Date int
	// Indicates the (unixtime) expiration date of the boost in peer (0 if peer is not set).
	Expires int
	// If peer is set, indicates the (unixtime) date after which this boost can be reassigned
	// to another channel.
	//
	// Use SetCooldownUntilDate and GetCooldownUntilDate helpers.
	CooldownUntilDate int
}

// MyBoostTypeID is TL type id of MyBoost.
const MyBoostTypeID = 0xc448415c

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

func (m *MyBoost) Zero() bool {
	if m == nil {
		return true
	}
	if !(m.Flags.Zero()) {
		return false
	}
	if !(m.Slot == 0) {
		return false
	}
	if !(m.Peer == nil) {
		return false
	}
	if !(m.Date == 0) {
		return false
	}
	if !(m.Expires == 0) {
		return false
	}
	if !(m.CooldownUntilDate == 0) {
		return false
	}

	return true
}

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

// FillFrom fills MyBoost from given interface.
func (m *MyBoost) FillFrom(from interface {
	GetSlot() (value int)
	GetPeer() (value PeerClass, ok bool)
	GetDate() (value int)
	GetExpires() (value int)
	GetCooldownUntilDate() (value int, ok bool)
}) {
	m.Slot = from.GetSlot()
	if val, ok := from.GetPeer(); ok {
		m.Peer = val
	}

	m.Date = from.GetDate()
	m.Expires = from.GetExpires()
	if val, ok := from.GetCooldownUntilDate(); ok {
		m.CooldownUntilDate = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (m *MyBoost) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "myBoost",
		ID:   MyBoostTypeID,
	}
	if m == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Slot",
			SchemaName: "slot",
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
			Null:       !m.Flags.Has(0),
		},
		{
			Name:       "Date",
			SchemaName: "date",
		},
		{
			Name:       "Expires",
			SchemaName: "expires",
		},
		{
			Name:       "CooldownUntilDate",
			SchemaName: "cooldown_until_date",
			Null:       !m.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (m *MyBoost) SetFlags() {
	if !(m.Peer == nil) {
		m.Flags.Set(0)
	}
	if !(m.CooldownUntilDate == 0) {
		m.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (m *MyBoost) Encode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode myBoost#c448415c as nil")
	}
	b.PutID(MyBoostTypeID)
	return m.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (m *MyBoost) EncodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't encode myBoost#c448415c as nil")
	}
	m.SetFlags()
	if err := m.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode myBoost#c448415c: field flags: %w", err)
	}
	b.PutInt(m.Slot)
	if m.Flags.Has(0) {
		if m.Peer == nil {
			return fmt.Errorf("unable to encode myBoost#c448415c: field peer is nil")
		}
		if err := m.Peer.Encode(b); err != nil {
			return fmt.Errorf("unable to encode myBoost#c448415c: field peer: %w", err)
		}
	}
	b.PutInt(m.Date)
	b.PutInt(m.Expires)
	if m.Flags.Has(1) {
		b.PutInt(m.CooldownUntilDate)
	}
	return nil
}

// Decode implements bin.Decoder.
func (m *MyBoost) Decode(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode myBoost#c448415c to nil")
	}
	if err := b.ConsumeID(MyBoostTypeID); err != nil {
		return fmt.Errorf("unable to decode myBoost#c448415c: %w", err)
	}
	return m.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (m *MyBoost) DecodeBare(b *bin.Buffer) error {
	if m == nil {
		return fmt.Errorf("can't decode myBoost#c448415c to nil")
	}
	{
		if err := m.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode myBoost#c448415c: field flags: %w", err)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode myBoost#c448415c: field slot: %w", err)
		}
		m.Slot = value
	}
	if m.Flags.Has(0) {
		value, err := DecodePeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode myBoost#c448415c: field peer: %w", err)
		}
		m.Peer = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode myBoost#c448415c: field date: %w", err)
		}
		m.Date = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode myBoost#c448415c: field expires: %w", err)
		}
		m.Expires = value
	}
	if m.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode myBoost#c448415c: field cooldown_until_date: %w", err)
		}
		m.CooldownUntilDate = value
	}
	return nil
}

// GetSlot returns value of Slot field.
func (m *MyBoost) GetSlot() (value int) {
	if m == nil {
		return
	}
	return m.Slot
}

// SetPeer sets value of Peer conditional field.
func (m *MyBoost) SetPeer(value PeerClass) {
	m.Flags.Set(0)
	m.Peer = value
}

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

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

// GetExpires returns value of Expires field.
func (m *MyBoost) GetExpires() (value int) {
	if m == nil {
		return
	}
	return m.Expires
}

// SetCooldownUntilDate sets value of CooldownUntilDate conditional field.
func (m *MyBoost) SetCooldownUntilDate(value int) {
	m.Flags.Set(1)
	m.CooldownUntilDate = value
}

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