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

// SetChatPermissionsRequest represents TL type `setChatPermissions#7f7706fe`.
type SetChatPermissionsRequest struct {
	// Chat identifier
	ChatID int64
	// New non-administrator members permissions in the chat
	Permissions ChatPermissions
}

// SetChatPermissionsRequestTypeID is TL type id of SetChatPermissionsRequest.
const SetChatPermissionsRequestTypeID = 0x7f7706fe

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

func (s *SetChatPermissionsRequest) Zero() bool {
	if s == nil {
		return true
	}
	if !(s.ChatID == 0) {
		return false
	}
	if !(s.Permissions.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SetChatPermissionsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "setChatPermissions",
		ID:   SetChatPermissionsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "Permissions",
			SchemaName: "permissions",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SetChatPermissionsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatPermissions#7f7706fe as nil")
	}
	b.PutID(SetChatPermissionsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SetChatPermissionsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatPermissions#7f7706fe as nil")
	}
	b.PutInt53(s.ChatID)
	if err := s.Permissions.Encode(b); err != nil {
		return fmt.Errorf("unable to encode setChatPermissions#7f7706fe: field permissions: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (s *SetChatPermissionsRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setChatPermissions#7f7706fe to nil")
	}
	if err := b.ConsumeID(SetChatPermissionsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode setChatPermissions#7f7706fe: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SetChatPermissionsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode setChatPermissions#7f7706fe to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode setChatPermissions#7f7706fe: field chat_id: %w", err)
		}
		s.ChatID = value
	}
	{
		if err := s.Permissions.Decode(b); err != nil {
			return fmt.Errorf("unable to decode setChatPermissions#7f7706fe: field permissions: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (s *SetChatPermissionsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if s == nil {
		return fmt.Errorf("can't encode setChatPermissions#7f7706fe as nil")
	}
	b.ObjStart()
	b.PutID("setChatPermissions")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(s.ChatID)
	b.Comma()
	b.FieldStart("permissions")
	if err := s.Permissions.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode setChatPermissions#7f7706fe: field permissions: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SetChatPermissionsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode setChatPermissions#7f7706fe to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("setChatPermissions"); err != nil {
				return fmt.Errorf("unable to decode setChatPermissions#7f7706fe: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode setChatPermissions#7f7706fe: field chat_id: %w", err)
			}
			s.ChatID = value
		case "permissions":
			if err := s.Permissions.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode setChatPermissions#7f7706fe: field permissions: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

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

// GetPermissions returns value of Permissions field.
func (s *SetChatPermissionsRequest) GetPermissions() (value ChatPermissions) {
	if s == nil {
		return
	}
	return s.Permissions
}

// SetChatPermissions invokes method setChatPermissions#7f7706fe returning error if any.
func (c *Client) SetChatPermissions(ctx context.Context, request *SetChatPermissionsRequest) error {
	var ok Ok

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