// Code generated by gotdgen, DO NOT EDIT.

package tg

import (
	"context"
	"errors"
	"fmt"
	"sort"
	"strings"

	"go.uber.org/multierr"

	"github.com/gotd/td/bin"
	"github.com/gotd/td/tdjson"
	"github.com/gotd/td/tdp"
	"github.com/gotd/td/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{}
)

// ChannelsDeactivateAllUsernamesRequest represents TL type `channels.deactivateAllUsernames#a245dd3`.
// Disable all purchased usernames of a supergroup or channel
//
// See https://core.telegram.org/method/channels.deactivateAllUsernames for reference.
type ChannelsDeactivateAllUsernamesRequest struct {
	// Supergroup or channel
	Channel InputChannelClass
}

// ChannelsDeactivateAllUsernamesRequestTypeID is TL type id of ChannelsDeactivateAllUsernamesRequest.
const ChannelsDeactivateAllUsernamesRequestTypeID = 0xa245dd3

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

func (d *ChannelsDeactivateAllUsernamesRequest) Zero() bool {
	if d == nil {
		return true
	}
	if !(d.Channel == nil) {
		return false
	}

	return true
}

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

// FillFrom fills ChannelsDeactivateAllUsernamesRequest from given interface.
func (d *ChannelsDeactivateAllUsernamesRequest) FillFrom(from interface {
	GetChannel() (value InputChannelClass)
}) {
	d.Channel = from.GetChannel()
}

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

// TypeName returns name of type in TL schema.
func (*ChannelsDeactivateAllUsernamesRequest) TypeName() string {
	return "channels.deactivateAllUsernames"
}

// TypeInfo returns info about TL type.
func (d *ChannelsDeactivateAllUsernamesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "channels.deactivateAllUsernames",
		ID:   ChannelsDeactivateAllUsernamesRequestTypeID,
	}
	if d == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Channel",
			SchemaName: "channel",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (d *ChannelsDeactivateAllUsernamesRequest) Encode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode channels.deactivateAllUsernames#a245dd3 as nil")
	}
	b.PutID(ChannelsDeactivateAllUsernamesRequestTypeID)
	return d.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (d *ChannelsDeactivateAllUsernamesRequest) EncodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't encode channels.deactivateAllUsernames#a245dd3 as nil")
	}
	if d.Channel == nil {
		return fmt.Errorf("unable to encode channels.deactivateAllUsernames#a245dd3: field channel is nil")
	}
	if err := d.Channel.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.deactivateAllUsernames#a245dd3: field channel: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (d *ChannelsDeactivateAllUsernamesRequest) Decode(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode channels.deactivateAllUsernames#a245dd3 to nil")
	}
	if err := b.ConsumeID(ChannelsDeactivateAllUsernamesRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode channels.deactivateAllUsernames#a245dd3: %w", err)
	}
	return d.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (d *ChannelsDeactivateAllUsernamesRequest) DecodeBare(b *bin.Buffer) error {
	if d == nil {
		return fmt.Errorf("can't decode channels.deactivateAllUsernames#a245dd3 to nil")
	}
	{
		value, err := DecodeInputChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.deactivateAllUsernames#a245dd3: field channel: %w", err)
		}
		d.Channel = value
	}
	return nil
}

// GetChannel returns value of Channel field.
func (d *ChannelsDeactivateAllUsernamesRequest) GetChannel() (value InputChannelClass) {
	if d == nil {
		return
	}
	return d.Channel
}

// GetChannelAsNotEmpty returns mapped value of Channel field.
func (d *ChannelsDeactivateAllUsernamesRequest) GetChannelAsNotEmpty() (NotEmptyInputChannel, bool) {
	return d.Channel.AsNotEmpty()
}

// ChannelsDeactivateAllUsernames invokes method channels.deactivateAllUsernames#a245dd3 returning error if any.
// Disable all purchased usernames of a supergroup or channel
//
// See https://core.telegram.org/method/channels.deactivateAllUsernames for reference.
// Can be used by bots.
func (c *Client) ChannelsDeactivateAllUsernames(ctx context.Context, channel InputChannelClass) (bool, error) {
	var result BoolBox

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