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

// GetPremiumFeaturesRequest represents TL type `getPremiumFeatures#b4dc2649`.
type GetPremiumFeaturesRequest struct {
	// Source of the request; pass null if the method is called from some non-standard source
	Source PremiumSourceClass
}

// GetPremiumFeaturesRequestTypeID is TL type id of GetPremiumFeaturesRequest.
const GetPremiumFeaturesRequestTypeID = 0xb4dc2649

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

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

	return true
}

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

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

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

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

// Encode implements bin.Encoder.
func (g *GetPremiumFeaturesRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getPremiumFeatures#b4dc2649 as nil")
	}
	b.PutID(GetPremiumFeaturesRequestTypeID)
	return g.EncodeBare(b)
}

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

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

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

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

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

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

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

// GetPremiumFeatures invokes method getPremiumFeatures#b4dc2649 returning error if any.
func (c *Client) GetPremiumFeatures(ctx context.Context, source PremiumSourceClass) (*PremiumFeatures, error) {
	var result PremiumFeatures

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