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

// StoriesStartLiveRequest represents TL type `stories.startLive#d069ccde`.
//
// See https://core.telegram.org/method/stories.startLive for reference.
type StoriesStartLiveRequest struct {
	// Flags field of StoriesStartLiveRequest.
	Flags bin.Fields
	// Pinned field of StoriesStartLiveRequest.
	Pinned bool
	// Noforwards field of StoriesStartLiveRequest.
	Noforwards bool
	// RtmpStream field of StoriesStartLiveRequest.
	RtmpStream bool
	// Peer field of StoriesStartLiveRequest.
	Peer InputPeerClass
	// Caption field of StoriesStartLiveRequest.
	//
	// Use SetCaption and GetCaption helpers.
	Caption string
	// Entities field of StoriesStartLiveRequest.
	//
	// Use SetEntities and GetEntities helpers.
	Entities []MessageEntityClass
	// PrivacyRules field of StoriesStartLiveRequest.
	PrivacyRules []InputPrivacyRuleClass
	// RandomID field of StoriesStartLiveRequest.
	RandomID int64
	// MessagesEnabled field of StoriesStartLiveRequest.
	//
	// Use SetMessagesEnabled and GetMessagesEnabled helpers.
	MessagesEnabled bool
	// SendPaidMessagesStars field of StoriesStartLiveRequest.
	//
	// Use SetSendPaidMessagesStars and GetSendPaidMessagesStars helpers.
	SendPaidMessagesStars int64
}

// StoriesStartLiveRequestTypeID is TL type id of StoriesStartLiveRequest.
const StoriesStartLiveRequestTypeID = 0xd069ccde

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

func (s *StoriesStartLiveRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.Pinned == false) {
		return false
	}
	if !(s.Noforwards == false) {
		return false
	}
	if !(s.RtmpStream == false) {
		return false
	}
	if !(s.Peer == nil) {
		return false
	}
	if !(s.Caption == "") {
		return false
	}
	if !(s.Entities == nil) {
		return false
	}
	if !(s.PrivacyRules == nil) {
		return false
	}
	if !(s.RandomID == 0) {
		return false
	}
	if !(s.MessagesEnabled == false) {
		return false
	}
	if !(s.SendPaidMessagesStars == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesStartLiveRequest from given interface.
func (s *StoriesStartLiveRequest) FillFrom(from interface {
	GetPinned() (value bool)
	GetNoforwards() (value bool)
	GetRtmpStream() (value bool)
	GetPeer() (value InputPeerClass)
	GetCaption() (value string, ok bool)
	GetEntities() (value []MessageEntityClass, ok bool)
	GetPrivacyRules() (value []InputPrivacyRuleClass)
	GetRandomID() (value int64)
	GetMessagesEnabled() (value bool, ok bool)
	GetSendPaidMessagesStars() (value int64, ok bool)
}) {
	s.Pinned = from.GetPinned()
	s.Noforwards = from.GetNoforwards()
	s.RtmpStream = from.GetRtmpStream()
	s.Peer = from.GetPeer()
	if val, ok := from.GetCaption(); ok {
		s.Caption = val
	}

	if val, ok := from.GetEntities(); ok {
		s.Entities = val
	}

	s.PrivacyRules = from.GetPrivacyRules()
	s.RandomID = from.GetRandomID()
	if val, ok := from.GetMessagesEnabled(); ok {
		s.MessagesEnabled = val
	}

	if val, ok := from.GetSendPaidMessagesStars(); ok {
		s.SendPaidMessagesStars = val
	}

}

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

// TypeName returns name of type in TL schema.
func (*StoriesStartLiveRequest) TypeName() string {
	return "stories.startLive"
}

// TypeInfo returns info about TL type.
func (s *StoriesStartLiveRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.startLive",
		ID:   StoriesStartLiveRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Pinned",
			SchemaName: "pinned",
			Null:       !s.Flags.Has(2),
		},
		{
			Name:       "Noforwards",
			SchemaName: "noforwards",
			Null:       !s.Flags.Has(4),
		},
		{
			Name:       "RtmpStream",
			SchemaName: "rtmp_stream",
			Null:       !s.Flags.Has(5),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Caption",
			SchemaName: "caption",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "Entities",
			SchemaName: "entities",
			Null:       !s.Flags.Has(1),
		},
		{
			Name:       "PrivacyRules",
			SchemaName: "privacy_rules",
		},
		{
			Name:       "RandomID",
			SchemaName: "random_id",
		},
		{
			Name:       "MessagesEnabled",
			SchemaName: "messages_enabled",
			Null:       !s.Flags.Has(6),
		},
		{
			Name:       "SendPaidMessagesStars",
			SchemaName: "send_paid_messages_stars",
			Null:       !s.Flags.Has(7),
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *StoriesStartLiveRequest) SetFlags() {
	if !(s.Pinned == false) {
		s.Flags.Set(2)
	}
	if !(s.Noforwards == false) {
		s.Flags.Set(4)
	}
	if !(s.RtmpStream == false) {
		s.Flags.Set(5)
	}
	if !(s.Caption == "") {
		s.Flags.Set(0)
	}
	if !(s.Entities == nil) {
		s.Flags.Set(1)
	}
	if !(s.MessagesEnabled == false) {
		s.Flags.Set(6)
	}
	if !(s.SendPaidMessagesStars == 0) {
		s.Flags.Set(7)
	}
}

// Encode implements bin.Encoder.
func (s *StoriesStartLiveRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode stories.startLive#d069ccde as nil")
	}
	b.PutID(StoriesStartLiveRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *StoriesStartLiveRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode stories.startLive#d069ccde as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.startLive#d069ccde: field flags: %w", err)
	}
	if s.Peer == nil {
		return fmt.Errorf("unable to encode stories.startLive#d069ccde: field peer is nil")
	}
	if err := s.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.startLive#d069ccde: field peer: %w", err)
	}
	if s.Flags.Has(0) {
		b.PutString(s.Caption)
	}
	if s.Flags.Has(1) {
		b.PutVectorHeader(len(s.Entities))
		for idx, v := range s.Entities {
			if v == nil {
				return fmt.Errorf("unable to encode stories.startLive#d069ccde: field entities element with index %d is nil", idx)
			}
			if err := v.Encode(b); err != nil {
				return fmt.Errorf("unable to encode stories.startLive#d069ccde: field entities element with index %d: %w", idx, err)
			}
		}
	}
	b.PutVectorHeader(len(s.PrivacyRules))
	for idx, v := range s.PrivacyRules {
		if v == nil {
			return fmt.Errorf("unable to encode stories.startLive#d069ccde: field privacy_rules element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode stories.startLive#d069ccde: field privacy_rules element with index %d: %w", idx, err)
		}
	}
	b.PutLong(s.RandomID)
	if s.Flags.Has(6) {
		b.PutBool(s.MessagesEnabled)
	}
	if s.Flags.Has(7) {
		b.PutLong(s.SendPaidMessagesStars)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *StoriesStartLiveRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode stories.startLive#d069ccde to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode stories.startLive#d069ccde: field flags: %w", err)
		}
	}
	s.Pinned = s.Flags.Has(2)
	s.Noforwards = s.Flags.Has(4)
	s.RtmpStream = s.Flags.Has(5)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode stories.startLive#d069ccde: field peer: %w", err)
		}
		s.Peer = value
	}
	if s.Flags.Has(0) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode stories.startLive#d069ccde: field caption: %w", err)
		}
		s.Caption = value
	}
	if s.Flags.Has(1) {
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.startLive#d069ccde: field entities: %w", err)
		}

		if headerLen > 0 {
			s.Entities = make([]MessageEntityClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeMessageEntity(b)
			if err != nil {
				return fmt.Errorf("unable to decode stories.startLive#d069ccde: field entities: %w", err)
			}
			s.Entities = append(s.Entities, value)
		}
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.startLive#d069ccde: field privacy_rules: %w", err)
		}

		if headerLen > 0 {
			s.PrivacyRules = make([]InputPrivacyRuleClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputPrivacyRule(b)
			if err != nil {
				return fmt.Errorf("unable to decode stories.startLive#d069ccde: field privacy_rules: %w", err)
			}
			s.PrivacyRules = append(s.PrivacyRules, value)
		}
	}
	{
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode stories.startLive#d069ccde: field random_id: %w", err)
		}
		s.RandomID = value
	}
	if s.Flags.Has(6) {
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode stories.startLive#d069ccde: field messages_enabled: %w", err)
		}
		s.MessagesEnabled = value
	}
	if s.Flags.Has(7) {
		value, err := b.Long()
		if err != nil {
			return fmt.Errorf("unable to decode stories.startLive#d069ccde: field send_paid_messages_stars: %w", err)
		}
		s.SendPaidMessagesStars = value
	}
	return nil
}

// SetPinned sets value of Pinned conditional field.
func (s *StoriesStartLiveRequest) SetPinned(value bool) {
	if value {
		s.Flags.Set(2)
		s.Pinned = true
	} else {
		s.Flags.Unset(2)
		s.Pinned = false
	}
}

// GetPinned returns value of Pinned conditional field.
func (s *StoriesStartLiveRequest) GetPinned() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(2)
}

// SetNoforwards sets value of Noforwards conditional field.
func (s *StoriesStartLiveRequest) SetNoforwards(value bool) {
	if value {
		s.Flags.Set(4)
		s.Noforwards = true
	} else {
		s.Flags.Unset(4)
		s.Noforwards = false
	}
}

// GetNoforwards returns value of Noforwards conditional field.
func (s *StoriesStartLiveRequest) GetNoforwards() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(4)
}

// SetRtmpStream sets value of RtmpStream conditional field.
func (s *StoriesStartLiveRequest) SetRtmpStream(value bool) {
	if value {
		s.Flags.Set(5)
		s.RtmpStream = true
	} else {
		s.Flags.Unset(5)
		s.RtmpStream = false
	}
}

// GetRtmpStream returns value of RtmpStream conditional field.
func (s *StoriesStartLiveRequest) GetRtmpStream() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(5)
}

// GetPeer returns value of Peer field.
func (s *StoriesStartLiveRequest) GetPeer() (value InputPeerClass) {
	if s == nil {
		return
	}
	return s.Peer
}

// SetCaption sets value of Caption conditional field.
func (s *StoriesStartLiveRequest) SetCaption(value string) {
	s.Flags.Set(0)
	s.Caption = value
}

// GetCaption returns value of Caption conditional field and
// boolean which is true if field was set.
func (s *StoriesStartLiveRequest) GetCaption() (value string, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(0) {
		return value, false
	}
	return s.Caption, true
}

// SetEntities sets value of Entities conditional field.
func (s *StoriesStartLiveRequest) SetEntities(value []MessageEntityClass) {
	s.Flags.Set(1)
	s.Entities = value
}

// GetEntities returns value of Entities conditional field and
// boolean which is true if field was set.
func (s *StoriesStartLiveRequest) GetEntities() (value []MessageEntityClass, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(1) {
		return value, false
	}
	return s.Entities, true
}

// GetPrivacyRules returns value of PrivacyRules field.
func (s *StoriesStartLiveRequest) GetPrivacyRules() (value []InputPrivacyRuleClass) {
	if s == nil {
		return
	}
	return s.PrivacyRules
}

// GetRandomID returns value of RandomID field.
func (s *StoriesStartLiveRequest) GetRandomID() (value int64) {
	if s == nil {
		return
	}
	return s.RandomID
}

// SetMessagesEnabled sets value of MessagesEnabled conditional field.
func (s *StoriesStartLiveRequest) SetMessagesEnabled(value bool) {
	s.Flags.Set(6)
	s.MessagesEnabled = value
}

// GetMessagesEnabled returns value of MessagesEnabled conditional field and
// boolean which is true if field was set.
func (s *StoriesStartLiveRequest) GetMessagesEnabled() (value bool, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(6) {
		return value, false
	}
	return s.MessagesEnabled, true
}

// SetSendPaidMessagesStars sets value of SendPaidMessagesStars conditional field.
func (s *StoriesStartLiveRequest) SetSendPaidMessagesStars(value int64) {
	s.Flags.Set(7)
	s.SendPaidMessagesStars = value
}

// GetSendPaidMessagesStars returns value of SendPaidMessagesStars conditional field and
// boolean which is true if field was set.
func (s *StoriesStartLiveRequest) GetSendPaidMessagesStars() (value int64, ok bool) {
	if s == nil {
		return
	}
	if !s.Flags.Has(7) {
		return value, false
	}
	return s.SendPaidMessagesStars, true
}

// MapEntities returns field Entities wrapped in MessageEntityClassArray helper.
func (s *StoriesStartLiveRequest) MapEntities() (value MessageEntityClassArray, ok bool) {
	if !s.Flags.Has(1) {
		return value, false
	}
	return MessageEntityClassArray(s.Entities), true
}

// MapPrivacyRules returns field PrivacyRules wrapped in InputPrivacyRuleClassArray helper.
func (s *StoriesStartLiveRequest) MapPrivacyRules() (value InputPrivacyRuleClassArray) {
	return InputPrivacyRuleClassArray(s.PrivacyRules)
}

// StoriesStartLive invokes method stories.startLive#d069ccde returning error if any.
//
// See https://core.telegram.org/method/stories.startLive for reference.
func (c *Client) StoriesStartLive(ctx context.Context, request *StoriesStartLiveRequest) (UpdatesClass, error) {
	var result UpdatesBox

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