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

// ContactsSetBlockedRequest represents TL type `contacts.setBlocked#94c65c76`.
// Replace the contents of an entire blocklist, see here for more info »¹.
//
// Links:
//  1. https://core.telegram.org/api/block
//
// See https://core.telegram.org/method/contacts.setBlocked for reference.
type ContactsSetBlockedRequest struct {
	// Flags, see TL conditional fields¹
	//
	// Links:
	//  1) https://core.telegram.org/mtproto/TL-combinators#conditional-fields
	Flags bin.Fields
	// Whether to edit the story blocklist; if not set, will edit the main blocklist. See
	// here »¹ for differences between the two.
	//
	// Links:
	//  1) https://core.telegram.org/api/block
	MyStoriesFrom bool
	// Full content of the blocklist.
	ID []InputPeerClass
	// Maximum number of results to return, see pagination¹
	//
	// Links:
	//  1) https://core.telegram.org/api/offsets
	Limit int
}

// ContactsSetBlockedRequestTypeID is TL type id of ContactsSetBlockedRequest.
const ContactsSetBlockedRequestTypeID = 0x94c65c76

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

func (s *ContactsSetBlockedRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.Flags.Zero()) {
		return false
	}
	if !(s.MyStoriesFrom == false) {
		return false
	}
	if !(s.ID == nil) {
		return false
	}
	if !(s.Limit == 0) {
		return false
	}

	return true
}

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

// FillFrom fills ContactsSetBlockedRequest from given interface.
func (s *ContactsSetBlockedRequest) FillFrom(from interface {
	GetMyStoriesFrom() (value bool)
	GetID() (value []InputPeerClass)
	GetLimit() (value int)
}) {
	s.MyStoriesFrom = from.GetMyStoriesFrom()
	s.ID = from.GetID()
	s.Limit = from.GetLimit()
}

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

// TypeName returns name of type in TL schema.
func (*ContactsSetBlockedRequest) TypeName() string {
	return "contacts.setBlocked"
}

// TypeInfo returns info about TL type.
func (s *ContactsSetBlockedRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "contacts.setBlocked",
		ID:   ContactsSetBlockedRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "MyStoriesFrom",
			SchemaName: "my_stories_from",
			Null:       !s.Flags.Has(0),
		},
		{
			Name:       "ID",
			SchemaName: "id",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (s *ContactsSetBlockedRequest) SetFlags() {
	if !(s.MyStoriesFrom == false) {
		s.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (s *ContactsSetBlockedRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode contacts.setBlocked#94c65c76 as nil")
	}
	b.PutID(ContactsSetBlockedRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *ContactsSetBlockedRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode contacts.setBlocked#94c65c76 as nil")
	}
	s.SetFlags()
	if err := s.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode contacts.setBlocked#94c65c76: field flags: %w", err)
	}
	b.PutVectorHeader(len(s.ID))
	for idx, v := range s.ID {
		if v == nil {
			return fmt.Errorf("unable to encode contacts.setBlocked#94c65c76: field id element with index %d is nil", idx)
		}
		if err := v.Encode(b); err != nil {
			return fmt.Errorf("unable to encode contacts.setBlocked#94c65c76: field id element with index %d: %w", idx, err)
		}
	}
	b.PutInt(s.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (s *ContactsSetBlockedRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode contacts.setBlocked#94c65c76 to nil")
	}
	if err := b.ConsumeID(ContactsSetBlockedRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode contacts.setBlocked#94c65c76: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *ContactsSetBlockedRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode contacts.setBlocked#94c65c76 to nil")
	}
	{
		if err := s.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode contacts.setBlocked#94c65c76: field flags: %w", err)
		}
	}
	s.MyStoriesFrom = s.Flags.Has(0)
	{
		headerLen, err := b.VectorHeader()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.setBlocked#94c65c76: field id: %w", err)
		}

		if headerLen > 0 {
			s.ID = make([]InputPeerClass, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			value, err := DecodeInputPeer(b)
			if err != nil {
				return fmt.Errorf("unable to decode contacts.setBlocked#94c65c76: field id: %w", err)
			}
			s.ID = append(s.ID, value)
		}
	}
	{
		value, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode contacts.setBlocked#94c65c76: field limit: %w", err)
		}
		s.Limit = value
	}
	return nil
}

// SetMyStoriesFrom sets value of MyStoriesFrom conditional field.
func (s *ContactsSetBlockedRequest) SetMyStoriesFrom(value bool) {
	if value {
		s.Flags.Set(0)
		s.MyStoriesFrom = true
	} else {
		s.Flags.Unset(0)
		s.MyStoriesFrom = false
	}
}

// GetMyStoriesFrom returns value of MyStoriesFrom conditional field.
func (s *ContactsSetBlockedRequest) GetMyStoriesFrom() (value bool) {
	if s == nil {
		return
	}
	return s.Flags.Has(0)
}

// GetID returns value of ID field.
func (s *ContactsSetBlockedRequest) GetID() (value []InputPeerClass) {
	if s == nil {
		return
	}
	return s.ID
}

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

// MapID returns field ID wrapped in InputPeerClassArray helper.
func (s *ContactsSetBlockedRequest) MapID() (value InputPeerClassArray) {
	return InputPeerClassArray(s.ID)
}

// ContactsSetBlocked invokes method contacts.setBlocked#94c65c76 returning error if any.
// Replace the contents of an entire blocklist, see here for more info »¹.
//
// Links:
//  1. https://core.telegram.org/api/block
//
// See https://core.telegram.org/method/contacts.setBlocked for reference.
func (c *Client) ContactsSetBlocked(ctx context.Context, request *ContactsSetBlockedRequest) (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
}
