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

// SetChatActiveStoriesListRequest represents TL type `setChatActiveStoriesList#e0e35d11`.
type SetChatActiveStoriesListRequest struct {
	// Identifier of the chat that posted stories
	ChatID int64
	// New list for active stories posted by the chat
	StoryList StoryListClass
}

// SetChatActiveStoriesListRequestTypeID is TL type id of SetChatActiveStoriesListRequest.
const SetChatActiveStoriesListRequestTypeID = 0xe0e35d11

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

func (s *SetChatActiveStoriesListRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ChatID == 0) {
		return false
	}
	if !(s.StoryList == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetChatActiveStoriesListRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setChatActiveStoriesList",
		ID:   SetChatActiveStoriesListRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "StoryList",
			SchemaName: "story_list",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetChatActiveStoriesListRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatActiveStoriesList#e0e35d11 as nil")
	}
	b.PutID(SetChatActiveStoriesListRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetChatActiveStoriesListRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatActiveStoriesList#e0e35d11 as nil")
	}
	b.PutInt53(s.ChatID)
	if s.StoryList == nil {
		return fmt.Errorf("unable to encode setChatActiveStoriesList#e0e35d11: field story_list is nil")
	}
	if err := s.StoryList.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setChatActiveStoriesList#e0e35d11: field story_list: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SetChatActiveStoriesListRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setChatActiveStoriesList#e0e35d11 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setChatActiveStoriesList#e0e35d11: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := DecodeStoryList(b)
		if err != nil {
			return fmt.Errorf("unable to decode setChatActiveStoriesList#e0e35d11: field story_list: %w", err)
		}
		s.StoryList = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetChatActiveStoriesListRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatActiveStoriesList#e0e35d11 as nil")
	}
	b.ObjStart()
	b.PutID("setChatActiveStoriesList")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("story_list")
	if s.StoryList == nil {
		return fmt.Errorf("unable to encode setChatActiveStoriesList#e0e35d11: field story_list is nil")
	}
	if err := s.StoryList.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setChatActiveStoriesList#e0e35d11: field story_list: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetChatActiveStoriesListRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setChatActiveStoriesList#e0e35d11 to nil")
	}

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

// GetChatID returns value of ChatID field.
func (s *SetChatActiveStoriesListRequest) GetChatID() (value int64) {
	if s == nil {
		return
	}
	return s.ChatID
}

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

// SetChatActiveStoriesList invokes method setChatActiveStoriesList#e0e35d11 returning error if any.
func (c *Client) SetChatActiveStoriesList(ctx context.Context, request *SetChatActiveStoriesListRequest) error {
	var ok Ok

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