// Code generated by gotdgen, DO NOT EDIT.

package tdapi

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

// LoadActiveStoriesRequest represents TL type `loadActiveStories#7d8cf738`.
type LoadActiveStoriesRequest struct {
	// The story list in which to load active stories
	StoryList StoryListClass
}

// LoadActiveStoriesRequestTypeID is TL type id of LoadActiveStoriesRequest.
const LoadActiveStoriesRequestTypeID = 0x7d8cf738

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

func (l *LoadActiveStoriesRequest) Zero() bool {
	if l == nil {
		return true
	}
	if !(l.StoryList == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (l *LoadActiveStoriesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "loadActiveStories",
		ID:   LoadActiveStoriesRequestTypeID,
	}
	if l == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "StoryList",
			SchemaName: "story_list",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (l *LoadActiveStoriesRequest) Encode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode loadActiveStories#7d8cf738 as nil")
	}
	b.PutID(LoadActiveStoriesRequestTypeID)
	return l.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (l *LoadActiveStoriesRequest) EncodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't encode loadActiveStories#7d8cf738 as nil")
	}
	if l.StoryList == nil {
		return fmt.Errorf("unable to encode loadActiveStories#7d8cf738: field story_list is nil")
	}
	if err := l.StoryList.Encode(b); err != nil {
		return fmt.Errorf("unable to encode loadActiveStories#7d8cf738: field story_list: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (l *LoadActiveStoriesRequest) Decode(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode loadActiveStories#7d8cf738 to nil")
	}
	if err := b.ConsumeID(LoadActiveStoriesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode loadActiveStories#7d8cf738: %w", err)
	}
	return l.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (l *LoadActiveStoriesRequest) DecodeBare(b *bin.Buffer) error {
	if l == nil {
		return fmt.Errorf("can't decode loadActiveStories#7d8cf738 to nil")
	}
	{
		value, err := DecodeStoryList(b)
		if err != nil {
			return fmt.Errorf("unable to decode loadActiveStories#7d8cf738: field story_list: %w", err)
		}
		l.StoryList = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (l *LoadActiveStoriesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if l == nil {
		return fmt.Errorf("can't encode loadActiveStories#7d8cf738 as nil")
	}
	b.ObjStart()
	b.PutID("loadActiveStories")
	b.Comma()
	b.FieldStart("story_list")
	if l.StoryList == nil {
		return fmt.Errorf("unable to encode loadActiveStories#7d8cf738: field story_list is nil")
	}
	if err := l.StoryList.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode loadActiveStories#7d8cf738: field story_list: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (l *LoadActiveStoriesRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if l == nil {
		return fmt.Errorf("can't decode loadActiveStories#7d8cf738 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("loadActiveStories"); err != nil {
				return fmt.Errorf("unable to decode loadActiveStories#7d8cf738: %w", err)
			}
		case "story_list":
			value, err := DecodeTDLibJSONStoryList(b)
			if err != nil {
				return fmt.Errorf("unable to decode loadActiveStories#7d8cf738: field story_list: %w", err)
			}
			l.StoryList = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetStoryList returns value of StoryList field.
func (l *LoadActiveStoriesRequest) GetStoryList() (value StoryListClass) {
	if l == nil {
		return
	}
	return l.StoryList
}

// LoadActiveStories invokes method loadActiveStories#7d8cf738 returning error if any.
func (c *Client) LoadActiveStories(ctx context.Context, storylist StoryListClass) error {
	var ok Ok

	request := &LoadActiveStoriesRequest{
		StoryList: storylist,
	}
	if err := c.rpc.Invoke(ctx, request, &ok); err != nil {
		return err
	}
	return nil
}
