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

// ViewPremiumFeatureRequest represents TL type `viewPremiumFeature#b8031b2`.
type ViewPremiumFeatureRequest struct {
	// The viewed premium feature
	Feature PremiumFeatureClass
}

// ViewPremiumFeatureRequestTypeID is TL type id of ViewPremiumFeatureRequest.
const ViewPremiumFeatureRequestTypeID = 0xb8031b2

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

func (v *ViewPremiumFeatureRequest) Zero() bool {
	if v == nil {
		return true
	}
	if !(v.Feature == nil) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (v *ViewPremiumFeatureRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "viewPremiumFeature",
		ID:   ViewPremiumFeatureRequestTypeID,
	}
	if v == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Feature",
			SchemaName: "feature",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (v *ViewPremiumFeatureRequest) Encode(b *bin.Buffer) error {
	if v == nil {
		return fmt.Errorf("can't encode viewPremiumFeature#b8031b2 as nil")
	}
	b.PutID(ViewPremiumFeatureRequestTypeID)
	return v.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (v *ViewPremiumFeatureRequest) EncodeBare(b *bin.Buffer) error {
	if v == nil {
		return fmt.Errorf("can't encode viewPremiumFeature#b8031b2 as nil")
	}
	if v.Feature == nil {
		return fmt.Errorf("unable to encode viewPremiumFeature#b8031b2: field feature is nil")
	}
	if err := v.Feature.Encode(b); err != nil {
		return fmt.Errorf("unable to encode viewPremiumFeature#b8031b2: field feature: %w", err)
	}
	return nil
}

// Decode implements bin.Decoder.
func (v *ViewPremiumFeatureRequest) Decode(b *bin.Buffer) error {
	if v == nil {
		return fmt.Errorf("can't decode viewPremiumFeature#b8031b2 to nil")
	}
	if err := b.ConsumeID(ViewPremiumFeatureRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode viewPremiumFeature#b8031b2: %w", err)
	}
	return v.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (v *ViewPremiumFeatureRequest) DecodeBare(b *bin.Buffer) error {
	if v == nil {
		return fmt.Errorf("can't decode viewPremiumFeature#b8031b2 to nil")
	}
	{
		value, err := DecodePremiumFeature(b)
		if err != nil {
			return fmt.Errorf("unable to decode viewPremiumFeature#b8031b2: field feature: %w", err)
		}
		v.Feature = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (v *ViewPremiumFeatureRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if v == nil {
		return fmt.Errorf("can't encode viewPremiumFeature#b8031b2 as nil")
	}
	b.ObjStart()
	b.PutID("viewPremiumFeature")
	b.Comma()
	b.FieldStart("feature")
	if v.Feature == nil {
		return fmt.Errorf("unable to encode viewPremiumFeature#b8031b2: field feature is nil")
	}
	if err := v.Feature.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode viewPremiumFeature#b8031b2: field feature: %w", err)
	}
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (v *ViewPremiumFeatureRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if v == nil {
		return fmt.Errorf("can't decode viewPremiumFeature#b8031b2 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("viewPremiumFeature"); err != nil {
				return fmt.Errorf("unable to decode viewPremiumFeature#b8031b2: %w", err)
			}
		case "feature":
			value, err := DecodeTDLibJSONPremiumFeature(b)
			if err != nil {
				return fmt.Errorf("unable to decode viewPremiumFeature#b8031b2: field feature: %w", err)
			}
			v.Feature = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetFeature returns value of Feature field.
func (v *ViewPremiumFeatureRequest) GetFeature() (value PremiumFeatureClass) {
	if v == nil {
		return
	}
	return v.Feature
}

// ViewPremiumFeature invokes method viewPremiumFeature#b8031b2 returning error if any.
func (c *Client) ViewPremiumFeature(ctx context.Context, feature PremiumFeatureClass) error {
	var ok Ok

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