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

// ToggleUsernameIsActiveRequest represents TL type `toggleUsernameIsActive#4a276de3`.
type ToggleUsernameIsActiveRequest struct {
	// The username to change
	Username string
	// Pass true to activate the username; pass false to disable it
	IsActive bool
}

// ToggleUsernameIsActiveRequestTypeID is TL type id of ToggleUsernameIsActiveRequest.
const ToggleUsernameIsActiveRequestTypeID = 0x4a276de3

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (t *ToggleUsernameIsActiveRequest) Encode(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleUsernameIsActive#4a276de3 as nil")
	}
	b.PutID(ToggleUsernameIsActiveRequestTypeID)
	return t.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (t *ToggleUsernameIsActiveRequest) EncodeBare(b *bin.Buffer) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleUsernameIsActive#4a276de3 as nil")
	}
	b.PutString(t.Username)
	b.PutBool(t.IsActive)
	return nil
}

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

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

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (t *ToggleUsernameIsActiveRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if t == nil {
		return fmt.Errorf("can't encode toggleUsernameIsActive#4a276de3 as nil")
	}
	b.ObjStart()
	b.PutID("toggleUsernameIsActive")
	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 *ToggleUsernameIsActiveRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if t == nil {
		return fmt.Errorf("can't decode toggleUsernameIsActive#4a276de3 to nil")
	}

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

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

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

// ToggleUsernameIsActive invokes method toggleUsernameIsActive#4a276de3 returning error if any.
func (c *Client) ToggleUsernameIsActive(ctx context.Context, request *ToggleUsernameIsActiveRequest) error {
	var ok Ok

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