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

// FoundStory represents TL type `foundStory#e87acbc0`.
// A story found using global story search »¹.
//
// Links:
//  1. https://core.telegram.org/api/stories#searching-stories
//
// See https://core.telegram.org/constructor/foundStory for reference.
type FoundStory struct {
	// The peer that posted the story.
	Peer PeerClass
	// The story.
	Story StoryItemClass
}

// FoundStoryTypeID is TL type id of FoundStory.
const FoundStoryTypeID = 0xe87acbc0

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

func (f *FoundStory) Zero() bool {
	if f == nil {
		return true
	}
	if !(f.Peer == nil) {
		return false
	}
	if !(f.Story == nil) {
		return false
	}

	return true
}

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

// FillFrom fills FoundStory from given interface.
func (f *FoundStory) FillFrom(from interface {
	GetPeer() (value PeerClass)
	GetStory() (value StoryItemClass)
}) {
	f.Peer = from.GetPeer()
	f.Story = from.GetStory()
}

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

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

// TypeInfo returns info about TL type.
func (f *FoundStory) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "foundStory",
		ID:   FoundStoryTypeID,
	}
	if f == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Story",
			SchemaName: "story",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (f *FoundStory) Encode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundStory#e87acbc0 as nil")
	}
	b.PutID(FoundStoryTypeID)
	return f.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (f *FoundStory) EncodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't encode foundStory#e87acbc0 as nil")
	}
	if f.Peer == nil {
		return fmt.Errorf("unable to encode foundStory#e87acbc0: field peer is nil")
	}
	if err := f.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode foundStory#e87acbc0: field peer: %w", err)
	}
	if f.Story == nil {
		return fmt.Errorf("unable to encode foundStory#e87acbc0: field story is nil")
	}
	if err := f.Story.Encode(b); err != nil {
		return fmt.Errorf("unable to encode foundStory#e87acbc0: field story: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (f *FoundStory) Decode(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode foundStory#e87acbc0 to nil")
	}
	if err := b.ConsumeID(FoundStoryTypeID); err != nil {
		return fmt.Errorf("unable to decode foundStory#e87acbc0: %w", err)
	}
	return f.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (f *FoundStory) DecodeBare(b *bin.Buffer) error {
	if f == nil {
		return fmt.Errorf("can't decode foundStory#e87acbc0 to nil")
	}
	{
		value, err := DecodePeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode foundStory#e87acbc0: field peer: %w", err)
		}
		f.Peer = value
	}
	{
		value, err := DecodeStoryItem(b)
		if err != nil {
			return fmt.Errorf("unable to decode foundStory#e87acbc0: field story: %w", err)
		}
		f.Story = value
	}
	return nil
}

// GetPeer returns value of Peer field.
func (f *FoundStory) GetPeer() (value PeerClass) {
	if f == nil {
		return
	}
	return f.Peer
}

// GetStory returns value of Story field.
func (f *FoundStory) GetStory() (value StoryItemClass) {
	if f == nil {
		return
	}
	return f.Story
}
