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

// SetMessageSenderBlockListRequest represents TL type `setMessageSenderBlockList#898b5c91`.
type SetMessageSenderBlockListRequest struct {
	// Identifier of a message sender to block/unblock
	SenderID MessageSenderClass
	// New block list for the message sender; pass null to unblock the message sender
	BlockList BlockListClass
}

// SetMessageSenderBlockListRequestTypeID is TL type id of SetMessageSenderBlockListRequest.
const SetMessageSenderBlockListRequestTypeID = 0x898b5c91

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

func (s *SetMessageSenderBlockListRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.SenderID == nil) {
		return false
	}
	if !(s.BlockList == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetMessageSenderBlockListRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setMessageSenderBlockList",
		ID:   SetMessageSenderBlockListRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SenderID",
			SchemaName: "sender_id",
		},
		{
			Name:       "BlockList",
			SchemaName: "block_list",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetMessageSenderBlockListRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setMessageSenderBlockList#898b5c91 as nil")
	}
	b.PutID(SetMessageSenderBlockListRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetMessageSenderBlockListRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setMessageSenderBlockList#898b5c91 as nil")
	}
	if s.SenderID == nil {
		return fmt.Errorf("unable to encode setMessageSenderBlockList#898b5c91: field sender_id is nil")
	}
	if err := s.SenderID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setMessageSenderBlockList#898b5c91: field sender_id: %w", err)
	}
	if s.BlockList == nil {
		return fmt.Errorf("unable to encode setMessageSenderBlockList#898b5c91: field block_list is nil")
	}
	if err := s.BlockList.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setMessageSenderBlockList#898b5c91: field block_list: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (s *SetMessageSenderBlockListRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setMessageSenderBlockList#898b5c91 to nil")
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode setMessageSenderBlockList#898b5c91: field sender_id: %w", err)
		}
		s.SenderID = value
	}
	{
		value, err := DecodeBlockList(b)
		if err != nil {
			return fmt.Errorf("unable to decode setMessageSenderBlockList#898b5c91: field block_list: %w", err)
		}
		s.BlockList = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetMessageSenderBlockListRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setMessageSenderBlockList#898b5c91 as nil")
	}
	b.ObjStart()
	b.PutID("setMessageSenderBlockList")
	b.Comma()
	b.FieldStart("sender_id")
	if s.SenderID == nil {
		return fmt.Errorf("unable to encode setMessageSenderBlockList#898b5c91: field sender_id is nil")
	}
	if err := s.SenderID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setMessageSenderBlockList#898b5c91: field sender_id: %w", err)
	}
	b.Comma()
	b.FieldStart("block_list")
	if s.BlockList == nil {
		return fmt.Errorf("unable to encode setMessageSenderBlockList#898b5c91: field block_list is nil")
	}
	if err := s.BlockList.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setMessageSenderBlockList#898b5c91: field block_list: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setMessageSenderBlockList"); err != nil {
				return fmt.Errorf("unable to decode setMessageSenderBlockList#898b5c91: %w", err)
			}
		case "sender_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode setMessageSenderBlockList#898b5c91: field sender_id: %w", err)
			}
			s.SenderID = value
		case "block_list":
			value, err := DecodeTDLibJSONBlockList(b)
			if err != nil {
				return fmt.Errorf("unable to decode setMessageSenderBlockList#898b5c91: field block_list: %w", err)
			}
			s.BlockList = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSenderID returns value of SenderID field.
func (s *SetMessageSenderBlockListRequest) GetSenderID() (value MessageSenderClass) {
	if s == nil {
		return
	}
	return s.SenderID
}

// GetBlockList returns value of BlockList field.
func (s *SetMessageSenderBlockListRequest) GetBlockList() (value BlockListClass) {
	if s == nil {
		return
	}
	return s.BlockList
}

// SetMessageSenderBlockList invokes method setMessageSenderBlockList#898b5c91 returning error if any.
func (c *Client) SetMessageSenderBlockList(ctx context.Context, request *SetMessageSenderBlockListRequest) error {
	var ok Ok

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