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

// SearchAffiliateProgramsRequest represents TL type `searchAffiliatePrograms#2899a011`.
type SearchAffiliateProgramsRequest struct {
	// The affiliate for which affiliate programs are searched for
	Affiliate AffiliateTypeClass
	// Sort order for the results
	SortOrder AffiliateProgramSortOrderClass
	// 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
}

// SearchAffiliateProgramsRequestTypeID is TL type id of SearchAffiliateProgramsRequest.
const SearchAffiliateProgramsRequestTypeID = 0x2899a011

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

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

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (s *SearchAffiliateProgramsRequest) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "searchAffiliatePrograms",
		ID:   SearchAffiliateProgramsRequestTypeID,
	}
	if s == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "Affiliate",
			SchemaName: "affiliate",
		},
		{
			Name:       "SortOrder",
			SchemaName: "sort_order",
		},
		{
			Name:       "Offset",
			SchemaName: "offset",
		},
		{
			Name:       "Limit",
			SchemaName: "limit",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (s *SearchAffiliateProgramsRequest) Encode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchAffiliatePrograms#2899a011 as nil")
	}
	b.PutID(SearchAffiliateProgramsRequestTypeID)
	return s.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (s *SearchAffiliateProgramsRequest) EncodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't encode searchAffiliatePrograms#2899a011 as nil")
	}
	if s.Affiliate == nil {
		return fmt.Errorf("unable to encode searchAffiliatePrograms#2899a011: field affiliate is nil")
	}
	if err := s.Affiliate.Encode(b); err != nil {
		return fmt.Errorf("unable to encode searchAffiliatePrograms#2899a011: field affiliate: %w", err)
	}
	if s.SortOrder == nil {
		return fmt.Errorf("unable to encode searchAffiliatePrograms#2899a011: field sort_order is nil")
	}
	if err := s.SortOrder.Encode(b); err != nil {
		return fmt.Errorf("unable to encode searchAffiliatePrograms#2899a011: field sort_order: %w", err)
	}
	b.PutString(s.Offset)
	b.PutInt32(s.Limit)
	return nil
}

// Decode implements bin.Decoder.
func (s *SearchAffiliateProgramsRequest) Decode(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchAffiliatePrograms#2899a011 to nil")
	}
	if err := b.ConsumeID(SearchAffiliateProgramsRequestTypeID); err != nil {
		return fmt.Errorf("unable to decode searchAffiliatePrograms#2899a011: %w", err)
	}
	return s.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (s *SearchAffiliateProgramsRequest) DecodeBare(b *bin.Buffer) error {
	if s == nil {
		return fmt.Errorf("can't decode searchAffiliatePrograms#2899a011 to nil")
	}
	{
		value, err := DecodeAffiliateType(b)
		if err != nil {
			return fmt.Errorf("unable to decode searchAffiliatePrograms#2899a011: field affiliate: %w", err)
		}
		s.Affiliate = value
	}
	{
		value, err := DecodeAffiliateProgramSortOrder(b)
		if err != nil {
			return fmt.Errorf("unable to decode searchAffiliatePrograms#2899a011: field sort_order: %w", err)
		}
		s.SortOrder = value
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode searchAffiliatePrograms#2899a011: field offset: %w", err)
		}
		s.Offset = value
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode searchAffiliatePrograms#2899a011: field limit: %w", err)
		}
		s.Limit = value
	}
	return nil
}

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

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (s *SearchAffiliateProgramsRequest) DecodeTDLibJSON(b tdjson.Decoder) error {
	if s == nil {
		return fmt.Errorf("can't decode searchAffiliatePrograms#2899a011 to nil")
	}

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

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

// GetSortOrder returns value of SortOrder field.
func (s *SearchAffiliateProgramsRequest) GetSortOrder() (value AffiliateProgramSortOrderClass) {
	if s == nil {
		return
	}
	return s.SortOrder
}

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

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

// SearchAffiliatePrograms invokes method searchAffiliatePrograms#2899a011 returning error if any.
func (c *Client) SearchAffiliatePrograms(ctx context.Context, request *SearchAffiliateProgramsRequest) (*FoundAffiliatePrograms, error) {
	var result FoundAffiliatePrograms

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