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

// ChannelsToggleUsernameRequest represents TL type `channels.toggleUsername#50f24105`.
// Activate or deactivate a purchased fragment.com¹ username associated to a supergroup
// or channel² we own.
//
// Links:
//  1. https://fragment.com
//  2. https://core.telegram.org/api/channel
//
// See https://core.telegram.org/method/channels.toggleUsername for reference.
type ChannelsToggleUsernameRequest struct {
	// Supergroup or channel¹
	//
	// Links:
	//  1) https://core.telegram.org/api/channel
	Channel InputChannelClass
	// Username
	Username string
	// Whether to activate or deactivate the username
	Active bool
}

// ChannelsToggleUsernameRequestTypeID is TL type id of ChannelsToggleUsernameRequest.
const ChannelsToggleUsernameRequestTypeID = 0x50f24105

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

func (t *ChannelsToggleUsernameRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.Channel == nil) {
		return false
	}
	if !(t.Username == "") {
		return false
	}
	if !(t.Active == false) {
		return false
	}

	return true
}

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

// FillFrom fills ChannelsToggleUsernameRequest from given interface.
func (t *ChannelsToggleUsernameRequest) FillFrom(from interface {
	GetChannel() (value InputChannelClass)
	GetUsername() (value string)
	GetActive() (value bool)
}) {
	t.Channel = from.GetChannel()
	t.Username = from.GetUsername()
	t.Active = from.GetActive()
}

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

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

// TypeInfo returns info about TL type.
func (t *ChannelsToggleUsernameRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "channels.toggleUsername",
		ID:   ChannelsToggleUsernameRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Channel",
			SchemaName: "channel",
		},
		{
			Name:       "Username",
			SchemaName: "username",
		},
		{
			Name:       "Active",
			SchemaName: "active",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *ChannelsToggleUsernameRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode channels.toggleUsername#50f24105 as nil")
	}
	b.PutID(ChannelsToggleUsernameRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *ChannelsToggleUsernameRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode channels.toggleUsername#50f24105 as nil")
	}
	if t.Channel == nil {
		return fmt.Errorf("unable to encode channels.toggleUsername#50f24105: field channel is nil")
	}
	if err := t.Channel.Encode(b); err != nil {
		return fmt.Errorf("unable to encode channels.toggleUsername#50f24105: field channel: %w", err)
	}
	b.PutString(t.Username)
	b.PutBool(t.Active)
	return nil
}

// Decode implements bin.Decoder.
func (t *ChannelsToggleUsernameRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode channels.toggleUsername#50f24105 to nil")
	}
	if err := b.ConsumeID(ChannelsToggleUsernameRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode channels.toggleUsername#50f24105: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *ChannelsToggleUsernameRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode channels.toggleUsername#50f24105 to nil")
	}
	{
		value, err := DecodeInputChannel(b)
		if err != nil {
			return fmt.Errorf("unable to decode channels.toggleUsername#50f24105: field channel: %w", err)
		}
		t.Channel = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode channels.toggleUsername#50f24105: field username: %w", err)
		}
		t.Username = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode channels.toggleUsername#50f24105: field active: %w", err)
		}
		t.Active = value
	}
	return nil
}

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

// GetUsername returns value of Username field.
func (t *ChannelsToggleUsernameRequest) GetUsername() (value string) {
	if t == nil {
		return
	}
	return t.Username
}

// GetActive returns value of Active field.
func (t *ChannelsToggleUsernameRequest) GetActive() (value bool) {
	if t == nil {
		return
	}
	return t.Active
}

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

// ChannelsToggleUsername invokes method channels.toggleUsername#50f24105 returning error if any.
// Activate or deactivate a purchased fragment.com¹ username associated to a supergroup
// or channel² we own.
//
// Links:
//  1. https://fragment.com
//  2. https://core.telegram.org/api/channel
//
// Possible errors:
//
//	400 CHANNEL_INVALID: The provided channel is invalid.
//	400 CHANNEL_PRIVATE: You haven't joined this channel/supergroup.
//	400 CHAT_ADMIN_REQUIRED: You must be an admin in this chat to do this.
//	400 CHAT_NOT_MODIFIED: No changes were made to chat information because the new information you passed is identical to the current information.
//	400 USERNAMES_ACTIVE_TOO_MUCH: The maximum number of active usernames was reached.
//	400 USERNAME_INVALID: The provided username is not valid.
//	400 USERNAME_NOT_MODIFIED: The username was not modified.
//
// See https://core.telegram.org/method/channels.toggleUsername for reference.
func (c *Client) ChannelsToggleUsername(ctx context.Context, request *ChannelsToggleUsernameRequest) (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
}
