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

// GetConnectedAffiliateProgramsRequest represents TL type `getConnectedAffiliatePrograms#8b2c5272`.
type GetConnectedAffiliateProgramsRequest struct {
	// The affiliate to which the affiliate program were connected
	Affiliate AffiliateTypeClass
	// Offset of the first affiliate program to return as received from the previous request;
	// use empty string to get the first chunk of results
	Offset string
	// The maximum number of affiliate programs to return
	Limit int32
}

// GetConnectedAffiliateProgramsRequestTypeID is TL type id of GetConnectedAffiliateProgramsRequest.
const GetConnectedAffiliateProgramsRequestTypeID = 0x8b2c5272

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

func (g *GetConnectedAffiliateProgramsRequest) Zero() bool {
	if g == nil {
		return true
	}
	if !(g.Affiliate == nil) {
		return false
	}
	if !(g.Offset == "") {
		return false
	}
	if !(g.Limit == 0) {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (g *GetConnectedAffiliateProgramsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "getConnectedAffiliatePrograms",
		ID:   GetConnectedAffiliateProgramsRequestTypeID,
	}
	if g == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Affiliate",
			SchemaName: "affiliate",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (g *GetConnectedAffiliateProgramsRequest) Encode(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getConnectedAffiliatePrograms#8b2c5272 as nil")
	}
	b.PutID(GetConnectedAffiliateProgramsRequestTypeID)
	return g.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (g *GetConnectedAffiliateProgramsRequest) EncodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't encode getConnectedAffiliatePrograms#8b2c5272 as nil")
	}
	if g.Affiliate == nil {
		return fmt.Errorf("unable to encode getConnectedAffiliatePrograms#8b2c5272: field affiliate is nil")
	}
	if err := g.Affiliate.Encode(b); err != nil {
		return fmt.Errorf("unable to encode getConnectedAffiliatePrograms#8b2c5272: field affiliate: %w", err)
	}
	b.PutString(g.Offset)
	b.PutInt32(g.Limit)
	return nil
}

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

// DecodeBare implements bin.BareDecoder.
func (g *GetConnectedAffiliateProgramsRequest) DecodeBare(b *bin.Buffer) error {
	if g == nil {
		return fmt.Errorf("can't decode getConnectedAffiliatePrograms#8b2c5272 to nil")
	}
	{
		value, err := DecodeAffiliateType(b)
		if err != nil {
			return fmt.Errorf("unable to decode getConnectedAffiliatePrograms#8b2c5272: field affiliate: %w", err)
		}
		g.Affiliate = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode getConnectedAffiliatePrograms#8b2c5272: field offset: %w", err)
		}
		g.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode getConnectedAffiliatePrograms#8b2c5272: field limit: %w", err)
		}
		g.Limit = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (g *GetConnectedAffiliateProgramsRequest) EncodeTDLibJSON(b tdjson.Encoder) error {
	if g == nil {
		return fmt.Errorf("can't encode getConnectedAffiliatePrograms#8b2c5272 as nil")
	}
	b.ObjStart()
	b.PutID("getConnectedAffiliatePrograms")
	b.Comma()
	b.FieldStart("affiliate")
	if g.Affiliate == nil {
		return fmt.Errorf("unable to encode getConnectedAffiliatePrograms#8b2c5272: field affiliate is nil")
	}
	if err := g.Affiliate.EncodeTDLibJSON(b); err != nil {
		return fmt.Errorf("unable to encode getConnectedAffiliatePrograms#8b2c5272: field affiliate: %w", err)
	}
	b.Comma()
	b.FieldStart("offset")
	b.PutString(g.Offset)
	b.Comma()
	b.FieldStart("limit")
	b.PutInt32(g.Limit)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

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

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("getConnectedAffiliatePrograms"); err != nil {
				return fmt.Errorf("unable to decode getConnectedAffiliatePrograms#8b2c5272: %w", err)
			}
		case "affiliate":
			value, err := DecodeTDLibJSONAffiliateType(b)
			if err != nil {
				return fmt.Errorf("unable to decode getConnectedAffiliatePrograms#8b2c5272: field affiliate: %w", err)
			}
			g.Affiliate = value
		case "offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode getConnectedAffiliatePrograms#8b2c5272: field offset: %w", err)
			}
			g.Offset = value
		case "limit":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode getConnectedAffiliatePrograms#8b2c5272: field limit: %w", err)
			}
			g.Limit = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetAffiliate returns value of Affiliate field.
func (g *GetConnectedAffiliateProgramsRequest) GetAffiliate() (value AffiliateTypeClass) {
	if g == nil {
		return
	}
	return g.Affiliate
}

// GetOffset returns value of Offset field.
func (g *GetConnectedAffiliateProgramsRequest) GetOffset() (value string) {
	if g == nil {
		return
	}
	return g.Offset
}

// GetLimit returns value of Limit field.
func (g *GetConnectedAffiliateProgramsRequest) GetLimit() (value int32) {
	if g == nil {
		return
	}
	return g.Limit
}

// GetConnectedAffiliatePrograms invokes method getConnectedAffiliatePrograms#8b2c5272 returning error if any.
func (c *Client) GetConnectedAffiliatePrograms(ctx context.Context, request *GetConnectedAffiliateProgramsRequest) (*ConnectedAffiliatePrograms, error) {
	var result ConnectedAffiliatePrograms

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