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

// BotsCreateBotRequest represents TL type `bots.createBot#e5b17f2b`.
//
// See https://core.telegram.org/method/bots.createBot for reference.
type BotsCreateBotRequest struct {
	// Flags field of BotsCreateBotRequest.
	Flags bin.Fields
	// ViaDeeplink field of BotsCreateBotRequest.
	ViaDeeplink bool
	// Name field of BotsCreateBotRequest.
	Name string
	// Username field of BotsCreateBotRequest.
	Username string
	// ManagerID field of BotsCreateBotRequest.
	ManagerID InputUserClass
}

// BotsCreateBotRequestTypeID is TL type id of BotsCreateBotRequest.
const BotsCreateBotRequestTypeID = 0xe5b17f2b

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

func (c *BotsCreateBotRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.Flags.Zero()) {
		return false
	}
	if !(c.ViaDeeplink == false) {
		return false
	}
	if !(c.Name == "") {
		return false
	}
	if !(c.Username == "") {
		return false
	}
	if !(c.ManagerID == nil) {
		return false
	}

	return true
}

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

// FillFrom fills BotsCreateBotRequest from given interface.
func (c *BotsCreateBotRequest) FillFrom(from interface {
	GetViaDeeplink() (value bool)
	GetName() (value string)
	GetUsername() (value string)
	GetManagerID() (value InputUserClass)
}) {
	c.ViaDeeplink = from.GetViaDeeplink()
	c.Name = from.GetName()
	c.Username = from.GetUsername()
	c.ManagerID = from.GetManagerID()
}

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

// TypeName returns name of type in TL schema.
func (*BotsCreateBotRequest) TypeName() string {
	return "bots.createBot"
}

// TypeInfo returns info about TL type.
func (c *BotsCreateBotRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "bots.createBot",
		ID:   BotsCreateBotRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ViaDeeplink",
			SchemaName: "via_deeplink",
			Null:       !c.Flags.Has(0),
		},
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "Username",
			SchemaName: "username",
		},
		{
			Name:       "ManagerID",
			SchemaName: "manager_id",
		},
	}
	return typ
}

// SetFlags sets flags for non-zero fields.
func (c *BotsCreateBotRequest) SetFlags() {
	if !(c.ViaDeeplink == false) {
		c.Flags.Set(0)
	}
}

// Encode implements bin.Encoder.
func (c *BotsCreateBotRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode bots.createBot#e5b17f2b as nil")
	}
	b.PutID(BotsCreateBotRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *BotsCreateBotRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode bots.createBot#e5b17f2b as nil")
	}
	c.SetFlags()
	if err := c.Flags.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.createBot#e5b17f2b: field flags: %w", err)
	}
	b.PutString(c.Name)
	b.PutString(c.Username)
	if c.ManagerID == nil {
		return fmt.Errorf("unable to encode bots.createBot#e5b17f2b: field manager_id is nil")
	}
	if err := c.ManagerID.Encode(b); err != nil {
		return fmt.Errorf("unable to encode bots.createBot#e5b17f2b: field manager_id: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *BotsCreateBotRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode bots.createBot#e5b17f2b to nil")
	}
	if err := b.ConsumeID(BotsCreateBotRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode bots.createBot#e5b17f2b: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *BotsCreateBotRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode bots.createBot#e5b17f2b to nil")
	}
	{
		if err := c.Flags.Decode(b); err != nil {
			return fmt.Errorf("unable to decode bots.createBot#e5b17f2b: field flags: %w", err)
		}
	}
	c.ViaDeeplink = c.Flags.Has(0)
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode bots.createBot#e5b17f2b: field name: %w", err)
		}
		c.Name = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode bots.createBot#e5b17f2b: field username: %w", err)
		}
		c.Username = value
	}
	{
		value, err := DecodeInputUser(b)
		if err != nil {
			return fmt.Errorf("unable to decode bots.createBot#e5b17f2b: field manager_id: %w", err)
		}
		c.ManagerID = value
	}
	return nil
}

// SetViaDeeplink sets value of ViaDeeplink conditional field.
func (c *BotsCreateBotRequest) SetViaDeeplink(value bool) {
	if value {
		c.Flags.Set(0)
		c.ViaDeeplink = true
	} else {
		c.Flags.Unset(0)
		c.ViaDeeplink = false
	}
}

// GetViaDeeplink returns value of ViaDeeplink conditional field.
func (c *BotsCreateBotRequest) GetViaDeeplink() (value bool) {
	if c == nil {
		return
	}
	return c.Flags.Has(0)
}

// GetName returns value of Name field.
func (c *BotsCreateBotRequest) GetName() (value string) {
	if c == nil {
		return
	}
	return c.Name
}

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

// GetManagerID returns value of ManagerID field.
func (c *BotsCreateBotRequest) GetManagerID() (value InputUserClass) {
	if c == nil {
		return
	}
	return c.ManagerID
}

// BotsCreateBot invokes method bots.createBot#e5b17f2b returning error if any.
//
// See https://core.telegram.org/method/bots.createBot for reference.
func (c *Client) BotsCreateBot(ctx context.Context, request *BotsCreateBotRequest) (UserClass, error) {
	var result UserBox

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