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

// RecentStory represents TL type `recentStory#711d692d`.
//
// See https://core.telegram.org/constructor/recentStory for reference.
type RecentStory struct {
	// Flags field of RecentStory.
	Flags bin.Fields
	// Live field of RecentStory.
	Live bool
	// MaxID field of RecentStory.
	//
	// Use SetMaxID and GetMaxID helpers.
	MaxID int
}

// RecentStoryTypeID is TL type id of RecentStory.
const RecentStoryTypeID = 0x711d692d

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

func (r *RecentStory) Zero() bool {
	if r == nil {
		return true
	}
	if !(r.Flags.Zero()) {
		return false
	}
	if !(r.Live == false) {
		return false
	}
	if !(r.MaxID == 0) {
		return false
	}

	return true
}

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

// FillFrom fills RecentStory from given interface.
func (r *RecentStory) FillFrom(from interface {
	GetLive() (value bool)
	GetMaxID() (value int, ok bool)
}) {
	r.Live = from.GetLive()
	if val, ok := from.GetMaxID(); ok {
		r.MaxID = val
	}

}

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

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

// TypeInfo returns info about TL type.
func (r *RecentStory) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "recentStory",
		ID:   RecentStoryTypeID,
	}
	if r == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Live",
			SchemaName: "live",
			Null:       !r.Flags.Has(0),
		},
		{
			Name:       "MaxID",
			SchemaName: "max_id",
			Null:       !r.Flags.Has(1),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (r *RecentStory) SetFlags() {
	if !(r.Live == false) {
		r.Flags.Set(0)
	}
	if !(r.MaxID == 0) {
		r.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (r *RecentStory) Encode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode recentStory#711d692d as nil")
	}
	b.PutID(RecentStoryTypeID)
	return r.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (r *RecentStory) EncodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't encode recentStory#711d692d as nil")
	}
	r.SetFlags()
	if err := r.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode recentStory#711d692d: field flags: %w", err)
	}
	if r.Flags.Has(1) {
		b.PutInt(r.MaxID)
	}
	return nil
}

// Decode implements bin.Decoder.
func (r *RecentStory) Decode(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode recentStory#711d692d to nil")
	}
	if err := b.ConsumeID(RecentStoryTypeID); err != nil {
		return fmt.Errorf("unable to decode recentStory#711d692d: %w", err)
	}
	return r.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (r *RecentStory) DecodeBare(b *bin.Buffer) error {
	if r == nil {
		return fmt.Errorf("can't decode recentStory#711d692d to nil")
	}
	{
		if err := r.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode recentStory#711d692d: field flags: %w", err)
		}
	}
	r.Live = r.Flags.Has(0)
	if r.Flags.Has(1) {
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode recentStory#711d692d: field max_id: %w", err)
		}
		r.MaxID = value
	}
	return nil
}

// SetLive sets value of Live conditional field.
func (r *RecentStory) SetLive(value bool) {
	if value {
		r.Flags.Set(0)
		r.Live = true
	} else {
		r.Flags.Unset(0)
		r.Live = false
	}
}

// GetLive returns value of Live conditional field.
func (r *RecentStory) GetLive() (value bool) {
	if r == nil {
		return
	}
	return r.Flags.Has(0)
}

// SetMaxID sets value of MaxID conditional field.
func (r *RecentStory) SetMaxID(value int) {
	r.Flags.Set(1)
	r.MaxID = value
}

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