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

// StoriesIncrementStoryViewsRequest represents TL type `stories.incrementStoryViews#b2028afb`.
// Increment the view counter of one or more stories.
//
// See https://core.telegram.org/method/stories.incrementStoryViews for reference.
type StoriesIncrementStoryViewsRequest struct {
	// Peer where the stories were posted.
	Peer InputPeerClass
	// IDs of the stories (maximum 200 at a time).
	ID []int
}

// StoriesIncrementStoryViewsRequestTypeID is TL type id of StoriesIncrementStoryViewsRequest.
const StoriesIncrementStoryViewsRequestTypeID = 0xb2028afb

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

func (i *StoriesIncrementStoryViewsRequest) Zero() bool {
	if i == nil {
		return true
	}
	if !(i.Peer == nil) {
		return false
	}
	if !(i.ID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills StoriesIncrementStoryViewsRequest from given interface.
func (i *StoriesIncrementStoryViewsRequest) FillFrom(from interface {
	GetPeer() (value InputPeerClass)
	GetID() (value []int)
}) {
	i.Peer = from.GetPeer()
	i.ID = from.GetID()
}

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

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

// TypeInfo returns info about TL type.
func (i *StoriesIncrementStoryViewsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "stories.incrementStoryViews",
		ID:   StoriesIncrementStoryViewsRequestTypeID,
	}
	if i == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Peer",
			SchemaName: "peer",
		},
		{
			Name:       "ID",
			SchemaName: "id",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (i *StoriesIncrementStoryViewsRequest) Encode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode stories.incrementStoryViews#b2028afb as nil")
	}
	b.PutID(StoriesIncrementStoryViewsRequestTypeID)
	return i.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (i *StoriesIncrementStoryViewsRequest) EncodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't encode stories.incrementStoryViews#b2028afb as nil")
	}
	if i.Peer == nil {
		return fmt.Errorf("unable to encode stories.incrementStoryViews#b2028afb: field peer is nil")
	}
	if err := i.Peer.Encode(b); err != nil {
		return fmt.Errorf("unable to encode stories.incrementStoryViews#b2028afb: field peer: %w", err)
	}
	b.PutVectorHeader(len(i.ID))
	for _, v := range i.ID {
		b.PutInt(v)
	}
	return nil
}

// Decode implements bin.Decoder.
func (i *StoriesIncrementStoryViewsRequest) Decode(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode stories.incrementStoryViews#b2028afb to nil")
	}
	if err := b.ConsumeID(StoriesIncrementStoryViewsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode stories.incrementStoryViews#b2028afb: %w", err)
	}
	return i.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (i *StoriesIncrementStoryViewsRequest) DecodeBare(b *bin.Buffer) error {
	if i == nil {
		return fmt.Errorf("can't decode stories.incrementStoryViews#b2028afb to nil")
	}
	{
		value, err := DecodeInputPeer(b)
		if err != nil {
			return fmt.Errorf("unable to decode stories.incrementStoryViews#b2028afb: field peer: %w", err)
		}
		i.Peer = value
	}
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode stories.incrementStoryViews#b2028afb: field id: %w", err)
		}

		if headerLen > 0 {
			i.ID = make([]int, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := b.Int()
			if err != nil {
				return fmt.Errorf("unable to decode stories.incrementStoryViews#b2028afb: field id: %w", err)
			}
			i.ID = append(i.ID, value)
		}
	}
	return nil
}

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

// GetID returns value of ID field.
func (i *StoriesIncrementStoryViewsRequest) GetID() (value []int) {
	if i == nil {
		return
	}
	return i.ID
}

// StoriesIncrementStoryViews invokes method stories.incrementStoryViews#b2028afb returning error if any.
// Increment the view counter of one or more stories.
//
// Possible errors:
//
//	400 PEER_ID_INVALID: The provided peer id is invalid.
//	400 STORY_ID_EMPTY: You specified no story IDs.
//
// See https://core.telegram.org/method/stories.incrementStoryViews for reference.
func (c *Client) StoriesIncrementStoryViews(ctx context.Context, request *StoriesIncrementStoryViewsRequest) (bool, error) {
	var result BoolBox

	if err := c.rpc.Invoke(ctx, request, &result); err != nil {
		return false, err
	}
	_, ok := result.Bool.(*BoolTrue)
	return ok, nil
}
