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

// StoriesGetStoryViewsListRequest represents TL type `stories.getStoryViewsList#7ed23c57`.
// Obtain the list of users that have viewed a specific story we posted¹
//
// Links:
//  1. https://core.telegram.org/api/stories
//
// See https://core.telegram.org/method/stories.getStoryViewsList for reference.
type StoriesGetStoryViewsListRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether to only fetch view reaction/views made by our contacts¹
	//
	// Links:
	//  1) https://core.telegram.org/api/contacts
	JustContacts bool
	// Whether to return storyView¹ info about users that reacted to the story (i.e. if set,
	// the server will first sort results by view date as usual, and then also additionally
	// sort the list by putting storyView²s with an associated reaction first in the list).
	// Ignored if forwards_first is set.
	//
	// Links:
	//  1) https://core.telegram.org/constructor/storyView
	//  2) https://core.telegram.org/constructor/storyView
	ReactionsFirst bool
	// If set, returns forwards and reposts first, then reactions, then other views;
	// otherwise returns interactions sorted just by interaction date.
	ForwardsFirst bool
	// Peer where the story was posted
	Peer InputPeerClass
	// Search for specific peers
	//
	// Use SetQ and GetQ helpers.
	Q string
	// Story ID
	ID int
	// Offset for pagination, obtained from stories.storyViewsList¹.next_offset
	//
	// Links:
	//  1) https://core.telegram.org/constructor/stories.storyViewsList
	Offset string
	// Maximum number of results to return, see pagination¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	Limit int
}

// StoriesGetStoryViewsListRequestTypeID is TL type id of StoriesGetStoryViewsListRequest.
const StoriesGetStoryViewsListRequestTypeID = 0x7ed23c57

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

func (g *StoriesGetStoryViewsListRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Flags.Zero()) {
		return false
	}
	if !(g.JustContacts == false) {
		return false
	}
	if !(g.ReactionsFirst == false) {
		return false
	}
	if !(g.ForwardsFirst == false) {
		return false
	}
	if !(g.Peer == nil) {
		return false
	}
	if !(g.Q == "") {
		return false
	}
	if !(g.ID == 0) {
		return false
	}
	if !(g.Offset == "") {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesGetStoryViewsListRequest from given interface.
func (g *StoriesGetStoryViewsListRequest) FillFrom(from interface {
	GetJustContacts() (value bool)
	GetReactionsFirst() (value bool)
	GetForwardsFirst() (value bool)
	GetPeer() (value InputPeerClass)
	GetQ() (value string, ok bool)
	GetID() (value int)
	GetOffset() (value string)
	GetLimit() (value int)
}) {
	g.JustContacts = from.GetJustContacts()
	g.ReactionsFirst = from.GetReactionsFirst()
	g.ForwardsFirst = from.GetForwardsFirst()
	g.Peer = from.GetPeer()
	if val, ok := from.GetQ(); ok {
		g.Q = val
	}

	g.ID = from.GetID()
	g.Offset = from.GetOffset()
	g.Limit = from.GetLimit()
}

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

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

// TypeInfo returns info about TL type.
func (g *StoriesGetStoryViewsListRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.getStoryViewsList",
		ID:   StoriesGetStoryViewsListRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "JustContacts",
			SchemaName: "just_contacts",
			Null:       !g.Flags.Has(0),
		},
		{
			Name:       "ReactionsFirst",
			SchemaName: "reactions_first",
			Null:       !g.Flags.Has(2),
		},
		{
			Name:       "ForwardsFirst",
			SchemaName: "forwards_first",
			Null:       !g.Flags.Has(3),
		},
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "Q",
			SchemaName: "q",
			Null:       !g.Flags.Has(1),
		},
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (g *StoriesGetStoryViewsListRequest) SetFlags() {
	if !(g.JustContacts == false) {
		g.Flags.Set(0)
	}
	if !(g.ReactionsFirst == false) {
		g.Flags.Set(2)
	}
	if !(g.ForwardsFirst == false) {
		g.Flags.Set(3)
	}
	if !(g.Q == "") {
		g.Flags.Set(1)
	}
}

// Encode implements bin.Encoder.
func (g *StoriesGetStoryViewsListRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stories.getStoryViewsList#7ed23c57 as nil")
	}
	b.PutID(StoriesGetStoryViewsListRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *StoriesGetStoryViewsListRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode stories.getStoryViewsList#7ed23c57 as nil")
	}
	g.SetFlags()
	if err := g.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.getStoryViewsList#7ed23c57: field flags: %w", err)
	}
	if g.Peer == nil {
		return fmt.Errorf("unable to encode stories.getStoryViewsList#7ed23c57: field peer is nil")
	}
	if err := g.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.getStoryViewsList#7ed23c57: field peer: %w", err)
	}
	if g.Flags.Has(1) {
		b.PutString(g.Q)
	}
	b.PutInt(g.ID)
	b.PutString(g.Offset)
	b.PutInt(g.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (g *StoriesGetStoryViewsListRequest) Decode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode stories.getStoryViewsList#7ed23c57 to nil")
	}
	if err := b.ConsumeID(StoriesGetStoryViewsListRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode stories.getStoryViewsList#7ed23c57: %w", err)
	}
	return g.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (g *StoriesGetStoryViewsListRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode stories.getStoryViewsList#7ed23c57 to nil")
	}
	{
		if err := g.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode stories.getStoryViewsList#7ed23c57: field flags: %w", err)
		}
	}
	g.JustContacts = g.Flags.Has(0)
	g.ReactionsFirst = g.Flags.Has(2)
	g.ForwardsFirst = g.Flags.Has(3)
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode stories.getStoryViewsList#7ed23c57: field peer: %w", err)
		}
		g.Peer = value
	}
	if g.Flags.Has(1) {
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode stories.getStoryViewsList#7ed23c57: field q: %w", err)
		}
		g.Q = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode stories.getStoryViewsList#7ed23c57: field id: %w", err)
		}
		g.ID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode stories.getStoryViewsList#7ed23c57: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode stories.getStoryViewsList#7ed23c57: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// SetJustContacts sets value of JustContacts conditional field.
func (g *StoriesGetStoryViewsListRequest) SetJustContacts(value bool) {
	if value {
		g.Flags.Set(0)
		g.JustContacts = true
	} else {
		g.Flags.Unset(0)
		g.JustContacts = false
	}
}

// GetJustContacts returns value of JustContacts conditional field.
func (g *StoriesGetStoryViewsListRequest) GetJustContacts() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(0)
}

// SetReactionsFirst sets value of ReactionsFirst conditional field.
func (g *StoriesGetStoryViewsListRequest) SetReactionsFirst(value bool) {
	if value {
		g.Flags.Set(2)
		g.ReactionsFirst = true
	} else {
		g.Flags.Unset(2)
		g.ReactionsFirst = false
	}
}

// GetReactionsFirst returns value of ReactionsFirst conditional field.
func (g *StoriesGetStoryViewsListRequest) GetReactionsFirst() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(2)
}

// SetForwardsFirst sets value of ForwardsFirst conditional field.
func (g *StoriesGetStoryViewsListRequest) SetForwardsFirst(value bool) {
	if value {
		g.Flags.Set(3)
		g.ForwardsFirst = true
	} else {
		g.Flags.Unset(3)
		g.ForwardsFirst = false
	}
}

// GetForwardsFirst returns value of ForwardsFirst conditional field.
func (g *StoriesGetStoryViewsListRequest) GetForwardsFirst() (value bool) {
	if g == nil {
		return
	}
	return g.Flags.Has(3)
}

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

// SetQ sets value of Q conditional field.
func (g *StoriesGetStoryViewsListRequest) SetQ(value string) {
	g.Flags.Set(1)
	g.Q = value
}

// GetQ returns value of Q conditional field and
// boolean which is true if field was set.
func (g *StoriesGetStoryViewsListRequest) GetQ() (value string, ok bool) {
	if g == nil {
		return
	}
	if !g.Flags.Has(1) {
		return value, false
	}
	return g.Q, true
}

// GetID returns value of ID field.
func (g *StoriesGetStoryViewsListRequest) GetID() (value int) {
	if g == nil {
		return
	}
	return g.ID
}

// GetOffset returns value of Offset field.
func (g *StoriesGetStoryViewsListRequest) GetOffset() (value string) {
	if g == nil {
		return
	}
	return g.Offset
}

// GetLimit returns value of Limit field.
func (g *StoriesGetStoryViewsListRequest) GetLimit() (value int) {
	if g == nil {
		return
	}
	return g.Limit
}

// StoriesGetStoryViewsList invokes method stories.getStoryViewsList#7ed23c57 returning error if any.
// Obtain the list of users that have viewed a specific story we posted¹
//
// Links:
//  1. https://core.telegram.org/api/stories
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//	400 STORY_ID_INVALID: The specified story ID is invalid.
//
// See https://core.telegram.org/method/stories.getStoryViewsList for reference.
func (c *Client) StoriesGetStoryViewsList(ctx context.Context, request *StoriesGetStoryViewsListRequest) (*StoriesStoryViewsList, error) {
	var result StoriesStoryViewsList

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