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

// SetChatMemberStatusRequest represents TL type `setChatMemberStatus#4e0171f`.
type SetChatMemberStatusRequest struct {
	// Chat identifier
	ChatID int64
	// Member identifier. Chats can be only banned and unbanned in supergroups and channels
	MemberID MessageSenderClass
	// The new status of the member in the chat
	Status ChatMemberStatusClass
}

// SetChatMemberStatusRequestTypeID is TL type id of SetChatMemberStatusRequest.
const SetChatMemberStatusRequestTypeID = 0x4e0171f

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

func (s *SetChatMemberStatusRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ChatID == 0) {
		return false
	}
	if !(s.MemberID == nil) {
		return false
	}
	if !(s.Status == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetChatMemberStatusRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setChatMemberStatus",
		ID:   SetChatMemberStatusRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "MemberID",
			SchemaName: "member_id",
		},
		{
			Name:       "Status",
			SchemaName: "status",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetChatMemberStatusRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatMemberStatus#4e0171f as nil")
	}
	b.PutID(SetChatMemberStatusRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetChatMemberStatusRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatMemberStatus#4e0171f as nil")
	}
	b.PutInt53(s.ChatID)
	if s.MemberID == nil {
		return fmt.Errorf("unable to encode setChatMemberStatus#4e0171f: field member_id is nil")
	}
	if err := s.MemberID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setChatMemberStatus#4e0171f: field member_id: %w", err)
	}
	if s.Status == nil {
		return fmt.Errorf("unable to encode setChatMemberStatus#4e0171f: field status is nil")
	}
	if err := s.Status.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setChatMemberStatus#4e0171f: field status: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetChatMemberStatusRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setChatMemberStatus#4e0171f to nil")
	}
	if err := b.ConsumeID(SetChatMemberStatusRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setChatMemberStatus#4e0171f: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetChatMemberStatusRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setChatMemberStatus#4e0171f to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setChatMemberStatus#4e0171f: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		value, err := DecodeMessageSender(b)
		if err != nil {
			return fmt.Errorf("unable to decode setChatMemberStatus#4e0171f: field member_id: %w", err)
		}
		s.MemberID = value
	}
	{
		value, err := DecodeChatMemberStatus(b)
		if err != nil {
			return fmt.Errorf("unable to decode setChatMemberStatus#4e0171f: field status: %w", err)
		}
		s.Status = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetChatMemberStatusRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatMemberStatus#4e0171f as nil")
	}
	b.ObjStart()
	b.PutID("setChatMemberStatus")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("member_id")
	if s.MemberID == nil {
		return fmt.Errorf("unable to encode setChatMemberStatus#4e0171f: field member_id is nil")
	}
	if err := s.MemberID.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setChatMemberStatus#4e0171f: field member_id: %w", err)
	}
	b.Comma()
	b.FieldStart("status")
	if s.Status == nil {
		return fmt.Errorf("unable to encode setChatMemberStatus#4e0171f: field status is nil")
	}
	if err := s.Status.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setChatMemberStatus#4e0171f: field status: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetChatMemberStatusRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setChatMemberStatus#4e0171f to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setChatMemberStatus"); err != nil {
				return fmt.Errorf("unable to decode setChatMemberStatus#4e0171f: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setChatMemberStatus#4e0171f: field chat_id: %w", err)
			}
			s.ChatID = value
		case "member_id":
			value, err := DecodeTDLibJSONMessageSender(b)
			if err != nil {
				return fmt.Errorf("unable to decode setChatMemberStatus#4e0171f: field member_id: %w", err)
			}
			s.MemberID = value
		case "status":
			value, err := DecodeTDLibJSONChatMemberStatus(b)
			if err != nil {
				return fmt.Errorf("unable to decode setChatMemberStatus#4e0171f: field status: %w", err)
			}
			s.Status = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (s *SetChatMemberStatusRequest) GetChatID() (value int64) {
	if s == nil {
		return
	}
	return s.ChatID
}

// GetMemberID returns value of MemberID field.
func (s *SetChatMemberStatusRequest) GetMemberID() (value MessageSenderClass) {
	if s == nil {
		return
	}
	return s.MemberID
}

// GetStatus returns value of Status field.
func (s *SetChatMemberStatusRequest) GetStatus() (value ChatMemberStatusClass) {
	if s == nil {
		return
	}
	return s.Status
}

// SetChatMemberStatus invokes method setChatMemberStatus#4e0171f returning error if any.
func (c *Client) SetChatMemberStatus(ctx context.Context, request *SetChatMemberStatusRequest) error {
	var ok Ok

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