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

// ToggleSupergroupUsernameIsActiveRequest represents TL type `toggleSupergroupUsernameIsActive#a68b6dff`.
type ToggleSupergroupUsernameIsActiveRequest struct {
	// Identifier of the supergroup or channel
	SupergroupID int64
	// The username to change
	Username string
	// Pass true to activate the username; pass false to disable it
	IsActive bool
}

// ToggleSupergroupUsernameIsActiveRequestTypeID is TL type id of ToggleSupergroupUsernameIsActiveRequest.
const ToggleSupergroupUsernameIsActiveRequestTypeID = 0xa68b6dff

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

func (t *ToggleSupergroupUsernameIsActiveRequest) Zero() bool {
	if t == nil {
		return true
	}
	if !(t.SupergroupID == 0) {
		return false
	}
	if !(t.Username == "") {
		return false
	}
	if !(t.IsActive == false) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (t *ToggleSupergroupUsernameIsActiveRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "toggleSupergroupUsernameIsActive",
		ID:   ToggleSupergroupUsernameIsActiveRequestTypeID,
	}
	if t == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "SupergroupID",
			SchemaName: "supergroup_id",
		},
		{
			Name:       "Username",
			SchemaName: "username",
		},
		{
			Name:       "IsActive",
			SchemaName: "is_active",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (t *ToggleSupergroupUsernameIsActiveRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSupergroupUsernameIsActive#a68b6dff as nil")
	}
	b.PutID(ToggleSupergroupUsernameIsActiveRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *ToggleSupergroupUsernameIsActiveRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSupergroupUsernameIsActive#a68b6dff as nil")
	}
	b.PutInt53(t.SupergroupID)
	b.PutString(t.Username)
	b.PutBool(t.IsActive)
	return nil
}

// Decode implements bin.Decoder.
func (t *ToggleSupergroupUsernameIsActiveRequest) Decode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleSupergroupUsernameIsActive#a68b6dff to nil")
	}
	if err := b.ConsumeID(ToggleSupergroupUsernameIsActiveRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode toggleSupergroupUsernameIsActive#a68b6dff: %w", err)
	}
	return t.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (t *ToggleSupergroupUsernameIsActiveRequest) DecodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleSupergroupUsernameIsActive#a68b6dff to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode toggleSupergroupUsernameIsActive#a68b6dff: field supergroup_id: %w", err)
		}
		t.SupergroupID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode toggleSupergroupUsernameIsActive#a68b6dff: field username: %w", err)
		}
		t.Username = value
	}
	{
		value, err := b.Bool()
		if err != nil {
			return fmt.Errorf("unable to decode toggleSupergroupUsernameIsActive#a68b6dff: field is_active: %w", err)
		}
		t.IsActive = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *ToggleSupergroupUsernameIsActiveRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleSupergroupUsernameIsActive#a68b6dff as nil")
	}
	b.ObjStart()
	b.PutID("toggleSupergroupUsernameIsActive")
	b.Comma()
	b.FieldStart("supergroup_id")
	b.PutInt53(t.SupergroupID)
	b.Comma()
	b.FieldStart("username")
	b.PutString(t.Username)
	b.Comma()
	b.FieldStart("is_active")
	b.PutBool(t.IsActive)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (t *ToggleSupergroupUsernameIsActiveRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleSupergroupUsernameIsActive#a68b6dff to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("toggleSupergroupUsernameIsActive"); err != nil {
				return fmt.Errorf("unable to decode toggleSupergroupUsernameIsActive#a68b6dff: %w", err)
			}
		case "supergroup_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode toggleSupergroupUsernameIsActive#a68b6dff: field supergroup_id: %w", err)
			}
			t.SupergroupID = value
		case "username":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode toggleSupergroupUsernameIsActive#a68b6dff: field username: %w", err)
			}
			t.Username = value
		case "is_active":
			value, err := b.Bool()
			if err != nil {
				return fmt.Errorf("unable to decode toggleSupergroupUsernameIsActive#a68b6dff: field is_active: %w", err)
			}
			t.IsActive = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSupergroupID returns value of SupergroupID field.
func (t *ToggleSupergroupUsernameIsActiveRequest) GetSupergroupID() (value int64) {
	if t == nil {
		return
	}
	return t.SupergroupID
}

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

// GetIsActive returns value of IsActive field.
func (t *ToggleSupergroupUsernameIsActiveRequest) GetIsActive() (value bool) {
	if t == nil {
		return
	}
	return t.IsActive
}

// ToggleSupergroupUsernameIsActive invokes method toggleSupergroupUsernameIsActive#a68b6dff returning error if any.
func (c *Client) ToggleSupergroupUsernameIsActive(ctx context.Context, request *ToggleSupergroupUsernameIsActiveRequest) error {
	var ok Ok

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