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

// ConnectedAffiliatePrograms represents TL type `connectedAffiliatePrograms#59231e33`.
type ConnectedAffiliatePrograms struct {
	// The total number of affiliate programs that were connected to the affiliate
	TotalCount int32
	// The list of connected affiliate programs
	Programs []ConnectedAffiliateProgram
	// The offset for the next request. If empty, then there are no more results
	NextOffset string
}

// ConnectedAffiliateProgramsTypeID is TL type id of ConnectedAffiliatePrograms.
const ConnectedAffiliateProgramsTypeID = 0x59231e33

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

func (c *ConnectedAffiliatePrograms) Zero() bool {
	if c == nil {
		return true
	}
	if !(c.TotalCount == 0) {
		return false
	}
	if !(c.Programs == nil) {
		return false
	}
	if !(c.NextOffset == "") {
		return false
	}

	return true
}

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

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

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

// TypeInfo returns info about TL type.
func (c *ConnectedAffiliatePrograms) TypeInfo() tdp.Type {
	typ := tdp.Type{
		Name: "connectedAffiliatePrograms",
		ID:   ConnectedAffiliateProgramsTypeID,
	}
	if c == nil {
		typ.Null = true
		return typ
	}
	typ.Fields = []tdp.Field{
		{
			Name:       "TotalCount",
			SchemaName: "total_count",
		},
		{
			Name:       "Programs",
			SchemaName: "programs",
		},
		{
			Name:       "NextOffset",
			SchemaName: "next_offset",
		},
	}
	return typ
}

// Encode implements bin.Encoder.
func (c *ConnectedAffiliatePrograms) Encode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode connectedAffiliatePrograms#59231e33 as nil")
	}
	b.PutID(ConnectedAffiliateProgramsTypeID)
	return c.EncodeBare(b)
}

// EncodeBare implements bin.BareEncoder.
func (c *ConnectedAffiliatePrograms) EncodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't encode connectedAffiliatePrograms#59231e33 as nil")
	}
	b.PutInt32(c.TotalCount)
	b.PutInt(len(c.Programs))
	for idx, v := range c.Programs {
		if err := v.EncodeBare(b); err != nil {
			return fmt.Errorf("unable to encode bare connectedAffiliatePrograms#59231e33: field programs element with index %d: %w", idx, err)
		}
	}
	b.PutString(c.NextOffset)
	return nil
}

// Decode implements bin.Decoder.
func (c *ConnectedAffiliatePrograms) Decode(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode connectedAffiliatePrograms#59231e33 to nil")
	}
	if err := b.ConsumeID(ConnectedAffiliateProgramsTypeID); err != nil {
		return fmt.Errorf("unable to decode connectedAffiliatePrograms#59231e33: %w", err)
	}
	return c.DecodeBare(b)
}

// DecodeBare implements bin.BareDecoder.
func (c *ConnectedAffiliatePrograms) DecodeBare(b *bin.Buffer) error {
	if c == nil {
		return fmt.Errorf("can't decode connectedAffiliatePrograms#59231e33 to nil")
	}
	{
		value, err := b.Int32()
		if err != nil {
			return fmt.Errorf("unable to decode connectedAffiliatePrograms#59231e33: field total_count: %w", err)
		}
		c.TotalCount = value
	}
	{
		headerLen, err := b.Int()
		if err != nil {
			return fmt.Errorf("unable to decode connectedAffiliatePrograms#59231e33: field programs: %w", err)
		}

		if headerLen > 0 {
			c.Programs = make([]ConnectedAffiliateProgram, 0, headerLen%bin.PreallocateLimit)
		}
		for idx := 0; idx < headerLen; idx++ {
			var value ConnectedAffiliateProgram
			if err := value.DecodeBare(b); err != nil {
				return fmt.Errorf("unable to decode bare connectedAffiliatePrograms#59231e33: field programs: %w", err)
			}
			c.Programs = append(c.Programs, value)
		}
	}
	{
		value, err := b.String()
		if err != nil {
			return fmt.Errorf("unable to decode connectedAffiliatePrograms#59231e33: field next_offset: %w", err)
		}
		c.NextOffset = value
	}
	return nil
}

// EncodeTDLibJSON implements tdjson.TDLibEncoder.
func (c *ConnectedAffiliatePrograms) EncodeTDLibJSON(b tdjson.Encoder) error {
	if c == nil {
		return fmt.Errorf("can't encode connectedAffiliatePrograms#59231e33 as nil")
	}
	b.ObjStart()
	b.PutID("connectedAffiliatePrograms")
	b.Comma()
	b.FieldStart("total_count")
	b.PutInt32(c.TotalCount)
	b.Comma()
	b.FieldStart("programs")
	b.ArrStart()
	for idx, v := range c.Programs {
		if err := v.EncodeTDLibJSON(b); err != nil {
			return fmt.Errorf("unable to encode connectedAffiliatePrograms#59231e33: field programs element with index %d: %w", idx, err)
		}
		b.Comma()
	}
	b.StripComma()
	b.ArrEnd()
	b.Comma()
	b.FieldStart("next_offset")
	b.PutString(c.NextOffset)
	b.Comma()
	b.StripComma()
	b.ObjEnd()
	return nil
}

// DecodeTDLibJSON implements tdjson.TDLibDecoder.
func (c *ConnectedAffiliatePrograms) DecodeTDLibJSON(b tdjson.Decoder) error {
	if c == nil {
		return fmt.Errorf("can't decode connectedAffiliatePrograms#59231e33 to nil")
	}

	return b.Obj(func(b tdjson.Decoder, key []byte) error {
		switch string(key) {
		case tdjson.TypeField:
			if err := b.ConsumeID("connectedAffiliatePrograms"); err != nil {
				return fmt.Errorf("unable to decode connectedAffiliatePrograms#59231e33: %w", err)
			}
		case "total_count":
			value, err := b.Int32()
			if err != nil {
				return fmt.Errorf("unable to decode connectedAffiliatePrograms#59231e33: field total_count: %w", err)
			}
			c.TotalCount = value
		case "programs":
			if err := b.Arr(func(b tdjson.Decoder) error {
				var value ConnectedAffiliateProgram
				if err := value.DecodeTDLibJSON(b); err != nil {
					return fmt.Errorf("unable to decode connectedAffiliatePrograms#59231e33: field programs: %w", err)
				}
				c.Programs = append(c.Programs, value)
				return nil
			}); err != nil {
				return fmt.Errorf("unable to decode connectedAffiliatePrograms#59231e33: field programs: %w", err)
			}
		case "next_offset":
			value, err := b.String()
			if err != nil {
				return fmt.Errorf("unable to decode connectedAffiliatePrograms#59231e33: field next_offset: %w", err)
			}
			c.NextOffset = value
		default:
			return b.Skip()
		}
		return nil
	})
}

// GetTotalCount returns value of TotalCount field.
func (c *ConnectedAffiliatePrograms) GetTotalCount() (value int32) {
	if c == nil {
		return
	}
	return c.TotalCount
}

// GetPrograms returns value of Programs field.
func (c *ConnectedAffiliatePrograms) GetPrograms() (value []ConnectedAffiliateProgram) {
	if c == nil {
		return
	}
	return c.Programs
}

// GetNextOffset returns value of NextOffset field.
func (c *ConnectedAffiliatePrograms) GetNextOffset() (value string) {
	if c == nil {
		return
	}
	return c.NextOffset
}
