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

// CreateChatSubscriptionInviteLinkRequest represents TL type `createChatSubscriptionInviteLink#226b65`.
type CreateChatSubscriptionInviteLinkRequest struct {
	// Chat identifier
	ChatID int64
	// Invite link name; 0-32 characters
	Name string
	// Information about subscription plan that will be applied to the users joining the chat
	// via the link.
	SubscriptionPricing StarSubscriptionPricing
}

// CreateChatSubscriptionInviteLinkRequestTypeID is TL type id of CreateChatSubscriptionInviteLinkRequest.
const CreateChatSubscriptionInviteLinkRequestTypeID = 0x226b65

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

func (c *CreateChatSubscriptionInviteLinkRequest) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.ChatID == 0) {
		return false
	}
	if !(c.Name == "") {
		return false
	}
	if !(c.SubscriptionPricing.Zero()) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *CreateChatSubscriptionInviteLinkRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "createChatSubscriptionInviteLink",
		ID:   CreateChatSubscriptionInviteLinkRequestTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "ChatID",
			SchemaName: "chat_id",
		},
		{
			Name:       "Name",
			SchemaName: "name",
		},
		{
			Name:       "SubscriptionPricing",
			SchemaName: "subscription_pricing",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *CreateChatSubscriptionInviteLinkRequest) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createChatSubscriptionInviteLink#226b65 as nil")
	}
	b.PutID(CreateChatSubscriptionInviteLinkRequestTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *CreateChatSubscriptionInviteLinkRequest) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode createChatSubscriptionInviteLink#226b65 as nil")
	}
	b.PutInt53(c.ChatID)
	b.PutString(c.Name)
	if err := c.SubscriptionPricing.Encode(b); err != nil {
		return fmt.Errorf("unable to encode createChatSubscriptionInviteLink#226b65: field subscription_pricing: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (c *CreateChatSubscriptionInviteLinkRequest) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode createChatSubscriptionInviteLink#226b65 to nil")
	}
	if err := b.ConsumeID(CreateChatSubscriptionInviteLinkRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode createChatSubscriptionInviteLink#226b65: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *CreateChatSubscriptionInviteLinkRequest) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode createChatSubscriptionInviteLink#226b65 to nil")
	}
	{
		value, err := b.Int53()
		if err != nil {
			return fmt.Errorf("unable to decode createChatSubscriptionInviteLink#226b65: field chat_id: %w", err)
		}
		c.ChatID = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode createChatSubscriptionInviteLink#226b65: field name: %w", err)
		}
		c.Name = value
	}
	{
		if err := c.SubscriptionPricing.Decode(b); err != nil {
			return fmt.Errorf("unable to decode createChatSubscriptionInviteLink#226b65: field subscription_pricing: %w", err)
		}
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *CreateChatSubscriptionInviteLinkRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode createChatSubscriptionInviteLink#226b65 as nil")
	}
	b.ObjStart()
	b.PutID("createChatSubscriptionInviteLink")
	b.Comma()
	b.FieldStart("chat_id")
	b.PutInt53(c.ChatID)
	b.Comma()
	b.FieldStart("name")
	b.PutString(c.Name)
	b.Comma()
	b.FieldStart("subscription_pricing")
	if err := c.SubscriptionPricing.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode createChatSubscriptionInviteLink#226b65: field subscription_pricing: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *CreateChatSubscriptionInviteLinkRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode createChatSubscriptionInviteLink#226b65 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("createChatSubscriptionInviteLink"); err != nil {
				return fmt.Errorf("unable to decode createChatSubscriptionInviteLink#226b65: %w", err)
			}
		case "chat_id":
			value, err := b.Int53()
			if err != nil {
				return fmt.Errorf("unable to decode createChatSubscriptionInviteLink#226b65: field chat_id: %w", err)
			}
			c.ChatID = value
		case "name":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode createChatSubscriptionInviteLink#226b65: field name: %w", err)
			}
			c.Name = value
		case "subscription_pricing":
			if err := c.SubscriptionPricing.DecodeTDLibJSON(b); err != nil {
				return fmt.Errorf("unable to decode createChatSubscriptionInviteLink#226b65: field subscription_pricing: %w", err)
			}
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetChatID returns value of ChatID field.
func (c *CreateChatSubscriptionInviteLinkRequest) GetChatID() (value int64) {
	if c == nil {
		return
	}
	return c.ChatID
}

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

// GetSubscriptionPricing returns value of SubscriptionPricing field.
func (c *CreateChatSubscriptionInviteLinkRequest) GetSubscriptionPricing() (value StarSubscriptionPricing) {
	if c == nil {
		return
	}
	return c.SubscriptionPricing
}

// CreateChatSubscriptionInviteLink invokes method createChatSubscriptionInviteLink#226b65 returning error if any.
func (c *Client) CreateChatSubscriptionInviteLink(ctx context.Context, request *CreateChatSubscriptionInviteLinkRequest) (*ChatInviteLink, error) {
	var result ChatInviteLink

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