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

// GetBusinessFeaturesRequest represents TL type `getBusinessFeatures#c4906001`.
type GetBusinessFeaturesRequest struct {
	// Source of the request; pass null if the method is called from settings or some
	// non-standard source
	Source BusinessFeatureClass
}

// GetBusinessFeaturesRequestTypeID is TL type id of GetBusinessFeaturesRequest.
const GetBusinessFeaturesRequestTypeID = 0xc4906001

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

func (g *GetBusinessFeaturesRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Source == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetBusinessFeaturesRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getBusinessFeatures",
		ID:   GetBusinessFeaturesRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Source",
			SchemaName: "source",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetBusinessFeaturesRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBusinessFeatures#c4906001 as nil")
	}
	b.PutID(GetBusinessFeaturesRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetBusinessFeaturesRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getBusinessFeatures#c4906001 as nil")
	}
	if g.Source == nil {
		return fmt.Errorf("unable to encode getBusinessFeatures#c4906001: field source is nil")
	}
	if err := g.Source.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getBusinessFeatures#c4906001: field source: %w", err)
	}
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetBusinessFeaturesRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getBusinessFeatures#c4906001 to nil")
	}
	{
		value, err := DecodeBusinessFeature(b)
		if err != nil {
			return fmt.Errorf("unable to decode getBusinessFeatures#c4906001: field source: %w", err)
		}
		g.Source = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetBusinessFeaturesRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getBusinessFeatures#c4906001 as nil")
	}
	b.ObjStart()
	b.PutID("getBusinessFeatures")
	b.Comma()
	b.FieldStart("source")
	if g.Source == nil {
		return fmt.Errorf("unable to encode getBusinessFeatures#c4906001: field source is nil")
	}
	if err := g.Source.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getBusinessFeatures#c4906001: field source: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getBusinessFeatures"); err != nil {
				return fmt.Errorf("unable to decode getBusinessFeatures#c4906001: %w", err)
			}
		case "source":
			value, err := DecodeTDLibJSONBusinessFeature(b)
			if err != nil {
				return fmt.Errorf("unable to decode getBusinessFeatures#c4906001: field source: %w", err)
			}
			g.Source = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetSource returns value of Source field.
func (g *GetBusinessFeaturesRequest) GetSource() (value BusinessFeatureClass) {
	if g == nil {
		return
	}
	return g.Source
}

// GetBusinessFeatures invokes method getBusinessFeatures#c4906001 returning error if any.
func (c *Client) GetBusinessFeatures(ctx context.Context, source BusinessFeatureClass) (*BusinessFeatures, error) {
	var result BusinessFeatures

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